Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005, 2006 Rene Scharfe |
| 3 | */ |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 4 | #include "cache.h" |
| 5 | #include "commit.h" |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 6 | #include "tar.h" |
| 7 | #include "builtin.h" |
| 8 | #include "archive.h" |
| 9 | |
| 10 | #define RECORDSIZE (512) |
| 11 | #define BLOCKSIZE (RECORDSIZE * 20) |
| 12 | |
| 13 | static char block[BLOCKSIZE]; |
| 14 | static unsigned long offset; |
| 15 | |
| 16 | static time_t archive_time; |
René Scharfe | f08b3b0 | 2007-01-05 23:30:22 +0100 | [diff] [blame] | 17 | static int tar_umask = 002; |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 18 | static int verbose; |
René Scharfe | 8460b2f | 2007-09-03 20:07:01 +0200 | [diff] [blame] | 19 | static const struct commit *commit; |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 20 | |
| 21 | /* writes out the whole block, but only if it is full */ |
| 22 | static 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 | */ |
| 34 | static 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 | */ |
| 70 | static 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 | |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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 | */ |
| 87 | static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword, |
| 88 | const char *value, unsigned int valuelen) |
| 89 | { |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 90 | int len, tmp; |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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 | |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 97 | strbuf_grow(sb, len); |
| 98 | strbuf_addf(sb, "%u %s=", len, keyword); |
| 99 | strbuf_add(sb, value, valuelen); |
| 100 | strbuf_addch(sb, '\n'); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static 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 | |
| 116 | static 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 | |
| 127 | static 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)); |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 134 | strbuf_init(&ext_header, 0); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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) |
Pierre Habouzit | b449f4c | 2007-09-06 13:20:05 +0200 | [diff] [blame] | 146 | fprintf(stderr, "%.*s\n", (int)path->len, path->buf); |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 147 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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 | |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 191 | sprintf(header.uid, "%07o", 0); |
| 192 | sprintf(header.gid, "%07o", 0); |
René Scharfe | f08b3b0 | 2007-01-05 23:30:22 +0100 | [diff] [blame] | 193 | strlcpy(header.uname, "root", sizeof(header.uname)); |
| 194 | strlcpy(header.gname, "root", sizeof(header.gname)); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 205 | } |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 206 | strbuf_release(&ext_header); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 207 | write_blocked(&header, sizeof(header)); |
| 208 | if (S_ISREG(mode) && buffer && size > 0) |
| 209 | write_blocked(buffer, size); |
| 210 | } |
| 211 | |
| 212 | static void write_global_extended_header(const unsigned char *sha1) |
| 213 | { |
| 214 | struct strbuf ext_header; |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 215 | |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 216 | strbuf_init(&ext_header, 0); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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); |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 219 | strbuf_release(&ext_header); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static int git_tar_config(const char *var, const char *value) |
| 223 | { |
| 224 | if (!strcmp(var, "tar.umask")) { |
Junio C Hamano | cc1816b | 2008-02-08 20:38:22 -0800 | [diff] [blame] | 225 | if (value && !strcmp(value, "user")) { |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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 | |
| 236 | static int write_tar_entry(const unsigned char *sha1, |
| 237 | const char *base, int baselen, |
| 238 | const char *filename, unsigned mode, int stage) |
| 239 | { |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 240 | static struct strbuf path = STRBUF_INIT; |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 241 | void *buffer; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 242 | enum object_type type; |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 243 | unsigned long size; |
| 244 | |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 245 | strbuf_reset(&path); |
Pierre Habouzit | e03e05f | 2007-09-20 00:42:10 +0200 | [diff] [blame] | 246 | strbuf_grow(&path, PATH_MAX); |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 247 | strbuf_add(&path, base, baselen); |
Pierre Habouzit | e03e05f | 2007-09-20 00:42:10 +0200 | [diff] [blame] | 248 | strbuf_addstr(&path, filename); |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 249 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
Pierre Habouzit | 7a604f1 | 2007-09-06 13:20:06 +0200 | [diff] [blame] | 250 | strbuf_addch(&path, '/'); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 251 | buffer = NULL; |
| 252 | size = 0; |
| 253 | } else { |
René Scharfe | 8460b2f | 2007-09-03 20:07:01 +0200 | [diff] [blame] | 254 | buffer = sha1_file_to_archive(path.buf, sha1, mode, &type, |
| 255 | &size, commit); |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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 | |
| 266 | int 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; |
René Scharfe | 8460b2f | 2007-09-03 20:07:01 +0200 | [diff] [blame] | 274 | commit = args->commit; |
Rene Scharfe | 3d74982 | 2006-09-24 17:31:10 +0200 | [diff] [blame] | 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 | } |