2 * Copyright (c) 2005, 2006 Rene Scharfe
14 #define RECORDSIZE (512)
15 #define BLOCKSIZE (RECORDSIZE * 20)
17 static const char tar_tree_usage
[] =
18 "git-tar-tree [--remote=<repo>] <tree-ish> [basedir]";
20 static char block
[BLOCKSIZE
];
21 static unsigned long offset
;
23 static time_t archive_time
;
26 /* writes out the whole block, but only if it is full */
27 static void write_if_needed(void)
29 if (offset
== BLOCKSIZE
) {
30 write_or_die(1, block
, BLOCKSIZE
);
36 * queues up writes, so that all our write(2) calls write exactly one
37 * full block; pads writes to RECORDSIZE
39 static void write_blocked(const void *data
, unsigned long size
)
41 const char *buf
= data
;
45 unsigned long chunk
= BLOCKSIZE
- offset
;
48 memcpy(block
+ offset
, buf
, chunk
);
54 while (size
>= BLOCKSIZE
) {
55 write_or_die(1, buf
, BLOCKSIZE
);
60 memcpy(block
+ offset
, buf
, size
);
63 tail
= offset
% RECORDSIZE
;
65 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
66 offset
+= RECORDSIZE
- tail
;
72 * The end of tar archives is marked by 2*512 nul bytes and after that
73 * follows the rest of the block (if any).
75 static void write_trailer(void)
77 int tail
= BLOCKSIZE
- offset
;
78 memset(block
+ offset
, 0, tail
);
79 write_or_die(1, block
, BLOCKSIZE
);
80 if (tail
< 2 * RECORDSIZE
) {
81 memset(block
, 0, offset
);
82 write_or_die(1, block
, BLOCKSIZE
);
86 static void strbuf_append_string(struct strbuf
*sb
, const char *s
)
89 int total
= sb
->len
+ slen
;
90 if (total
> sb
->alloc
) {
91 sb
->buf
= xrealloc(sb
->buf
, total
);
94 memcpy(sb
->buf
+ sb
->len
, s
, slen
);
99 * pax extended header records have the format "%u %s=%s\n". %u contains
100 * the size of the whole string (including the %u), the first %s is the
101 * keyword, the second one is the value. This function constructs such a
102 * string and appends it to a struct strbuf.
104 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
105 const char *value
, unsigned int valuelen
)
111 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
112 for (tmp
= len
; tmp
> 9; tmp
/= 10)
115 total
= sb
->len
+ len
;
116 if (total
> sb
->alloc
) {
117 sb
->buf
= xrealloc(sb
->buf
, total
);
122 p
+= sprintf(p
, "%u %s=", len
, keyword
);
123 memcpy(p
, value
, valuelen
);
129 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
131 char *p
= (char *)header
;
132 unsigned int chksum
= 0;
133 while (p
< header
->chksum
)
135 chksum
+= sizeof(header
->chksum
) * ' ';
136 p
+= sizeof(header
->chksum
);
137 while (p
< (char *)header
+ sizeof(struct ustar_header
))
142 static int get_path_prefix(const struct strbuf
*path
, int maxlen
)
149 } while (i
> 0 && path
->buf
[i
] != '/');
153 static void write_entry(const unsigned char *sha1
, struct strbuf
*path
,
154 unsigned int mode
, void *buffer
, unsigned long size
)
156 struct ustar_header header
;
157 struct strbuf ext_header
;
159 memset(&header
, 0, sizeof(header
));
160 ext_header
.buf
= NULL
;
161 ext_header
.len
= ext_header
.alloc
= 0;
164 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
166 strcpy(header
.name
, "pax_global_header");
168 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
170 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
173 *header
.typeflag
= TYPEFLAG_DIR
;
174 mode
= (mode
| 0777) & ~tar_umask
;
175 } else if (S_ISLNK(mode
)) {
176 *header
.typeflag
= TYPEFLAG_LNK
;
178 } else if (S_ISREG(mode
)) {
179 *header
.typeflag
= TYPEFLAG_REG
;
180 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
182 error("unsupported file mode: 0%o (SHA1: %s)",
183 mode
, sha1_to_hex(sha1
));
186 if (path
->len
> sizeof(header
.name
)) {
187 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
188 int rest
= path
->len
- plen
- 1;
189 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
190 memcpy(header
.prefix
, path
->buf
, plen
);
191 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
193 sprintf(header
.name
, "%s.data",
195 strbuf_append_ext_header(&ext_header
, "path",
196 path
->buf
, path
->len
);
199 memcpy(header
.name
, path
->buf
, path
->len
);
202 if (S_ISLNK(mode
) && buffer
) {
203 if (size
> sizeof(header
.linkname
)) {
204 sprintf(header
.linkname
, "see %s.paxheader",
206 strbuf_append_ext_header(&ext_header
, "linkpath",
209 memcpy(header
.linkname
, buffer
, size
);
212 sprintf(header
.mode
, "%07o", mode
& 07777);
213 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
214 sprintf(header
.mtime
, "%011lo", archive_time
);
216 /* XXX: should we provide more meaningful info here? */
217 sprintf(header
.uid
, "%07o", 0);
218 sprintf(header
.gid
, "%07o", 0);
219 strlcpy(header
.uname
, "git", sizeof(header
.uname
));
220 strlcpy(header
.gname
, "git", sizeof(header
.gname
));
221 sprintf(header
.devmajor
, "%07o", 0);
222 sprintf(header
.devminor
, "%07o", 0);
224 memcpy(header
.magic
, "ustar", 6);
225 memcpy(header
.version
, "00", 2);
227 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
229 if (ext_header
.len
> 0) {
230 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
231 free(ext_header
.buf
);
233 write_blocked(&header
, sizeof(header
));
234 if (S_ISREG(mode
) && buffer
&& size
> 0)
235 write_blocked(buffer
, size
);
238 static void write_global_extended_header(const unsigned char *sha1
)
240 struct strbuf ext_header
;
241 ext_header
.buf
= NULL
;
242 ext_header
.len
= ext_header
.alloc
= 0;
243 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
244 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
245 free(ext_header
.buf
);
248 static void traverse_tree(struct tree_desc
*tree
, struct strbuf
*path
)
250 int pathlen
= path
->len
;
251 struct name_entry entry
;
253 while (tree_entry(tree
, &entry
)) {
256 unsigned long eltsize
;
258 eltbuf
= read_sha1_file(entry
.sha1
, elttype
, &eltsize
);
260 die("cannot read %s", sha1_to_hex(entry
.sha1
));
263 strbuf_append_string(path
, entry
.path
);
264 if (S_ISDIR(entry
.mode
))
265 strbuf_append_string(path
, "/");
267 write_entry(entry
.sha1
, path
, entry
.mode
, eltbuf
, eltsize
);
269 if (S_ISDIR(entry
.mode
)) {
270 struct tree_desc subtree
;
271 subtree
.buf
= eltbuf
;
272 subtree
.size
= eltsize
;
273 traverse_tree(&subtree
, path
);
279 static int git_tar_config(const char *var
, const char *value
)
281 if (!strcmp(var
, "tar.umask")) {
282 if (!strcmp(value
, "user")) {
283 tar_umask
= umask(0);
286 tar_umask
= git_config_int(var
, value
);
290 return git_default_config(var
, value
);
293 static int generate_tar(int argc
, const char **argv
, const char *prefix
)
295 unsigned char sha1
[20], tree_sha1
[20];
296 struct commit
*commit
;
297 struct tree_desc tree
;
298 struct strbuf current_path
;
301 current_path
.buf
= xmalloc(PATH_MAX
);
302 current_path
.alloc
= PATH_MAX
;
303 current_path
.len
= current_path
.eof
= 0;
305 git_config(git_tar_config
);
309 strbuf_append_string(¤t_path
, argv
[2]);
310 strbuf_append_string(¤t_path
, "/");
313 if (get_sha1(argv
[1], sha1
))
314 die("Not a valid object name %s", argv
[1]);
317 usage(tar_tree_usage
);
320 commit
= lookup_commit_reference_gently(sha1
, 1);
322 write_global_extended_header(commit
->object
.sha1
);
323 archive_time
= commit
->date
;
325 archive_time
= time(NULL
);
327 tree
.buf
= buffer
= read_object_with_reference(sha1
, tree_type
,
328 &tree
.size
, tree_sha1
);
330 die("not a reference to a tag, commit or tree object: %s",
333 if (current_path
.len
> 0)
334 write_entry(tree_sha1
, ¤t_path
, 040777, NULL
, 0);
335 traverse_tree(&tree
, ¤t_path
);
338 free(current_path
.buf
);
342 static int write_tar_entry(const unsigned char *sha1
,
343 const char *base
, int baselen
,
344 const char *filename
, unsigned mode
, int stage
)
346 static struct strbuf path
;
347 int filenamelen
= strlen(filename
);
353 path
.buf
= xmalloc(PATH_MAX
);
354 path
.alloc
= PATH_MAX
;
355 path
.len
= path
.eof
= 0;
357 if (path
.alloc
< baselen
+ filenamelen
) {
359 path
.buf
= xmalloc(baselen
+ filenamelen
);
360 path
.alloc
= baselen
+ filenamelen
;
362 memcpy(path
.buf
, base
, baselen
);
363 memcpy(path
.buf
+ baselen
, filename
, filenamelen
);
364 path
.len
= baselen
+ filenamelen
;
366 strbuf_append_string(&path
, "/");
370 buffer
= read_sha1_file(sha1
, type
, &size
);
372 die("cannot read %s", sha1_to_hex(sha1
));
375 write_entry(sha1
, &path
, mode
, buffer
, size
);
378 return READ_TREE_RECURSIVE
;
381 int write_tar_archive(struct archiver_args
*args
)
383 int plen
= strlen(args
->base
);
385 git_config(git_tar_config
);
387 archive_time
= args
->time
;
389 if (args
->commit_sha1
)
390 write_global_extended_header(args
->commit_sha1
);
392 if (args
->base
&& plen
> 0 && args
->base
[plen
- 1] == '/') {
393 char *base
= xstrdup(args
->base
);
394 int baselen
= strlen(base
);
396 while (baselen
> 0 && base
[baselen
- 1] == '/')
397 base
[--baselen
] = '\0';
398 write_tar_entry(args
->tree
->object
.sha1
, "", 0, base
, 040777, 0);
401 read_tree_recursive(args
->tree
, args
->base
, plen
, 0,
402 args
->pathspec
, write_tar_entry
);
408 static const char *exec
= "git-upload-tar";
410 static int remote_tar(int argc
, const char **argv
)
417 if (argc
< 3 || 4 < argc
)
418 usage(tar_tree_usage
);
420 /* --remote=<repo> */
421 url
= xstrdup(argv
[1]+9);
422 pid
= git_connect(fd
, url
, exec
);
426 packet_write(fd
[1], "want %s\n", argv
[2]);
428 packet_write(fd
[1], "base %s\n", argv
[3]);
431 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
433 die("git-tar-tree: expected ACK/NAK, got EOF");
434 if (buf
[len
-1] == '\n')
436 if (strcmp(buf
, "ACK")) {
437 if (5 < len
&& !strncmp(buf
, "NACK ", 5))
438 die("git-tar-tree: NACK %s", buf
+ 5);
439 die("git-tar-tree: protocol error");
442 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
444 die("git-tar-tree: expected a flush");
446 /* Now, start reading from fd[0] and spit it out to stdout */
447 ret
= copy_fd(fd
[0], 1);
450 ret
|= finish_connect(pid
);
454 int cmd_tar_tree(int argc
, const char **argv
, const char *prefix
)
457 usage(tar_tree_usage
);
458 if (!strncmp("--remote=", argv
[1], 9))
459 return remote_tar(argc
, argv
);
460 return generate_tar(argc
, argv
, prefix
);
463 /* ustar header + extended global header content */
464 #define HEADERSIZE (2 * RECORDSIZE)
466 int cmd_get_tar_commit_id(int argc
, const char **argv
, const char *prefix
)
468 char buffer
[HEADERSIZE
];
469 struct ustar_header
*header
= (struct ustar_header
*)buffer
;
470 char *content
= buffer
+ RECORDSIZE
;
473 n
= xread(0, buffer
, HEADERSIZE
);
475 die("git-get-tar-commit-id: read error");
476 if (header
->typeflag
[0] != 'g')
478 if (memcmp(content
, "52 comment=", 11))
481 n
= xwrite(1, content
+ 11, 41);
483 die("git-get-tar-commit-id: write error");