tar-tree: Use write_entry() to write the archive contents
[git/gitweb-caching.git] / tar-tree.c
blob2be42fe6cbbf2ac835ba08ec7a091005fad8315f
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"
11 #define RECORDSIZE (512)
12 #define BLOCKSIZE (RECORDSIZE * 20)
14 #define EXT_HEADER_PATH 1
15 #define EXT_HEADER_LINKPATH 2
17 static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
19 static char block[BLOCKSIZE];
20 static unsigned long offset;
22 static const char *basedir;
23 static time_t archive_time;
25 struct path_prefix {
26 struct path_prefix *prev;
27 const char *name;
30 /* tries hard to write, either succeeds or dies in the attempt */
31 static void reliable_write(void *buf, unsigned long size)
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?");
42 size -= ret;
43 buf += ret;
47 /* writes out the whole block, but only if it is full */
48 static void write_if_needed(void)
50 if (offset == BLOCKSIZE) {
51 reliable_write(block, BLOCKSIZE);
52 offset = 0;
56 /* acquire the next record from the buffer; user must call write_if_needed() */
57 static char *get_record(void)
59 char *p = block + offset;
60 memset(p, 0, RECORDSIZE);
61 offset += RECORDSIZE;
62 return p;
66 * The end of tar archives is marked by 1024 nul bytes and after that
67 * follows the rest of the block (if any).
69 static void write_trailer(void)
71 get_record();
72 write_if_needed();
73 get_record();
74 write_if_needed();
75 while (offset) {
76 get_record();
77 write_if_needed();
82 * queues up writes, so that all our write(2) calls write exactly one
83 * full block; pads writes to RECORDSIZE
85 static void write_blocked(void *buf, unsigned long size)
87 unsigned long tail;
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();
99 while (size >= BLOCKSIZE) {
100 reliable_write(buf, BLOCKSIZE);
101 size -= BLOCKSIZE;
102 buf += BLOCKSIZE;
104 if (size) {
105 memcpy(block + offset, buf, size);
106 buf += size;
107 offset += size;
109 tail = offset % RECORDSIZE;
110 if (tail) {
111 memset(block + offset, 0, RECORDSIZE - tail);
112 offset += RECORDSIZE - tail;
114 write_if_needed();
117 static void strbuf_append_string(struct strbuf *sb, const char *s)
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;
125 memcpy(sb->buf + sb->len, s, slen);
126 sb->len = total;
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.
135 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
136 const char *value, unsigned int valuelen)
138 char *p;
139 int len, total, tmp;
141 /* "%u %s=%s\n" */
142 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
143 for (tmp = len; tmp > 9; tmp /= 10)
144 len++;
146 total = sb->len + len;
147 if (total > sb->alloc) {
148 sb->buf = xrealloc(sb->buf, total);
149 sb->alloc = total;
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;
160 static unsigned int ustar_header_chksum(const struct ustar_header *header)
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;
173 static void write_entry(const unsigned char *sha1, struct strbuf *path,
174 unsigned int mode, void *buffer, unsigned long size)
176 struct ustar_header header;
177 struct strbuf ext_header;
179 memset(&header, 0, sizeof(header));
180 ext_header.buf = NULL;
181 ext_header.len = ext_header.alloc = 0;
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;
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);
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);
224 sprintf(header.mode, "%07o", mode & 07777);
225 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
226 sprintf(header.mtime, "%011lo", archive_time);
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);
236 memcpy(header.magic, "ustar", 6);
237 memcpy(header.version, "00", 2);
239 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
241 if (ext_header.len > 0) {
242 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
243 free(ext_header.buf);
245 write_blocked(&header, sizeof(header));
246 if (S_ISREG(mode) && buffer && size > 0)
247 write_blocked(buffer, size);
250 static void append_string(char **p, const char *s)
252 unsigned int len = strlen(s);
253 memcpy(*p, s, len);
254 *p += len;
257 static void append_char(char **p, char c)
259 **p = c;
260 *p += 1;
263 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
265 if (!prefix)
266 return;
267 append_path_prefix(buffer, prefix->prev);
268 append_string(buffer, prefix->name);
269 append_char(buffer, '/');
272 static unsigned int path_prefix_len(struct path_prefix *prefix)
274 if (!prefix)
275 return 0;
276 return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
279 static void append_path(char **p, int is_dir, const char *basepath,
280 struct path_prefix *prefix, const char *path)
282 if (basepath) {
283 append_string(p, basepath);
284 append_char(p, '/');
286 append_path_prefix(p, prefix);
287 append_string(p, path);
288 if (is_dir)
289 append_char(p, '/');
292 static unsigned int path_len(int is_dir, const char *basepath,
293 struct path_prefix *prefix, const char *path)
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;
304 static void append_extended_header_prefix(char **p, unsigned int size,
305 const char *keyword)
307 int len = sprintf(*p, "%u %s=", size, keyword);
308 *p += len;
311 static unsigned int extended_header_len(const char *keyword,
312 unsigned int valuelen)
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;
323 static void append_extended_header(char **p, const char *keyword,
324 const char *value, unsigned int len)
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');
333 static void write_header(const unsigned char *, char, const char *, struct path_prefix *,
334 const char *, unsigned int, void *, unsigned long);
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)
343 char *buffer, *p;
344 unsigned int pathlen, size, linkpathlen = 0;
346 size = pathlen = extended_header_len("path", namelen);
347 if (flags & EXT_HEADER_LINKPATH) {
348 linkpathlen = extended_header_len("linkpath", contentsize);
349 size += linkpathlen;
351 write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
352 0100600, NULL, size);
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);
366 static void write_global_extended_header(const unsigned char *sha1)
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);
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)
381 unsigned int namelen;
382 char *header = NULL;
383 unsigned int checksum = 0;
384 int i;
385 unsigned int ext_header = 0;
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;
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;
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);
411 header = get_record();
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);
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);
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);
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 */
444 if (S_ISDIR(mode) || S_ISLNK(mode))
445 size = 0;
446 sprintf(&header[124], "%011lo", size);
447 sprintf(&header[136], "%011lo", archive_time);
449 header[156] = typeflag;
451 memcpy(&header[257], "ustar", 6);
452 memcpy(&header[263], "00", 2);
454 sprintf(&header[329], "%07o", 0); /* devmajor */
455 sprintf(&header[337], "%07o", 0); /* devminor */
457 memset(&header[148], ' ', 8);
458 for (i = 0; i < RECORDSIZE; i++)
459 checksum += header[i];
460 sprintf(&header[148], "%07o", checksum & 0x1fffff);
462 write_if_needed();
465 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
467 int pathlen = path->len;
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;
477 sha1 = tree_entry_extract(tree, &name, &mode);
478 update_tree_entry(tree);
480 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
481 if (!eltbuf)
482 die("cannot read %s", sha1_to_hex(sha1));
484 path->len = pathlen;
485 strbuf_append_string(path, name);
486 if (S_ISDIR(mode))
487 strbuf_append_string(path, "/");
489 write_entry(sha1, path, mode, eltbuf, eltsize);
491 if (S_ISDIR(mode)) {
492 struct tree_desc subtree;
493 subtree.buf = eltbuf;
494 subtree.size = eltsize;
495 traverse_tree(&subtree, path);
497 free(eltbuf);
501 int main(int argc, char **argv)
503 unsigned char sha1[20], tree_sha1[20];
504 struct commit *commit;
505 struct tree_desc tree;
506 struct strbuf current_path;
508 current_path.buf = xmalloc(PATH_MAX);
509 current_path.alloc = PATH_MAX;
510 current_path.len = current_path.eof = 0;
512 setup_git_directory();
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);
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);
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));
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;