]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-21987: Fix TarFile.getmember getting a dir with a trailing slash (GH-30283...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 23 Jan 2022 17:54:13 +0000 (09:54 -0800)
committerGitHub <noreply@github.com>
Sun, 23 Jan 2022 17:54:13 +0000 (19:54 +0200)
(cherry picked from commit cfadcc31ea84617b1c73022ce54d4ae831333e8d)

Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2021-12-28-11-55-10.bpo-21987.avBK-p.rst [new file with mode: 0644]

index 043a4ab5a52a6f847c06aedfd3e4feb1efa8330f..a738e25684c481280c21d7016593a38465a7364e 100755 (executable)
@@ -1785,7 +1785,7 @@ class TarFile(object):
            than once in the archive, its last occurrence is assumed to be the
            most up-to-date version.
         """
-        tarinfo = self._getmember(name)
+        tarinfo = self._getmember(name.rstrip('/'))
         if tarinfo is None:
             raise KeyError("filename %r not found" % name)
         return tarinfo
index 06fb97212d8567b71c41fafc5864fc25e6e68fc8..a4b63ff82b3ae338f3a03e48237d0c73c00088f5 100644 (file)
@@ -219,6 +219,25 @@ class UstarReadTest(ReadTest, unittest.TestCase):
     def test_issue14160(self):
         self._test_fileobj_link("symtype2", "ustar/regtype")
 
+    def test_add_dir_getmember(self):
+        # bpo-21987
+        self.add_dir_and_getmember('bar')
+        self.add_dir_and_getmember('a'*101)
+
+    def add_dir_and_getmember(self, name):
+        with support.temp_cwd():
+            with tarfile.open(tmpname, 'w') as tar:
+                try:
+                    os.mkdir(name)
+                    tar.add(name)
+                finally:
+                    os.rmdir(name)
+            with tarfile.open(tmpname) as tar:
+                self.assertEqual(
+                    tar.getmember(name),
+                    tar.getmember(name + '/')
+                )
+
 class GzipUstarReadTest(GzipTest, UstarReadTest):
     pass
 
diff --git a/Misc/NEWS.d/next/Library/2021-12-28-11-55-10.bpo-21987.avBK-p.rst b/Misc/NEWS.d/next/Library/2021-12-28-11-55-10.bpo-21987.avBK-p.rst
new file mode 100644 (file)
index 0000000..305dd16
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an issue with :meth:`tarfile.TarFile.getmember` getting a directory name
+with a trailing slash.