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