@matheusbsbs / matheus-bagliano / commits / 62cdce17c5
Fork 0

git-archive --format=zip: add symlink support

Add symlink support to ZIP file creation, and a few tests. This implementation sets the "version made by" field (creator_version) to Unix for symlinks, only; regular files and directories are still marked as originating from FAT/VFAT/NTFS. Also set "external file attributes" (attr2) to 0 for regular files and 16 for directories (FAT attribute), and to the file mode for symlinks. We could always set the creator_version to Unix and include the mode, but then Info-ZIP unzip would set the mode of the extracted files to *exactly* the value stored in attr2. The FAT trick makes it apply the umask instead. Note: FAT has no executable bit, so this information is not stored in the ZIP file. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>

Rene Scharfe committed Oct 7, 2006 at 01:47 UTC 62cdce17c57a28240048c5064fab57edae19657f
2 files changed +44 -4
archive-zip.c
+9 -4
@@ -145,6 +145,7 @@ static int write_zip_entry(const unsigned char *sha1,
145 {
146 struct zip_local_header header;
147 struct zip_dir_header dirent;
148 + unsigned long attr2;
149 unsigned long compressed_size;
150 unsigned long uncompressed_size;
151 unsigned long crc;
@@ -172,12 +173,16 @@ static int write_zip_entry(const unsigned char *sha1,
173
174 if (S_ISDIR(mode)) {
175 method = 0;
176 + attr2 = 16;
177 result = READ_TREE_RECURSIVE;
178 out = NULL;
179 uncompressed_size = 0;
180 compressed_size = 0;
179 - } else if (S_ISREG(mode)) {
180 - method = zlib_compression_level == 0 ? 0 : 8;
181 + } else if (S_ISREG(mode) || S_ISLNK(mode)) {
182 + method = 0;
183 + attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : 0;
184 + if (S_ISREG(mode) && zlib_compression_level != 0)
185 + method = 8;
186 result = 0;
187 buffer = read_sha1_file(sha1, type, &size);
188 if (!buffer)
@@ -213,7 +218,7 @@ static int write_zip_entry(const unsigned char *sha1,
218 }
219
220 copy_le32(dirent.magic, 0x02014b50);
216 - copy_le16(dirent.creator_version, 0);
221 + copy_le16(dirent.creator_version, S_ISLNK(mode) ? 0x0317 : 0);
222 copy_le16(dirent.version, 10);
223 copy_le16(dirent.flags, 0);
224 copy_le16(dirent.compression_method, method);
@@ -227,7 +232,7 @@ static int write_zip_entry(const unsigned char *sha1,
232 copy_le16(dirent.comment_length, 0);
233 copy_le16(dirent.disk, 0);
234 copy_le16(dirent.attr1, 0);
230 - copy_le32(dirent.attr2, 0);
235 + copy_le32(dirent.attr2, attr2);
236 copy_le32(dirent.offset, zip_offset);
237 memcpy(zip_dir + zip_dir_offset, &dirent, sizeof(struct zip_dir_header));
238 zip_dir_offset += sizeof(struct zip_dir_header);
t/t5000-tar-tree.sh
+35
@@ -26,6 +26,7 @@ commit id embedding:
26
27 . ./test-lib.sh
28 TAR=${TAR:-tar}
29 +UNZIP=${UNZIP:-unzip}
30
31 test_expect_success \
32 'populate workdir' \
@@ -95,4 +96,38 @@ test_expect_success \
96 'validate file contents with prefix' \
97 'diff -r a c/prefix/a'
98
99 +test_expect_success \
100 + 'git-archive --format=zip' \
101 + 'git-archive --format=zip HEAD >d.zip'
102 +
103 +test_expect_success \
104 + 'extract ZIP archive' \
105 + '(mkdir d && cd d && $UNZIP ../d.zip)'
106 +
107 +test_expect_success \
108 + 'validate filenames' \
109 + '(cd d/a && find .) | sort >d.lst &&
110 + diff a.lst d.lst'
111 +
112 +test_expect_success \
113 + 'validate file contents' \
114 + 'diff -r a d/a'
115 +
116 +test_expect_success \
117 + 'git-archive --format=zip with prefix' \
118 + 'git-archive --format=zip --prefix=prefix/ HEAD >e.zip'
119 +
120 +test_expect_success \
121 + 'extract ZIP archive with prefix' \
122 + '(mkdir e && cd e && $UNZIP ../e.zip)'
123 +
124 +test_expect_success \
125 + 'validate filenames with prefix' \
126 + '(cd e/prefix/a && find .) | sort >e.lst &&
127 + diff a.lst e.lst'
128 +
129 +test_expect_success \
130 + 'validate file contents with prefix' \
131 + 'diff -r a e/prefix/a'
132 +
133 test_done