Skip to content

Commit efa8159

Browse files
committed
rpminfo: Directory identification using mode bits
Currently, if a CPIO archive contains an entry for a directory we attempt to identify it via nlinks. Since it should have at least two nlinks if it's a directoy. However, I've stumbled across a case where this is not true. By using the mode bits we should be able to correctly identify directories without relying on nlinks. Fixes: #37 Signed-off-by: John Andersen <[email protected]>
1 parent 2acffc8 commit efa8159

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

rpmfile/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ def _read_new(cls, fileobj, magic=None):
7878
file_size = int(d[6], 16)
7979
fileobj.seek(file_size, 1)
8080
fileobj.seek(pad(fileobj), 1)
81-
nlink = int(d[4], 16)
82-
isdir = nlink == 2 and file_size == 0
81+
# https://www.mankier.com/5/cpio under Old Binary Format mode bits
82+
mode = int(d[1], 16)
83+
isdir = mode & int("0040000", 8)
8384
return cls(name, file_start, file_size, initial_offset, isdir)
8485

8586

tests/test_extract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_zstd_xmlstarlet(self, rpmpath):
8989
self.assertEqual(rpm.headers.get("arch", "noarch"), b"x86_64")
9090

9191
members = list(rpm.getmembers())
92-
self.assertEqual(len(members), 15)
92+
self.assertEqual(len(members), 12)
9393

9494
with rpm.extractfile("./usr/bin/xmlstarlet") as fd:
9595
calculated = hashlib.md5(fd.read()).hexdigest()

0 commit comments

Comments
 (0)