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
;
27 /* writes out the whole block, but only if it is full */
28 static void write_if_needed(void)
30 if (offset
== BLOCKSIZE
) {
31 write_or_die(1, block
, BLOCKSIZE
);
37 * queues up writes, so that all our write(2) calls write exactly one
38 * full block; pads writes to RECORDSIZE
40 static void write_blocked(const void *data
, unsigned long size
)
42 const char *buf
= data
;
46 unsigned long chunk
= BLOCKSIZE
- offset
;
49 memcpy(block
+ offset
, buf
, chunk
);
55 while (size
>= BLOCKSIZE
) {
56 write_or_die(1, buf
, BLOCKSIZE
);
61 memcpy(block
+ offset
, buf
, size
);
64 tail
= offset
% RECORDSIZE
;
66 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
67 offset
+= RECORDSIZE
- tail
;
73 * The end of tar archives is marked by 2*512 nul bytes and after that
74 * follows the rest of the block (if any).
76 static void write_trailer(void)
78 int tail
= BLOCKSIZE
- offset
;
79 memset(block
+ offset
, 0, tail
);
80 write_or_die(1, block
, BLOCKSIZE
);
81 if (tail
< 2 * RECORDSIZE
) {
82 memset(block
, 0, offset
);
83 write_or_die(1, block
, BLOCKSIZE
);
87 static void strbuf_append_string(struct strbuf
*sb
, const char *s
)
90 int total
= sb
->len
+ slen
;
91 if (total
> sb
->alloc
) {
92 sb
->buf
= xrealloc(sb
->buf
, total
);
95 memcpy(sb
->buf
+ sb
->len
, s
, slen
);
100 * pax extended header records have the format "%u %s=%s\n". %u contains
101 * the size of the whole string (including the %u), the first %s is the
102 * keyword, the second one is the value. This function constructs such a
103 * string and appends it to a struct strbuf.
105 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
106 const char *value
, unsigned int valuelen
)
112 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
113 for (tmp
= len
; tmp
> 9; tmp
/= 10)
116 total
= sb
->len
+ len
;
117 if (total
> sb
->alloc
) {
118 sb
->buf
= xrealloc(sb
->buf
, total
);
123 p
+= sprintf(p
, "%u %s=", len
, keyword
);
124 memcpy(p
, value
, valuelen
);
130 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
132 char *p
= (char *)header
;
133 unsigned int chksum
= 0;
134 while (p
< header
->chksum
)
136 chksum
+= sizeof(header
->chksum
) * ' ';
137 p
+= sizeof(header
->chksum
);
138 while (p
< (char *)header
+ sizeof(struct ustar_header
))
143 static int get_path_prefix(const struct strbuf
*path
, int maxlen
)
150 } while (i
> 0 && path
->buf
[i
] != '/');
154 static void write_entry(const unsigned char *sha1
, struct strbuf
*path
,
155 unsigned int mode
, void *buffer
, unsigned long size
)
157 struct ustar_header header
;
158 struct strbuf ext_header
;
160 memset(&header
, 0, sizeof(header
));
161 ext_header
.buf
= NULL
;
162 ext_header
.len
= ext_header
.alloc
= 0;
165 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
167 strcpy(header
.name
, "pax_global_header");
169 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
171 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
174 fprintf(stderr
, "%.*s\n", path
->len
, path
->buf
);
176 *header
.typeflag
= TYPEFLAG_DIR
;
177 mode
= (mode
| 0777) & ~tar_umask
;
178 } else if (S_ISLNK(mode
)) {
179 *header
.typeflag
= TYPEFLAG_LNK
;
181 } else if (S_ISREG(mode
)) {
182 *header
.typeflag
= TYPEFLAG_REG
;
183 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
185 error("unsupported file mode: 0%o (SHA1: %s)",
186 mode
, sha1_to_hex(sha1
));
189 if (path
->len
> sizeof(header
.name
)) {
190 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
191 int rest
= path
->len
- plen
- 1;
192 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
193 memcpy(header
.prefix
, path
->buf
, plen
);
194 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
196 sprintf(header
.name
, "%s.data",
198 strbuf_append_ext_header(&ext_header
, "path",
199 path
->buf
, path
->len
);
202 memcpy(header
.name
, path
->buf
, path
->len
);
205 if (S_ISLNK(mode
) && buffer
) {
206 if (size
> sizeof(header
.linkname
)) {
207 sprintf(header
.linkname
, "see %s.paxheader",
209 strbuf_append_ext_header(&ext_header
, "linkpath",
212 memcpy(header
.linkname
, buffer
, size
);
215 sprintf(header
.mode
, "%07o", mode
& 07777);
216 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
217 sprintf(header
.mtime
, "%011lo", archive_time
);
219 /* XXX: should we provide more meaningful info here? */
220 sprintf(header
.uid
, "%07o", 0);
221 sprintf(header
.gid
, "%07o", 0);
222 strlcpy(header
.uname
, "git", sizeof(header
.uname
));
223 strlcpy(header
.gname
, "git", sizeof(header
.gname
));
224 sprintf(header
.devmajor
, "%07o", 0);
225 sprintf(header
.devminor
, "%07o", 0);
227 memcpy(header
.magic
, "ustar", 6);
228 memcpy(header
.version
, "00", 2);
230 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
232 if (ext_header
.len
> 0) {
233 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
234 free(ext_header
.buf
);
236 write_blocked(&header
, sizeof(header
));
237 if (S_ISREG(mode
) && buffer
&& size
> 0)
238 write_blocked(buffer
, size
);
241 static void write_global_extended_header(const unsigned char *sha1
)
243 struct strbuf ext_header
;
244 ext_header
.buf
= NULL
;
245 ext_header
.len
= ext_header
.alloc
= 0;
246 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
247 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
248 free(ext_header
.buf
);
251 static void traverse_tree(struct tree_desc
*tree
, struct strbuf
*path
)
253 int pathlen
= path
->len
;
254 struct name_entry entry
;
256 while (tree_entry(tree
, &entry
)) {
259 unsigned long eltsize
;
261 eltbuf
= read_sha1_file(entry
.sha1
, elttype
, &eltsize
);
263 die("cannot read %s", sha1_to_hex(entry
.sha1
));
266 strbuf_append_string(path
, entry
.path
);
267 if (S_ISDIR(entry
.mode
))
268 strbuf_append_string(path
, "/");
270 write_entry(entry
.sha1
, path
, entry
.mode
, eltbuf
, eltsize
);
272 if (S_ISDIR(entry
.mode
)) {
273 struct tree_desc subtree
;
274 subtree
.buf
= eltbuf
;
275 subtree
.size
= eltsize
;
276 traverse_tree(&subtree
, path
);
282 static int git_tar_config(const char *var
, const char *value
)
284 if (!strcmp(var
, "tar.umask")) {
285 if (!strcmp(value
, "user")) {
286 tar_umask
= umask(0);
289 tar_umask
= git_config_int(var
, value
);
293 return git_default_config(var
, value
);
296 static int generate_tar(int argc
, const char **argv
, const char *prefix
)
298 unsigned char sha1
[20], tree_sha1
[20];
299 struct commit
*commit
;
300 struct tree_desc tree
;
301 struct strbuf current_path
;
304 current_path
.buf
= xmalloc(PATH_MAX
);
305 current_path
.alloc
= PATH_MAX
;
306 current_path
.len
= current_path
.eof
= 0;
308 git_config(git_tar_config
);
312 strbuf_append_string(¤t_path
, argv
[2]);
313 strbuf_append_string(¤t_path
, "/");
316 if (get_sha1(argv
[1], sha1
))
317 die("Not a valid object name %s", argv
[1]);
320 usage(tar_tree_usage
);
323 commit
= lookup_commit_reference_gently(sha1
, 1);
325 write_global_extended_header(commit
->object
.sha1
);
326 archive_time
= commit
->date
;
328 archive_time
= time(NULL
);
330 tree
.buf
= buffer
= read_object_with_reference(sha1
, tree_type
,
331 &tree
.size
, tree_sha1
);
333 die("not a reference to a tag, commit or tree object: %s",
336 if (current_path
.len
> 0)
337 write_entry(tree_sha1
, ¤t_path
, 040777, NULL
, 0);
338 traverse_tree(&tree
, ¤t_path
);
341 free(current_path
.buf
);
345 static int write_tar_entry(const unsigned char *sha1
,
346 const char *base
, int baselen
,
347 const char *filename
, unsigned mode
, int stage
)
349 static struct strbuf path
;
350 int filenamelen
= strlen(filename
);
356 path
.buf
= xmalloc(PATH_MAX
);
357 path
.alloc
= PATH_MAX
;
358 path
.len
= path
.eof
= 0;
360 if (path
.alloc
< baselen
+ filenamelen
) {
362 path
.buf
= xmalloc(baselen
+ filenamelen
);
363 path
.alloc
= baselen
+ filenamelen
;
365 memcpy(path
.buf
, base
, baselen
);
366 memcpy(path
.buf
+ baselen
, filename
, filenamelen
);
367 path
.len
= baselen
+ filenamelen
;
369 strbuf_append_string(&path
, "/");
373 buffer
= read_sha1_file(sha1
, type
, &size
);
375 die("cannot read %s", sha1_to_hex(sha1
));
378 write_entry(sha1
, &path
, mode
, buffer
, size
);
381 return READ_TREE_RECURSIVE
;
384 int write_tar_archive(struct archiver_args
*args
)
386 int plen
= strlen(args
->base
);
388 git_config(git_tar_config
);
390 archive_time
= args
->time
;
391 verbose
= args
->verbose
;
393 if (args
->commit_sha1
)
394 write_global_extended_header(args
->commit_sha1
);
396 if (args
->base
&& plen
> 0 && args
->base
[plen
- 1] == '/') {
397 char *base
= xstrdup(args
->base
);
398 int baselen
= strlen(base
);
400 while (baselen
> 0 && base
[baselen
- 1] == '/')
401 base
[--baselen
] = '\0';
402 write_tar_entry(args
->tree
->object
.sha1
, "", 0, base
, 040777, 0);
405 read_tree_recursive(args
->tree
, args
->base
, plen
, 0,
406 args
->pathspec
, write_tar_entry
);
412 static const char *exec
= "git-upload-tar";
414 static int remote_tar(int argc
, const char **argv
)
421 if (argc
< 3 || 4 < argc
)
422 usage(tar_tree_usage
);
424 /* --remote=<repo> */
425 url
= xstrdup(argv
[1]+9);
426 pid
= git_connect(fd
, url
, exec
);
430 packet_write(fd
[1], "want %s\n", argv
[2]);
432 packet_write(fd
[1], "base %s\n", argv
[3]);
435 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
437 die("git-tar-tree: expected ACK/NAK, got EOF");
438 if (buf
[len
-1] == '\n')
440 if (strcmp(buf
, "ACK")) {
441 if (5 < len
&& !strncmp(buf
, "NACK ", 5))
442 die("git-tar-tree: NACK %s", buf
+ 5);
443 die("git-tar-tree: protocol error");
446 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
448 die("git-tar-tree: expected a flush");
450 /* Now, start reading from fd[0] and spit it out to stdout */
451 ret
= copy_fd(fd
[0], 1);
454 ret
|= finish_connect(pid
);
458 int cmd_tar_tree(int argc
, const char **argv
, const char *prefix
)
461 usage(tar_tree_usage
);
462 if (!strncmp("--remote=", argv
[1], 9))
463 return remote_tar(argc
, argv
);
464 return generate_tar(argc
, argv
, prefix
);
467 /* ustar header + extended global header content */
468 #define HEADERSIZE (2 * RECORDSIZE)
470 int cmd_get_tar_commit_id(int argc
, const char **argv
, const char *prefix
)
472 char buffer
[HEADERSIZE
];
473 struct ustar_header
*header
= (struct ustar_header
*)buffer
;
474 char *content
= buffer
+ RECORDSIZE
;
477 n
= xread(0, buffer
, HEADERSIZE
);
479 die("git-get-tar-commit-id: read error");
480 if (header
->typeflag
[0] != 'g')
482 if (memcmp(content
, "52 comment=", 11))
485 n
= xwrite(1, content
+ 11, 41);
487 die("git-get-tar-commit-id: write error");