changeset 35467:dba1e2dfd8de 2.4

Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 27 Aug 2005 10:08:21 +0000
parents 1312946f32ab
children 200e2ef4dbdc
files Lib/tarfile.py Lib/test/test_tarfile.py Misc/NEWS
diffstat 3 files changed, 49 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/tarfile.py	Fri Aug 26 13:24:09 2005 +0000
+++ b/Lib/tarfile.py	Sat Aug 27 10:08:21 2005 +0000
@@ -1132,17 +1132,16 @@
 
         # Fill the TarInfo object with all
         # information we can get.
-        tarinfo.name  = arcname
-        tarinfo.mode  = stmd
-        tarinfo.uid   = statres.st_uid
-        tarinfo.gid   = statres.st_gid
-        if stat.S_ISDIR(stmd):
-            # For a directory, the size must be 0
-            tarinfo.size  = 0
+        tarinfo.name = arcname
+        tarinfo.mode = stmd
+        tarinfo.uid = statres.st_uid
+        tarinfo.gid = statres.st_gid
+        if stat.S_ISREG(stmd):
+            tarinfo.size = statres.st_size
         else:
-            tarinfo.size = statres.st_size
+            tarinfo.size = 0L
         tarinfo.mtime = statres.st_mtime
-        tarinfo.type  = type
+        tarinfo.type = type
         tarinfo.linkname = linkname
         if pwd:
             try:
@@ -1233,16 +1232,15 @@
             self.addfile(tarinfo, f)
             f.close()
 
-        if tarinfo.type in (LNKTYPE, SYMTYPE, FIFOTYPE, CHRTYPE, BLKTYPE):
-            tarinfo.size = 0L
-            self.addfile(tarinfo)
-
-        if tarinfo.isdir():
+        elif tarinfo.isdir():
             self.addfile(tarinfo)
             if recursive:
                 for f in os.listdir(name):
                     self.add(os.path.join(name, f), os.path.join(arcname, f))
 
+        else:
+            self.addfile(tarinfo)
+
     def addfile(self, tarinfo, fileobj=None):
         """Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
            given, tarinfo.size bytes are read from it and added to the archive.
--- a/Lib/test/test_tarfile.py	Fri Aug 26 13:24:09 2005 +0000
+++ b/Lib/test/test_tarfile.py	Sat Aug 27 10:08:21 2005 +0000
@@ -208,6 +208,40 @@
             else:
                 self.dst.addfile(tarinfo, f)
 
+class WriteSize0Test(BaseTest):
+    mode = 'w'
+
+    def setUp(self):
+        self.tmpdir = dirname()
+        self.dstname = tmpname()
+        self.dst = tarfile.open(self.dstname, "w")
+
+    def tearDown(self):
+        self.dst.close()
+
+    def test_file(self):
+        path = os.path.join(self.tmpdir, "file")
+        file(path, "w")
+        tarinfo = self.dst.gettarinfo(path)
+        self.assertEqual(tarinfo.size, 0)
+        file(path, "w").write("aaa")
+        tarinfo = self.dst.gettarinfo(path)
+        self.assertEqual(tarinfo.size, 3)
+
+    def test_directory(self):
+        path = os.path.join(self.tmpdir, "directory")
+        os.mkdir(path)
+        tarinfo = self.dst.gettarinfo(path)
+        self.assertEqual(tarinfo.size, 0)
+
+    def test_symlink(self):
+        if hasattr(os, "symlink"):
+            path = os.path.join(self.tmpdir, "symlink")
+            os.symlink("link_target", path)
+            tarinfo = self.dst.gettarinfo(path)
+            self.assertEqual(tarinfo.size, 0)
+
+
 class WriteStreamTest(WriteTest):
     sep = '|'
 
@@ -366,6 +400,7 @@
         ReadTest,
         ReadStreamTest,
         WriteTest,
+        WriteSize0Test,
         WriteStreamTest,
         WriteGNULongTest,
     ]
--- a/Misc/NEWS	Fri Aug 26 13:24:09 2005 +0000
+++ b/Misc/NEWS	Sat Aug 27 10:08:21 2005 +0000
@@ -79,6 +79,8 @@
 Library
 -------
 
+- Bug #1167128: Fix size of a symlink in a tarfile to be 0.
+
 - Patch #810023: Fix off-by-one bug in urllib.urlretrieve reporthook
   functionality.