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 *, 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
, NULL
, NULL
, headerfilename
, 0100600, size
);
182 records
= block
+ offset
;
183 memset(records
, 0, RECORDSIZE
);
184 offset
+= RECORDSIZE
;
186 append_long(&p
, size
);
187 append_string(&p
, " path=");
188 append_path(&p
, is_dir
, basepath
, prefix
, path
);
189 append_char(&p
, '\n');
193 /* stores a ustar header directly in the block buffer */
194 static void write_header(const char *sha1
, const char *basepath
,
195 struct path_prefix
*prefix
, const char *path
,
196 unsigned int mode
, unsigned long size
)
198 unsigned int namelen
;
199 char *p
, *header
= NULL
;
200 unsigned int checksum
= 0;
203 namelen
= path_len(S_ISDIR(mode
), basepath
, prefix
, path
);
205 die("tar-tree: name too log of object %s\n", sha1_to_hex(sha1
));
206 } else if (namelen
> 100) {
207 char *sha1_hex
= sha1_to_hex(sha1
);
208 char headerfilename
[51];
209 sprintf(headerfilename
, "%s.paxheader", sha1_hex
);
210 /* the extended header must be written before the normal one */
211 write_extended_header(headerfilename
, S_ISDIR(mode
), basepath
,
212 prefix
, path
, namelen
);
214 header
= block
+ offset
;
215 memset(header
, 0, RECORDSIZE
);
216 offset
+= RECORDSIZE
;
217 sprintf(header
, "%s.data", sha1_hex
);
219 header
= block
+ offset
;
220 memset(header
, 0, RECORDSIZE
);
221 offset
+= RECORDSIZE
;
223 append_path(&p
, S_ISDIR(mode
), basepath
, prefix
, path
);
227 mode
|= 0755; /* GIT doesn't store permissions of dirs */
228 sprintf(&header
[100], "%07o", mode
& 07777);
230 /* XXX: should we provide more meaningful info here? */
231 sprintf(&header
[108], "%07o", 0); /* uid */
232 sprintf(&header
[116], "%07o", 0); /* gid */
233 strncpy(&header
[265], "git", 31); /* uname */
234 strncpy(&header
[297], "git", 31); /* gname */
236 sprintf(&header
[124], "%011lo", S_ISDIR(mode
) ? 0 : size
);
237 sprintf(&header
[136], "%011lo", archive_time
);
241 header
[156] = 'x'; /* extended header */
243 header
[156] = S_ISDIR(mode
) ? '5' : '0';
245 memcpy(&header
[257], "ustar", 6);
246 memcpy(&header
[263], "00", 2);
248 printf(&header
[329], "%07o", 0); /* devmajor */
249 printf(&header
[337], "%07o", 0); /* devminor */
251 memset(&header
[148], ' ', 8);
252 for (i
= 0; i
< RECORDSIZE
; i
++)
253 checksum
+= header
[i
];
254 sprintf(&header
[148], "%07o", checksum
& 0x1fffff);
259 static void traverse_tree(void *buffer
, unsigned long size
,
260 struct path_prefix
*prefix
)
262 struct path_prefix this_prefix
;
263 this_prefix
.prev
= prefix
;
266 int namelen
= strlen(buffer
)+1;
269 unsigned long eltsize
;
270 unsigned char *sha1
= buffer
+ namelen
;
271 char *path
= strchr(buffer
, ' ') + 1;
274 if (size
< namelen
+ 20 || sscanf(buffer
, "%o", &mode
) != 1)
275 die("corrupt 'tree' file");
277 size
-= namelen
+ 20;
279 eltbuf
= read_sha1_file(sha1
, elttype
, &eltsize
);
281 die("cannot read %s", sha1_to_hex(sha1
));
282 write_header(sha1
, basedir
, prefix
, path
, mode
, eltsize
);
283 if (!strcmp(elttype
, "tree")) {
284 this_prefix
.name
= path
;
285 traverse_tree(eltbuf
, eltsize
, &this_prefix
);
286 } else if (!strcmp(elttype
, "blob")) {
287 write_blocked(eltbuf
, eltsize
);
293 /* get commit time from committer line of commit object */
294 time_t commit_time(void * buffer
, unsigned long size
)
300 char *endp
= memchr(p
, '\n', size
);
301 if (!endp
|| endp
== p
)
304 if (endp
- p
> 10 && !memcmp(p
, "committer ", 10)) {
305 char *nump
= strrchr(p
, '>');
309 result
= strtoul(nump
, &endp
, 10);
314 size
-= endp
- p
- 1;
320 int main(int argc
, char **argv
)
322 unsigned char sha1
[20];
331 if (get_sha1_hex(argv
[1], sha1
) < 0)
332 usage(tar_tree_usage
);
335 usage(tar_tree_usage
);
338 sha1_file_directory
= getenv(DB_ENVIRONMENT
);
339 if (!sha1_file_directory
)
340 sha1_file_directory
= DEFAULT_DB_ENVIRONMENT
;
342 buffer
= read_object_with_reference(sha1
, "commit", &size
, NULL
);
344 archive_time
= commit_time(buffer
, size
);
347 buffer
= read_object_with_reference(sha1
, "tree", &size
, NULL
);
349 die("not a reference to a tag, commit or tree object: %s",
352 archive_time
= time(NULL
);
354 write_header("0", NULL
, NULL
, basedir
, 040755, 0);
355 traverse_tree(buffer
, size
, NULL
);