2 * Copyright (c) 2005 Rene Scharfe
9 #define RECORDSIZE (512)
10 #define BLOCKSIZE (RECORDSIZE * 20)
12 #define TYPEFLAG_AUTO '\0'
13 #define TYPEFLAG_REG '0'
14 #define TYPEFLAG_LNK '2'
15 #define TYPEFLAG_DIR '5'
16 #define TYPEFLAG_GLOBAL_HEADER 'g'
17 #define TYPEFLAG_EXT_HEADER 'x'
19 #define EXT_HEADER_PATH 1
20 #define EXT_HEADER_LINKPATH 2
22 static const char tar_tree_usage
[] = "git-tar-tree <key> [basedir]";
24 static char block
[BLOCKSIZE
];
25 static unsigned long offset
;
27 static const char *basedir
;
28 static time_t archive_time
;
31 struct path_prefix
*prev
;
35 /* tries hard to write, either succeeds or dies in the attempt */
36 static void reliable_write(void *buf
, unsigned long size
)
39 long ret
= xwrite(1, buf
, size
);
43 die("git-tar-tree: %s", strerror(errno
));
45 die("git-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)
87 * queues up writes, so that all our write(2) calls write exactly one
88 * full block; pads writes to RECORDSIZE
90 static void write_blocked(void *buf
, unsigned long size
)
95 unsigned long chunk
= BLOCKSIZE
- offset
;
98 memcpy(block
+ offset
, buf
, chunk
);
104 while (size
>= BLOCKSIZE
) {
105 reliable_write(buf
, BLOCKSIZE
);
110 memcpy(block
+ offset
, buf
, size
);
114 tail
= offset
% RECORDSIZE
;
116 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
117 offset
+= RECORDSIZE
- tail
;
122 static void append_string(char **p
, const char *s
)
124 unsigned int len
= strlen(s
);
129 static void append_char(char **p
, char c
)
135 static void append_path_prefix(char **buffer
, struct path_prefix
*prefix
)
139 append_path_prefix(buffer
, prefix
->prev
);
140 append_string(buffer
, prefix
->name
);
141 append_char(buffer
, '/');
144 static unsigned int path_prefix_len(struct path_prefix
*prefix
)
148 return path_prefix_len(prefix
->prev
) + strlen(prefix
->name
) + 1;
151 static void append_path(char **p
, int is_dir
, const char *basepath
,
152 struct path_prefix
*prefix
, const char *path
)
155 append_string(p
, basepath
);
158 append_path_prefix(p
, prefix
);
159 append_string(p
, path
);
164 static unsigned int path_len(int is_dir
, const char *basepath
,
165 struct path_prefix
*prefix
, const char *path
)
167 unsigned int len
= 0;
169 len
+= strlen(basepath
) + 1;
170 len
+= path_prefix_len(prefix
) + strlen(path
);
176 static void append_extended_header_prefix(char **p
, unsigned int size
,
179 int len
= sprintf(*p
, "%u %s=", size
, keyword
);
183 static unsigned int extended_header_len(const char *keyword
,
184 unsigned int valuelen
)
187 unsigned int len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
195 static void append_extended_header(char **p
, const char *keyword
,
196 const char *value
, unsigned int len
)
198 unsigned int size
= extended_header_len(keyword
, len
);
199 append_extended_header_prefix(p
, size
, keyword
);
200 memcpy(*p
, value
, len
);
202 append_char(p
, '\n');
205 static void write_header(const unsigned char *, char, const char *, struct path_prefix
*,
206 const char *, unsigned int, void *, unsigned long);
208 /* stores a pax extended header directly in the block buffer */
209 static void write_extended_header(const char *headerfilename
, int is_dir
,
210 unsigned int flags
, const char *basepath
,
211 struct path_prefix
*prefix
,
212 const char *path
, unsigned int namelen
,
213 void *content
, unsigned int contentsize
)
216 unsigned int pathlen
, size
, linkpathlen
= 0;
218 size
= pathlen
= extended_header_len("path", namelen
);
219 if (flags
& EXT_HEADER_LINKPATH
) {
220 linkpathlen
= extended_header_len("linkpath", contentsize
);
223 write_header(NULL
, TYPEFLAG_EXT_HEADER
, NULL
, NULL
, headerfilename
,
224 0100600, NULL
, size
);
226 buffer
= p
= malloc(size
);
228 die("git-tar-tree: %s", strerror(errno
));
229 append_extended_header_prefix(&p
, pathlen
, "path");
230 append_path(&p
, is_dir
, basepath
, prefix
, path
);
231 append_char(&p
, '\n');
232 if (flags
& EXT_HEADER_LINKPATH
)
233 append_extended_header(&p
, "linkpath", content
, contentsize
);
234 write_blocked(buffer
, size
);
238 static void write_global_extended_header(const unsigned char *sha1
)
243 size
= extended_header_len("comment", 40);
244 write_header(NULL
, TYPEFLAG_GLOBAL_HEADER
, NULL
, NULL
,
245 "pax_global_header", 0100600, NULL
, size
);
248 append_extended_header(&p
, "comment", sha1_to_hex(sha1
), 40);
252 /* stores a ustar header directly in the block buffer */
253 static void write_header(const unsigned char *sha1
, char typeflag
, const char *basepath
,
254 struct path_prefix
*prefix
, const char *path
,
255 unsigned int mode
, void *buffer
, unsigned long size
)
257 unsigned int namelen
;
259 unsigned int checksum
= 0;
261 unsigned int ext_header
= 0;
263 if (typeflag
== TYPEFLAG_AUTO
) {
265 typeflag
= TYPEFLAG_DIR
;
266 else if (S_ISLNK(mode
))
267 typeflag
= TYPEFLAG_LNK
;
269 typeflag
= TYPEFLAG_REG
;
272 namelen
= path_len(S_ISDIR(mode
), basepath
, prefix
, path
);
274 ext_header
|= EXT_HEADER_PATH
;
275 if (typeflag
== TYPEFLAG_LNK
&& size
> 100)
276 ext_header
|= EXT_HEADER_LINKPATH
;
278 /* the extended header must be written before the normal one */
280 char headerfilename
[51];
281 sprintf(headerfilename
, "%s.paxheader", sha1_to_hex(sha1
));
282 write_extended_header(headerfilename
, S_ISDIR(mode
),
283 ext_header
, basepath
, prefix
, path
,
284 namelen
, buffer
, size
);
287 header
= get_record();
290 sprintf(header
, "%s.data", sha1_to_hex(sha1
));
293 append_path(&p
, S_ISDIR(mode
), basepath
, prefix
, path
);
296 if (typeflag
== TYPEFLAG_LNK
) {
297 if (ext_header
& EXT_HEADER_LINKPATH
) {
298 sprintf(&header
[157], "see %s.paxheader",
302 strncpy(&header
[157], buffer
, size
);
307 mode
|= 0755; /* GIT doesn't store permissions of dirs */
309 mode
|= 0777; /* ... nor of symlinks */
310 sprintf(&header
[100], "%07o", mode
& 07777);
312 /* XXX: should we provide more meaningful info here? */
313 sprintf(&header
[108], "%07o", 0); /* uid */
314 sprintf(&header
[116], "%07o", 0); /* gid */
315 strncpy(&header
[265], "git", 31); /* uname */
316 strncpy(&header
[297], "git", 31); /* gname */
318 if (S_ISDIR(mode
) || S_ISLNK(mode
))
320 sprintf(&header
[124], "%011lo", size
);
321 sprintf(&header
[136], "%011lo", archive_time
);
323 header
[156] = typeflag
;
325 memcpy(&header
[257], "ustar", 6);
326 memcpy(&header
[263], "00", 2);
328 sprintf(&header
[329], "%07o", 0); /* devmajor */
329 sprintf(&header
[337], "%07o", 0); /* devminor */
331 memset(&header
[148], ' ', 8);
332 for (i
= 0; i
< RECORDSIZE
; i
++)
333 checksum
+= header
[i
];
334 sprintf(&header
[148], "%07o", checksum
& 0x1fffff);
339 static void traverse_tree(struct tree
*tree
,
340 struct path_prefix
*prefix
)
342 struct path_prefix this_prefix
;
343 struct tree_entry_list
*item
;
344 this_prefix
.prev
= prefix
;
347 item
= tree
->entries
;
352 unsigned long eltsize
;
354 eltbuf
= read_sha1_file(item
->item
.any
->sha1
,
357 die("cannot read %s",
358 sha1_to_hex(item
->item
.any
->sha1
));
359 write_header(item
->item
.any
->sha1
, TYPEFLAG_AUTO
, basedir
,
361 item
->mode
, eltbuf
, eltsize
);
362 if (item
->directory
) {
363 this_prefix
.name
= item
->name
;
364 traverse_tree(item
->item
.tree
, &this_prefix
);
365 } else if (!item
->symlink
) {
366 write_blocked(eltbuf
, eltsize
);
373 int main(int argc
, char **argv
)
375 unsigned char sha1
[20];
376 struct commit
*commit
;
379 setup_git_directory();
386 if (get_sha1(argv
[1], sha1
) < 0)
387 usage(tar_tree_usage
);
390 usage(tar_tree_usage
);
393 commit
= lookup_commit_reference(sha1
);
395 write_global_extended_header(commit
->object
.sha1
);
396 archive_time
= commit
->date
;
398 tree
= parse_tree_indirect(sha1
);
400 die("not a reference to a tag, commit or tree object: %s",
403 archive_time
= time(NULL
);
405 write_header((unsigned char *)"0", TYPEFLAG_DIR
, NULL
, NULL
,
406 basedir
, 040777, NULL
, 0);
407 traverse_tree(tree
, NULL
);