@matheusbsbs / matheus-bagliano / commits / cb0c6df6f5
Fork 0

tar-tree: Use write_entry() to write the archive contents

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>

Rene Scharfe committed Mar 25, 2006 at 23:21 UTC cb0c6df6f5e850875af28b47c5c94482a326293f
1 file changed +38 -18
tar-tree.c
+38 -18
@@ -114,6 +114,18 @@ static void write_blocked(void *buf, unsigned long size)
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
@@ -450,11 +462,9 @@ static void write_header(const unsigned char *sha1, char typeflag, const char *b
462 write_if_needed();
463 }
464
453 -static void traverse_tree(struct tree_desc *tree,
454 - struct path_prefix *prefix)
465 +static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
466 {
456 - struct path_prefix this_prefix;
457 - this_prefix.prev = prefix;
467 + int pathlen = path->len;
468
469 while (tree->size) {
470 const char *name;
@@ -470,16 +480,19 @@ static void traverse_tree(struct tree_desc *tree,
480 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
481 if (!eltbuf)
482 die("cannot read %s", sha1_to_hex(sha1));
473 - write_header(sha1, TYPEFLAG_AUTO, basedir,
474 - prefix, name, mode, eltbuf, eltsize);
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;
479 - this_prefix.name = name;
480 - traverse_tree(&subtree, &this_prefix);
481 - } else if (!S_ISLNK(mode)) {
482 - write_blocked(eltbuf, eltsize);
495 + traverse_tree(&subtree, path);
496 }
497 free(eltbuf);
498 }
@@ -490,12 +503,18 @@ 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;
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:
498 - basedir = argv[2];
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)
@@ -509,18 +528,19 @@ int main(int argc, char **argv)
528 if (commit) {
529 write_global_extended_header(commit->object.sha1);
530 archive_time = commit->date;
512 - }
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));
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);
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 }