archive-zip: support UTF-8 paths

Set general purpose flag 11 if we encounter a path that contains non-ASCII characters. We assume that all paths are given as UTF-8; no conversion is done. The flag seems to be ignored by unzip unless we also mark the archive entry as coming from a Unix system. This is done by setting the field creator_version ("version made by" in the standard[1]) to 0x03NN. The NN part represents the version of the standard supported by us, and this patch sets it to 3f (for version 6.3) for Unix paths. We keep creator_version set to 0 (FAT filesystem, standard version 0) in the non-special cases, as before. But when we declare a file to have a Unix path, then we have to set the file mode as well, or unzip will extract the files with the permission set 0000, i.e. inaccessible by all. [1] http://www.pkware.com/documents/casestudies/APPNOTE.TXT Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 4, 2012 at 22:23 UTC 2162bd8cc461d6c3a12ab81c5db5a44bf5ecabc3
1 file changed +21 -6
archive-zip.c
+21 -6
@@ -4,6 +4,8 @@
4 #include "cache.h"
5 #include "archive.h"
6 #include "streaming.h"
7 +#include "commit.h"
8 +#include "utf8.h"
9
10 static int zip_date;
11 static int zip_time;
@@ -16,7 +18,8 @@ static unsigned int zip_dir_offset;
18 static unsigned int zip_dir_entries;
19
20 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
19 -#define ZIP_STREAM (8)
21 +#define ZIP_STREAM (1 << 3)
22 +#define ZIP_UTF8 (1 << 11)
23
24 struct zip_local_header {
25 unsigned char magic[4];
@@ -173,7 +176,8 @@ static int write_zip_entry(struct archiver_args *args,
176 {
177 struct zip_local_header header;
178 struct zip_dir_header dirent;
176 - unsigned long attr2;
179 + unsigned int creator_version = 0;
180 + unsigned long attr2 = 0;
181 unsigned long compressed_size;
182 unsigned long crc;
183 unsigned long direntsize;
@@ -187,6 +191,13 @@ static int write_zip_entry(struct archiver_args *args,
191
192 crc = crc32(0, NULL, 0);
193
194 + if (has_non_ascii(path)) {
195 + if (is_utf8(path))
196 + flags |= ZIP_UTF8;
197 + else
198 + warning("Path is not valid UTF-8: %s", path);
199 + }
200 +
201 if (pathlen > 0xffff) {
202 return error("path too long (%d chars, SHA1: %s): %s",
203 (int)pathlen, sha1_to_hex(sha1), path);
@@ -204,10 +215,15 @@ static int write_zip_entry(struct archiver_args *args,
215 enum object_type type = sha1_object_info(sha1, &size);
216
217 method = 0;
207 - attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
208 - (mode & 0111) ? ((mode) << 16) : 0;
218 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
219 method = 8;
220 + if (S_ISLNK(mode) || (mode & 0111) || (flags & ZIP_UTF8)) {
221 + creator_version = 0x033f;
222 + attr2 = mode;
223 + if (S_ISLNK(mode))
224 + attr2 |= 0777;
225 + attr2 <<= 16;
226 + }
227 compressed_size = size;
228
229 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
@@ -254,8 +270,7 @@ static int write_zip_entry(struct archiver_args *args,
270 }
271
272 copy_le32(dirent.magic, 0x02014b50);
257 - copy_le16(dirent.creator_version,
258 - S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
273 + copy_le16(dirent.creator_version, creator_version);
274 copy_le16(dirent.version, 10);
275 copy_le16(dirent.flags, flags);
276 copy_le16(dirent.compression_method, method);