4 #define RECORDSIZE (512)
5 #define BLOCKSIZE (RECORDSIZE * 20)
7 static const char *tar_tree_usage = "tar-tree <key> [basedir]";
9 static char block[BLOCKSIZE];
10 static unsigned long offset;
12 static const char *basedir;
13 static time_t archive_time;
16 struct path_prefix *prev;
20 /* tries hard to write, either succeeds or dies in the attempt */
21 static void reliable_write(void *buf, unsigned long size)
24 long ret = write(1, buf, size);
30 die("tar-tree: %s", strerror(errno));
32 die("tar-tree: disk full?");
39 /* writes out the whole block, but only if it is full */
40 static void write_if_needed(void)
42 if (offset == BLOCKSIZE) {
43 reliable_write(block, BLOCKSIZE);
48 /* acquire the next record from the buffer; user must call write_if_needed() */
49 static char *get_record(void)
51 char *p = block + offset;
52 memset(p, 0, RECORDSIZE);
58 * The end of tar archives is marked by 1024 nul bytes and after that
59 * follows the rest of the block (if any).
61 static void write_trailer(void)
63 memset(block + offset, 0, RECORDSIZE);
66 memset(block + offset, 0, RECORDSIZE);
70 memset(block + offset, 0, BLOCKSIZE - offset);
71 reliable_write(block, BLOCKSIZE);
77 * queues up writes, so that all our write(2) calls write exactly one
78 * full block; pads writes to RECORDSIZE
80 static void write_blocked(void *buf, unsigned long size)
85 unsigned long chunk = BLOCKSIZE - offset;
88 memcpy(block + offset, buf, chunk);
94 while (size >= BLOCKSIZE) {
95 reliable_write(buf, BLOCKSIZE);
100 memcpy(block + offset, buf, size);
104 tail = offset % RECORDSIZE;
106 memset(block + offset, 0, RECORDSIZE - tail);
107 offset += RECORDSIZE - tail;
112 static void append_string(char **p, const char *s)
114 unsigned int len = strlen(s);
119 static void append_char(char **p, char c)
125 static void append_long(char **p, long n)
127 int len = sprintf(*p, "%ld", n);
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 write_header(const char *, char, const char *, struct path_prefix *,
173 const char *, unsigned int, unsigned long);
175 /* stores a pax extended header directly in the block buffer */
176 static void write_extended_header(const char *headerfilename, int is_dir,
177 const char *basepath,
178 struct path_prefix *prefix,
179 const char *path, unsigned int namelen)
182 unsigned int size = 1 + 6 + namelen + 1;
187 if (size > RECORDSIZE)
188 die("tar-tree: extended header too big, wtf?");
189 write_header(NULL, 'x', NULL, NULL, headerfilename, 0100600, size);
191 append_long(&p, size);
192 append_string(&p, " path=");
193 append_path(&p, is_dir, basepath, prefix, path);
194 append_char(&p, '\n');
198 static void write_global_extended_header(const char *sha1)
201 write_header(NULL, 'g', NULL, NULL, "pax_global_header", 0, 52);
203 append_long(&p, 52); /* 2 + 9 + 40 + 1 */
204 append_string(&p, " comment=");
205 append_string(&p, sha1_to_hex(sha1));
206 append_char(&p, '\n');
210 /* stores a ustar header directly in the block buffer */
211 static void write_header(const char *sha1, char typeflag, const char *basepath,
212 struct path_prefix *prefix, const char *path,
213 unsigned int mode, unsigned long size)
215 unsigned int namelen;
216 char *p, *header = NULL;
217 unsigned int checksum = 0;
220 namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
222 die("tar-tree: name too log of object %s\n", sha1_to_hex(sha1));
223 } else if (namelen > 100) {
224 char *sha1_hex = sha1_to_hex(sha1);
225 char headerfilename[51];
226 sprintf(headerfilename, "%s.paxheader", sha1_hex);
227 /* the extended header must be written before the normal one */
228 write_extended_header(headerfilename, S_ISDIR(mode), basepath,
229 prefix, path, namelen);
231 header = get_record();
232 sprintf(header, "%s.data", sha1_hex);
234 p = header = get_record();
235 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
239 mode |= 0755; /* GIT doesn't store permissions of dirs */
240 sprintf(&header[100], "%07o", mode & 07777);
242 /* XXX: should we provide more meaningful info here? */
243 sprintf(&header[108], "%07o", 0); /* uid */
244 sprintf(&header[116], "%07o", 0); /* gid */
245 strncpy(&header[265], "git", 31); /* uname */
246 strncpy(&header[297], "git", 31); /* gname */
248 sprintf(&header[124], "%011lo", S_ISDIR(mode) ? 0 : size);
249 sprintf(&header[136], "%011lo", archive_time);
251 header[156] = typeflag;
253 memcpy(&header[257], "ustar", 6);
254 memcpy(&header[263], "00", 2);
256 printf(&header[329], "%07o", 0); /* devmajor */
257 printf(&header[337], "%07o", 0); /* devminor */
259 memset(&header[148], ' ', 8);
260 for (i = 0; i < RECORDSIZE; i++)
261 checksum += header[i];
262 sprintf(&header[148], "%07o", checksum & 0x1fffff);
267 static void traverse_tree(void *buffer, unsigned long size,
268 struct path_prefix *prefix)
270 struct path_prefix this_prefix;
271 this_prefix.prev = prefix;
274 int namelen = strlen(buffer)+1;
277 unsigned long eltsize;
278 unsigned char *sha1 = buffer + namelen;
279 char *path = strchr(buffer, ' ') + 1;
282 if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
283 die("corrupt 'tree' file");
285 size -= namelen + 20;
287 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
289 die("cannot read %s", sha1_to_hex(sha1));
290 write_header(sha1, S_ISDIR(mode) ? '5' : '0', basedir,
291 prefix, path, mode, eltsize);
292 if (!strcmp(elttype, "tree")) {
293 this_prefix.name = path;
294 traverse_tree(eltbuf, eltsize, &this_prefix);
295 } else if (!strcmp(elttype, "blob")) {
296 write_blocked(eltbuf, eltsize);
302 /* get commit time from committer line of commit object */
303 time_t commit_time(void * buffer, unsigned long size)
309 char *endp = memchr(p, '\n', size);
310 if (!endp || endp == p)
313 if (endp - p > 10 && !memcmp(p, "committer ", 10)) {
314 char *nump = strrchr(p, '>');
318 result = strtoul(nump, &endp, 10);
323 size -= endp - p - 1;
329 int main(int argc, char **argv)
331 unsigned char sha1[20];
332 unsigned char commit_sha1[20];
341 if (get_sha1(argv[1], sha1) < 0)
342 usage(tar_tree_usage);
345 usage(tar_tree_usage);
348 sha1_file_directory = getenv(DB_ENVIRONMENT);
349 if (!sha1_file_directory)
350 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
352 buffer = read_object_with_reference(sha1, "commit", &size, commit_sha1);
354 write_global_extended_header(commit_sha1);
355 archive_time = commit_time(buffer, size);
358 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
360 die("not a reference to a tag, commit or tree object: %s",
363 archive_time = time(NULL);
365 write_header("0", '5', NULL, NULL, basedir, 040755, 0);
366 traverse_tree(buffer, size, NULL);