10000 gh-91387: Strip trailing slash from tarfile longname directories (GH-… · python/cpython@c1e1942 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1e1942

Browse files
gh-91387: Strip trailing slash from tarfile longname directories (GH-32423)
Co-authored-by: Brett Cannon <brett@python.org>
1 parent b1ae4af commit c1e1942

File tree

3 files changed

+28
-0
lines changed
  • < 10000 div class="PRIVATE_TreeView-item-visual prc-TreeView-TreeViewItemVisual-dRlGq" aria-hidden="true">
    Lib
  • Misc/NEWS.d/next/Library
  • 3 files changed

    +28
    -0
    lines changed

    Lib/tarfile.py

    Lines changed: 10 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1163,6 +1163,11 @@ def _proc_builtin(self, tarfile):
    11631163
    # header information.
    11641164
    self._apply_pax_info(tarfile.pax_headers, tarfile.encoding, tarfile.errors)
    11651165

    1166+
    # Remove redundant slashes from directories. This is to be consistent
    1167+
    # with frombuf().
    1168+
    if self.isdir():
    1169+
    self.name = self.name.rstrip("/")
    1170+
    11661171
    return self
    11671172

    11681173
    def _proc_gnulong(self, tarfile):
    @@ -1185,6 +1190,11 @@ def _proc_gnulong(self, tarfile):
    11851190
    elif self.type == GNUTYPE_LONGLINK:
    11861191
    next.linkname = nts(buf, tarfile.encoding, tarfile.errors)
    11871192

    1193+
    # Remove redundant slashes from directories. This is to be consistent
    1194+
    # with frombuf().
    1195+
    if next.isdir():
    1196+
    next.name = next.name.removesuffix("/")
    1197+
    11881198
    return next
    11891199

    11901200
    def _proc_sparse(self, tarfile):

    Lib/test/test_tarfile.py

    Lines changed: 17 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -228,6 +228,7 @@ def test_add_dir_getmember(self):
    228228
    def add_dir_and_getmember(self, name):
    229229
    with os_helper.temp_cwd():
    230230
    with tarfile.open(tmpname, 'w') as tar:
    231+
    tar.format = tarfile.USTAR_FORMAT
    231232
    try:
    232233
    os.mkdir(name)
    233234
    tar.add(name)
    @@ -1020,11 +1021,26 @@ def test_header_offset(self):
    10201021
    "iso8859-1", "strict")
    10211022
    self.assertEqual(tarinfo.type, self.longnametype)
    10221023

    1024+
    def test_longname_directory(self):
    1025+
    # Test reading a longlink directory. Issue #47231.
    1026+
    longdir = ('a' * 101) + '/'
    1027+
    with os_helper.temp_cwd():
    1028+
    with tarfile.open(tmpname, 'w') as tar:
    1029+
    tar.format = self.format
    1030+
    try:
    1031+
    os.mkdir(longdir)
    1032+
    tar.add(longdir)
    1033+
    finally:
    1034+
    os.rmdir(longdir)
    1035+
    with tarfile.open(tmpname) as tar:
    1036+
    self.assertIsNotNone(tar.getmember(longdir))
    1037+
    self.assertIsNotNone(tar.getmember(longdir.removesuffix('/')))
    10231038

    10241039
    class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase):
    10251040

    10261041
    subdir = "gnu"
    10271042
    longnametype = tarfile.GNUTYPE_LONGNAME
    1043+
    format = tarfile.GNU_FORMAT
    10281044

    10291045
    # Since 3.2 tarfile is supposed to accurately restore sparse members and
    10301046
    # produce files with holes. This is what we actually want to test here.
    @@ -1084,6 +1100,7 @@ class PaxReadTest(LongnameTest, ReadTest, unittest.TestCase):
    10841100

    10851101
    subdir = "pax"
    10861102
    longnametype = tarfile.XHDTYPE
    1103+
    format = tarfile.PAX_FORMAT
    10871104

    10881105
    def test_pax_global_headers(self):
    10891106
    tar = tarfile.open(tarname, encoding="iso8859-1")
    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1 @@
    1+
    Fixed an issue with inconsistent trailing slashes in tarfile longname directories.

    0 commit comments

    Comments
     (0)
    0