Skip to content

Commit 5a13c01

Browse files
committed
Magic test for archives with zero-length entry
Noticed when attempting to unpack a python `.whl` wheel file; They're just renamed .zip's, and usually work OK, but there are casess where the first file in the `.whl` archive is zero bytes, and that causes `file -z` to error: ```bash ❯ file -zL tests/test-1.23.zip tests/test-1.23.zip: ERROR:[gzip: ] (data) ❯ file tests/test-1.23.zip tests/test-1.23.zip: Zip archive data, made by v2.3 UNIX, extract using at least v1.0, last modified Oct 28 2006 14:38:44, uncompressed size 0, method=store ❯ unzip -l tests/test-1.23.zip Archive: tests/test-1.23.zip Length Date Time Name --------- ---------- ----- ---- 0 2006-10-28 14:38 1/2/3 0 2006-10-28 14:38 a/b 0 2006-10-28 14:38 foobar --------- ------- 0 3 files ``` This means we were not using `file` magic for any archive that started with a zero-length file (or directory) entry. Add a fallback when the magic test fails in this case, and add a test to cover it.
1 parent 34eecd1 commit 5a13c01

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

dtrx/dtrx.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,11 +1357,22 @@ def try_by_magic(self, filename):
13571357
status = process.wait()
13581358
if status != 0:
13591359
return []
1360+
# if output contains 'ERROR:[', there was an error unzipping the
1361+
# first archive entry. re-run without -z.
1362+
output = process.stdout.readline().decode("ascii")
1363+
process.stdout.close()
1364+
if "ERROR:[" in output:
1365+
process.stdout.close()
1366+
process = subprocess.Popen(["file", "-L", filename], stdout=subprocess.PIPE)
1367+
status = process.wait()
1368+
if status != 0:
1369+
return []
1370+
output = process.stdout.readline().decode("ascii")
1371+
process.stdout.close()
1372+
13601373
except FileNotFoundError:
13611374
logger.error("'file' command not found, skipping magic test")
13621375
return []
1363-
output = process.stdout.readline().decode("ascii")
1364-
process.stdout.close()
13651376
if output.startswith("%s: " % filename):
13661377
output = output[len(filename) + 2 :]
13671378
mimes = self.magic_map_matches(output, self.magic_mime_map)

tests/test-1.23.whl

380 Bytes
Binary file not shown.

tests/tests.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,10 @@
987987
filenames: getting-started.crx
988988
baseline: |
989989
unzip -q $1 -d getting-started
990+
991+
- name: whl
992+
filenames: test-1.23.whl
993+
baseline: |
994+
mkdir test-1.23
995+
cd test-1.23
996+
unzip -q ../$1

0 commit comments

Comments
 (0)