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