]>
Commit | Line | Data |
---|---|---|
92747a90 PB |
1 | /* |
2 | * Copyright (c) 2005 Rene Scharfe | |
3 | */ | |
731ab9cc RS |
4 | #include <time.h> |
5 | #include "cache.h" | |
6 | ||
7 | #define RECORDSIZE (512) | |
8 | #define BLOCKSIZE (RECORDSIZE * 20) | |
9 | ||
03d791ff RS |
10 | #define TYPEFLAG_AUTO '\0' |
11 | #define TYPEFLAG_REG '0' | |
d5776d50 | 12 | #define TYPEFLAG_LNK '2' |
03d791ff RS |
13 | #define TYPEFLAG_DIR '5' |
14 | #define TYPEFLAG_GLOBAL_HEADER 'g' | |
15 | #define TYPEFLAG_EXT_HEADER 'x' | |
16 | ||
d5776d50 RS |
17 | #define EXT_HEADER_PATH 1 |
18 | #define EXT_HEADER_LINKPATH 2 | |
19 | ||
4d1f1190 | 20 | static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]"; |
731ab9cc RS |
21 | |
22 | static char block[BLOCKSIZE]; | |
23 | static unsigned long offset; | |
24 | ||
25 | static const char *basedir; | |
26 | static time_t archive_time; | |
27 | ||
28 | struct path_prefix { | |
29 | struct path_prefix *prev; | |
30 | const char *name; | |
31 | }; | |
32 | ||
33 | /* tries hard to write, either succeeds or dies in the attempt */ | |
34 | static void reliable_write(void *buf, unsigned long size) | |
35 | { | |
36 | while (size > 0) { | |
1c15afb9 | 37 | long ret = xwrite(1, buf, size); |
731ab9cc | 38 | if (ret < 0) { |
731ab9cc RS |
39 | if (errno == EPIPE) |
40 | exit(0); | |
667bb59b | 41 | die("git-tar-tree: %s", strerror(errno)); |
731ab9cc | 42 | } else if (!ret) { |
667bb59b | 43 | die("git-tar-tree: disk full?"); |
731ab9cc RS |
44 | } |
45 | size -= ret; | |
46 | buf += ret; | |
47 | } | |
48 | } | |
49 | ||
50 | /* writes out the whole block, but only if it is full */ | |
51 | static void write_if_needed(void) | |
52 | { | |
53 | if (offset == BLOCKSIZE) { | |
54 | reliable_write(block, BLOCKSIZE); | |
55 | offset = 0; | |
56 | } | |
57 | } | |
58 | ||
a90a6e6a RS |
59 | /* acquire the next record from the buffer; user must call write_if_needed() */ |
60 | static char *get_record(void) | |
61 | { | |
62 | char *p = block + offset; | |
63 | memset(p, 0, RECORDSIZE); | |
64 | offset += RECORDSIZE; | |
65 | return p; | |
66 | } | |
67 | ||
731ab9cc RS |
68 | /* |
69 | * The end of tar archives is marked by 1024 nul bytes and after that | |
70 | * follows the rest of the block (if any). | |
71 | */ | |
72 | static void write_trailer(void) | |
73 | { | |
9b5b9f39 | 74 | get_record(); |
731ab9cc | 75 | write_if_needed(); |
9b5b9f39 | 76 | get_record(); |
731ab9cc | 77 | write_if_needed(); |
a325a11b | 78 | while (offset) { |
9b5b9f39 RS |
79 | get_record(); |
80 | write_if_needed(); | |
731ab9cc RS |
81 | } |
82 | } | |
83 | ||
84 | /* | |
85 | * queues up writes, so that all our write(2) calls write exactly one | |
86 | * full block; pads writes to RECORDSIZE | |
87 | */ | |
88 | static void write_blocked(void *buf, unsigned long size) | |
89 | { | |
90 | unsigned long tail; | |
91 | ||
92 | if (offset) { | |
93 | unsigned long chunk = BLOCKSIZE - offset; | |
94 | if (size < chunk) | |
95 | chunk = size; | |
96 | memcpy(block + offset, buf, chunk); | |
97 | size -= chunk; | |
98 | offset += chunk; | |
99 | buf += chunk; | |
100 | write_if_needed(); | |
101 | } | |
102 | while (size >= BLOCKSIZE) { | |
103 | reliable_write(buf, BLOCKSIZE); | |
104 | size -= BLOCKSIZE; | |
105 | buf += BLOCKSIZE; | |
106 | } | |
107 | if (size) { | |
108 | memcpy(block + offset, buf, size); | |
109 | buf += size; | |
110 | offset += size; | |
111 | } | |
112 | tail = offset % RECORDSIZE; | |
113 | if (tail) { | |
114 | memset(block + offset, 0, RECORDSIZE - tail); | |
115 | offset += RECORDSIZE - tail; | |
116 | } | |
117 | write_if_needed(); | |
118 | } | |
119 | ||
120 | static void append_string(char **p, const char *s) | |
121 | { | |
122 | unsigned int len = strlen(s); | |
123 | memcpy(*p, s, len); | |
124 | *p += len; | |
125 | } | |
126 | ||
127 | static void append_char(char **p, char c) | |
128 | { | |
129 | **p = c; | |
130 | *p += 1; | |
131 | } | |
132 | ||
731ab9cc RS |
133 | static void append_path_prefix(char **buffer, struct path_prefix *prefix) |
134 | { | |
135 | if (!prefix) | |
136 | return; | |
137 | append_path_prefix(buffer, prefix->prev); | |
138 | append_string(buffer, prefix->name); | |
139 | append_char(buffer, '/'); | |
140 | } | |
141 | ||
142 | static unsigned int path_prefix_len(struct path_prefix *prefix) | |
143 | { | |
144 | if (!prefix) | |
145 | return 0; | |
146 | return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1; | |
147 | } | |
148 | ||
149 | static void append_path(char **p, int is_dir, const char *basepath, | |
150 | struct path_prefix *prefix, const char *path) | |
151 | { | |
152 | if (basepath) { | |
153 | append_string(p, basepath); | |
154 | append_char(p, '/'); | |
155 | } | |
156 | append_path_prefix(p, prefix); | |
157 | append_string(p, path); | |
158 | if (is_dir) | |
159 | append_char(p, '/'); | |
160 | } | |
161 | ||
162 | static unsigned int path_len(int is_dir, const char *basepath, | |
163 | struct path_prefix *prefix, const char *path) | |
164 | { | |
165 | unsigned int len = 0; | |
166 | if (basepath) | |
167 | len += strlen(basepath) + 1; | |
168 | len += path_prefix_len(prefix) + strlen(path); | |
169 | if (is_dir) | |
170 | len++; | |
171 | return len; | |
172 | } | |
173 | ||
71058b1f RS |
174 | static void append_extended_header_prefix(char **p, unsigned int size, |
175 | const char *keyword) | |
176 | { | |
177 | int len = sprintf(*p, "%u %s=", size, keyword); | |
178 | *p += len; | |
179 | } | |
180 | ||
181 | static unsigned int extended_header_len(const char *keyword, | |
182 | unsigned int valuelen) | |
183 | { | |
184 | /* "%u %s=%s\n" */ | |
185 | unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1; | |
186 | if (len > 9) | |
187 | len++; | |
188 | if (len > 99) | |
189 | len++; | |
190 | return len; | |
191 | } | |
192 | ||
193 | static void append_extended_header(char **p, const char *keyword, | |
194 | const char *value, unsigned int len) | |
195 | { | |
196 | unsigned int size = extended_header_len(keyword, len); | |
197 | append_extended_header_prefix(p, size, keyword); | |
198 | memcpy(*p, value, len); | |
199 | *p += len; | |
200 | append_char(p, '\n'); | |
201 | } | |
202 | ||
bf0f910d | 203 | static void write_header(const unsigned char *, char, const char *, struct path_prefix *, |
08f09dac | 204 | const char *, unsigned int, void *, unsigned long); |
731ab9cc RS |
205 | |
206 | /* stores a pax extended header directly in the block buffer */ | |
207 | static void write_extended_header(const char *headerfilename, int is_dir, | |
08f09dac | 208 | unsigned int flags, const char *basepath, |
731ab9cc | 209 | struct path_prefix *prefix, |
08f09dac RS |
210 | const char *path, unsigned int namelen, |
211 | void *content, unsigned int contentsize) | |
731ab9cc | 212 | { |
e7d3dd24 | 213 | char *buffer, *p; |
d5776d50 | 214 | unsigned int pathlen, size, linkpathlen = 0; |
71058b1f RS |
215 | |
216 | size = pathlen = extended_header_len("path", namelen); | |
d5776d50 RS |
217 | if (flags & EXT_HEADER_LINKPATH) { |
218 | linkpathlen = extended_header_len("linkpath", contentsize); | |
219 | size += linkpathlen; | |
220 | } | |
03d791ff | 221 | write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename, |
08f09dac | 222 | 0100600, NULL, size); |
71058b1f | 223 | |
e7d3dd24 RS |
224 | buffer = p = malloc(size); |
225 | if (!buffer) | |
226 | die("git-tar-tree: %s", strerror(errno)); | |
71058b1f | 227 | append_extended_header_prefix(&p, pathlen, "path"); |
731ab9cc RS |
228 | append_path(&p, is_dir, basepath, prefix, path); |
229 | append_char(&p, '\n'); | |
d5776d50 RS |
230 | if (flags & EXT_HEADER_LINKPATH) |
231 | append_extended_header(&p, "linkpath", content, contentsize); | |
e7d3dd24 RS |
232 | write_blocked(buffer, size); |
233 | free(buffer); | |
731ab9cc RS |
234 | } |
235 | ||
bf0f910d | 236 | static void write_global_extended_header(const unsigned char *sha1) |
87fec8fc RS |
237 | { |
238 | char *p; | |
71058b1f RS |
239 | unsigned int size; |
240 | ||
241 | size = extended_header_len("comment", 40); | |
03d791ff | 242 | write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL, |
08f09dac | 243 | "pax_global_header", 0100600, NULL, size); |
71058b1f | 244 | |
a90a6e6a | 245 | p = get_record(); |
71058b1f | 246 | append_extended_header(&p, "comment", sha1_to_hex(sha1), 40); |
87fec8fc RS |
247 | write_if_needed(); |
248 | } | |
249 | ||
731ab9cc | 250 | /* stores a ustar header directly in the block buffer */ |
bf0f910d | 251 | static void write_header(const unsigned char *sha1, char typeflag, const char *basepath, |
731ab9cc | 252 | struct path_prefix *prefix, const char *path, |
08f09dac | 253 | unsigned int mode, void *buffer, unsigned long size) |
731ab9cc RS |
254 | { |
255 | unsigned int namelen; | |
d5776d50 | 256 | char *header = NULL; |
731ab9cc RS |
257 | unsigned int checksum = 0; |
258 | int i; | |
d5776d50 | 259 | unsigned int ext_header = 0; |
731ab9cc | 260 | |
03d791ff RS |
261 | if (typeflag == TYPEFLAG_AUTO) { |
262 | if (S_ISDIR(mode)) | |
263 | typeflag = TYPEFLAG_DIR; | |
d5776d50 RS |
264 | else if (S_ISLNK(mode)) |
265 | typeflag = TYPEFLAG_LNK; | |
03d791ff RS |
266 | else |
267 | typeflag = TYPEFLAG_REG; | |
268 | } | |
269 | ||
731ab9cc | 270 | namelen = path_len(S_ISDIR(mode), basepath, prefix, path); |
e7d3dd24 | 271 | if (namelen > 100) |
d5776d50 RS |
272 | ext_header |= EXT_HEADER_PATH; |
273 | if (typeflag == TYPEFLAG_LNK && size > 100) | |
274 | ext_header |= EXT_HEADER_LINKPATH; | |
275 | ||
276 | /* the extended header must be written before the normal one */ | |
277 | if (ext_header) { | |
731ab9cc | 278 | char headerfilename[51]; |
d5776d50 | 279 | sprintf(headerfilename, "%s.paxheader", sha1_to_hex(sha1)); |
08f09dac | 280 | write_extended_header(headerfilename, S_ISDIR(mode), |
d5776d50 | 281 | ext_header, basepath, prefix, path, |
08f09dac | 282 | namelen, buffer, size); |
d5776d50 | 283 | } |
731ab9cc | 284 | |
d5776d50 RS |
285 | header = get_record(); |
286 | ||
287 | if (ext_header) { | |
288 | sprintf(header, "%s.data", sha1_to_hex(sha1)); | |
731ab9cc | 289 | } else { |
d5776d50 | 290 | char *p = header; |
731ab9cc RS |
291 | append_path(&p, S_ISDIR(mode), basepath, prefix, path); |
292 | } | |
293 | ||
d5776d50 RS |
294 | if (typeflag == TYPEFLAG_LNK) { |
295 | if (ext_header & EXT_HEADER_LINKPATH) { | |
296 | sprintf(&header[157], "see %s.paxheader", | |
297 | sha1_to_hex(sha1)); | |
298 | } else { | |
299 | if (buffer) | |
300 | strncpy(&header[157], buffer, size); | |
301 | } | |
302 | } | |
303 | ||
731ab9cc RS |
304 | if (S_ISDIR(mode)) |
305 | mode |= 0755; /* GIT doesn't store permissions of dirs */ | |
d5776d50 RS |
306 | if (S_ISLNK(mode)) |
307 | mode |= 0777; /* ... nor of symlinks */ | |
731ab9cc RS |
308 | sprintf(&header[100], "%07o", mode & 07777); |
309 | ||
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 */ | |
315 | ||
d5776d50 RS |
316 | if (S_ISDIR(mode) || S_ISLNK(mode)) |
317 | size = 0; | |
318 | sprintf(&header[124], "%011lo", size); | |
731ab9cc RS |
319 | sprintf(&header[136], "%011lo", archive_time); |
320 | ||
87fec8fc | 321 | header[156] = typeflag; |
731ab9cc RS |
322 | |
323 | memcpy(&header[257], "ustar", 6); | |
324 | memcpy(&header[263], "00", 2); | |
325 | ||
4ec99bf0 TS |
326 | sprintf(&header[329], "%07o", 0); /* devmajor */ |
327 | sprintf(&header[337], "%07o", 0); /* devminor */ | |
731ab9cc RS |
328 | |
329 | memset(&header[148], ' ', 8); | |
330 | for (i = 0; i < RECORDSIZE; i++) | |
331 | checksum += header[i]; | |
332 | sprintf(&header[148], "%07o", checksum & 0x1fffff); | |
333 | ||
334 | write_if_needed(); | |
335 | } | |
336 | ||
337 | static void traverse_tree(void *buffer, unsigned long size, | |
338 | struct path_prefix *prefix) | |
339 | { | |
340 | struct path_prefix this_prefix; | |
341 | this_prefix.prev = prefix; | |
342 | ||
343 | while (size) { | |
344 | int namelen = strlen(buffer)+1; | |
345 | void *eltbuf; | |
346 | char elttype[20]; | |
347 | unsigned long eltsize; | |
348 | unsigned char *sha1 = buffer + namelen; | |
349 | char *path = strchr(buffer, ' ') + 1; | |
350 | unsigned int mode; | |
351 | ||
352 | if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1) | |
353 | die("corrupt 'tree' file"); | |
38ec15a9 JH |
354 | if (S_ISDIR(mode) || S_ISREG(mode)) |
355 | mode |= (mode & 0100) ? 0777 : 0666; | |
731ab9cc RS |
356 | buffer = sha1 + 20; |
357 | size -= namelen + 20; | |
358 | ||
359 | eltbuf = read_sha1_file(sha1, elttype, &eltsize); | |
360 | if (!eltbuf) | |
361 | die("cannot read %s", sha1_to_hex(sha1)); | |
03d791ff | 362 | write_header(sha1, TYPEFLAG_AUTO, basedir, prefix, path, |
08f09dac | 363 | mode, eltbuf, eltsize); |
731ab9cc RS |
364 | if (!strcmp(elttype, "tree")) { |
365 | this_prefix.name = path; | |
366 | traverse_tree(eltbuf, eltsize, &this_prefix); | |
d5776d50 | 367 | } else if (!strcmp(elttype, "blob") && !S_ISLNK(mode)) { |
731ab9cc RS |
368 | write_blocked(eltbuf, eltsize); |
369 | } | |
370 | free(eltbuf); | |
371 | } | |
372 | } | |
373 | ||
374 | /* get commit time from committer line of commit object */ | |
e99d59ff | 375 | static time_t commit_time(void * buffer, unsigned long size) |
731ab9cc | 376 | { |
731ab9cc | 377 | time_t result = 0; |
db413479 | 378 | char *p = buffer; |
731ab9cc | 379 | |
db413479 RS |
380 | while (size > 0) { |
381 | char *endp = memchr(p, '\n', size); | |
382 | if (!endp || endp == p) | |
383 | break; | |
384 | *endp = '\0'; | |
385 | if (endp - p > 10 && !memcmp(p, "committer ", 10)) { | |
386 | char *nump = strrchr(p, '>'); | |
387 | if (!nump) | |
731ab9cc | 388 | break; |
db413479 RS |
389 | nump++; |
390 | result = strtoul(nump, &endp, 10); | |
391 | if (*endp != ' ') | |
392 | result = 0; | |
393 | break; | |
731ab9cc | 394 | } |
db413479 RS |
395 | size -= endp - p - 1; |
396 | p = endp + 1; | |
731ab9cc RS |
397 | } |
398 | return result; | |
399 | } | |
400 | ||
401 | int main(int argc, char **argv) | |
402 | { | |
403 | unsigned char sha1[20]; | |
87fec8fc | 404 | unsigned char commit_sha1[20]; |
731ab9cc RS |
405 | void *buffer; |
406 | unsigned long size; | |
731ab9cc | 407 | |
53228a5f JH |
408 | setup_git_directory(); |
409 | ||
731ab9cc RS |
410 | switch (argc) { |
411 | case 3: | |
412 | basedir = argv[2]; | |
413 | /* FALLTHROUGH */ | |
414 | case 2: | |
3c249c95 | 415 | if (get_sha1(argv[1], sha1) < 0) |
731ab9cc RS |
416 | usage(tar_tree_usage); |
417 | break; | |
418 | default: | |
419 | usage(tar_tree_usage); | |
420 | } | |
421 | ||
87fec8fc | 422 | buffer = read_object_with_reference(sha1, "commit", &size, commit_sha1); |
db413479 | 423 | if (buffer) { |
87fec8fc | 424 | write_global_extended_header(commit_sha1); |
db413479 RS |
425 | archive_time = commit_time(buffer, size); |
426 | free(buffer); | |
427 | } | |
428 | buffer = read_object_with_reference(sha1, "tree", &size, NULL); | |
731ab9cc | 429 | if (!buffer) |
db413479 RS |
430 | die("not a reference to a tag, commit or tree object: %s", |
431 | sha1_to_hex(sha1)); | |
731ab9cc RS |
432 | if (!archive_time) |
433 | archive_time = time(NULL); | |
434 | if (basedir) | |
d565b341 MK |
435 | write_header((unsigned char *)"0", TYPEFLAG_DIR, NULL, NULL, |
436 | basedir, 040755, NULL, 0); | |
731ab9cc RS |
437 | traverse_tree(buffer, size, NULL); |
438 | free(buffer); | |
439 | write_trailer(); | |
440 | return 0; | |
441 | } |