#define RECORDSIZE (512)
#define BLOCKSIZE (RECORDSIZE * 20)
+#define TYPEFLAG_AUTO '\0'
+#define TYPEFLAG_REG '0'
+#define TYPEFLAG_DIR '5'
+#define TYPEFLAG_GLOBAL_HEADER 'g'
+#define TYPEFLAG_EXT_HEADER 'x'
+
static const char *tar_tree_usage = "tar-tree <key> [basedir]";
static char block[BLOCKSIZE];
}
}
+/* acquire the next record from the buffer; user must call write_if_needed() */
+static char *get_record(void)
+{
+ char *p = block + offset;
+ memset(p, 0, RECORDSIZE);
+ offset += RECORDSIZE;
+ return p;
+}
+
/*
* The end of tar archives is marked by 1024 nul bytes and after that
* follows the rest of the block (if any).
size++;
if (size > RECORDSIZE)
die("tar-tree: extended header too big, wtf?");
- write_header(NULL, 'x', NULL, NULL, headerfilename, 0100600, size);
- p = block + offset;
- memset(p, 0, RECORDSIZE);
- offset += RECORDSIZE;
+ write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
+ 0100600, size);
+ p = get_record();
append_long(&p, size);
append_string(&p, " path=");
append_path(&p, is_dir, basepath, prefix, path);
static void write_global_extended_header(const char *sha1)
{
char *p;
- write_header(NULL, 'g', NULL, NULL, "pax_global_header", 0, 52);
- p = block + offset;
- memset(p, 0, RECORDSIZE);
- offset += RECORDSIZE;
+ write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL,
+ "pax_global_header", 0100600, 52);
+ p = get_record();
append_long(&p, 52); /* 2 + 9 + 40 + 1 */
append_string(&p, " comment=");
append_string(&p, sha1_to_hex(sha1));
unsigned int checksum = 0;
int i;
+ if (typeflag == TYPEFLAG_AUTO) {
+ if (S_ISDIR(mode))
+ typeflag = TYPEFLAG_DIR;
+ else
+ typeflag = TYPEFLAG_REG;
+ }
+
namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
if (namelen > 500) {
die("tar-tree: name too log of object %s\n", sha1_to_hex(sha1));
write_extended_header(headerfilename, S_ISDIR(mode), basepath,
prefix, path, namelen);
- header = block + offset;
- memset(header, 0, RECORDSIZE);
- offset += RECORDSIZE;
+ header = get_record();
sprintf(header, "%s.data", sha1_hex);
} else {
- header = block + offset;
- memset(header, 0, RECORDSIZE);
- offset += RECORDSIZE;
- p = header;
+ p = header = get_record();
append_path(&p, S_ISDIR(mode), basepath, prefix, path);
}
eltbuf = read_sha1_file(sha1, elttype, &eltsize);
if (!eltbuf)
die("cannot read %s", sha1_to_hex(sha1));
- write_header(sha1, S_ISDIR(mode) ? '5' : '0', basedir,
- prefix, path, mode, eltsize);
+ write_header(sha1, TYPEFLAG_AUTO, basedir, prefix, path,
+ mode, eltsize);
if (!strcmp(elttype, "tree")) {
this_prefix.name = path;
traverse_tree(eltbuf, eltsize, &this_prefix);
basedir = argv[2];
/* FALLTHROUGH */
case 2:
- if (get_sha1_hex(argv[1], sha1) < 0)
+ if (get_sha1(argv[1], sha1) < 0)
usage(tar_tree_usage);
break;
default:
if (!archive_time)
archive_time = time(NULL);
if (basedir)
- write_header("0", '5', NULL, NULL, basedir, 040755, 0);
+ write_header("0", TYPEFLAG_DIR, NULL, NULL, basedir, 040755, 0);
traverse_tree(buffer, size, NULL);
free(buffer);
write_trailer();