diff options
author | Marat Idrisov <idrisov.m.ti@gmail.com> | 2023-12-19 19:04:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-19 19:04:43 (GMT) |
commit | e1117cb8864ca337a26388a6186aa85f7cc29a94 (patch) | |
tree | de6e948630636debbbb7f9ba09c01c8774d53bcb | |
parent | 6a1d5a4c0f209d51ab33d6529935d643bcdb3ba2 (diff) | |
download | cpython-e1117cb8864ca337a26388a6186aa85f7cc29a94.zip cpython-e1117cb8864ca337a26388a6186aa85f7cc29a94.tar.gz cpython-e1117cb8864ca337a26388a6186aa85f7cc29a94.tar.bz2 |
gh-87264: Convert tarinfo type to stat type (GH-113230)
Co-authored-by: val-shkolnikov <val@nvsoft.net>
-rwxr-xr-x | Lib/tarfile.py | 7 | ||||
-rw-r--r-- | Lib/test/test_tarfile.py | 20 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2023-12-17-13-56-30.gh-issue-87264.RgfHCv.rst | 1 |
3 files changed, 23 insertions, 5 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index ec32f9b..5ada0ad 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -2106,6 +2106,10 @@ class TarFile(object): 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: @@ -2115,7 +2119,8 @@ class TarFile(object): 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(): diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index edfeac6..da50091 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -323,11 +323,23 @@ class ListTest(ReadTest, unittest.TestCase): # 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) diff --git a/Misc/NEWS.d/next/Library/2023-12-17-13-56-30.gh-issue-87264.RgfHCv.rst b/Misc/NEWS.d/next/Library/2023-12-17-13-56-30.gh-issue-87264.RgfHCv.rst new file mode 100644 index 0000000..fa987d4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-17-13-56-30.gh-issue-87264.RgfHCv.rst @@ -0,0 +1 @@ +Fixed tarfile list() method to show file type. |