Christopher Brannon <[EMAIL PROTECTED]> added the comment:

Here is a patch containing code and a unit test.  I set external_attr
to 0600, for the following reason.
When I extract with Infozip, my umask is ignored when setting permissions of
extracted entries.  They have the permissions assigned to them when archived.
tar does respect umask, but it's not pertinent.
The following shell script demonstrates Infozip's behavior:

=====begin=====
#!/bin/sh
mkdir ziptest_dir
echo hello > ziptest_dir/foo.txt
chmod 666 ziptest_dir/foo.txt
zip -r ziptest_file.zip ziptest_dir/
rm -rf ziptest_dir
umask 077
unzip ziptest_file.zip
=====end=====

Setting permissions to 0600 seems like the safest course.

I'm not sure if this patch should be accompanied by some documentation,
since the zipfile docs don't say much about external_attr or permissions.

PS.  My earlier comments about timestamps were incorrect and spurious!

----------
keywords: +patch
Added file: http://bugs.python.org/file10933/writestr_usable_permissions.diff

_______________________________________
Python tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue3394>
_______________________________________
Index: Lib/zipfile.py
===================================================================
--- Lib/zipfile.py      (revision 65056)
+++ Lib/zipfile.py      (working copy)
@@ -2,7 +2,7 @@
 Read and write ZIP files.
 """
 import struct, os, time, sys, shutil
-import binascii, cStringIO
+import binascii, cStringIO, stat
 
 try:
     import zlib # We may need its compression method
@@ -1064,6 +1064,8 @@
             zinfo = ZipInfo(filename=zinfo_or_arcname,
                             date_time=time.localtime(time.time())[:6])
             zinfo.compress_type = self.compression
+            # zinfo.external_attr = 0600 << 16
+            zinfo.external_attr = (stat.S_IRUSR | stat.S_IWUSR) << 16
         else:
             zinfo = zinfo_or_arcname
 
Index: Lib/test/test_zipfile.py
===================================================================
--- Lib/test/test_zipfile.py    (revision 65056)
+++ Lib/test/test_zipfile.py    (working copy)
@@ -372,6 +372,20 @@
         # remove the test file subdirectories
         shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
 
+    def zip_test_writestr_permissions(self, f, compression):
+        # Make sure that writestr creates files with mode 0600,
+        # when it is passed a name rather than a ZipInfo instance.
+
+        self.makeTestArchive(f, compression)
+        zipfp = zipfile.ZipFile(f, "r")
+        zinfo = zipfp.getinfo('strfile')
+        self.assertEqual(zinfo.external_attr, 0600 << 16)
+
+    def test_writestr_permissions(self):
+        for f in (TESTFN2, TemporaryFile(), StringIO()):
+            self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
+
+
     def tearDown(self):
         os.remove(TESTFN)
         os.remove(TESTFN2)
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to