57a15406d9724e70c6dfaaf6d1c0dba64f2156ad
   1/*
   2 * Copyright (c) 2005, 2006 Rene Scharfe
   3 */
   4#include "cache.h"
   5#include "tar.h"
   6#include "archive.h"
   7#include "streaming.h"
   8#include "run-command.h"
   9
  10#define RECORDSIZE      (512)
  11#define BLOCKSIZE       (RECORDSIZE * 20)
  12
  13static char block[BLOCKSIZE];
  14static unsigned long offset;
  15
  16static int tar_umask = 002;
  17
  18static int write_tar_filter_archive(const struct archiver *ar,
  19                                    struct archiver_args *args);
  20
  21/*
  22 * This is the max value that a ustar size header can specify, as it is fixed
  23 * at 11 octal digits. POSIX specifies that we switch to extended headers at
  24 * this size.
  25 */
  26#define USTAR_MAX_SIZE 077777777777UL
  27
  28/* writes out the whole block, but only if it is full */
  29static void write_if_needed(void)
  30{
  31        if (offset == BLOCKSIZE) {
  32                write_or_die(1, block, BLOCKSIZE);
  33                offset = 0;
  34        }
  35}
  36
  37/*
  38 * queues up writes, so that all our write(2) calls write exactly one
  39 * full block; pads writes to RECORDSIZE
  40 */
  41static void do_write_blocked(const void *data, unsigned long size)
  42{
  43        const char *buf = data;
  44
  45        if (offset) {
  46                unsigned long chunk = BLOCKSIZE - offset;
  47                if (size < chunk)
  48                        chunk = size;
  49                memcpy(block + offset, buf, chunk);
  50                size -= chunk;
  51                offset += chunk;
  52                buf += chunk;
  53                write_if_needed();
  54        }
  55        while (size >= BLOCKSIZE) {
  56                write_or_die(1, buf, BLOCKSIZE);
  57                size -= BLOCKSIZE;
  58                buf += BLOCKSIZE;
  59        }
  60        if (size) {
  61                memcpy(block + offset, buf, size);
  62                offset += size;
  63        }
  64}
  65
  66static void finish_record(void)
  67{
  68        unsigned long tail;
  69        tail = offset % RECORDSIZE;
  70        if (tail)  {
  71                memset(block + offset, 0, RECORDSIZE - tail);
  72                offset += RECORDSIZE - tail;
  73        }
  74        write_if_needed();
  75}
  76
  77static void write_blocked(const void *data, unsigned long size)
  78{
  79        do_write_blocked(data, size);
  80        finish_record();
  81}
  82
  83/*
  84 * The end of tar archives is marked by 2*512 nul bytes and after that
  85 * follows the rest of the block (if any).
  86 */
  87static void write_trailer(void)
  88{
  89        int tail = BLOCKSIZE - offset;
  90        memset(block + offset, 0, tail);
  91        write_or_die(1, block, BLOCKSIZE);
  92        if (tail < 2 * RECORDSIZE) {
  93                memset(block, 0, offset);
  94                write_or_die(1, block, BLOCKSIZE);
  95        }
  96}
  97
  98/*
  99 * queues up writes, so that all our write(2) calls write exactly one
 100 * full block; pads writes to RECORDSIZE
 101 */
 102static int stream_blocked(const unsigned char *sha1)
 103{
 104        struct git_istream *st;
 105        enum object_type type;
 106        unsigned long sz;
 107        char buf[BLOCKSIZE];
 108        ssize_t readlen;
 109
 110        st = open_istream(sha1, &type, &sz, NULL);
 111        if (!st)
 112                return error("cannot stream blob %s", sha1_to_hex(sha1));
 113        for (;;) {
 114                readlen = read_istream(st, buf, sizeof(buf));
 115                if (readlen <= 0)
 116                        break;
 117                do_write_blocked(buf, readlen);
 118        }
 119        close_istream(st);
 120        if (!readlen)
 121                finish_record();
 122        return readlen;
 123}
 124
 125/*
 126 * pax extended header records have the format "%u %s=%s\n".  %u contains
 127 * the size of the whole string (including the %u), the first %s is the
 128 * keyword, the second one is the value.  This function constructs such a
 129 * string and appends it to a struct strbuf.
 130 */
 131static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
 132                                     const char *value, unsigned int valuelen)
 133{
 134        int len, tmp;
 135
 136        /* "%u %s=%s\n" */
 137        len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
 138        for (tmp = len; tmp > 9; tmp /= 10)
 139                len++;
 140
 141        strbuf_grow(sb, len);
 142        strbuf_addf(sb, "%u %s=", len, keyword);
 143        strbuf_add(sb, value, valuelen);
 144        strbuf_addch(sb, '\n');
 145}
 146
 147/*
 148 * Like strbuf_append_ext_header, but for numeric values.
 149 */
 150static void strbuf_append_ext_header_uint(struct strbuf *sb,
 151                                          const char *keyword,
 152                                          uintmax_t value)
 153{
 154        char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
 155        int len;
 156
 157        len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
 158        strbuf_append_ext_header(sb, keyword, buf, len);
 159}
 160
 161static unsigned int ustar_header_chksum(const struct ustar_header *header)
 162{
 163        const unsigned char *p = (const unsigned char *)header;
 164        unsigned int chksum = 0;
 165        while (p < (const unsigned char *)header->chksum)
 166                chksum += *p++;
 167        chksum += sizeof(header->chksum) * ' ';
 168        p += sizeof(header->chksum);
 169        while (p < (const unsigned char *)header + sizeof(struct ustar_header))
 170                chksum += *p++;
 171        return chksum;
 172}
 173
 174static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
 175{
 176        size_t i = pathlen;
 177        if (i > 1 && path[i - 1] == '/')
 178                i--;
 179        if (i > maxlen)
 180                i = maxlen;
 181        do {
 182                i--;
 183        } while (i > 0 && path[i] != '/');
 184        return i;
 185}
 186
 187static void prepare_header(struct archiver_args *args,
 188                           struct ustar_header *header,
 189                           unsigned int mode, unsigned long size)
 190{
 191        xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
 192        xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
 193        xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
 194
 195        xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
 196        xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
 197        strlcpy(header->uname, "root", sizeof(header->uname));
 198        strlcpy(header->gname, "root", sizeof(header->gname));
 199        xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
 200        xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
 201
 202        memcpy(header->magic, "ustar", 6);
 203        memcpy(header->version, "00", 2);
 204
 205        xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
 206}
 207
 208static int write_extended_header(struct archiver_args *args,
 209                                 const unsigned char *sha1,
 210                                 const void *buffer, unsigned long size)
 211{
 212        struct ustar_header header;
 213        unsigned int mode;
 214        memset(&header, 0, sizeof(header));
 215        *header.typeflag = TYPEFLAG_EXT_HEADER;
 216        mode = 0100666;
 217        xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
 218        prepare_header(args, &header, mode, size);
 219        write_blocked(&header, sizeof(header));
 220        write_blocked(buffer, size);
 221        return 0;
 222}
 223
 224static int write_tar_entry(struct archiver_args *args,
 225                           const unsigned char *sha1,
 226                           const char *path, size_t pathlen,
 227                           unsigned int mode)
 228{
 229        struct ustar_header header;
 230        struct strbuf ext_header = STRBUF_INIT;
 231        unsigned int old_mode = mode;
 232        unsigned long size, size_in_header;
 233        void *buffer;
 234        int err = 0;
 235
 236        memset(&header, 0, sizeof(header));
 237
 238        if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
 239                *header.typeflag = TYPEFLAG_DIR;
 240                mode = (mode | 0777) & ~tar_umask;
 241        } else if (S_ISLNK(mode)) {
 242                *header.typeflag = TYPEFLAG_LNK;
 243                mode |= 0777;
 244        } else if (S_ISREG(mode)) {
 245                *header.typeflag = TYPEFLAG_REG;
 246                mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
 247        } else {
 248                return error("unsupported file mode: 0%o (SHA1: %s)",
 249                             mode, sha1_to_hex(sha1));
 250        }
 251        if (pathlen > sizeof(header.name)) {
 252                size_t plen = get_path_prefix(path, pathlen,
 253                                              sizeof(header.prefix));
 254                size_t rest = pathlen - plen - 1;
 255                if (plen > 0 && rest <= sizeof(header.name)) {
 256                        memcpy(header.prefix, path, plen);
 257                        memcpy(header.name, path + plen + 1, rest);
 258                } else {
 259                        xsnprintf(header.name, sizeof(header.name), "%s.data",
 260                                  sha1_to_hex(sha1));
 261                        strbuf_append_ext_header(&ext_header, "path",
 262                                                 path, pathlen);
 263                }
 264        } else
 265                memcpy(header.name, path, pathlen);
 266
 267        if (S_ISREG(mode) && !args->convert &&
 268            sha1_object_info(sha1, &size) == OBJ_BLOB &&
 269            size > big_file_threshold)
 270                buffer = NULL;
 271        else if (S_ISLNK(mode) || S_ISREG(mode)) {
 272                enum object_type type;
 273                buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
 274                if (!buffer)
 275                        return error("cannot read %s", sha1_to_hex(sha1));
 276        } else {
 277                buffer = NULL;
 278                size = 0;
 279        }
 280
 281        if (S_ISLNK(mode)) {
 282                if (size > sizeof(header.linkname)) {
 283                        xsnprintf(header.linkname, sizeof(header.linkname),
 284                                  "see %s.paxheader", sha1_to_hex(sha1));
 285                        strbuf_append_ext_header(&ext_header, "linkpath",
 286                                                 buffer, size);
 287                } else
 288                        memcpy(header.linkname, buffer, size);
 289        }
 290
 291        size_in_header = size;
 292        if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
 293                size_in_header = 0;
 294                strbuf_append_ext_header_uint(&ext_header, "size", size);
 295        }
 296
 297        prepare_header(args, &header, mode, size_in_header);
 298
 299        if (ext_header.len > 0) {
 300                err = write_extended_header(args, sha1, ext_header.buf,
 301                                            ext_header.len);
 302                if (err) {
 303                        free(buffer);
 304                        return err;
 305                }
 306        }
 307        strbuf_release(&ext_header);
 308        write_blocked(&header, sizeof(header));
 309        if (S_ISREG(mode) && size > 0) {
 310                if (buffer)
 311                        write_blocked(buffer, size);
 312                else
 313                        err = stream_blocked(sha1);
 314        }
 315        free(buffer);
 316        return err;
 317}
 318
 319static int write_global_extended_header(struct archiver_args *args)
 320{
 321        const unsigned char *sha1 = args->commit_sha1;
 322        struct strbuf ext_header = STRBUF_INIT;
 323        struct ustar_header header;
 324        unsigned int mode;
 325        int err = 0;
 326
 327        strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
 328        memset(&header, 0, sizeof(header));
 329        *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
 330        mode = 0100666;
 331        xsnprintf(header.name, sizeof(header.name), "pax_global_header");
 332        prepare_header(args, &header, mode, ext_header.len);
 333        write_blocked(&header, sizeof(header));
 334        write_blocked(ext_header.buf, ext_header.len);
 335        strbuf_release(&ext_header);
 336        return err;
 337}
 338
 339static struct archiver **tar_filters;
 340static int nr_tar_filters;
 341static int alloc_tar_filters;
 342
 343static struct archiver *find_tar_filter(const char *name, int len)
 344{
 345        int i;
 346        for (i = 0; i < nr_tar_filters; i++) {
 347                struct archiver *ar = tar_filters[i];
 348                if (!strncmp(ar->name, name, len) && !ar->name[len])
 349                        return ar;
 350        }
 351        return NULL;
 352}
 353
 354static int tar_filter_config(const char *var, const char *value, void *data)
 355{
 356        struct archiver *ar;
 357        const char *name;
 358        const char *type;
 359        int namelen;
 360
 361        if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
 362                return 0;
 363
 364        ar = find_tar_filter(name, namelen);
 365        if (!ar) {
 366                ar = xcalloc(1, sizeof(*ar));
 367                ar->name = xmemdupz(name, namelen);
 368                ar->write_archive = write_tar_filter_archive;
 369                ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
 370                ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
 371                tar_filters[nr_tar_filters++] = ar;
 372        }
 373
 374        if (!strcmp(type, "command")) {
 375                if (!value)
 376                        return config_error_nonbool(var);
 377                free(ar->data);
 378                ar->data = xstrdup(value);
 379                return 0;
 380        }
 381        if (!strcmp(type, "remote")) {
 382                if (git_config_bool(var, value))
 383                        ar->flags |= ARCHIVER_REMOTE;
 384                else
 385                        ar->flags &= ~ARCHIVER_REMOTE;
 386                return 0;
 387        }
 388
 389        return 0;
 390}
 391
 392static int git_tar_config(const char *var, const char *value, void *cb)
 393{
 394        if (!strcmp(var, "tar.umask")) {
 395                if (value && !strcmp(value, "user")) {
 396                        tar_umask = umask(0);
 397                        umask(tar_umask);
 398                } else {
 399                        tar_umask = git_config_int(var, value);
 400                }
 401                return 0;
 402        }
 403
 404        return tar_filter_config(var, value, cb);
 405}
 406
 407static int write_tar_archive(const struct archiver *ar,
 408                             struct archiver_args *args)
 409{
 410        int err = 0;
 411
 412        if (args->commit_sha1)
 413                err = write_global_extended_header(args);
 414        if (!err)
 415                err = write_archive_entries(args, write_tar_entry);
 416        if (!err)
 417                write_trailer();
 418        return err;
 419}
 420
 421static int write_tar_filter_archive(const struct archiver *ar,
 422                                    struct archiver_args *args)
 423{
 424        struct strbuf cmd = STRBUF_INIT;
 425        struct child_process filter = CHILD_PROCESS_INIT;
 426        const char *argv[2];
 427        int r;
 428
 429        if (!ar->data)
 430                die("BUG: tar-filter archiver called with no filter defined");
 431
 432        strbuf_addstr(&cmd, ar->data);
 433        if (args->compression_level >= 0)
 434                strbuf_addf(&cmd, " -%d", args->compression_level);
 435
 436        argv[0] = cmd.buf;
 437        argv[1] = NULL;
 438        filter.argv = argv;
 439        filter.use_shell = 1;
 440        filter.in = -1;
 441
 442        if (start_command(&filter) < 0)
 443                die_errno("unable to start '%s' filter", argv[0]);
 444        close(1);
 445        if (dup2(filter.in, 1) < 0)
 446                die_errno("unable to redirect descriptor");
 447        close(filter.in);
 448
 449        r = write_tar_archive(ar, args);
 450
 451        close(1);
 452        if (finish_command(&filter) != 0)
 453                die("'%s' filter reported error", argv[0]);
 454
 455        strbuf_release(&cmd);
 456        return r;
 457}
 458
 459static struct archiver tar_archiver = {
 460        "tar",
 461        write_tar_archive,
 462        ARCHIVER_REMOTE
 463};
 464
 465void init_tar_archiver(void)
 466{
 467        int i;
 468        register_archiver(&tar_archiver);
 469
 470        tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
 471        tar_filter_config("tar.tgz.remote", "true", NULL);
 472        tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
 473        tar_filter_config("tar.tar.gz.remote", "true", NULL);
 474        git_config(git_tar_config, NULL);
 475        for (i = 0; i < nr_tar_filters; i++) {
 476                /* omit any filters that never had a command configured */
 477                if (tar_filters[i]->data)
 478                        register_archiver(tar_filters[i]);
 479        }
 480}