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
= "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
= write(1, buf
, size
);
43 die("tar-tree: %s", strerror(errno
));
45 die("tar-tree: disk full?");
52 /* writes out the whole block, but only if it is full */
53 static void write_if_needed(void)
55 if (offset
== BLOCKSIZE
) {
56 reliable_write(block
, BLOCKSIZE
);
61 /* acquire the next record from the buffer; user must call write_if_needed() */
62 static char *get_record(void)
64 char *p
= block
+ offset
;
65 memset(p
, 0, RECORDSIZE
);
71 * The end of tar archives is marked by 1024 nul bytes and after that
72 * follows the rest of the block (if any).
74 static void write_trailer(void)
76 memset(block
+ offset
, 0, RECORDSIZE
);
79 memset(block
+ offset
, 0, RECORDSIZE
);
83 memset(block
+ offset
, 0, BLOCKSIZE
- offset
);
84 reliable_write(block
, BLOCKSIZE
);
90 * queues up writes, so that all our write(2) calls write exactly one
91 * full block; pads writes to RECORDSIZE
93 static void write_blocked(void *buf
, unsigned long size
)
98 unsigned long chunk
= BLOCKSIZE
- offset
;
101 memcpy(block
+ offset
, buf
, chunk
);
107 while (size
>= BLOCKSIZE
) {
108 reliable_write(buf
, BLOCKSIZE
);
113 memcpy(block
+ offset
, buf
, size
);
117 tail
= offset
% RECORDSIZE
;
119 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
120 offset
+= RECORDSIZE
- tail
;
125 static void append_string(char **p
, const char *s
)
127 unsigned int len
= strlen(s
);
132 static void append_char(char **p
, char c
)
138 static void append_path_prefix(char **buffer
, struct path_prefix
*prefix
)
142 append_path_prefix(buffer
, prefix
->prev
);
143 append_string(buffer
, prefix
->name
);
144 append_char(buffer
, '/');
147 static unsigned int path_prefix_len(struct path_prefix
*prefix
)
151 return path_prefix_len(prefix
->prev
) + strlen(prefix
->name
) + 1;
154 static void append_path(char **p
, int is_dir
, const char *basepath
,
155 struct path_prefix
*prefix
, const char *path
)
158 append_string(p
, basepath
);
161 append_path_prefix(p
, prefix
);
162 append_string(p
, path
);
167 static unsigned int path_len(int is_dir
, const char *basepath
,
168 struct path_prefix
*prefix
, const char *path
)
170 unsigned int len
= 0;
172 len
+= strlen(basepath
) + 1;
173 len
+= path_prefix_len(prefix
) + strlen(path
);
179 static void append_extended_header_prefix(char **p
, unsigned int size
,
182 int len
= sprintf(*p
, "%u %s=", size
, keyword
);
186 static unsigned int extended_header_len(const char *keyword
,
187 unsigned int valuelen
)
190 unsigned int len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
198 static void append_extended_header(char **p
, const char *keyword
,
199 const char *value
, unsigned int len
)
201 unsigned int size
= extended_header_len(keyword
, len
);
202 append_extended_header_prefix(p
, size
, keyword
);
203 memcpy(*p
, value
, len
);
205 append_char(p
, '\n');
208 static void write_header(const char *, char, const char *, struct path_prefix
*,
209 const char *, unsigned int, void *, unsigned long);
211 /* stores a pax extended header directly in the block buffer */
212 static void write_extended_header(const char *headerfilename
, int is_dir
,
213 unsigned int flags
, const char *basepath
,
214 struct path_prefix
*prefix
,
215 const char *path
, unsigned int namelen
,
216 void *content
, unsigned int contentsize
)
219 unsigned int pathlen
, size
, linkpathlen
= 0;
221 size
= pathlen
= extended_header_len("path", namelen
);
222 if (flags
& EXT_HEADER_LINKPATH
) {
223 linkpathlen
= extended_header_len("linkpath", contentsize
);
226 write_header(NULL
, TYPEFLAG_EXT_HEADER
, NULL
, NULL
, headerfilename
,
227 0100600, NULL
, size
);
229 buffer
= p
= malloc(size
);
231 die("git-tar-tree: %s", strerror(errno
));
232 append_extended_header_prefix(&p
, pathlen
, "path");
233 append_path(&p
, is_dir
, basepath
, prefix
, path
);
234 append_char(&p
, '\n');
235 if (flags
& EXT_HEADER_LINKPATH
)
236 append_extended_header(&p
, "linkpath", content
, contentsize
);
237 write_blocked(buffer
, size
);
241 static void write_global_extended_header(const char *sha1
)
246 size
= extended_header_len("comment", 40);
247 write_header(NULL
, TYPEFLAG_GLOBAL_HEADER
, NULL
, NULL
,
248 "pax_global_header", 0100600, NULL
, size
);
251 append_extended_header(&p
, "comment", sha1_to_hex(sha1
), 40);
255 /* stores a ustar header directly in the block buffer */
256 static void write_header(const char *sha1
, char typeflag
, const char *basepath
,
257 struct path_prefix
*prefix
, const char *path
,
258 unsigned int mode
, void *buffer
, unsigned long size
)
260 unsigned int namelen
;
262 unsigned int checksum
= 0;
264 unsigned int ext_header
= 0;
266 if (typeflag
== TYPEFLAG_AUTO
) {
268 typeflag
= TYPEFLAG_DIR
;
269 else if (S_ISLNK(mode
))
270 typeflag
= TYPEFLAG_LNK
;
272 typeflag
= TYPEFLAG_REG
;
275 namelen
= path_len(S_ISDIR(mode
), basepath
, prefix
, path
);
277 ext_header
|= EXT_HEADER_PATH
;
278 if (typeflag
== TYPEFLAG_LNK
&& size
> 100)
279 ext_header
|= EXT_HEADER_LINKPATH
;
281 /* the extended header must be written before the normal one */
283 char headerfilename
[51];
284 sprintf(headerfilename
, "%s.paxheader", sha1_to_hex(sha1
));
285 write_extended_header(headerfilename
, S_ISDIR(mode
),
286 ext_header
, basepath
, prefix
, path
,
287 namelen
, buffer
, size
);
290 header
= get_record();
293 sprintf(header
, "%s.data", sha1_to_hex(sha1
));
296 append_path(&p
, S_ISDIR(mode
), basepath
, prefix
, path
);
299 if (typeflag
== TYPEFLAG_LNK
) {
300 if (ext_header
& EXT_HEADER_LINKPATH
) {
301 sprintf(&header
[157], "see %s.paxheader",
305 strncpy(&header
[157], buffer
, size
);
310 mode
|= 0755; /* GIT doesn't store permissions of dirs */
312 mode
|= 0777; /* ... nor of symlinks */
313 sprintf(&header
[100], "%07o", mode
& 07777);
315 /* XXX: should we provide more meaningful info here? */
316 sprintf(&header
[108], "%07o", 0); /* uid */
317 sprintf(&header
[116], "%07o", 0); /* gid */
318 strncpy(&header
[265], "git", 31); /* uname */
319 strncpy(&header
[297], "git", 31); /* gname */
321 if (S_ISDIR(mode
) || S_ISLNK(mode
))
323 sprintf(&header
[124], "%011lo", size
);
324 sprintf(&header
[136], "%011lo", archive_time
);
326 header
[156] = typeflag
;
328 memcpy(&header
[257], "ustar", 6);
329 memcpy(&header
[263], "00", 2);
331 printf(&header
[329], "%07o", 0); /* devmajor */
332 printf(&header
[337], "%07o", 0); /* devminor */
334 memset(&header
[148], ' ', 8);
335 for (i
= 0; i
< RECORDSIZE
; i
++)
336 checksum
+= header
[i
];
337 sprintf(&header
[148], "%07o", checksum
& 0x1fffff);
342 static void traverse_tree(void *buffer
, unsigned long size
,
343 struct path_prefix
*prefix
)
345 struct path_prefix this_prefix
;
346 this_prefix
.prev
= prefix
;
349 int namelen
= strlen(buffer
)+1;
352 unsigned long eltsize
;
353 unsigned char *sha1
= buffer
+ namelen
;
354 char *path
= strchr(buffer
, ' ') + 1;
357 if (size
< namelen
+ 20 || sscanf(buffer
, "%o", &mode
) != 1)
358 die("corrupt 'tree' file");
360 size
-= namelen
+ 20;
362 eltbuf
= read_sha1_file(sha1
, elttype
, &eltsize
);
364 die("cannot read %s", sha1_to_hex(sha1
));
365 write_header(sha1
, TYPEFLAG_AUTO
, basedir
, prefix
, path
,
366 mode
, eltbuf
, eltsize
);
367 if (!strcmp(elttype
, "tree")) {
368 this_prefix
.name
= path
;
369 traverse_tree(eltbuf
, eltsize
, &this_prefix
);
370 } else if (!strcmp(elttype
, "blob") && !S_ISLNK(mode
)) {
371 write_blocked(eltbuf
, eltsize
);
377 /* get commit time from committer line of commit object */
378 time_t commit_time(void * buffer
, unsigned long size
)
384 char *endp
= memchr(p
, '\n', size
);
385 if (!endp
|| endp
== p
)
388 if (endp
- p
> 10 && !memcmp(p
, "committer ", 10)) {
389 char *nump
= strrchr(p
, '>');
393 result
= strtoul(nump
, &endp
, 10);
398 size
-= endp
- p
- 1;
404 int main(int argc
, char **argv
)
406 unsigned char sha1
[20];
407 unsigned char commit_sha1
[20];
416 if (get_sha1(argv
[1], sha1
) < 0)
417 usage(tar_tree_usage
);
420 usage(tar_tree_usage
);
423 buffer
= read_object_with_reference(sha1
, "commit", &size
, commit_sha1
);
425 write_global_extended_header(commit_sha1
);
426 archive_time
= commit_time(buffer
, size
);
429 buffer
= read_object_with_reference(sha1
, "tree", &size
, NULL
);
431 die("not a reference to a tag, commit or tree object: %s",
434 archive_time
= time(NULL
);
436 write_header("0", TYPEFLAG_DIR
, NULL
, NULL
, basedir
, 040755,
438 traverse_tree(buffer
, size
, NULL
);