builtin-zip-tree.con commit Add git-upload-archive (39345a2)
   1/*
   2 * Copyright (c) 2006 Rene Scharfe
   3 */
   4#include <time.h>
   5#include "cache.h"
   6#include "commit.h"
   7#include "blob.h"
   8#include "tree.h"
   9#include "quote.h"
  10#include "builtin.h"
  11#include "archive.h"
  12
  13static const char zip_tree_usage[] =
  14"git-zip-tree [-0|...|-9] <tree-ish> [ <base> ]";
  15
  16static int zip_date;
  17static int zip_time;
  18
  19static unsigned char *zip_dir;
  20static unsigned int zip_dir_size;
  21
  22static unsigned int zip_offset;
  23static unsigned int zip_dir_offset;
  24static unsigned int zip_dir_entries;
  25
  26#define ZIP_DIRECTORY_MIN_SIZE  (1024 * 1024)
  27
  28struct zip_local_header {
  29        unsigned char magic[4];
  30        unsigned char version[2];
  31        unsigned char flags[2];
  32        unsigned char compression_method[2];
  33        unsigned char mtime[2];
  34        unsigned char mdate[2];
  35        unsigned char crc32[4];
  36        unsigned char compressed_size[4];
  37        unsigned char size[4];
  38        unsigned char filename_length[2];
  39        unsigned char extra_length[2];
  40};
  41
  42struct zip_dir_header {
  43        unsigned char magic[4];
  44        unsigned char creator_version[2];
  45        unsigned char version[2];
  46        unsigned char flags[2];
  47        unsigned char compression_method[2];
  48        unsigned char mtime[2];
  49        unsigned char mdate[2];
  50        unsigned char crc32[4];
  51        unsigned char compressed_size[4];
  52        unsigned char size[4];
  53        unsigned char filename_length[2];
  54        unsigned char extra_length[2];
  55        unsigned char comment_length[2];
  56        unsigned char disk[2];
  57        unsigned char attr1[2];
  58        unsigned char attr2[4];
  59        unsigned char offset[4];
  60};
  61
  62struct zip_dir_trailer {
  63        unsigned char magic[4];
  64        unsigned char disk[2];
  65        unsigned char directory_start_disk[2];
  66        unsigned char entries_on_this_disk[2];
  67        unsigned char entries[2];
  68        unsigned char size[4];
  69        unsigned char offset[4];
  70        unsigned char comment_length[2];
  71};
  72
  73static void copy_le16(unsigned char *dest, unsigned int n)
  74{
  75        dest[0] = 0xff & n;
  76        dest[1] = 0xff & (n >> 010);
  77}
  78
  79static void copy_le32(unsigned char *dest, unsigned int n)
  80{
  81        dest[0] = 0xff & n;
  82        dest[1] = 0xff & (n >> 010);
  83        dest[2] = 0xff & (n >> 020);
  84        dest[3] = 0xff & (n >> 030);
  85}
  86
  87static void *zlib_deflate(void *data, unsigned long size,
  88                          unsigned long *compressed_size)
  89{
  90        z_stream stream;
  91        unsigned long maxsize;
  92        void *buffer;
  93        int result;
  94
  95        memset(&stream, 0, sizeof(stream));
  96        deflateInit(&stream, zlib_compression_level);
  97        maxsize = deflateBound(&stream, size);
  98        buffer = xmalloc(maxsize);
  99
 100        stream.next_in = data;
 101        stream.avail_in = size;
 102        stream.next_out = buffer;
 103        stream.avail_out = maxsize;
 104
 105        do {
 106                result = deflate(&stream, Z_FINISH);
 107        } while (result == Z_OK);
 108
 109        if (result != Z_STREAM_END) {
 110                free(buffer);
 111                return NULL;
 112        }
 113
 114        deflateEnd(&stream);
 115        *compressed_size = stream.total_out;
 116
 117        return buffer;
 118}
 119
 120static char *construct_path(const char *base, int baselen,
 121                            const char *filename, int isdir, int *pathlen)
 122{
 123        int filenamelen = strlen(filename);
 124        int len = baselen + filenamelen;
 125        char *path, *p;
 126
 127        if (isdir)
 128                len++;
 129        p = path = xmalloc(len + 1);
 130
 131        memcpy(p, base, baselen);
 132        p += baselen;
 133        memcpy(p, filename, filenamelen);
 134        p += filenamelen;
 135        if (isdir)
 136                *p++ = '/';
 137        *p = '\0';
 138
 139        *pathlen = len;
 140
 141        return path;
 142}
 143
 144static int write_zip_entry(const unsigned char *sha1,
 145                           const char *base, int baselen,
 146                           const char *filename, unsigned mode, int stage)
 147{
 148        struct zip_local_header header;
 149        struct zip_dir_header dirent;
 150        unsigned long compressed_size;
 151        unsigned long uncompressed_size;
 152        unsigned long crc;
 153        unsigned long direntsize;
 154        unsigned long size;
 155        int method;
 156        int result = -1;
 157        int pathlen;
 158        unsigned char *out;
 159        char *path;
 160        char type[20];
 161        void *buffer = NULL;
 162        void *deflated = NULL;
 163
 164        crc = crc32(0, Z_NULL, 0);
 165
 166        path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
 167        if (pathlen > 0xffff) {
 168                error("path too long (%d chars, SHA1: %s): %s", pathlen,
 169                      sha1_to_hex(sha1), path);
 170                goto out;
 171        }
 172
 173        if (S_ISDIR(mode)) {
 174                method = 0;
 175                result = READ_TREE_RECURSIVE;
 176                out = NULL;
 177                uncompressed_size = 0;
 178                compressed_size = 0;
 179        } else if (S_ISREG(mode)) {
 180                method = zlib_compression_level == 0 ? 0 : 8;
 181                result = 0;
 182                buffer = read_sha1_file(sha1, type, &size);
 183                if (!buffer)
 184                        die("cannot read %s", sha1_to_hex(sha1));
 185                crc = crc32(crc, buffer, size);
 186                out = buffer;
 187                uncompressed_size = size;
 188                compressed_size = size;
 189        } else {
 190                error("unsupported file mode: 0%o (SHA1: %s)", mode,
 191                      sha1_to_hex(sha1));
 192                goto out;
 193        }
 194
 195        if (method == 8) {
 196                deflated = zlib_deflate(buffer, size, &compressed_size);
 197                if (deflated && compressed_size - 6 < size) {
 198                        /* ZLIB --> raw compressed data (see RFC 1950) */
 199                        /* CMF and FLG ... */
 200                        out = (unsigned char *)deflated + 2;
 201                        compressed_size -= 6;   /* ... and ADLER32 */
 202                } else {
 203                        method = 0;
 204                        compressed_size = size;
 205                }
 206        }
 207
 208        /* make sure we have enough free space in the dictionary */
 209        direntsize = sizeof(struct zip_dir_header) + pathlen;
 210        while (zip_dir_size < zip_dir_offset + direntsize) {
 211                zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
 212                zip_dir = xrealloc(zip_dir, zip_dir_size);
 213        }
 214
 215        copy_le32(dirent.magic, 0x02014b50);
 216        copy_le16(dirent.creator_version, 0);
 217        copy_le16(dirent.version, 20);
 218        copy_le16(dirent.flags, 0);
 219        copy_le16(dirent.compression_method, method);
 220        copy_le16(dirent.mtime, zip_time);
 221        copy_le16(dirent.mdate, zip_date);
 222        copy_le32(dirent.crc32, crc);
 223        copy_le32(dirent.compressed_size, compressed_size);
 224        copy_le32(dirent.size, uncompressed_size);
 225        copy_le16(dirent.filename_length, pathlen);
 226        copy_le16(dirent.extra_length, 0);
 227        copy_le16(dirent.comment_length, 0);
 228        copy_le16(dirent.disk, 0);
 229        copy_le16(dirent.attr1, 0);
 230        copy_le32(dirent.attr2, 0);
 231        copy_le32(dirent.offset, zip_offset);
 232        memcpy(zip_dir + zip_dir_offset, &dirent, sizeof(struct zip_dir_header));
 233        zip_dir_offset += sizeof(struct zip_dir_header);
 234        memcpy(zip_dir + zip_dir_offset, path, pathlen);
 235        zip_dir_offset += pathlen;
 236        zip_dir_entries++;
 237
 238        copy_le32(header.magic, 0x04034b50);
 239        copy_le16(header.version, 20);
 240        copy_le16(header.flags, 0);
 241        copy_le16(header.compression_method, method);
 242        copy_le16(header.mtime, zip_time);
 243        copy_le16(header.mdate, zip_date);
 244        copy_le32(header.crc32, crc);
 245        copy_le32(header.compressed_size, compressed_size);
 246        copy_le32(header.size, uncompressed_size);
 247        copy_le16(header.filename_length, pathlen);
 248        copy_le16(header.extra_length, 0);
 249        write_or_die(1, &header, sizeof(struct zip_local_header));
 250        zip_offset += sizeof(struct zip_local_header);
 251        write_or_die(1, path, pathlen);
 252        zip_offset += pathlen;
 253        if (compressed_size > 0) {
 254                write_or_die(1, out, compressed_size);
 255                zip_offset += compressed_size;
 256        }
 257
 258out:
 259        free(buffer);
 260        free(deflated);
 261        free(path);
 262
 263        return result;
 264}
 265
 266static void write_zip_trailer(const unsigned char *sha1)
 267{
 268        struct zip_dir_trailer trailer;
 269
 270        copy_le32(trailer.magic, 0x06054b50);
 271        copy_le16(trailer.disk, 0);
 272        copy_le16(trailer.directory_start_disk, 0);
 273        copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
 274        copy_le16(trailer.entries, zip_dir_entries);
 275        copy_le32(trailer.size, zip_dir_offset);
 276        copy_le32(trailer.offset, zip_offset);
 277        copy_le16(trailer.comment_length, sha1 ? 40 : 0);
 278
 279        write_or_die(1, zip_dir, zip_dir_offset);
 280        write_or_die(1, &trailer, sizeof(struct zip_dir_trailer));
 281        if (sha1)
 282                write_or_die(1, sha1_to_hex(sha1), 40);
 283}
 284
 285static void dos_time(time_t *time, int *dos_date, int *dos_time)
 286{
 287        struct tm *t = localtime(time);
 288
 289        *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
 290                    (t->tm_year + 1900 - 1980) * 512;
 291        *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
 292}
 293
 294int cmd_zip_tree(int argc, const char **argv, const char *prefix)
 295{
 296        unsigned char sha1[20];
 297        struct tree *tree;
 298        struct commit *commit;
 299        time_t archive_time;
 300        char *base;
 301        int baselen;
 302
 303        git_config(git_default_config);
 304
 305        if (argc > 1 && argv[1][0] == '-') {
 306                if (isdigit(argv[1][1]) && argv[1][2] == '\0') {
 307                        zlib_compression_level = argv[1][1] - '0';
 308                        argc--;
 309                        argv++;
 310                }
 311        }
 312
 313        switch (argc) {
 314        case 3:
 315                base = xstrdup(argv[2]);
 316                baselen = strlen(base);
 317                break;
 318        case 2:
 319                base = xstrdup("");
 320                baselen = 0;
 321                break;
 322        default:
 323                usage(zip_tree_usage);
 324        }
 325
 326        if (get_sha1(argv[1], sha1))
 327                die("Not a valid object name %s", argv[1]);
 328
 329        commit = lookup_commit_reference_gently(sha1, 1);
 330        archive_time = commit ? commit->date : time(NULL);
 331        dos_time(&archive_time, &zip_date, &zip_time);
 332
 333        zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
 334        zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
 335
 336        tree = parse_tree_indirect(sha1);
 337        if (!tree)
 338                die("not a tree object");
 339
 340        if (baselen > 0) {
 341                write_zip_entry(tree->object.sha1, "", 0, base, 040777, 0);
 342                base = xrealloc(base, baselen + 1);
 343                base[baselen] = '/';
 344                baselen++;
 345                base[baselen] = '\0';
 346        }
 347        read_tree_recursive(tree, base, baselen, 0, NULL, write_zip_entry);
 348        write_zip_trailer(commit ? commit->object.sha1 : NULL);
 349
 350        free(zip_dir);
 351        free(base);
 352
 353        return 0;
 354}
 355
 356int write_zip_archive(struct archiver_args *args)
 357{
 358        int plen = strlen(args->base);
 359
 360        dos_time(&args->time, &zip_date, &zip_time);
 361
 362        zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
 363        zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
 364
 365        if (args->base && plen > 0 && args->base[plen - 1] == '/') {
 366                char *base = strdup(args->base);
 367                int baselen = strlen(base);
 368
 369                while (baselen > 0 && base[baselen - 1] == '/')
 370                        base[--baselen] = '\0';
 371                write_zip_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
 372                free(base);
 373        }
 374        read_tree_recursive(args->tree, args->base, plen, 0,
 375                            args->pathspec, write_zip_entry);
 376        write_zip_trailer(args->commit_sha1);
 377
 378        free(zip_dir);
 379
 380        return 0;
 381}