2 * Copyright (c) 2005, 2006 Rene Scharfe
13 #define RECORDSIZE (512)
14 #define BLOCKSIZE (RECORDSIZE * 20)
16 static const char tar_tree_usage
[] =
17 "git-tar-tree [--remote=<repo>] <tree-ish> [basedir]";
19 static char block
[BLOCKSIZE
];
20 static unsigned long offset
;
22 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 fprintf(stderr
, "%.*s\n", path
->len
, path
->buf
);
175 *header
.typeflag
= TYPEFLAG_DIR
;
176 mode
= (mode
| 0777) & ~tar_umask
;
177 } else if (S_ISLNK(mode
)) {
178 *header
.typeflag
= TYPEFLAG_LNK
;
180 } else if (S_ISREG(mode
)) {
181 *header
.typeflag
= TYPEFLAG_REG
;
182 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
184 error("unsupported file mode: 0%o (SHA1: %s)",
185 mode
, sha1_to_hex(sha1
));
188 if (path
->len
> sizeof(header
.name
)) {
189 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
190 int rest
= path
->len
- plen
- 1;
191 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
192 memcpy(header
.prefix
, path
->buf
, plen
);
193 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
195 sprintf(header
.name
, "%s.data",
197 strbuf_append_ext_header(&ext_header
, "path",
198 path
->buf
, path
->len
);
201 memcpy(header
.name
, path
->buf
, path
->len
);
204 if (S_ISLNK(mode
) && buffer
) {
205 if (size
> sizeof(header
.linkname
)) {
206 sprintf(header
.linkname
, "see %s.paxheader",
208 strbuf_append_ext_header(&ext_header
, "linkpath",
211 memcpy(header
.linkname
, buffer
, size
);
214 sprintf(header
.mode
, "%07o", mode
& 07777);
215 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
216 sprintf(header
.mtime
, "%011lo", archive_time
);
218 /* XXX: should we provide more meaningful info here? */
219 sprintf(header
.uid
, "%07o", 0);
220 sprintf(header
.gid
, "%07o", 0);
221 strlcpy(header
.uname
, "git", sizeof(header
.uname
));
222 strlcpy(header
.gname
, "git", sizeof(header
.gname
));
223 sprintf(header
.devmajor
, "%07o", 0);
224 sprintf(header
.devminor
, "%07o", 0);
226 memcpy(header
.magic
, "ustar", 6);
227 memcpy(header
.version
, "00", 2);
229 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
231 if (ext_header
.len
> 0) {
232 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
233 free(ext_header
.buf
);
235 write_blocked(&header
, sizeof(header
));
236 if (S_ISREG(mode
) && buffer
&& size
> 0)
237 write_blocked(buffer
, size
);
240 static void write_global_extended_header(const unsigned char *sha1
)
242 struct strbuf ext_header
;
243 ext_header
.buf
= NULL
;
244 ext_header
.len
= ext_header
.alloc
= 0;
245 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
246 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
247 free(ext_header
.buf
);
250 static int git_tar_config(const char *var
, const char *value
)
252 if (!strcmp(var
, "tar.umask")) {
253 if (!strcmp(value
, "user")) {
254 tar_umask
= umask(0);
257 tar_umask
= git_config_int(var
, value
);
261 return git_default_config(var
, value
);
264 static int generate_tar(int argc
, const char **argv
, const char *prefix
)
266 struct archiver_args args
;
270 memset(&args
, 0, sizeof(args
));
271 if (argc
!= 2 && argc
!= 3)
272 usage(tar_tree_usage
);
274 int baselen
= strlen(argv
[2]);
275 base
= xmalloc(baselen
+ 2);
276 memcpy(base
, argv
[2], baselen
);
278 base
[baselen
+ 1] = '\0';
281 parse_treeish_arg(argv
+ 1, &args
, NULL
);
283 result
= write_tar_archive(&args
);
289 static int write_tar_entry(const unsigned char *sha1
,
290 const char *base
, int baselen
,
291 const char *filename
, unsigned mode
, int stage
)
293 static struct strbuf path
;
294 int filenamelen
= strlen(filename
);
300 path
.buf
= xmalloc(PATH_MAX
);
301 path
.alloc
= PATH_MAX
;
302 path
.len
= path
.eof
= 0;
304 if (path
.alloc
< baselen
+ filenamelen
) {
306 path
.buf
= xmalloc(baselen
+ filenamelen
);
307 path
.alloc
= baselen
+ filenamelen
;
309 memcpy(path
.buf
, base
, baselen
);
310 memcpy(path
.buf
+ baselen
, filename
, filenamelen
);
311 path
.len
= baselen
+ filenamelen
;
313 strbuf_append_string(&path
, "/");
317 buffer
= read_sha1_file(sha1
, type
, &size
);
319 die("cannot read %s", sha1_to_hex(sha1
));
322 write_entry(sha1
, &path
, mode
, buffer
, size
);
325 return READ_TREE_RECURSIVE
;
328 int write_tar_archive(struct archiver_args
*args
)
330 int plen
= args
->base
? strlen(args
->base
) : 0;
332 git_config(git_tar_config
);
334 archive_time
= args
->time
;
335 verbose
= args
->verbose
;
337 if (args
->commit_sha1
)
338 write_global_extended_header(args
->commit_sha1
);
340 if (args
->base
&& plen
> 0 && args
->base
[plen
- 1] == '/') {
341 char *base
= xstrdup(args
->base
);
342 int baselen
= strlen(base
);
344 while (baselen
> 0 && base
[baselen
- 1] == '/')
345 base
[--baselen
] = '\0';
346 write_tar_entry(args
->tree
->object
.sha1
, "", 0, base
, 040777, 0);
349 read_tree_recursive(args
->tree
, args
->base
, plen
, 0,
350 args
->pathspec
, write_tar_entry
);
356 static const char *exec
= "git-upload-tar";
358 static int remote_tar(int argc
, const char **argv
)
365 if (argc
< 3 || 4 < argc
)
366 usage(tar_tree_usage
);
368 /* --remote=<repo> */
369 url
= xstrdup(argv
[1]+9);
370 pid
= git_connect(fd
, url
, exec
);
374 packet_write(fd
[1], "want %s\n", argv
[2]);
376 packet_write(fd
[1], "base %s\n", argv
[3]);
379 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
381 die("git-tar-tree: expected ACK/NAK, got EOF");
382 if (buf
[len
-1] == '\n')
384 if (strcmp(buf
, "ACK")) {
385 if (5 < len
&& !strncmp(buf
, "NACK ", 5))
386 die("git-tar-tree: NACK %s", buf
+ 5);
387 die("git-tar-tree: protocol error");
390 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
392 die("git-tar-tree: expected a flush");
394 /* Now, start reading from fd[0] and spit it out to stdout */
395 ret
= copy_fd(fd
[0], 1);
398 ret
|= finish_connect(pid
);
402 int cmd_tar_tree(int argc
, const char **argv
, const char *prefix
)
405 usage(tar_tree_usage
);
406 if (!strncmp("--remote=", argv
[1], 9))
407 return remote_tar(argc
, argv
);
408 return generate_tar(argc
, argv
, prefix
);
411 /* ustar header + extended global header content */
412 #define HEADERSIZE (2 * RECORDSIZE)
414 int cmd_get_tar_commit_id(int argc
, const char **argv
, const char *prefix
)
416 char buffer
[HEADERSIZE
];
417 struct ustar_header
*header
= (struct ustar_header
*)buffer
;
418 char *content
= buffer
+ RECORDSIZE
;
421 n
= xread(0, buffer
, HEADERSIZE
);
423 die("git-get-tar-commit-id: read error");
424 if (header
->typeflag
[0] != 'g')
426 if (memcmp(content
, "52 comment=", 11))
429 n
= xwrite(1, content
+ 11, 41);
431 die("git-get-tar-commit-id: write error");