2 * Copyright (c) 2005 Rene Scharfe
7 #define RECORDSIZE (512)
8 #define BLOCKSIZE (RECORDSIZE * 20)
10 #define TYPEFLAG_AUTO '\0'
11 #define TYPEFLAG_REG '0'
12 #define TYPEFLAG_LNK '2'
13 #define TYPEFLAG_DIR '5'
14 #define TYPEFLAG_GLOBAL_HEADER 'g'
15 #define TYPEFLAG_EXT_HEADER 'x'
17 #define EXT_HEADER_PATH 1
18 #define EXT_HEADER_LINKPATH 2
20 static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
22 static char block[BLOCKSIZE];
23 static unsigned long offset;
25 static const char *basedir;
26 static time_t archive_time;
29 struct path_prefix *prev;
33 /* tries hard to write, either succeeds or dies in the attempt */
34 static void reliable_write(void *buf, unsigned long size)
37 long ret = xwrite(1, buf, size);
41 die("git-tar-tree: %s", strerror(errno));
43 die("git-tar-tree: disk full?");
50 /* writes out the whole block, but only if it is full */
51 static void write_if_needed(void)
53 if (offset == BLOCKSIZE) {
54 reliable_write(block, BLOCKSIZE);
59 /* acquire the next record from the buffer; user must call write_if_needed() */
60 static char *get_record(void)
62 char *p = block + offset;
63 memset(p, 0, RECORDSIZE);
69 * The end of tar archives is marked by 1024 nul bytes and after that
70 * follows the rest of the block (if any).
72 static void write_trailer(void)
85 * queues up writes, so that all our write(2) calls write exactly one
86 * full block; pads writes to RECORDSIZE
88 static void write_blocked(void *buf, unsigned long size)
93 unsigned long chunk = BLOCKSIZE - offset;
96 memcpy(block + offset, buf, chunk);
102 while (size >= BLOCKSIZE) {
103 reliable_write(buf, BLOCKSIZE);
108 memcpy(block + offset, buf, size);
112 tail = offset % RECORDSIZE;
114 memset(block + offset, 0, RECORDSIZE - tail);
115 offset += RECORDSIZE - tail;
120 static void append_string(char **p, const char *s)
122 unsigned int len = strlen(s);
127 static void append_char(char **p, char c)
133 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
137 append_path_prefix(buffer, prefix->prev);
138 append_string(buffer, prefix->name);
139 append_char(buffer, '/');
142 static unsigned int path_prefix_len(struct path_prefix *prefix)
146 return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
149 static void append_path(char **p, int is_dir, const char *basepath,
150 struct path_prefix *prefix, const char *path)
153 append_string(p, basepath);
156 append_path_prefix(p, prefix);
157 append_string(p, path);
162 static unsigned int path_len(int is_dir, const char *basepath,
163 struct path_prefix *prefix, const char *path)
165 unsigned int len = 0;
167 len += strlen(basepath) + 1;
168 len += path_prefix_len(prefix) + strlen(path);
174 static void append_extended_header_prefix(char **p, unsigned int size,
177 int len = sprintf(*p, "%u %s=", size, keyword);
181 static unsigned int extended_header_len(const char *keyword,
182 unsigned int valuelen)
185 unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
193 static void append_extended_header(char **p, const char *keyword,
194 const char *value, unsigned int len)
196 unsigned int size = extended_header_len(keyword, len);
197 append_extended_header_prefix(p, size, keyword);
198 memcpy(*p, value, len);
200 append_char(p, '\n');
203 static void write_header(const unsigned char *, char, const char *, struct path_prefix *,
204 const char *, unsigned int, void *, unsigned long);
206 /* stores a pax extended header directly in the block buffer */
207 static void write_extended_header(const char *headerfilename, int is_dir,
208 unsigned int flags, const char *basepath,
209 struct path_prefix *prefix,
210 const char *path, unsigned int namelen,
211 void *content, unsigned int contentsize)
214 unsigned int pathlen, size, linkpathlen = 0;
216 size = pathlen = extended_header_len("path", namelen);
217 if (flags & EXT_HEADER_LINKPATH) {
218 linkpathlen = extended_header_len("linkpath", contentsize);
221 write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
222 0100600, NULL, size);
224 buffer = p = malloc(size);
226 die("git-tar-tree: %s", strerror(errno));
227 append_extended_header_prefix(&p, pathlen, "path");
228 append_path(&p, is_dir, basepath, prefix, path);
229 append_char(&p, '\n');
230 if (flags & EXT_HEADER_LINKPATH)
231 append_extended_header(&p, "linkpath", content, contentsize);
232 write_blocked(buffer, size);
236 static void write_global_extended_header(const unsigned char *sha1)
241 size = extended_header_len("comment", 40);
242 write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL,
243 "pax_global_header", 0100600, NULL, size);
246 append_extended_header(&p, "comment", sha1_to_hex(sha1), 40);
250 /* stores a ustar header directly in the block buffer */
251 static void write_header(const unsigned char *sha1, char typeflag, const char *basepath,
252 struct path_prefix *prefix, const char *path,
253 unsigned int mode, void *buffer, unsigned long size)
255 unsigned int namelen;
257 unsigned int checksum = 0;
259 unsigned int ext_header = 0;
261 if (typeflag == TYPEFLAG_AUTO) {
263 typeflag = TYPEFLAG_DIR;
264 else if (S_ISLNK(mode))
265 typeflag = TYPEFLAG_LNK;
267 typeflag = TYPEFLAG_REG;
270 namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
272 ext_header |= EXT_HEADER_PATH;
273 if (typeflag == TYPEFLAG_LNK && size > 100)
274 ext_header |= EXT_HEADER_LINKPATH;
276 /* the extended header must be written before the normal one */
278 char headerfilename[51];
279 sprintf(headerfilename, "%s.paxheader", sha1_to_hex(sha1));
280 write_extended_header(headerfilename, S_ISDIR(mode),
281 ext_header, basepath, prefix, path,
282 namelen, buffer, size);
285 header = get_record();
288 sprintf(header, "%s.data", sha1_to_hex(sha1));
291 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
294 if (typeflag == TYPEFLAG_LNK) {
295 if (ext_header & EXT_HEADER_LINKPATH) {
296 sprintf(&header[157], "see %s.paxheader",
300 strncpy(&header[157], buffer, size);
305 mode |= 0755; /* GIT doesn't store permissions of dirs */
307 mode |= 0777; /* ... nor of symlinks */
308 sprintf(&header[100], "%07o", mode & 07777);
310 /* XXX: should we provide more meaningful info here? */
311 sprintf(&header[108], "%07o", 0); /* uid */
312 sprintf(&header[116], "%07o", 0); /* gid */
313 strncpy(&header[265], "git", 31); /* uname */
314 strncpy(&header[297], "git", 31); /* gname */
316 if (S_ISDIR(mode) || S_ISLNK(mode))
318 sprintf(&header[124], "%011lo", size);
319 sprintf(&header[136], "%011lo", archive_time);
321 header[156] = typeflag;
323 memcpy(&header[257], "ustar", 6);
324 memcpy(&header[263], "00", 2);
326 sprintf(&header[329], "%07o", 0); /* devmajor */
327 sprintf(&header[337], "%07o", 0); /* devminor */
329 memset(&header[148], ' ', 8);
330 for (i = 0; i < RECORDSIZE; i++)
331 checksum += header[i];
332 sprintf(&header[148], "%07o", checksum & 0x1fffff);
337 static void traverse_tree(void *buffer, unsigned long size,
338 struct path_prefix *prefix)
340 struct path_prefix this_prefix;
341 this_prefix.prev = prefix;
344 int namelen = strlen(buffer)+1;
347 unsigned long eltsize;
348 unsigned char *sha1 = buffer + namelen;
349 char *path = strchr(buffer, ' ') + 1;
352 if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
353 die("corrupt 'tree' file");
354 if (S_ISDIR(mode) || S_ISREG(mode))
355 mode |= (mode & 0100) ? 0777 : 0666;
357 size -= namelen + 20;
359 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
361 die("cannot read %s", sha1_to_hex(sha1));
362 write_header(sha1, TYPEFLAG_AUTO, basedir, prefix, path,
363 mode, eltbuf, eltsize);
364 if (!strcmp(elttype, "tree")) {
365 this_prefix.name = path;
366 traverse_tree(eltbuf, eltsize, &this_prefix);
367 } else if (!strcmp(elttype, "blob") && !S_ISLNK(mode)) {
368 write_blocked(eltbuf, eltsize);
374 /* get commit time from committer line of commit object */
375 static time_t commit_time(void * buffer, unsigned long size)
381 char *endp = memchr(p, '\n', size);
382 if (!endp || endp == p)
385 if (endp - p > 10 && !memcmp(p, "committer ", 10)) {
386 char *nump = strrchr(p, '>');
390 result = strtoul(nump, &endp, 10);
395 size -= endp - p - 1;
401 int main(int argc, char **argv)
403 unsigned char sha1[20];
404 unsigned char commit_sha1[20];
408 setup_git_directory();
415 if (get_sha1(argv[1], sha1) < 0)
416 usage(tar_tree_usage);
419 usage(tar_tree_usage);
422 buffer = read_object_with_reference(sha1, "commit", &size, commit_sha1);
424 write_global_extended_header(commit_sha1);
425 archive_time = commit_time(buffer, size);
428 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
430 die("not a reference to a tag, commit or tree object: %s",
433 archive_time = time(NULL);
435 write_header((unsigned char *)"0", TYPEFLAG_DIR, NULL, NULL,
436 basedir, 040755, NULL, 0);
437 traverse_tree(buffer, size, NULL);