archive-tar.con commit git-svn: correctly track diff-less copies with do_switch (2fa6a23)
   1/*
   2 * Copyright (c) 2005, 2006 Rene Scharfe
   3 */
   4#include "cache.h"
   5#include "commit.h"
   6#include "strbuf.h"
   7#include "tar.h"
   8#include "builtin.h"
   9#include "archive.h"
  10
  11#define RECORDSIZE      (512)
  12#define BLOCKSIZE       (RECORDSIZE * 20)
  13
  14static char block[BLOCKSIZE];
  15static unsigned long offset;
  16
  17static time_t archive_time;
  18static int tar_umask = 002;
  19static int verbose;
  20
  21/* writes out the whole block, but only if it is full */
  22static void write_if_needed(void)
  23{
  24        if (offset == BLOCKSIZE) {
  25                write_or_die(1, block, BLOCKSIZE);
  26                offset = 0;
  27        }
  28}
  29
  30/*
  31 * queues up writes, so that all our write(2) calls write exactly one
  32 * full block; pads writes to RECORDSIZE
  33 */
  34static void write_blocked(const void *data, unsigned long size)
  35{
  36        const char *buf = data;
  37        unsigned long tail;
  38
  39        if (offset) {
  40                unsigned long chunk = BLOCKSIZE - offset;
  41                if (size < chunk)
  42                        chunk = size;
  43                memcpy(block + offset, buf, chunk);
  44                size -= chunk;
  45                offset += chunk;
  46                buf += chunk;
  47                write_if_needed();
  48        }
  49        while (size >= BLOCKSIZE) {
  50                write_or_die(1, buf, BLOCKSIZE);
  51                size -= BLOCKSIZE;
  52                buf += BLOCKSIZE;
  53        }
  54        if (size) {
  55                memcpy(block + offset, buf, size);
  56                offset += size;
  57        }
  58        tail = offset % RECORDSIZE;
  59        if (tail)  {
  60                memset(block + offset, 0, RECORDSIZE - tail);
  61                offset += RECORDSIZE - tail;
  62        }
  63        write_if_needed();
  64}
  65
  66/*
  67 * The end of tar archives is marked by 2*512 nul bytes and after that
  68 * follows the rest of the block (if any).
  69 */
  70static void write_trailer(void)
  71{
  72        int tail = BLOCKSIZE - offset;
  73        memset(block + offset, 0, tail);
  74        write_or_die(1, block, BLOCKSIZE);
  75        if (tail < 2 * RECORDSIZE) {
  76                memset(block, 0, offset);
  77                write_or_die(1, block, BLOCKSIZE);
  78        }
  79}
  80
  81static void strbuf_append_string(struct strbuf *sb, const char *s)
  82{
  83        int slen = strlen(s);
  84        int total = sb->len + slen;
  85        if (total > sb->alloc) {
  86                sb->buf = xrealloc(sb->buf, total);
  87                sb->alloc = total;
  88        }
  89        memcpy(sb->buf + sb->len, s, slen);
  90        sb->len = total;
  91}
  92
  93/*
  94 * pax extended header records have the format "%u %s=%s\n".  %u contains
  95 * the size of the whole string (including the %u), the first %s is the
  96 * keyword, the second one is the value.  This function constructs such a
  97 * string and appends it to a struct strbuf.
  98 */
  99static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
 100                                     const char *value, unsigned int valuelen)
 101{
 102        char *p;
 103        int len, total, tmp;
 104
 105        /* "%u %s=%s\n" */
 106        len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
 107        for (tmp = len; tmp > 9; tmp /= 10)
 108                len++;
 109
 110        total = sb->len + len;
 111        if (total > sb->alloc) {
 112                sb->buf = xrealloc(sb->buf, total);
 113                sb->alloc = total;
 114        }
 115
 116        p = sb->buf;
 117        p += sprintf(p, "%u %s=", len, keyword);
 118        memcpy(p, value, valuelen);
 119        p += valuelen;
 120        *p = '\n';
 121        sb->len = total;
 122}
 123
 124static unsigned int ustar_header_chksum(const struct ustar_header *header)
 125{
 126        char *p = (char *)header;
 127        unsigned int chksum = 0;
 128        while (p < header->chksum)
 129                chksum += *p++;
 130        chksum += sizeof(header->chksum) * ' ';
 131        p += sizeof(header->chksum);
 132        while (p < (char *)header + sizeof(struct ustar_header))
 133                chksum += *p++;
 134        return chksum;
 135}
 136
 137static int get_path_prefix(const struct strbuf *path, int maxlen)
 138{
 139        int i = path->len;
 140        if (i > maxlen)
 141                i = maxlen;
 142        do {
 143                i--;
 144        } while (i > 0 && path->buf[i] != '/');
 145        return i;
 146}
 147
 148static void write_entry(const unsigned char *sha1, struct strbuf *path,
 149                        unsigned int mode, void *buffer, unsigned long size)
 150{
 151        struct ustar_header header;
 152        struct strbuf ext_header;
 153
 154        memset(&header, 0, sizeof(header));
 155        ext_header.buf = NULL;
 156        ext_header.len = ext_header.alloc = 0;
 157
 158        if (!sha1) {
 159                *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
 160                mode = 0100666;
 161                strcpy(header.name, "pax_global_header");
 162        } else if (!path) {
 163                *header.typeflag = TYPEFLAG_EXT_HEADER;
 164                mode = 0100666;
 165                sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
 166        } else {
 167                if (verbose)
 168                        fprintf(stderr, "%.*s\n", path->len, path->buf);
 169                if (S_ISDIR(mode)) {
 170                        *header.typeflag = TYPEFLAG_DIR;
 171                        mode = (mode | 0777) & ~tar_umask;
 172                } else if (S_ISLNK(mode)) {
 173                        *header.typeflag = TYPEFLAG_LNK;
 174                        mode |= 0777;
 175                } else if (S_ISREG(mode)) {
 176                        *header.typeflag = TYPEFLAG_REG;
 177                        mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
 178                } else {
 179                        error("unsupported file mode: 0%o (SHA1: %s)",
 180                              mode, sha1_to_hex(sha1));
 181                        return;
 182                }
 183                if (path->len > sizeof(header.name)) {
 184                        int plen = get_path_prefix(path, sizeof(header.prefix));
 185                        int rest = path->len - plen - 1;
 186                        if (plen > 0 && rest <= sizeof(header.name)) {
 187                                memcpy(header.prefix, path->buf, plen);
 188                                memcpy(header.name, path->buf + plen + 1, rest);
 189                        } else {
 190                                sprintf(header.name, "%s.data",
 191                                        sha1_to_hex(sha1));
 192                                strbuf_append_ext_header(&ext_header, "path",
 193                                                         path->buf, path->len);
 194                        }
 195                } else
 196                        memcpy(header.name, path->buf, path->len);
 197        }
 198
 199        if (S_ISLNK(mode) && buffer) {
 200                if (size > sizeof(header.linkname)) {
 201                        sprintf(header.linkname, "see %s.paxheader",
 202                                sha1_to_hex(sha1));
 203                        strbuf_append_ext_header(&ext_header, "linkpath",
 204                                                 buffer, size);
 205                } else
 206                        memcpy(header.linkname, buffer, size);
 207        }
 208
 209        sprintf(header.mode, "%07o", mode & 07777);
 210        sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
 211        sprintf(header.mtime, "%011lo", archive_time);
 212
 213        sprintf(header.uid, "%07o", 0);
 214        sprintf(header.gid, "%07o", 0);
 215        strlcpy(header.uname, "root", sizeof(header.uname));
 216        strlcpy(header.gname, "root", sizeof(header.gname));
 217        sprintf(header.devmajor, "%07o", 0);
 218        sprintf(header.devminor, "%07o", 0);
 219
 220        memcpy(header.magic, "ustar", 6);
 221        memcpy(header.version, "00", 2);
 222
 223        sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
 224
 225        if (ext_header.len > 0) {
 226                write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
 227                free(ext_header.buf);
 228        }
 229        write_blocked(&header, sizeof(header));
 230        if (S_ISREG(mode) && buffer && size > 0)
 231                write_blocked(buffer, size);
 232}
 233
 234static void write_global_extended_header(const unsigned char *sha1)
 235{
 236        struct strbuf ext_header;
 237        ext_header.buf = NULL;
 238        ext_header.len = ext_header.alloc = 0;
 239        strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
 240        write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
 241        free(ext_header.buf);
 242}
 243
 244static int git_tar_config(const char *var, const char *value)
 245{
 246        if (!strcmp(var, "tar.umask")) {
 247                if (!strcmp(value, "user")) {
 248                        tar_umask = umask(0);
 249                        umask(tar_umask);
 250                } else {
 251                        tar_umask = git_config_int(var, value);
 252                }
 253                return 0;
 254        }
 255        return git_default_config(var, value);
 256}
 257
 258static int write_tar_entry(const unsigned char *sha1,
 259                           const char *base, int baselen,
 260                           const char *filename, unsigned mode, int stage)
 261{
 262        static struct strbuf path;
 263        int filenamelen = strlen(filename);
 264        void *buffer;
 265        char type[20];
 266        unsigned long size;
 267
 268        if (!path.alloc) {
 269                path.buf = xmalloc(PATH_MAX);
 270                path.alloc = PATH_MAX;
 271                path.len = path.eof = 0;
 272        }
 273        if (path.alloc < baselen + filenamelen) {
 274                free(path.buf);
 275                path.buf = xmalloc(baselen + filenamelen);
 276                path.alloc = baselen + filenamelen;
 277        }
 278        memcpy(path.buf, base, baselen);
 279        memcpy(path.buf + baselen, filename, filenamelen);
 280        path.len = baselen + filenamelen;
 281        if (S_ISDIR(mode)) {
 282                strbuf_append_string(&path, "/");
 283                buffer = NULL;
 284                size = 0;
 285        } else {
 286                buffer = read_sha1_file(sha1, type, &size);
 287                if (!buffer)
 288                        die("cannot read %s", sha1_to_hex(sha1));
 289        }
 290
 291        write_entry(sha1, &path, mode, buffer, size);
 292        free(buffer);
 293
 294        return READ_TREE_RECURSIVE;
 295}
 296
 297int write_tar_archive(struct archiver_args *args)
 298{
 299        int plen = args->base ? strlen(args->base) : 0;
 300
 301        git_config(git_tar_config);
 302
 303        archive_time = args->time;
 304        verbose = args->verbose;
 305
 306        if (args->commit_sha1)
 307                write_global_extended_header(args->commit_sha1);
 308
 309        if (args->base && plen > 0 && args->base[plen - 1] == '/') {
 310                char *base = xstrdup(args->base);
 311                int baselen = strlen(base);
 312
 313                while (baselen > 0 && base[baselen - 1] == '/')
 314                        base[--baselen] = '\0';
 315                write_tar_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
 316                free(base);
 317        }
 318        read_tree_recursive(args->tree, args->base, plen, 0,
 319                            args->pathspec, write_tar_entry);
 320        write_trailer();
 321
 322        return 0;
 323}