Code

Define compat version of mkdtemp for systems lacking it
[git.git] / archive-zip.c
index 28e7352e98eaa3bcbc3c86ce772bae35a605ceb2..74e30f6205f41112dc2bafe9371a790aca55f70c 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * Copyright (c) 2006 Rene Scharfe
  */
-#include <time.h>
 #include "cache.h"
 #include "commit.h"
 #include "blob.h"
@@ -13,6 +12,7 @@
 static int verbose;
 static int zip_date;
 static int zip_time;
+static const struct commit *commit;
 
 static unsigned char *zip_dir;
 static unsigned int zip_dir_size;
@@ -35,6 +35,7 @@ struct zip_local_header {
        unsigned char size[4];
        unsigned char filename_length[2];
        unsigned char extra_length[2];
+       unsigned char _end[1];
 };
 
 struct zip_dir_header {
@@ -55,6 +56,7 @@ struct zip_dir_header {
        unsigned char attr1[2];
        unsigned char attr2[4];
        unsigned char offset[4];
+       unsigned char _end[1];
 };
 
 struct zip_dir_trailer {
@@ -66,8 +68,18 @@ struct zip_dir_trailer {
        unsigned char size[4];
        unsigned char offset[4];
        unsigned char comment_length[2];
+       unsigned char _end[1];
 };
 
+/*
+ * On ARM, padding is added at the end of the struct, so a simple
+ * sizeof(struct ...) reports two bytes more than the payload size
+ * we're interested in.
+ */
+#define ZIP_LOCAL_HEADER_SIZE  offsetof(struct zip_local_header, _end)
+#define ZIP_DIR_HEADER_SIZE    offsetof(struct zip_dir_header, _end)
+#define ZIP_DIR_TRAILER_SIZE   offsetof(struct zip_dir_trailer, _end)
+
 static void copy_le16(unsigned char *dest, unsigned int n)
 {
        dest[0] = 0xff & n;
@@ -156,11 +168,11 @@ static int write_zip_entry(const unsigned char *sha1,
        int pathlen;
        unsigned char *out;
        char *path;
-       char type[20];
+       enum object_type type;
        void *buffer = NULL;
        void *deflated = NULL;
 
-       crc = crc32(0, Z_NULL, 0);
+       crc = crc32(0, NULL, 0);
 
        path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
        if (verbose)
@@ -171,20 +183,22 @@ static int write_zip_entry(const unsigned char *sha1,
                goto out;
        }
 
-       if (S_ISDIR(mode)) {
+       if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
                method = 0;
                attr2 = 16;
-               result = READ_TREE_RECURSIVE;
+               result = (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
                out = NULL;
                uncompressed_size = 0;
                compressed_size = 0;
        } else if (S_ISREG(mode) || S_ISLNK(mode)) {
                method = 0;
-               attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : 0;
+               attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
+                       (mode & 0111) ? ((mode) << 16) : 0;
                if (S_ISREG(mode) && zlib_compression_level != 0)
                        method = 8;
                result = 0;
-               buffer = read_sha1_file(sha1, type, &size);
+               buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
+                                             commit);
                if (!buffer)
                        die("cannot read %s", sha1_to_hex(sha1));
                crc = crc32(crc, buffer, size);
@@ -211,14 +225,15 @@ static int write_zip_entry(const unsigned char *sha1,
        }
 
        /* make sure we have enough free space in the dictionary */
-       direntsize = sizeof(struct zip_dir_header) + pathlen;
+       direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
        while (zip_dir_size < zip_dir_offset + direntsize) {
                zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
                zip_dir = xrealloc(zip_dir, zip_dir_size);
        }
 
        copy_le32(dirent.magic, 0x02014b50);
-       copy_le16(dirent.creator_version, S_ISLNK(mode) ? 0x0317 : 0);
+       copy_le16(dirent.creator_version,
+               S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
        copy_le16(dirent.version, 10);
        copy_le16(dirent.flags, 0);
        copy_le16(dirent.compression_method, method);
@@ -234,8 +249,8 @@ static int write_zip_entry(const unsigned char *sha1,
        copy_le16(dirent.attr1, 0);
        copy_le32(dirent.attr2, attr2);
        copy_le32(dirent.offset, zip_offset);
-       memcpy(zip_dir + zip_dir_offset, &dirent, sizeof(struct zip_dir_header));
-       zip_dir_offset += sizeof(struct zip_dir_header);
+       memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
+       zip_dir_offset += ZIP_DIR_HEADER_SIZE;
        memcpy(zip_dir + zip_dir_offset, path, pathlen);
        zip_dir_offset += pathlen;
        zip_dir_entries++;
@@ -251,8 +266,8 @@ static int write_zip_entry(const unsigned char *sha1,
        copy_le32(header.size, uncompressed_size);
        copy_le16(header.filename_length, pathlen);
        copy_le16(header.extra_length, 0);
-       write_or_die(1, &header, sizeof(struct zip_local_header));
-       zip_offset += sizeof(struct zip_local_header);
+       write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
+       zip_offset += ZIP_LOCAL_HEADER_SIZE;
        write_or_die(1, path, pathlen);
        zip_offset += pathlen;
        if (compressed_size > 0) {
@@ -282,7 +297,7 @@ static void write_zip_trailer(const unsigned char *sha1)
        copy_le16(trailer.comment_length, sha1 ? 40 : 0);
 
        write_or_die(1, zip_dir, zip_dir_offset);
-       write_or_die(1, &trailer, sizeof(struct zip_dir_trailer));
+       write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
        if (sha1)
                write_or_die(1, sha1_to_hex(sha1), 40);
 }
@@ -305,6 +320,7 @@ int write_zip_archive(struct archiver_args *args)
        zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
        zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
        verbose = args->verbose;
+       commit = args->commit;
 
        if (args->base && plen > 0 && args->base[plen - 1] == '/') {
                char *base = xstrdup(args->base);