diff options
Diffstat (limited to 'archive-zip.c')
-rw-r--r-- | archive-zip.c | 92 |
1 files changed, 52 insertions, 40 deletions
diff --git a/archive-zip.c b/archive-zip.c index e8913e5a26..e9f426298b 100644 --- a/archive-zip.c +++ b/archive-zip.c @@ -6,6 +6,7 @@ #include "archive.h" #include "streaming.h" #include "utf8.h" +#include "object-store.h" #include "userdiff.h" #include "xdiff-interface.h" @@ -23,6 +24,11 @@ static unsigned int max_creator_version; #define ZIP_STREAM (1 << 3) #define ZIP_UTF8 (1 << 11) +enum zip_method { + ZIP_METHOD_STORE = 0, + ZIP_METHOD_DEFLATE = 8 +}; + struct zip_local_header { unsigned char magic[4]; unsigned char version[2]; @@ -263,9 +269,10 @@ static int has_only_ascii(const char *s) } } -static int entry_is_binary(const char *path, const void *buffer, size_t size) +static int entry_is_binary(struct index_state *istate, const char *path, + const void *buffer, size_t size) { - struct userdiff_driver *driver = userdiff_find_by_path(path); + struct userdiff_driver *driver = userdiff_find_by_path(istate, path); if (!driver) driver = userdiff_find_by_name("default"); if (driver->binary != -1) @@ -276,7 +283,7 @@ static int entry_is_binary(const char *path, const void *buffer, size_t size) #define STREAM_BUFFER_SIZE (1024 * 16) static int write_zip_entry(struct archiver_args *args, - const unsigned char *sha1, + const struct object_id *oid, const char *path, size_t pathlen, unsigned int mode) { @@ -289,7 +296,7 @@ static int write_zip_entry(struct archiver_args *args, unsigned long attr2; unsigned long compressed_size; unsigned long crc; - int method; + enum zip_method method; unsigned char *out; void *deflated = NULL; void *buffer; @@ -309,67 +316,70 @@ static int write_zip_entry(struct archiver_args *args, if (is_utf8(path)) flags |= ZIP_UTF8; else - warning("Path is not valid UTF-8: %s", path); + warning(_("path is not valid UTF-8: %s"), path); } if (pathlen > 0xffff) { - return error("path too long (%d chars, SHA1: %s): %s", - (int)pathlen, sha1_to_hex(sha1), path); + return error(_("path too long (%d chars, SHA1: %s): %s"), + (int)pathlen, oid_to_hex(oid), path); } if (S_ISDIR(mode) || S_ISGITLINK(mode)) { - method = 0; + method = ZIP_METHOD_STORE; attr2 = 16; out = NULL; size = 0; compressed_size = 0; buffer = NULL; } else if (S_ISREG(mode) || S_ISLNK(mode)) { - enum object_type type = sha1_object_info(sha1, &size); + enum object_type type = oid_object_info(args->repo, oid, + &size); - method = 0; + method = ZIP_METHOD_STORE; attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : (mode & 0111) ? ((mode) << 16) : 0; if (S_ISLNK(mode) || (mode & 0111)) creator_version = 0x0317; if (S_ISREG(mode) && args->compression_level != 0 && size > 0) - method = 8; + method = ZIP_METHOD_DEFLATE; if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert && size > big_file_threshold) { - stream = open_istream(sha1, &type, &size, NULL); + stream = open_istream(args->repo, oid, &type, &size, + NULL); if (!stream) - return error("cannot stream blob %s", - sha1_to_hex(sha1)); + return error(_("cannot stream blob %s"), + oid_to_hex(oid)); flags |= ZIP_STREAM; out = buffer = NULL; } else { - buffer = sha1_file_to_archive(args, path, sha1, mode, - &type, &size); + buffer = object_file_to_archive(args, path, oid, mode, + &type, &size); if (!buffer) - return error("cannot read %s", - sha1_to_hex(sha1)); + return error(_("cannot read %s"), + oid_to_hex(oid)); crc = crc32(crc, buffer, size); - is_binary = entry_is_binary(path_without_prefix, + is_binary = entry_is_binary(args->repo->index, + path_without_prefix, buffer, size); out = buffer; } - compressed_size = (method == 0) ? size : 0; + compressed_size = (method == ZIP_METHOD_STORE) ? size : 0; } else { - return error("unsupported file mode: 0%o (SHA1: %s)", mode, - sha1_to_hex(sha1)); + return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode, + oid_to_hex(oid)); } if (creator_version > max_creator_version) max_creator_version = creator_version; - if (buffer && method == 8) { + if (buffer && method == ZIP_METHOD_DEFLATE) { out = deflated = zlib_deflate_raw(buffer, size, args->compression_level, &compressed_size); if (!out || compressed_size >= size) { out = buffer; - method = 0; + method = ZIP_METHOD_STORE; compressed_size = size; } } @@ -416,7 +426,7 @@ static int write_zip_entry(struct archiver_args *args, zip_offset += ZIP64_EXTRA_SIZE; } - if (stream && method == 0) { + if (stream && method == ZIP_METHOD_STORE) { unsigned char buf[STREAM_BUFFER_SIZE]; ssize_t readlen; @@ -426,7 +436,8 @@ static int write_zip_entry(struct archiver_args *args, break; crc = crc32(crc, buf, readlen); if (is_binary == -1) - is_binary = entry_is_binary(path_without_prefix, + is_binary = entry_is_binary(args->repo->index, + path_without_prefix, buf, readlen); write_or_die(1, buf, readlen); } @@ -438,7 +449,7 @@ static int write_zip_entry(struct archiver_args *args, zip_offset += compressed_size; write_zip_data_desc(size, compressed_size, crc); - } else if (stream && method == 8) { + } else if (stream && method == ZIP_METHOD_DEFLATE) { unsigned char buf[STREAM_BUFFER_SIZE]; ssize_t readlen; git_zstream zstream; @@ -458,14 +469,15 @@ static int write_zip_entry(struct archiver_args *args, break; crc = crc32(crc, buf, readlen); if (is_binary == -1) - is_binary = entry_is_binary(path_without_prefix, + is_binary = entry_is_binary(args->repo->index, + path_without_prefix, buf, readlen); zstream.next_in = buf; zstream.avail_in = readlen; result = git_deflate(&zstream, 0); if (result != Z_OK) - die("deflate error (%d)", result); + die(_("deflate error (%d)"), result); out_len = zstream.next_out - compressed; if (out_len > 0) { @@ -571,7 +583,7 @@ static void write_zip64_trailer(void) write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE); } -static void write_zip_trailer(const unsigned char *sha1) +static void write_zip_trailer(const struct object_id *oid) { struct zip_dir_trailer trailer; int clamped = 0; @@ -584,31 +596,31 @@ static void write_zip_trailer(const unsigned char *sha1) copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped); copy_le32(trailer.size, zip_dir.len); copy_le32_clamp(trailer.offset, zip_offset, &clamped); - copy_le16(trailer.comment_length, sha1 ? GIT_SHA1_HEXSZ : 0); + copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0); write_or_die(1, zip_dir.buf, zip_dir.len); if (clamped) write_zip64_trailer(); write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE); - if (sha1) - write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ); + if (oid) + write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz); } static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time) { time_t time; - struct tm *t; + struct tm tm; if (date_overflows(*timestamp)) - die("timestamp too large for this system: %"PRItime, + die(_("timestamp too large for this system: %"PRItime), *timestamp); time = (time_t)*timestamp; - t = localtime(&time); + localtime_r(&time, &tm); *timestamp = time; - *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 + - (t->tm_year + 1900 - 1980) * 512; - *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048; + *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 + + (tm.tm_year + 1900 - 1980) * 512; + *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048; } static int archive_zip_config(const char *var, const char *value, void *data) @@ -629,7 +641,7 @@ static int write_zip_archive(const struct archiver *ar, err = write_archive_entries(args, write_zip_entry); if (!err) - write_zip_trailer(args->commit_sha1); + write_zip_trailer(args->commit_oid); strbuf_release(&zip_dir); |