2 * Copyright (c) 2005 Rene Scharfe
8 #define RECORDSIZE (512)
9 #define BLOCKSIZE (RECORDSIZE * 20)
11 #define TYPEFLAG_AUTO '\0'
12 #define TYPEFLAG_REG '0'
13 #define TYPEFLAG_LNK '2'
14 #define TYPEFLAG_DIR '5'
15 #define TYPEFLAG_GLOBAL_HEADER 'g'
16 #define TYPEFLAG_EXT_HEADER 'x'
18 #define EXT_HEADER_PATH 1
19 #define EXT_HEADER_LINKPATH 2
21 static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
23 static char block[BLOCKSIZE];
24 static unsigned long offset;
26 static const char *basedir;
27 static time_t archive_time;
30 struct path_prefix *prev;
34 /* tries hard to write, either succeeds or dies in the attempt */
35 static void reliable_write(void *buf, unsigned long size)
38 long ret = xwrite(1, buf, size);
42 die("git-tar-tree: %s", strerror(errno));
44 die("git-tar-tree: disk full?");
51 /* writes out the whole block, but only if it is full */
52 static void write_if_needed(void)
54 if (offset == BLOCKSIZE) {
55 reliable_write(block, BLOCKSIZE);
60 /* acquire the next record from the buffer; user must call write_if_needed() */
61 static char *get_record(void)
63 char *p = block + offset;
64 memset(p, 0, RECORDSIZE);
70 * The end of tar archives is marked by 1024 nul bytes and after that
71 * follows the rest of the block (if any).
73 static void write_trailer(void)
86 * queues up writes, so that all our write(2) calls write exactly one
87 * full block; pads writes to RECORDSIZE
89 static void write_blocked(void *buf, unsigned long size)
94 unsigned long chunk = BLOCKSIZE - offset;
97 memcpy(block + offset, buf, chunk);
103 while (size >= BLOCKSIZE) {
104 reliable_write(buf, BLOCKSIZE);
109 memcpy(block + offset, buf, size);
113 tail = offset % RECORDSIZE;
115 memset(block + offset, 0, RECORDSIZE - tail);
116 offset += RECORDSIZE - tail;
121 static void append_string(char **p, const char *s)
123 unsigned int len = strlen(s);
128 static void append_char(char **p, char c)
134 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
138 append_path_prefix(buffer, prefix->prev);
139 append_string(buffer, prefix->name);
140 append_char(buffer, '/');
143 static unsigned int path_prefix_len(struct path_prefix *prefix)
147 return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
150 static void append_path(char **p, int is_dir, const char *basepath,
151 struct path_prefix *prefix, const char *path)
154 append_string(p, basepath);
157 append_path_prefix(p, prefix);
158 append_string(p, path);
163 static unsigned int path_len(int is_dir, const char *basepath,
164 struct path_prefix *prefix, const char *path)
166 unsigned int len = 0;
168 len += strlen(basepath) + 1;
169 len += path_prefix_len(prefix) + strlen(path);
175 static void append_extended_header_prefix(char **p, unsigned int size,
178 int len = sprintf(*p, "%u %s=", size, keyword);
182 static unsigned int extended_header_len(const char *keyword,
183 unsigned int valuelen)
186 unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
194 static void append_extended_header(char **p, const char *keyword,
195 const char *value, unsigned int len)
197 unsigned int size = extended_header_len(keyword, len);
198 append_extended_header_prefix(p, size, keyword);
199 memcpy(*p, value, len);
201 append_char(p, '\n');
204 static void write_header(const unsigned char *, char, const char *, struct path_prefix *,
205 const char *, unsigned int, void *, unsigned long);
207 /* stores a pax extended header directly in the block buffer */
208 static void write_extended_header(const char *headerfilename, int is_dir,
209 unsigned int flags, const char *basepath,
210 struct path_prefix *prefix,
211 const char *path, unsigned int namelen,
212 void *content, unsigned int contentsize)
215 unsigned int pathlen, size, linkpathlen = 0;
217 size = pathlen = extended_header_len("path", namelen);
218 if (flags & EXT_HEADER_LINKPATH) {
219 linkpathlen = extended_header_len("linkpath", contentsize);
222 write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
223 0100600, NULL, size);
225 buffer = p = malloc(size);
227 die("git-tar-tree: %s", strerror(errno));
228 append_extended_header_prefix(&p, pathlen, "path");
229 append_path(&p, is_dir, basepath, prefix, path);
230 append_char(&p, '\n');
231 if (flags & EXT_HEADER_LINKPATH)
232 append_extended_header(&p, "linkpath", content, contentsize);
233 write_blocked(buffer, size);
237 static void write_global_extended_header(const unsigned char *sha1)
242 size = extended_header_len("comment", 40);
243 write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL,
244 "pax_global_header", 0100600, NULL, size);
247 append_extended_header(&p, "comment", sha1_to_hex(sha1), 40);
251 /* stores a ustar header directly in the block buffer */
252 static void write_header(const unsigned char *sha1, char typeflag, const char *basepath,
253 struct path_prefix *prefix, const char *path,
254 unsigned int mode, void *buffer, unsigned long size)
256 unsigned int namelen;
258 unsigned int checksum = 0;
260 unsigned int ext_header = 0;
262 if (typeflag == TYPEFLAG_AUTO) {
264 typeflag = TYPEFLAG_DIR;
265 else if (S_ISLNK(mode))
266 typeflag = TYPEFLAG_LNK;
268 typeflag = TYPEFLAG_REG;
271 namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
273 ext_header |= EXT_HEADER_PATH;
274 if (typeflag == TYPEFLAG_LNK && size > 100)
275 ext_header |= EXT_HEADER_LINKPATH;
277 /* the extended header must be written before the normal one */
279 char headerfilename[51];
280 sprintf(headerfilename, "%s.paxheader", sha1_to_hex(sha1));
281 write_extended_header(headerfilename, S_ISDIR(mode),
282 ext_header, basepath, prefix, path,
283 namelen, buffer, size);
286 header = get_record();
289 sprintf(header, "%s.data", sha1_to_hex(sha1));
292 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
295 if (typeflag == TYPEFLAG_LNK) {
296 if (ext_header & EXT_HEADER_LINKPATH) {
297 sprintf(&header[157], "see %s.paxheader",
301 strncpy(&header[157], buffer, size);
306 mode |= 0755; /* GIT doesn't store permissions of dirs */
308 mode |= 0777; /* ... nor of symlinks */
309 sprintf(&header[100], "%07o", mode & 07777);
311 /* XXX: should we provide more meaningful info here? */
312 sprintf(&header[108], "%07o", 0); /* uid */
313 sprintf(&header[116], "%07o", 0); /* gid */
314 strncpy(&header[265], "git", 31); /* uname */
315 strncpy(&header[297], "git", 31); /* gname */
317 if (S_ISDIR(mode) || S_ISLNK(mode))
319 sprintf(&header[124], "%011lo", size);
320 sprintf(&header[136], "%011lo", archive_time);
322 header[156] = typeflag;
324 memcpy(&header[257], "ustar", 6);
325 memcpy(&header[263], "00", 2);
327 sprintf(&header[329], "%07o", 0); /* devmajor */
328 sprintf(&header[337], "%07o", 0); /* devminor */
330 memset(&header[148], ' ', 8);
331 for (i = 0; i < RECORDSIZE; i++)
332 checksum += header[i];
333 sprintf(&header[148], "%07o", checksum & 0x1fffff);
338 static void traverse_tree(struct tree *tree,
339 struct path_prefix *prefix)
341 struct path_prefix this_prefix;
342 struct tree_entry_list *item;
343 this_prefix.prev = prefix;
346 item = tree->entries;
351 unsigned long eltsize;
353 eltbuf = read_sha1_file(item->item.any->sha1,
356 die("cannot read %s",
357 sha1_to_hex(item->item.any->sha1));
358 write_header(item->item.any->sha1, TYPEFLAG_AUTO, basedir,
360 item->mode, eltbuf, eltsize);
361 if (item->directory) {
362 this_prefix.name = item->name;
363 traverse_tree(item->item.tree, &this_prefix);
364 } else if (!item->symlink) {
365 write_blocked(eltbuf, eltsize);
372 /* get commit time from committer line of commit object */
373 static time_t commit_time(void * buffer, unsigned long size)
379 char *endp = memchr(p, '\n', size);
380 if (!endp || endp == p)
383 if (endp - p > 10 && !memcmp(p, "committer ", 10)) {
384 char *nump = strrchr(p, '>');
388 result = strtoul(nump, &endp, 10);
393 size -= endp - p - 1;
399 int main(int argc, char **argv)
401 unsigned char sha1[20];
402 unsigned char commit_sha1[20];
407 setup_git_directory();
414 if (get_sha1(argv[1], sha1) < 0)
415 usage(tar_tree_usage);
418 usage(tar_tree_usage);
421 buffer = read_object_with_reference(sha1, "commit", &size, commit_sha1);
423 write_global_extended_header(commit_sha1);
424 archive_time = commit_time(buffer, size);
427 tree = parse_tree_indirect(sha1);
429 die("not a reference to a tag, commit or tree object: %s",
432 archive_time = time(NULL);
434 write_header((unsigned char *)"0", TYPEFLAG_DIR, NULL, NULL,
435 basedir, 040777, NULL, 0);
436 traverse_tree(tree, NULL);