Skip to content

Commit e1117cb

Browse files
gh-87264: Convert tarinfo type to stat type (GH-113230)
Co-authored-by: val-shkolnikov <[email protected]>
1 parent 6a1d5a4 commit e1117cb

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Lib/tarfile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,10 @@ def list(self, verbose=True, *, members=None):
21062106
output is produced. `members' is optional and must be a subset of the
21072107
list returned by getmembers().
21082108
"""
2109+
# Convert tarinfo type to stat type.
2110+
type2mode = {REGTYPE: stat.S_IFREG, SYMTYPE: stat.S_IFLNK,
2111+
FIFOTYPE: stat.S_IFIFO, CHRTYPE: stat.S_IFCHR,
2112+
DIRTYPE: stat.S_IFDIR, BLKTYPE: stat.S_IFBLK}
21092113
self._check()
21102114

21112115
if members is None:
@@ -2115,7 +2119,8 @@ def list(self, verbose=True, *, members=None):
21152119
if tarinfo.mode is None:
21162120
_safe_print("??????????")
21172121
else:
2118-
_safe_print(stat.filemode(tarinfo.mode))
2122+
modetype = type2mode.get(tarinfo.type, 0)
2123+
_safe_print(stat.filemode(modetype | tarinfo.mode))
21192124
_safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
21202125
tarinfo.gname or tarinfo.gid))
21212126
if tarinfo.ischr() or tarinfo.isblk():

Lib/test/test_tarfile.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,23 @@ def test_list_verbose(self):
323323
# accessories if verbose flag is being used
324324
# ...
325325
# ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype
326-
# ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
326+
# -rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
327+
# drwxr-xr-x tarfile/tarfile 0 2003-01-05 15:19:43 ustar/dirtype/
327328
# ...
328-
self.assertRegex(out, (br'\?rw-r--r-- tarfile/tarfile\s+7011 '
329-
br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
330-
br'ustar/\w+type ?\r?\n') * 2)
329+
#
330+
# Array of values to modify the regex below:
331+
# ((file_type, file_permissions, file_length), ...)
332+
type_perm_lengths = (
333+
(br'\?', b'rw-r--r--', b'7011'), (b'-', b'rw-r--r--', b'7011'),
334+
(b'd', b'rwxr-xr-x', b'0'), (b'd', b'rwxr-xr-x', b'255'),
335+
(br'\?', b'rw-r--r--', b'0'), (b'l', b'rwxrwxrwx', b'0'),
336+
(b'b', b'rw-rw----', b'3,0'), (b'c', b'rw-rw-rw-', b'1,3'),
337+
(b'p', b'rw-r--r--', b'0'))
338+
self.assertRegex(out, b''.join(
339+
[(tp + (br'%s tarfile/tarfile\s+%s ' % (perm, ln) +
340+
br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
341+
br'ustar/\w+type[/>\sa-z-]*\n')) for tp, perm, ln
342+
in type_perm_lengths]))
331343
# Make sure it prints the source of link with verbose flag
332344
self.assertIn(b'ustar/symtype -> regtype', out)
333345
self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed tarfile list() method to show file type.

0 commit comments

Comments
 (0)