archive-tar.con commit expose a helper function peel_to_type(). (8177631)
   1/*
   2 * Copyright (c) 2005, 2006 Rene Scharfe
   3 */
   4#include "cache.h"
   5#include "commit.h"
   6#include "tar.h"
   7#include "builtin.h"
   8#include "archive.h"
   9
  10#define RECORDSIZE      (512)
  11#define BLOCKSIZE       (RECORDSIZE * 20)
  12
  13static char block[BLOCKSIZE];
  14static unsigned long offset;
  15
  16static time_t archive_time;
  17static int tar_umask = 002;
  18static int verbose;
  19static const struct commit *commit;
  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
  81/*
  82 * pax extended header records have the format "%u %s=%s\n".  %u contains
  83 * the size of the whole string (including the %u), the first %s is the
  84 * keyword, the second one is the value.  This function constructs such a
  85 * string and appends it to a struct strbuf.
  86 */
  87static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
  88                                     const char *value, unsigned int valuelen)
  89{
  90        int len, tmp;
  91
  92        /* "%u %s=%s\n" */
  93        len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
  94        for (tmp = len; tmp > 9; tmp /= 10)
  95                len++;
  96
  97        strbuf_grow(sb, len);
  98        strbuf_addf(sb, "%u %s=", len, keyword);
  99        strbuf_add(sb, value, valuelen);
 100        strbuf_addch(sb, '\n');
 101}
 102
 103static unsigned int ustar_header_chksum(const struct ustar_header *header)
 104{
 105        char *p = (char *)header;
 106        unsigned int chksum = 0;
 107        while (p < header->chksum)
 108                chksum += *p++;
 109        chksum += sizeof(header->chksum) * ' ';
 110        p += sizeof(header->chksum);
 111        while (p < (char *)header + sizeof(struct ustar_header))
 112                chksum += *p++;
 113        return chksum;
 114}
 115
 116static int get_path_prefix(const struct strbuf *path, int maxlen)
 117{
 118        int i = path->len;
 119        if (i > maxlen)
 120                i = maxlen;
 121        do {
 122                i--;
 123        } while (i > 0 && path->buf[i] != '/');
 124        return i;
 125}
 126
 127static void write_entry(const unsigned char *sha1, struct strbuf *path,
 128                        unsigned int mode, void *buffer, unsigned long size)
 129{
 130        struct ustar_header header;
 131        struct strbuf ext_header;
 132
 133        memset(&header, 0, sizeof(header));
 134        strbuf_init(&ext_header, 0);
 135
 136        if (!sha1) {
 137                *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
 138                mode = 0100666;
 139                strcpy(header.name, "pax_global_header");
 140        } else if (!path) {
 141                *header.typeflag = TYPEFLAG_EXT_HEADER;
 142                mode = 0100666;
 143                sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
 144        } else {
 145                if (verbose)
 146                        fprintf(stderr, "%.*s\n", (int)path->len, path->buf);
 147                if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
 148                        *header.typeflag = TYPEFLAG_DIR;
 149                        mode = (mode | 0777) & ~tar_umask;
 150                } else if (S_ISLNK(mode)) {
 151                        *header.typeflag = TYPEFLAG_LNK;
 152                        mode |= 0777;
 153                } else if (S_ISREG(mode)) {
 154                        *header.typeflag = TYPEFLAG_REG;
 155                        mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
 156                } else {
 157                        error("unsupported file mode: 0%o (SHA1: %s)",
 158                              mode, sha1_to_hex(sha1));
 159                        return;
 160                }
 161                if (path->len > sizeof(header.name)) {
 162                        int plen = get_path_prefix(path, sizeof(header.prefix));
 163                        int rest = path->len - plen - 1;
 164                        if (plen > 0 && rest <= sizeof(header.name)) {
 165                                memcpy(header.prefix, path->buf, plen);
 166                                memcpy(header.name, path->buf + plen + 1, rest);
 167                        } else {
 168                                sprintf(header.name, "%s.data",
 169                                        sha1_to_hex(sha1));
 170                                strbuf_append_ext_header(&ext_header, "path",
 171                                                         path->buf, path->len);
 172                        }
 173                } else
 174                        memcpy(header.name, path->buf, path->len);
 175        }
 176
 177        if (S_ISLNK(mode) && buffer) {
 178                if (size > sizeof(header.linkname)) {
 179                        sprintf(header.linkname, "see %s.paxheader",
 180                                sha1_to_hex(sha1));
 181                        strbuf_append_ext_header(&ext_header, "linkpath",
 182                                                 buffer, size);
 183                } else
 184                        memcpy(header.linkname, buffer, size);
 185        }
 186
 187        sprintf(header.mode, "%07o", mode & 07777);
 188        sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
 189        sprintf(header.mtime, "%011lo", archive_time);
 190
 191        sprintf(header.uid, "%07o", 0);
 192        sprintf(header.gid, "%07o", 0);
 193        strlcpy(header.uname, "root", sizeof(header.uname));
 194        strlcpy(header.gname, "root", sizeof(header.gname));
 195        sprintf(header.devmajor, "%07o", 0);
 196        sprintf(header.devminor, "%07o", 0);
 197
 198        memcpy(header.magic, "ustar", 6);
 199        memcpy(header.version, "00", 2);
 200
 201        sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
 202
 203        if (ext_header.len > 0) {
 204                write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
 205        }
 206        strbuf_release(&ext_header);
 207        write_blocked(&header, sizeof(header));
 208        if (S_ISREG(mode) && buffer && size > 0)
 209                write_blocked(buffer, size);
 210}
 211
 212static void write_global_extended_header(const unsigned char *sha1)
 213{
 214        struct strbuf ext_header;
 215
 216        strbuf_init(&ext_header, 0);
 217        strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
 218        write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
 219        strbuf_release(&ext_header);
 220}
 221
 222static int git_tar_config(const char *var, const char *value)
 223{
 224        if (!strcmp(var, "tar.umask")) {
 225                if (value && !strcmp(value, "user")) {
 226                        tar_umask = umask(0);
 227                        umask(tar_umask);
 228                } else {
 229                        tar_umask = git_config_int(var, value);
 230                }
 231                return 0;
 232        }
 233        return git_default_config(var, value);
 234}
 235
 236static int write_tar_entry(const unsigned char *sha1,
 237                           const char *base, int baselen,
 238                           const char *filename, unsigned mode, int stage)
 239{
 240        static struct strbuf path = STRBUF_INIT;
 241        void *buffer;
 242        enum object_type type;
 243        unsigned long size;
 244
 245        strbuf_reset(&path);
 246        strbuf_grow(&path, PATH_MAX);
 247        strbuf_add(&path, base, baselen);
 248        strbuf_addstr(&path, filename);
 249        if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
 250                strbuf_addch(&path, '/');
 251                buffer = NULL;
 252                size = 0;
 253        } else {
 254                buffer = sha1_file_to_archive(path.buf, sha1, mode, &type,
 255                                              &size, commit);
 256                if (!buffer)
 257                        die("cannot read %s", sha1_to_hex(sha1));
 258        }
 259
 260        write_entry(sha1, &path, mode, buffer, size);
 261        free(buffer);
 262
 263        return READ_TREE_RECURSIVE;
 264}
 265
 266int write_tar_archive(struct archiver_args *args)
 267{
 268        int plen = args->base ? strlen(args->base) : 0;
 269
 270        git_config(git_tar_config);
 271
 272        archive_time = args->time;
 273        verbose = args->verbose;
 274        commit = args->commit;
 275
 276        if (args->commit_sha1)
 277                write_global_extended_header(args->commit_sha1);
 278
 279        if (args->base && plen > 0 && args->base[plen - 1] == '/') {
 280                char *base = xstrdup(args->base);
 281                int baselen = strlen(base);
 282
 283                while (baselen > 0 && base[baselen - 1] == '/')
 284                        base[--baselen] = '\0';
 285                write_tar_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
 286                free(base);
 287        }
 288        read_tree_recursive(args->tree, args->base, plen, 0,
 289                            args->pathspec, write_tar_entry);
 290        write_trailer();
 291
 292        return 0;
 293}