Skip to content

Commit 46d0e1c

Browse files
miss-islingtoncfernald
andauthoredJun 21, 2022
gh-91387: Strip trailing slash from tarfile longname directories (GH-32423)
Co-authored-by: Brett Cannon <brett@python.org> (cherry picked from commit c1e1942) Co-authored-by: Chris Fernald <chrisf671@gmail.com>
1 parent 223fde3 commit 46d0e1c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
 

‎Lib/tarfile.py

+10
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

+17
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)
@@ -1006,11 +1007,26 @@ def test_header_offset(self):
10061007
"iso8859-1", "strict")
10071008
self.assertEqual(tarinfo.type, self.longnametype)
10081009

1010+
def test_longname_directory(self):
1011+
# Test reading a longlink directory. Issue #47231.
1012+
longdir = ('a' * 101) + '/'
1013+
with os_helper.temp_cwd():
1014+
with tarfile.open(tmpname, 'w') as tar:
1015+
tar.format = self.format
1016+
try:
1017+
os.mkdir(longdir)
1018+
tar.add(longdir)
1019+
finally:
1020+
os.rmdir(longdir)
1021+
with tarfile.open(tmpname) as tar:
1022+
self.assertIsNotNone(tar.getmember(longdir))
1023+
self.assertIsNotNone(tar.getmember(longdir.removesuffix('/')))
10091024

10101025
class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase):
10111026

10121027
subdir = "gnu"
10131028
longnametype = tarfile.GNUTYPE_LONGNAME
1029+
format = tarfile.GNU_FORMAT
10141030

10151031
# Since 3.2 tarfile is supposed to accurately restore sparse members and
10161032
# produce files with holes. This is what we actually want to test here.
@@ -1070,6 +1086,7 @@ class PaxReadTest(LongnameTest, ReadTest, unittest.TestCase):
10701086

10711087
subdir = "pax"
10721088
longnametype = tarfile.XHDTYPE
1089+
format = tarfile.PAX_FORMAT
10731090

10741091
def test_pax_global_headers(self):
10751092
tar = tarfile.open(tarname, encoding="iso8859-1")
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)
Please sign in to comment.