4 #define RECORDSIZE (512)
5 #define BLOCKSIZE (RECORDSIZE * 20)
7 #define TYPEFLAG_AUTO '\0'
8 #define TYPEFLAG_REG '0'
9 #define TYPEFLAG_DIR '5'
10 #define TYPEFLAG_GLOBAL_HEADER 'g'
11 #define TYPEFLAG_EXT_HEADER 'x'
13 static const char *tar_tree_usage = "tar-tree <key> [basedir]";
15 static char block[BLOCKSIZE];
16 static unsigned long offset;
18 static const char *basedir;
19 static time_t archive_time;
22 struct path_prefix *prev;
26 /* tries hard to write, either succeeds or dies in the attempt */
27 static void reliable_write(void *buf, unsigned long size)
30 long ret = write(1, buf, size);
36 die("tar-tree: %s", strerror(errno));
38 die("tar-tree: disk full?");
45 /* writes out the whole block, but only if it is full */
46 static void write_if_needed(void)
48 if (offset == BLOCKSIZE) {
49 reliable_write(block, BLOCKSIZE);
54 /* acquire the next record from the buffer; user must call write_if_needed() */
55 static char *get_record(void)
57 char *p = block + offset;
58 memset(p, 0, RECORDSIZE);
64 * The end of tar archives is marked by 1024 nul bytes and after that
65 * follows the rest of the block (if any).
67 static void write_trailer(void)
69 memset(block + offset, 0, RECORDSIZE);
72 memset(block + offset, 0, RECORDSIZE);
76 memset(block + offset, 0, BLOCKSIZE - offset);
77 reliable_write(block, BLOCKSIZE);
83 * queues up writes, so that all our write(2) calls write exactly one
84 * full block; pads writes to RECORDSIZE
86 static void write_blocked(void *buf, unsigned long size)
91 unsigned long chunk = BLOCKSIZE - offset;
94 memcpy(block + offset, buf, chunk);
100 while (size >= BLOCKSIZE) {
101 reliable_write(buf, BLOCKSIZE);
106 memcpy(block + offset, buf, size);
110 tail = offset % RECORDSIZE;
112 memset(block + offset, 0, RECORDSIZE - tail);
113 offset += RECORDSIZE - tail;
118 static void append_string(char **p, const char *s)
120 unsigned int len = strlen(s);
125 static void append_char(char **p, char c)
131 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
135 append_path_prefix(buffer, prefix->prev);
136 append_string(buffer, prefix->name);
137 append_char(buffer, '/');
140 static unsigned int path_prefix_len(struct path_prefix *prefix)
144 return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
147 static void append_path(char **p, int is_dir, const char *basepath,
148 struct path_prefix *prefix, const char *path)
151 append_string(p, basepath);
154 append_path_prefix(p, prefix);
155 append_string(p, path);
160 static unsigned int path_len(int is_dir, const char *basepath,
161 struct path_prefix *prefix, const char *path)
163 unsigned int len = 0;
165 len += strlen(basepath) + 1;
166 len += path_prefix_len(prefix) + strlen(path);
172 static void append_extended_header_prefix(char **p, unsigned int size,
175 int len = sprintf(*p, "%u %s=", size, keyword);
179 static unsigned int extended_header_len(const char *keyword,
180 unsigned int valuelen)
183 unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
191 static void append_extended_header(char **p, const char *keyword,
192 const char *value, unsigned int len)
194 unsigned int size = extended_header_len(keyword, len);
195 append_extended_header_prefix(p, size, keyword);
196 memcpy(*p, value, len);
198 append_char(p, '\n');
201 static void write_header(const char *, char, const char *, struct path_prefix *,
202 const char *, unsigned int, unsigned long);
204 /* stores a pax extended header directly in the block buffer */
205 static void write_extended_header(const char *headerfilename, int is_dir,
206 const char *basepath,
207 struct path_prefix *prefix,
208 const char *path, unsigned int namelen)
211 unsigned int pathlen, size;
213 size = pathlen = extended_header_len("path", namelen);
214 if (size > RECORDSIZE)
215 die("tar-tree: extended header too big, wtf?");
216 write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
220 append_extended_header_prefix(&p, pathlen, "path");
221 append_path(&p, is_dir, basepath, prefix, path);
222 append_char(&p, '\n');
226 static void write_global_extended_header(const char *sha1)
231 size = extended_header_len("comment", 40);
232 write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL,
233 "pax_global_header", 0100600, size);
236 append_extended_header(&p, "comment", sha1_to_hex(sha1), 40);
240 /* stores a ustar header directly in the block buffer */
241 static void write_header(const char *sha1, char typeflag, const char *basepath,
242 struct path_prefix *prefix, const char *path,
243 unsigned int mode, unsigned long size)
245 unsigned int namelen;
246 char *p, *header = NULL;
247 unsigned int checksum = 0;
250 if (typeflag == TYPEFLAG_AUTO) {
252 typeflag = TYPEFLAG_DIR;
254 typeflag = TYPEFLAG_REG;
257 namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
259 die("tar-tree: name too log of object %s\n", sha1_to_hex(sha1));
260 } else if (namelen > 100) {
261 char *sha1_hex = sha1_to_hex(sha1);
262 char headerfilename[51];
263 sprintf(headerfilename, "%s.paxheader", sha1_hex);
264 /* the extended header must be written before the normal one */
265 write_extended_header(headerfilename, S_ISDIR(mode), basepath,
266 prefix, path, namelen);
268 header = get_record();
269 sprintf(header, "%s.data", sha1_hex);
271 p = header = get_record();
272 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
276 mode |= 0755; /* GIT doesn't store permissions of dirs */
277 sprintf(&header[100], "%07o", mode & 07777);
279 /* XXX: should we provide more meaningful info here? */
280 sprintf(&header[108], "%07o", 0); /* uid */
281 sprintf(&header[116], "%07o", 0); /* gid */
282 strncpy(&header[265], "git", 31); /* uname */
283 strncpy(&header[297], "git", 31); /* gname */
285 sprintf(&header[124], "%011lo", S_ISDIR(mode) ? 0 : size);
286 sprintf(&header[136], "%011lo", archive_time);
288 header[156] = typeflag;
290 memcpy(&header[257], "ustar", 6);
291 memcpy(&header[263], "00", 2);
293 printf(&header[329], "%07o", 0); /* devmajor */
294 printf(&header[337], "%07o", 0); /* devminor */
296 memset(&header[148], ' ', 8);
297 for (i = 0; i < RECORDSIZE; i++)
298 checksum += header[i];
299 sprintf(&header[148], "%07o", checksum & 0x1fffff);
304 static void traverse_tree(void *buffer, unsigned long size,
305 struct path_prefix *prefix)
307 struct path_prefix this_prefix;
308 this_prefix.prev = prefix;
311 int namelen = strlen(buffer)+1;
314 unsigned long eltsize;
315 unsigned char *sha1 = buffer + namelen;
316 char *path = strchr(buffer, ' ') + 1;
319 if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
320 die("corrupt 'tree' file");
322 size -= namelen + 20;
324 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
326 die("cannot read %s", sha1_to_hex(sha1));
327 write_header(sha1, TYPEFLAG_AUTO, basedir, prefix, path,
329 if (!strcmp(elttype, "tree")) {
330 this_prefix.name = path;
331 traverse_tree(eltbuf, eltsize, &this_prefix);
332 } else if (!strcmp(elttype, "blob")) {
333 write_blocked(eltbuf, eltsize);
339 /* get commit time from committer line of commit object */
340 time_t commit_time(void * buffer, unsigned long size)
346 char *endp = memchr(p, '\n', size);
347 if (!endp || endp == p)
350 if (endp - p > 10 && !memcmp(p, "committer ", 10)) {
351 char *nump = strrchr(p, '>');
355 result = strtoul(nump, &endp, 10);
360 size -= endp - p - 1;
366 int main(int argc, char **argv)
368 unsigned char sha1[20];
369 unsigned char commit_sha1[20];
378 if (get_sha1(argv[1], sha1) < 0)
379 usage(tar_tree_usage);
382 usage(tar_tree_usage);
385 sha1_file_directory = getenv(DB_ENVIRONMENT);
386 if (!sha1_file_directory)
387 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
389 buffer = read_object_with_reference(sha1, "commit", &size, commit_sha1);
391 write_global_extended_header(commit_sha1);
392 archive_time = commit_time(buffer, size);
395 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
397 die("not a reference to a tag, commit or tree object: %s",
400 archive_time = time(NULL);
402 write_header("0", TYPEFLAG_DIR, NULL, NULL, basedir, 040755, 0);
403 traverse_tree(buffer, size, NULL);