tar-tree: Use write_entry() to write the archive contents
[git.git] / tar-tree.c
1 /*
2  * Copyright (c) 2005, 2006 Rene Scharfe
3  */
4 #include <time.h>
5 #include "cache.h"
6 #include "diff.h"
7 #include "commit.h"
8 #include "strbuf.h"
9 #include "tar.h"
10
11 #define RECORDSIZE      (512)
12 #define BLOCKSIZE       (RECORDSIZE * 20)
13
14 #define EXT_HEADER_PATH         1
15 #define EXT_HEADER_LINKPATH     2
16
17 static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
18
19 static char block[BLOCKSIZE];
20 static unsigned long offset;
21
22 static const char *basedir;
23 static time_t archive_time;
24
25 struct path_prefix {
26         struct path_prefix *prev;
27         const char *name;
28 };
29
30 /* tries hard to write, either succeeds or dies in the attempt */
31 static void reliable_write(void *buf, unsigned long size)
32 {
33         while (size > 0) {
34                 long ret = xwrite(1, buf, size);
35                 if (ret < 0) {
36                         if (errno == EPIPE)
37                                 exit(0);
38                         die("git-tar-tree: %s", strerror(errno));
39                 } else if (!ret) {
40                         die("git-tar-tree: disk full?");
41                 }
42                 size -= ret;
43                 buf += ret;
44         }
45 }
46
47 /* writes out the whole block, but only if it is full */
48 static void write_if_needed(void)
49 {
50         if (offset == BLOCKSIZE) {
51                 reliable_write(block, BLOCKSIZE);
52                 offset = 0;
53         }
54 }
55
56 /* acquire the next record from the buffer; user must call write_if_needed() */
57 static char *get_record(void)
58 {
59         char *p = block + offset;
60         memset(p, 0, RECORDSIZE);
61         offset += RECORDSIZE;
62         return p;
63 }
64
65 /*
66  * The end of tar archives is marked by 1024 nul bytes and after that
67  * follows the rest of the block (if any).
68  */
69 static void write_trailer(void)
70 {
71         get_record();
72         write_if_needed();
73         get_record();
74         write_if_needed();
75         while (offset) {
76                 get_record();
77                 write_if_needed();
78         }
79 }
80
81 /*
82  * queues up writes, so that all our write(2) calls write exactly one
83  * full block; pads writes to RECORDSIZE
84  */
85 static void write_blocked(void *buf, unsigned long size)
86 {
87         unsigned long tail;
88
89         if (offset) {
90                 unsigned long chunk = BLOCKSIZE - offset;
91                 if (size < chunk)
92                         chunk = size;
93                 memcpy(block + offset, buf, chunk);
94                 size -= chunk;
95                 offset += chunk;
96                 buf += chunk;
97                 write_if_needed();
98         }
99         while (size >= BLOCKSIZE) {
100                 reliable_write(buf, BLOCKSIZE);
101                 size -= BLOCKSIZE;
102                 buf += BLOCKSIZE;
103         }
104         if (size) {
105                 memcpy(block + offset, buf, size);
106                 buf += size;
107                 offset += size;
108         }
109         tail = offset % RECORDSIZE;
110         if (tail)  {
111                 memset(block + offset, 0, RECORDSIZE - tail);
112                 offset += RECORDSIZE - tail;
113         }
114         write_if_needed();
115 }
116
117 static void strbuf_append_string(struct strbuf *sb, const char *s)
118 {
119         int slen = strlen(s);
120         int total = sb->len + slen;
121         if (total > sb->alloc) {
122                 sb->buf = xrealloc(sb->buf, total);
123                 sb->alloc = total;
124         }
125         memcpy(sb->buf + sb->len, s, slen);
126         sb->len = total;
127 }
128
129 /*
130  * pax extended header records have the format "%u %s=%s\n".  %u contains
131  * the size of the whole string (including the %u), the first %s is the
132  * keyword, the second one is the value.  This function constructs such a
133  * string and appends it to a struct strbuf.
134  */
135 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
136                                      const char *value, unsigned int valuelen)
137 {
138         char *p;
139         int len, total, tmp;
140
141         /* "%u %s=%s\n" */
142         len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
143         for (tmp = len; tmp > 9; tmp /= 10)
144                 len++;
145
146         total = sb->len + len;
147         if (total > sb->alloc) {
148                 sb->buf = xrealloc(sb->buf, total);
149                 sb->alloc = total;
150         }
151
152         p = sb->buf;
153         p += sprintf(p, "%u %s=", len, keyword);
154         memcpy(p, value, valuelen);
155         p += valuelen;
156         *p = '\n';
157         sb->len = total;
158 }
159
160 static unsigned int ustar_header_chksum(const struct ustar_header *header)
161 {
162         char *p = (char *)header;
163         unsigned int chksum = 0;
164         while (p < header->chksum)
165                 chksum += *p++;
166         chksum += sizeof(header->chksum) * ' ';
167         p += sizeof(header->chksum);
168         while (p < (char *)header + sizeof(struct ustar_header))
169                 chksum += *p++;
170         return chksum;
171 }
172
173 static void write_entry(const unsigned char *sha1, struct strbuf *path,
174                         unsigned int mode, void *buffer, unsigned long size)
175 {
176         struct ustar_header header;
177         struct strbuf ext_header;
178
179         memset(&header, 0, sizeof(header));
180         ext_header.buf = NULL;
181         ext_header.len = ext_header.alloc = 0;
182
183         if (!sha1) {
184                 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
185                 mode = 0100666;
186                 strcpy(header.name, "pax_global_header");
187         } else if (!path) {
188                 *header.typeflag = TYPEFLAG_EXT_HEADER;
189                 mode = 0100666;
190                 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
191         } else {
192                 if (S_ISDIR(mode)) {
193                         *header.typeflag = TYPEFLAG_DIR;
194                         mode |= 0777;
195                 } else if (S_ISLNK(mode)) {
196                         *header.typeflag = TYPEFLAG_LNK;
197                         mode |= 0777;
198                 } else if (S_ISREG(mode)) {
199                         *header.typeflag = TYPEFLAG_REG;
200                         mode |= (mode & 0100) ? 0777 : 0666;
201                 } else {
202                         error("unsupported file mode: 0%o (SHA1: %s)",
203                               mode, sha1_to_hex(sha1));
204                         return;
205                 }
206                 if (path->len > sizeof(header.name)) {
207                         sprintf(header.name, "%s.data", sha1_to_hex(sha1));
208                         strbuf_append_ext_header(&ext_header, "path",
209                                                  path->buf, path->len);
210                 } else
211                         memcpy(header.name, path->buf, path->len);
212         }
213
214         if (S_ISLNK(mode) && buffer) {
215                 if (size > sizeof(header.linkname)) {
216                         sprintf(header.linkname, "see %s.paxheader",
217                                 sha1_to_hex(sha1));
218                         strbuf_append_ext_header(&ext_header, "linkpath",
219                                                  buffer, size);
220                 } else
221                         memcpy(header.linkname, buffer, size);
222         }
223
224         sprintf(header.mode, "%07o", mode & 07777);
225         sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
226         sprintf(header.mtime, "%011lo", archive_time);
227
228         /* XXX: should we provide more meaningful info here? */
229         sprintf(header.uid, "%07o", 0);
230         sprintf(header.gid, "%07o", 0);
231         strncpy(header.uname, "git", 31);
232         strncpy(header.gname, "git", 31);
233         sprintf(header.devmajor, "%07o", 0);
234         sprintf(header.devminor, "%07o", 0);
235
236         memcpy(header.magic, "ustar", 6);
237         memcpy(header.version, "00", 2);
238
239         sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
240
241         if (ext_header.len > 0) {
242                 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
243                 free(ext_header.buf);
244         }
245         write_blocked(&header, sizeof(header));
246         if (S_ISREG(mode) && buffer && size > 0)
247                 write_blocked(buffer, size);
248 }
249
250 static void append_string(char **p, const char *s)
251 {
252         unsigned int len = strlen(s);
253         memcpy(*p, s, len);
254         *p += len;
255 }
256
257 static void append_char(char **p, char c)
258 {
259         **p = c;
260         *p += 1;
261 }
262
263 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
264 {
265         if (!prefix)
266                 return;
267         append_path_prefix(buffer, prefix->prev);
268         append_string(buffer, prefix->name);
269         append_char(buffer, '/');
270 }
271
272 static unsigned int path_prefix_len(struct path_prefix *prefix)
273 {
274         if (!prefix)
275                 return 0;
276         return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
277 }
278
279 static void append_path(char **p, int is_dir, const char *basepath,
280                         struct path_prefix *prefix, const char *path)
281 {
282         if (basepath) {
283                 append_string(p, basepath);
284                 append_char(p, '/');
285         }
286         append_path_prefix(p, prefix);
287         append_string(p, path);
288         if (is_dir)
289                 append_char(p, '/');
290 }
291
292 static unsigned int path_len(int is_dir, const char *basepath,
293                              struct path_prefix *prefix, const char *path)
294 {
295         unsigned int len = 0;
296         if (basepath)
297                 len += strlen(basepath) + 1;
298         len += path_prefix_len(prefix) + strlen(path);
299         if (is_dir)
300                 len++;
301         return len;
302 }
303
304 static void append_extended_header_prefix(char **p, unsigned int size,
305                                           const char *keyword)
306 {
307         int len = sprintf(*p, "%u %s=", size, keyword);
308         *p += len;
309 }
310
311 static unsigned int extended_header_len(const char *keyword,
312                                         unsigned int valuelen)
313 {
314         /* "%u %s=%s\n" */
315         unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
316         if (len > 9)
317                 len++;
318         if (len > 99)
319                 len++;
320         return len;
321 }
322
323 static void append_extended_header(char **p, const char *keyword,
324                                    const char *value, unsigned int len)
325 {
326         unsigned int size = extended_header_len(keyword, len);
327         append_extended_header_prefix(p, size, keyword);
328         memcpy(*p, value, len);
329         *p += len;
330         append_char(p, '\n');
331 }
332
333 static void write_header(const unsigned char *, char, const char *, struct path_prefix *,
334                          const char *, unsigned int, void *, unsigned long);
335
336 /* stores a pax extended header directly in the block buffer */
337 static void write_extended_header(const char *headerfilename, int is_dir,
338                                   unsigned int flags, const char *basepath,
339                                   struct path_prefix *prefix,
340                                   const char *path, unsigned int namelen,
341                                   void *content, unsigned int contentsize)
342 {
343         char *buffer, *p;
344         unsigned int pathlen, size, linkpathlen = 0;
345
346         size = pathlen = extended_header_len("path", namelen);
347         if (flags & EXT_HEADER_LINKPATH) {
348                 linkpathlen = extended_header_len("linkpath", contentsize);
349                 size += linkpathlen;
350         }
351         write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
352                      0100600, NULL, size);
353
354         buffer = p = malloc(size);
355         if (!buffer)
356                 die("git-tar-tree: %s", strerror(errno));
357         append_extended_header_prefix(&p, pathlen, "path");
358         append_path(&p, is_dir, basepath, prefix, path);
359         append_char(&p, '\n');
360         if (flags & EXT_HEADER_LINKPATH)
361                 append_extended_header(&p, "linkpath", content, contentsize);
362         write_blocked(buffer, size);
363         free(buffer);
364 }
365
366 static void write_global_extended_header(const unsigned char *sha1)
367 {
368         struct strbuf ext_header;
369         ext_header.buf = NULL;
370         ext_header.len = ext_header.alloc = 0;
371         strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
372         write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
373         free(ext_header.buf);
374 }
375
376 /* stores a ustar header directly in the block buffer */
377 static void write_header(const unsigned char *sha1, char typeflag, const char *basepath,
378                          struct path_prefix *prefix, const char *path,
379                          unsigned int mode, void *buffer, unsigned long size)
380 {
381         unsigned int namelen; 
382         char *header = NULL;
383         unsigned int checksum = 0;
384         int i;
385         unsigned int ext_header = 0;
386
387         if (typeflag == TYPEFLAG_AUTO) {
388                 if (S_ISDIR(mode))
389                         typeflag = TYPEFLAG_DIR;
390                 else if (S_ISLNK(mode))
391                         typeflag = TYPEFLAG_LNK;
392                 else
393                         typeflag = TYPEFLAG_REG;
394         }
395
396         namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
397         if (namelen > 100)
398                 ext_header |= EXT_HEADER_PATH;
399         if (typeflag == TYPEFLAG_LNK && size > 100)
400                 ext_header |= EXT_HEADER_LINKPATH;
401
402         /* the extended header must be written before the normal one */
403         if (ext_header) {
404                 char headerfilename[51];
405                 sprintf(headerfilename, "%s.paxheader", sha1_to_hex(sha1));
406                 write_extended_header(headerfilename, S_ISDIR(mode),
407                                       ext_header, basepath, prefix, path,
408                                       namelen, buffer, size);
409         }
410
411         header = get_record();
412
413         if (ext_header) {
414                 sprintf(header, "%s.data", sha1_to_hex(sha1));
415         } else {
416                 char *p = header;
417                 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
418         }
419
420         if (typeflag == TYPEFLAG_LNK) {
421                 if (ext_header & EXT_HEADER_LINKPATH) {
422                         sprintf(&header[157], "see %s.paxheader",
423                                 sha1_to_hex(sha1));
424                 } else {
425                         if (buffer)
426                                 strncpy(&header[157], buffer, size);
427                 }
428         }
429
430         if (S_ISDIR(mode))
431                 mode |= 0777;
432         else if (S_ISREG(mode))
433                 mode |= (mode & 0100) ? 0777 : 0666;
434         else if (S_ISLNK(mode))
435                 mode |= 0777;
436         sprintf(&header[100], "%07o", mode & 07777);
437
438         /* XXX: should we provide more meaningful info here? */
439         sprintf(&header[108], "%07o", 0);       /* uid */
440         sprintf(&header[116], "%07o", 0);       /* gid */
441         strncpy(&header[265], "git", 31);       /* uname */
442         strncpy(&header[297], "git", 31);       /* gname */
443
444         if (S_ISDIR(mode) || S_ISLNK(mode))
445                 size = 0;
446         sprintf(&header[124], "%011lo", size);
447         sprintf(&header[136], "%011lo", archive_time);
448
449         header[156] = typeflag;
450
451         memcpy(&header[257], "ustar", 6);
452         memcpy(&header[263], "00", 2);
453
454         sprintf(&header[329], "%07o", 0);       /* devmajor */
455         sprintf(&header[337], "%07o", 0);       /* devminor */
456
457         memset(&header[148], ' ', 8);
458         for (i = 0; i < RECORDSIZE; i++)
459                 checksum += header[i];
460         sprintf(&header[148], "%07o", checksum & 0x1fffff);
461
462         write_if_needed();
463 }
464
465 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
466 {
467         int pathlen = path->len;
468
469         while (tree->size) {
470                 const char *name;
471                 const unsigned char *sha1;
472                 unsigned mode;
473                 void *eltbuf;
474                 char elttype[20];
475                 unsigned long eltsize;
476
477                 sha1 = tree_entry_extract(tree, &name, &mode);
478                 update_tree_entry(tree);
479
480                 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
481                 if (!eltbuf)
482                         die("cannot read %s", sha1_to_hex(sha1));
483
484                 path->len = pathlen;
485                 strbuf_append_string(path, name);
486                 if (S_ISDIR(mode))
487                         strbuf_append_string(path, "/");
488
489                 write_entry(sha1, path, mode, eltbuf, eltsize);
490
491                 if (S_ISDIR(mode)) {
492                         struct tree_desc subtree;
493                         subtree.buf = eltbuf;
494                         subtree.size = eltsize;
495                         traverse_tree(&subtree, path);
496                 }
497                 free(eltbuf);
498         }
499 }
500
501 int main(int argc, char **argv)
502 {
503         unsigned char sha1[20], tree_sha1[20];
504         struct commit *commit;
505         struct tree_desc tree;
506         struct strbuf current_path;
507
508         current_path.buf = xmalloc(PATH_MAX);
509         current_path.alloc = PATH_MAX;
510         current_path.len = current_path.eof = 0;
511
512         setup_git_directory();
513
514         switch (argc) {
515         case 3:
516                 strbuf_append_string(&current_path, argv[2]);
517                 strbuf_append_string(&current_path, "/");
518                 /* FALLTHROUGH */
519         case 2:
520                 if (get_sha1(argv[1], sha1) < 0)
521                         usage(tar_tree_usage);
522                 break;
523         default:
524                 usage(tar_tree_usage);
525         }
526
527         commit = lookup_commit_reference_gently(sha1, 1);
528         if (commit) {
529                 write_global_extended_header(commit->object.sha1);
530                 archive_time = commit->date;
531         } else
532                 archive_time = time(NULL);
533
534         tree.buf = read_object_with_reference(sha1, "tree", &tree.size,
535                                               tree_sha1);
536         if (!tree.buf)
537                 die("not a reference to a tag, commit or tree object: %s",
538                     sha1_to_hex(sha1));
539
540         if (current_path.len > 0)
541                 write_entry(tree_sha1, &current_path, 040777, NULL, 0);
542         traverse_tree(&tree, &current_path);
543         write_trailer();
544         free(current_path.buf);
545         return 0;
546 }