tar-tree: Introduce write_entry()
[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 /*
118  * pax extended header records have the format "%u %s=%s\n".  %u contains
119  * the size of the whole string (including the %u), the first %s is the
120  * keyword, the second one is the value.  This function constructs such a
121  * string and appends it to a struct strbuf.
122  */
123 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
124                                      const char *value, unsigned int valuelen)
125 {
126         char *p;
127         int len, total, 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         total = sb->len + len;
135         if (total > sb->alloc) {
136                 sb->buf = xrealloc(sb->buf, total);
137                 sb->alloc = total;
138         }
139
140         p = sb->buf;
141         p += sprintf(p, "%u %s=", len, keyword);
142         memcpy(p, value, valuelen);
143         p += valuelen;
144         *p = '\n';
145         sb->len = total;
146 }
147
148 static unsigned int ustar_header_chksum(const struct ustar_header *header)
149 {
150         char *p = (char *)header;
151         unsigned int chksum = 0;
152         while (p < header->chksum)
153                 chksum += *p++;
154         chksum += sizeof(header->chksum) * ' ';
155         p += sizeof(header->chksum);
156         while (p < (char *)header + sizeof(struct ustar_header))
157                 chksum += *p++;
158         return chksum;
159 }
160
161 static void write_entry(const unsigned char *sha1, struct strbuf *path,
162                         unsigned int mode, void *buffer, unsigned long size)
163 {
164         struct ustar_header header;
165         struct strbuf ext_header;
166
167         memset(&header, 0, sizeof(header));
168         ext_header.buf = NULL;
169         ext_header.len = ext_header.alloc = 0;
170
171         if (!sha1) {
172                 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
173                 mode = 0100666;
174                 strcpy(header.name, "pax_global_header");
175         } else if (!path) {
176                 *header.typeflag = TYPEFLAG_EXT_HEADER;
177                 mode = 0100666;
178                 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
179         } else {
180                 if (S_ISDIR(mode)) {
181                         *header.typeflag = TYPEFLAG_DIR;
182                         mode |= 0777;
183                 } else if (S_ISLNK(mode)) {
184                         *header.typeflag = TYPEFLAG_LNK;
185                         mode |= 0777;
186                 } else if (S_ISREG(mode)) {
187                         *header.typeflag = TYPEFLAG_REG;
188                         mode |= (mode & 0100) ? 0777 : 0666;
189                 } else {
190                         error("unsupported file mode: 0%o (SHA1: %s)",
191                               mode, sha1_to_hex(sha1));
192                         return;
193                 }
194                 if (path->len > sizeof(header.name)) {
195                         sprintf(header.name, "%s.data", sha1_to_hex(sha1));
196                         strbuf_append_ext_header(&ext_header, "path",
197                                                  path->buf, path->len);
198                 } else
199                         memcpy(header.name, path->buf, path->len);
200         }
201
202         if (S_ISLNK(mode) && buffer) {
203                 if (size > sizeof(header.linkname)) {
204                         sprintf(header.linkname, "see %s.paxheader",
205                                 sha1_to_hex(sha1));
206                         strbuf_append_ext_header(&ext_header, "linkpath",
207                                                  buffer, size);
208                 } else
209                         memcpy(header.linkname, buffer, size);
210         }
211
212         sprintf(header.mode, "%07o", mode & 07777);
213         sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
214         sprintf(header.mtime, "%011lo", archive_time);
215
216         /* XXX: should we provide more meaningful info here? */
217         sprintf(header.uid, "%07o", 0);
218         sprintf(header.gid, "%07o", 0);
219         strncpy(header.uname, "git", 31);
220         strncpy(header.gname, "git", 31);
221         sprintf(header.devmajor, "%07o", 0);
222         sprintf(header.devminor, "%07o", 0);
223
224         memcpy(header.magic, "ustar", 6);
225         memcpy(header.version, "00", 2);
226
227         sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
228
229         if (ext_header.len > 0) {
230                 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
231                 free(ext_header.buf);
232         }
233         write_blocked(&header, sizeof(header));
234         if (S_ISREG(mode) && buffer && size > 0)
235                 write_blocked(buffer, size);
236 }
237
238 static void append_string(char **p, const char *s)
239 {
240         unsigned int len = strlen(s);
241         memcpy(*p, s, len);
242         *p += len;
243 }
244
245 static void append_char(char **p, char c)
246 {
247         **p = c;
248         *p += 1;
249 }
250
251 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
252 {
253         if (!prefix)
254                 return;
255         append_path_prefix(buffer, prefix->prev);
256         append_string(buffer, prefix->name);
257         append_char(buffer, '/');
258 }
259
260 static unsigned int path_prefix_len(struct path_prefix *prefix)
261 {
262         if (!prefix)
263                 return 0;
264         return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
265 }
266
267 static void append_path(char **p, int is_dir, const char *basepath,
268                         struct path_prefix *prefix, const char *path)
269 {
270         if (basepath) {
271                 append_string(p, basepath);
272                 append_char(p, '/');
273         }
274         append_path_prefix(p, prefix);
275         append_string(p, path);
276         if (is_dir)
277                 append_char(p, '/');
278 }
279
280 static unsigned int path_len(int is_dir, const char *basepath,
281                              struct path_prefix *prefix, const char *path)
282 {
283         unsigned int len = 0;
284         if (basepath)
285                 len += strlen(basepath) + 1;
286         len += path_prefix_len(prefix) + strlen(path);
287         if (is_dir)
288                 len++;
289         return len;
290 }
291
292 static void append_extended_header_prefix(char **p, unsigned int size,
293                                           const char *keyword)
294 {
295         int len = sprintf(*p, "%u %s=", size, keyword);
296         *p += len;
297 }
298
299 static unsigned int extended_header_len(const char *keyword,
300                                         unsigned int valuelen)
301 {
302         /* "%u %s=%s\n" */
303         unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
304         if (len > 9)
305                 len++;
306         if (len > 99)
307                 len++;
308         return len;
309 }
310
311 static void append_extended_header(char **p, const char *keyword,
312                                    const char *value, unsigned int len)
313 {
314         unsigned int size = extended_header_len(keyword, len);
315         append_extended_header_prefix(p, size, keyword);
316         memcpy(*p, value, len);
317         *p += len;
318         append_char(p, '\n');
319 }
320
321 static void write_header(const unsigned char *, char, const char *, struct path_prefix *,
322                          const char *, unsigned int, void *, unsigned long);
323
324 /* stores a pax extended header directly in the block buffer */
325 static void write_extended_header(const char *headerfilename, int is_dir,
326                                   unsigned int flags, const char *basepath,
327                                   struct path_prefix *prefix,
328                                   const char *path, unsigned int namelen,
329                                   void *content, unsigned int contentsize)
330 {
331         char *buffer, *p;
332         unsigned int pathlen, size, linkpathlen = 0;
333
334         size = pathlen = extended_header_len("path", namelen);
335         if (flags & EXT_HEADER_LINKPATH) {
336                 linkpathlen = extended_header_len("linkpath", contentsize);
337                 size += linkpathlen;
338         }
339         write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
340                      0100600, NULL, size);
341
342         buffer = p = malloc(size);
343         if (!buffer)
344                 die("git-tar-tree: %s", strerror(errno));
345         append_extended_header_prefix(&p, pathlen, "path");
346         append_path(&p, is_dir, basepath, prefix, path);
347         append_char(&p, '\n');
348         if (flags & EXT_HEADER_LINKPATH)
349                 append_extended_header(&p, "linkpath", content, contentsize);
350         write_blocked(buffer, size);
351         free(buffer);
352 }
353
354 static void write_global_extended_header(const unsigned char *sha1)
355 {
356         struct strbuf ext_header;
357         ext_header.buf = NULL;
358         ext_header.len = ext_header.alloc = 0;
359         strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
360         write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
361         free(ext_header.buf);
362 }
363
364 /* stores a ustar header directly in the block buffer */
365 static void write_header(const unsigned char *sha1, char typeflag, const char *basepath,
366                          struct path_prefix *prefix, const char *path,
367                          unsigned int mode, void *buffer, unsigned long size)
368 {
369         unsigned int namelen; 
370         char *header = NULL;
371         unsigned int checksum = 0;
372         int i;
373         unsigned int ext_header = 0;
374
375         if (typeflag == TYPEFLAG_AUTO) {
376                 if (S_ISDIR(mode))
377                         typeflag = TYPEFLAG_DIR;
378                 else if (S_ISLNK(mode))
379                         typeflag = TYPEFLAG_LNK;
380                 else
381                         typeflag = TYPEFLAG_REG;
382         }
383
384         namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
385         if (namelen > 100)
386                 ext_header |= EXT_HEADER_PATH;
387         if (typeflag == TYPEFLAG_LNK && size > 100)
388                 ext_header |= EXT_HEADER_LINKPATH;
389
390         /* the extended header must be written before the normal one */
391         if (ext_header) {
392                 char headerfilename[51];
393                 sprintf(headerfilename, "%s.paxheader", sha1_to_hex(sha1));
394                 write_extended_header(headerfilename, S_ISDIR(mode),
395                                       ext_header, basepath, prefix, path,
396                                       namelen, buffer, size);
397         }
398
399         header = get_record();
400
401         if (ext_header) {
402                 sprintf(header, "%s.data", sha1_to_hex(sha1));
403         } else {
404                 char *p = header;
405                 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
406         }
407
408         if (typeflag == TYPEFLAG_LNK) {
409                 if (ext_header & EXT_HEADER_LINKPATH) {
410                         sprintf(&header[157], "see %s.paxheader",
411                                 sha1_to_hex(sha1));
412                 } else {
413                         if (buffer)
414                                 strncpy(&header[157], buffer, size);
415                 }
416         }
417
418         if (S_ISDIR(mode))
419                 mode |= 0777;
420         else if (S_ISREG(mode))
421                 mode |= (mode & 0100) ? 0777 : 0666;
422         else if (S_ISLNK(mode))
423                 mode |= 0777;
424         sprintf(&header[100], "%07o", mode & 07777);
425
426         /* XXX: should we provide more meaningful info here? */
427         sprintf(&header[108], "%07o", 0);       /* uid */
428         sprintf(&header[116], "%07o", 0);       /* gid */
429         strncpy(&header[265], "git", 31);       /* uname */
430         strncpy(&header[297], "git", 31);       /* gname */
431
432         if (S_ISDIR(mode) || S_ISLNK(mode))
433                 size = 0;
434         sprintf(&header[124], "%011lo", size);
435         sprintf(&header[136], "%011lo", archive_time);
436
437         header[156] = typeflag;
438
439         memcpy(&header[257], "ustar", 6);
440         memcpy(&header[263], "00", 2);
441
442         sprintf(&header[329], "%07o", 0);       /* devmajor */
443         sprintf(&header[337], "%07o", 0);       /* devminor */
444
445         memset(&header[148], ' ', 8);
446         for (i = 0; i < RECORDSIZE; i++)
447                 checksum += header[i];
448         sprintf(&header[148], "%07o", checksum & 0x1fffff);
449
450         write_if_needed();
451 }
452
453 static void traverse_tree(struct tree_desc *tree,
454                           struct path_prefix *prefix)
455 {
456         struct path_prefix this_prefix;
457         this_prefix.prev = prefix;
458
459         while (tree->size) {
460                 const char *name;
461                 const unsigned char *sha1;
462                 unsigned mode;
463                 void *eltbuf;
464                 char elttype[20];
465                 unsigned long eltsize;
466
467                 sha1 = tree_entry_extract(tree, &name, &mode);
468                 update_tree_entry(tree);
469
470                 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
471                 if (!eltbuf)
472                         die("cannot read %s", sha1_to_hex(sha1));
473                 write_header(sha1, TYPEFLAG_AUTO, basedir, 
474                              prefix, name, mode, eltbuf, eltsize);
475                 if (S_ISDIR(mode)) {
476                         struct tree_desc subtree;
477                         subtree.buf = eltbuf;
478                         subtree.size = eltsize;
479                         this_prefix.name = name;
480                         traverse_tree(&subtree, &this_prefix);
481                 } else if (!S_ISLNK(mode)) {
482                         write_blocked(eltbuf, eltsize);
483                 }
484                 free(eltbuf);
485         }
486 }
487
488 int main(int argc, char **argv)
489 {
490         unsigned char sha1[20], tree_sha1[20];
491         struct commit *commit;
492         struct tree_desc tree;
493
494         setup_git_directory();
495
496         switch (argc) {
497         case 3:
498                 basedir = argv[2];
499                 /* FALLTHROUGH */
500         case 2:
501                 if (get_sha1(argv[1], sha1) < 0)
502                         usage(tar_tree_usage);
503                 break;
504         default:
505                 usage(tar_tree_usage);
506         }
507
508         commit = lookup_commit_reference_gently(sha1, 1);
509         if (commit) {
510                 write_global_extended_header(commit->object.sha1);
511                 archive_time = commit->date;
512         }
513         tree.buf = read_object_with_reference(sha1, "tree", &tree.size,
514                                               tree_sha1);
515         if (!tree.buf)
516                 die("not a reference to a tag, commit or tree object: %s",
517                     sha1_to_hex(sha1));
518         if (!archive_time)
519                 archive_time = time(NULL);
520         if (basedir)
521                 write_header(tree_sha1, TYPEFLAG_DIR, NULL, NULL,
522                         basedir, 040777, NULL, 0);
523         traverse_tree(&tree, NULL);
524         write_trailer();
525         return 0;
526 }