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
);
49 * The end of tar archives is marked by 1024 nul bytes and after that
50 * follows the rest of the block (if any).
52 static void write_trailer(void)
54 memset(block
+ offset
, 0, RECORDSIZE
);
57 memset(block
+ offset
, 0, RECORDSIZE
);
61 memset(block
+ offset
, 0, BLOCKSIZE
- offset
);
62 reliable_write(block
, BLOCKSIZE
);
68 * queues up writes, so that all our write(2) calls write exactly one
69 * full block; pads writes to RECORDSIZE
71 static void write_blocked(void *buf
, unsigned long size
)
76 unsigned long chunk
= BLOCKSIZE
- offset
;
79 memcpy(block
+ offset
, buf
, chunk
);
85 while (size
>= BLOCKSIZE
) {
86 reliable_write(buf
, BLOCKSIZE
);
91 memcpy(block
+ offset
, buf
, size
);
95 tail
= offset
% RECORDSIZE
;
97 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
98 offset
+= RECORDSIZE
- tail
;
103 static void append_string(char **p
, const char *s
)
105 unsigned int len
= strlen(s
);
110 static void append_char(char **p
, char c
)
116 static void append_long(char **p
, long n
)
118 int len
= sprintf(*p
, "%ld", n
);
122 static void append_path_prefix(char **buffer
, struct path_prefix
*prefix
)
126 append_path_prefix(buffer
, prefix
->prev
);
127 append_string(buffer
, prefix
->name
);
128 append_char(buffer
, '/');
131 static unsigned int path_prefix_len(struct path_prefix
*prefix
)
135 return path_prefix_len(prefix
->prev
) + strlen(prefix
->name
) + 1;
138 static void append_path(char **p
, int is_dir
, const char *basepath
,
139 struct path_prefix
*prefix
, const char *path
)
142 append_string(p
, basepath
);
145 append_path_prefix(p
, prefix
);
146 append_string(p
, path
);
151 static unsigned int path_len(int is_dir
, const char *basepath
,
152 struct path_prefix
*prefix
, const char *path
)
154 unsigned int len
= 0;
156 len
+= strlen(basepath
) + 1;
157 len
+= path_prefix_len(prefix
) + strlen(path
);
163 static void write_header(const char *, char, const char *, struct path_prefix
*,
164 const char *, unsigned int, unsigned long);
166 /* stores a pax extended header directly in the block buffer */
167 static void write_extended_header(const char *headerfilename
, int is_dir
,
168 const char *basepath
,
169 struct path_prefix
*prefix
,
170 const char *path
, unsigned int namelen
)
173 unsigned int size
= 1 + 6 + namelen
+ 1;
178 if (size
> RECORDSIZE
)
179 die("tar-tree: extended header too big, wtf?");
180 write_header(NULL
, 'x', NULL
, NULL
, headerfilename
, 0100600, size
);
182 memset(p
, 0, RECORDSIZE
);
183 offset
+= RECORDSIZE
;
184 append_long(&p
, size
);
185 append_string(&p
, " path=");
186 append_path(&p
, is_dir
, basepath
, prefix
, path
);
187 append_char(&p
, '\n');
191 static void write_global_extended_header(const char *sha1
)
194 write_header(NULL
, 'g', NULL
, NULL
, "pax_global_header", 0, 52);
196 memset(p
, 0, RECORDSIZE
);
197 offset
+= RECORDSIZE
;
198 append_long(&p
, 52); /* 2 + 9 + 40 + 1 */
199 append_string(&p
, " comment=");
200 append_string(&p
, sha1_to_hex(sha1
));
201 append_char(&p
, '\n');
205 /* stores a ustar header directly in the block buffer */
206 static void write_header(const char *sha1
, char typeflag
, const char *basepath
,
207 struct path_prefix
*prefix
, const char *path
,
208 unsigned int mode
, unsigned long size
)
210 unsigned int namelen
;
211 char *p
, *header
= NULL
;
212 unsigned int checksum
= 0;
215 namelen
= path_len(S_ISDIR(mode
), basepath
, prefix
, path
);
217 die("tar-tree: name too log of object %s\n", sha1_to_hex(sha1
));
218 } else if (namelen
> 100) {
219 char *sha1_hex
= sha1_to_hex(sha1
);
220 char headerfilename
[51];
221 sprintf(headerfilename
, "%s.paxheader", sha1_hex
);
222 /* the extended header must be written before the normal one */
223 write_extended_header(headerfilename
, S_ISDIR(mode
), basepath
,
224 prefix
, path
, namelen
);
226 header
= block
+ offset
;
227 memset(header
, 0, RECORDSIZE
);
228 offset
+= RECORDSIZE
;
229 sprintf(header
, "%s.data", sha1_hex
);
231 header
= block
+ offset
;
232 memset(header
, 0, RECORDSIZE
);
233 offset
+= RECORDSIZE
;
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
);