Skip to content

Commit

Permalink
pythongh-87264: Convert tarinfo type to stat type (pythonGH-113230)
Browse files Browse the repository at this point in the history
Co-authored-by: val-shkolnikov <[email protected]>
  • Loading branch information
alphavector and val-shkolnikov authored Dec 19, 2023
1 parent 6a1d5a4 commit e1117cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
7 changes: 6 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,6 +2106,10 @@ def list(self, verbose=True, *, members=None):
output is produced. `members' is optional and must be a subset of the
list returned by getmembers().
"""
# Convert tarinfo type to stat type.
type2mode = {REGTYPE: stat.S_IFREG, SYMTYPE: stat.S_IFLNK,
FIFOTYPE: stat.S_IFIFO, CHRTYPE: stat.S_IFCHR,
DIRTYPE: stat.S_IFDIR, BLKTYPE: stat.S_IFBLK}
self._check()

if members is None:
Expand All @@ -2115,7 +2119,8 @@ def list(self, verbose=True, *, members=None):
if tarinfo.mode is None:
_safe_print("??????????")
else:
_safe_print(stat.filemode(tarinfo.mode))
modetype = type2mode.get(tarinfo.type, 0)
_safe_print(stat.filemode(modetype | tarinfo.mode))
_safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
tarinfo.gname or tarinfo.gid))
if tarinfo.ischr() or tarinfo.isblk():
Expand Down
20 changes: 16 additions & 4 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,23 @@ def test_list_verbose(self):
# accessories if verbose flag is being used
# ...
# ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype
# ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
# -rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
# drwxr-xr-x tarfile/tarfile 0 2003-01-05 15:19:43 ustar/dirtype/
# ...
self.assertRegex(out, (br'\?rw-r--r-- tarfile/tarfile\s+7011 '
br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
br'ustar/\w+type ?\r?\n') * 2)
#
# Array of values to modify the regex below:
# ((file_type, file_permissions, file_length), ...)
type_perm_lengths = (
(br'\?', b'rw-r--r--', b'7011'), (b'-', b'rw-r--r--', b'7011'),
(b'd', b'rwxr-xr-x', b'0'), (b'd', b'rwxr-xr-x', b'255'),
(br'\?', b'rw-r--r--', b'0'), (b'l', b'rwxrwxrwx', b'0'),
(b'b', b'rw-rw----', b'3,0'), (b'c', b'rw-rw-rw-', b'1,3'),
(b'p', b'rw-r--r--', b'0'))
self.assertRegex(out, b''.join(
[(tp + (br'%s tarfile/tarfile\s+%s ' % (perm, ln) +
br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
br'ustar/\w+type[/>\sa-z-]*\n')) for tp, perm, ln
in type_perm_lengths]))
# Make sure it prints the source of link with verbose flag
self.assertIn(b'ustar/symtype -> regtype', out)
self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed tarfile list() method to show file type.

0 comments on commit e1117cb

Please sign in to comment.