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