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>] <ent> [basedir]";
19 static char block
[BLOCKSIZE
];
20 static unsigned long offset
;
22 static time_t archive_time
;
24 /* tries hard to write, either succeeds or dies in the attempt */
25 static void reliable_write(void *buf
, unsigned long size
)
28 long ret
= xwrite(1, buf
, size
);
32 die("git-tar-tree: %s", strerror(errno
));
34 die("git-tar-tree: disk full?");
41 /* writes out the whole block, but only if it is full */
42 static void write_if_needed(void)
44 if (offset
== BLOCKSIZE
) {
45 reliable_write(block
, BLOCKSIZE
);
51 * queues up writes, so that all our write(2) calls write exactly one
52 * full block; pads writes to RECORDSIZE
54 static void write_blocked(void *buf
, unsigned long size
)
59 unsigned long chunk
= BLOCKSIZE
- offset
;
62 memcpy(block
+ offset
, buf
, chunk
);
68 while (size
>= BLOCKSIZE
) {
69 reliable_write(buf
, BLOCKSIZE
);
74 memcpy(block
+ offset
, buf
, size
);
77 tail
= offset
% RECORDSIZE
;
79 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
80 offset
+= RECORDSIZE
- tail
;
86 * The end of tar archives is marked by 2*512 nul bytes and after that
87 * follows the rest of the block (if any).
89 static void write_trailer(void)
91 int tail
= BLOCKSIZE
- offset
;
92 memset(block
+ offset
, 0, tail
);
93 reliable_write(block
, BLOCKSIZE
);
94 if (tail
< 2 * RECORDSIZE
) {
95 memset(block
, 0, offset
);
96 reliable_write(block
, BLOCKSIZE
);
100 static void strbuf_append_string(struct strbuf
*sb
, const char *s
)
102 int slen
= strlen(s
);
103 int total
= sb
->len
+ slen
;
104 if (total
> sb
->alloc
) {
105 sb
->buf
= xrealloc(sb
->buf
, total
);
108 memcpy(sb
->buf
+ sb
->len
, s
, slen
);
113 * pax extended header records have the format "%u %s=%s\n". %u contains
114 * the size of the whole string (including the %u), the first %s is the
115 * keyword, the second one is the value. This function constructs such a
116 * string and appends it to a struct strbuf.
118 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
119 const char *value
, unsigned int valuelen
)
125 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
126 for (tmp
= len
; tmp
> 9; tmp
/= 10)
129 total
= sb
->len
+ len
;
130 if (total
> sb
->alloc
) {
131 sb
->buf
= xrealloc(sb
->buf
, total
);
136 p
+= sprintf(p
, "%u %s=", len
, keyword
);
137 memcpy(p
, value
, valuelen
);
143 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
145 char *p
= (char *)header
;
146 unsigned int chksum
= 0;
147 while (p
< header
->chksum
)
149 chksum
+= sizeof(header
->chksum
) * ' ';
150 p
+= sizeof(header
->chksum
);
151 while (p
< (char *)header
+ sizeof(struct ustar_header
))
156 static int get_path_prefix(const struct strbuf
*path
, int maxlen
)
163 } while (i
> 0 && path
->buf
[i
] != '/');
167 static void write_entry(const unsigned char *sha1
, struct strbuf
*path
,
168 unsigned int mode
, void *buffer
, unsigned long size
)
170 struct ustar_header header
;
171 struct strbuf ext_header
;
173 memset(&header
, 0, sizeof(header
));
174 ext_header
.buf
= NULL
;
175 ext_header
.len
= ext_header
.alloc
= 0;
178 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
180 strcpy(header
.name
, "pax_global_header");
182 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
184 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
187 *header
.typeflag
= TYPEFLAG_DIR
;
189 } else if (S_ISLNK(mode
)) {
190 *header
.typeflag
= TYPEFLAG_LNK
;
192 } else if (S_ISREG(mode
)) {
193 *header
.typeflag
= TYPEFLAG_REG
;
194 mode
|= (mode
& 0100) ? 0777 : 0666;
196 error("unsupported file mode: 0%o (SHA1: %s)",
197 mode
, sha1_to_hex(sha1
));
200 if (path
->len
> sizeof(header
.name
)) {
201 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
202 int rest
= path
->len
- plen
- 1;
203 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
204 memcpy(header
.prefix
, path
->buf
, plen
);
205 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
207 sprintf(header
.name
, "%s.data",
209 strbuf_append_ext_header(&ext_header
, "path",
210 path
->buf
, path
->len
);
213 memcpy(header
.name
, path
->buf
, path
->len
);
216 if (S_ISLNK(mode
) && buffer
) {
217 if (size
> sizeof(header
.linkname
)) {
218 sprintf(header
.linkname
, "see %s.paxheader",
220 strbuf_append_ext_header(&ext_header
, "linkpath",
223 memcpy(header
.linkname
, buffer
, size
);
226 sprintf(header
.mode
, "%07o", mode
& 07777);
227 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
228 sprintf(header
.mtime
, "%011lo", archive_time
);
230 /* XXX: should we provide more meaningful info here? */
231 sprintf(header
.uid
, "%07o", 0);
232 sprintf(header
.gid
, "%07o", 0);
233 safe_strncpy(header
.uname
, "git", sizeof(header
.uname
));
234 safe_strncpy(header
.gname
, "git", sizeof(header
.gname
));
235 sprintf(header
.devmajor
, "%07o", 0);
236 sprintf(header
.devminor
, "%07o", 0);
238 memcpy(header
.magic
, "ustar", 6);
239 memcpy(header
.version
, "00", 2);
241 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
243 if (ext_header
.len
> 0) {
244 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
245 free(ext_header
.buf
);
247 write_blocked(&header
, sizeof(header
));
248 if (S_ISREG(mode
) && buffer
&& size
> 0)
249 write_blocked(buffer
, size
);
252 static void write_global_extended_header(const unsigned char *sha1
)
254 struct strbuf ext_header
;
255 ext_header
.buf
= NULL
;
256 ext_header
.len
= ext_header
.alloc
= 0;
257 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
258 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
259 free(ext_header
.buf
);
262 static void traverse_tree(struct tree_desc
*tree
, struct strbuf
*path
)
264 int pathlen
= path
->len
;
265 struct name_entry entry
;
267 while (tree_entry(tree
, &entry
)) {
270 unsigned long eltsize
;
272 eltbuf
= read_sha1_file(entry
.sha1
, elttype
, &eltsize
);
274 die("cannot read %s", sha1_to_hex(entry
.sha1
));
277 strbuf_append_string(path
, entry
.path
);
278 if (S_ISDIR(entry
.mode
))
279 strbuf_append_string(path
, "/");
281 write_entry(entry
.sha1
, path
, entry
.mode
, eltbuf
, eltsize
);
283 if (S_ISDIR(entry
.mode
)) {
284 struct tree_desc subtree
;
285 subtree
.buf
= eltbuf
;
286 subtree
.size
= eltsize
;
287 traverse_tree(&subtree
, path
);
293 static int generate_tar(int argc
, const char **argv
, char** envp
)
295 unsigned char sha1
[20], tree_sha1
[20];
296 struct commit
*commit
;
297 struct tree_desc tree
;
298 struct strbuf current_path
;
300 current_path
.buf
= xmalloc(PATH_MAX
);
301 current_path
.alloc
= PATH_MAX
;
302 current_path
.len
= current_path
.eof
= 0;
304 setup_git_directory();
305 git_config(git_default_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
= read_object_with_reference(sha1
, tree_type
, &tree
.size
,
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
);
337 free(current_path
.buf
);
341 static const char *exec
= "git-upload-tar";
343 static int remote_tar(int argc
, const char **argv
)
350 if (argc
< 3 || 4 < argc
)
351 usage(tar_tree_usage
);
353 /* --remote=<repo> */
354 url
= strdup(argv
[1]+9);
355 pid
= git_connect(fd
, url
, exec
);
359 packet_write(fd
[1], "want %s\n", argv
[2]);
361 packet_write(fd
[1], "base %s\n", argv
[3]);
364 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
366 die("git-tar-tree: expected ACK/NAK, got EOF");
367 if (buf
[len
-1] == '\n')
369 if (strcmp(buf
, "ACK")) {
370 if (5 < len
&& !strncmp(buf
, "NACK ", 5))
371 die("git-tar-tree: NACK %s", buf
+ 5);
372 die("git-tar-tree: protocol error");
375 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
377 die("git-tar-tree: expected a flush");
379 /* Now, start reading from fd[0] and spit it out to stdout */
380 ret
= copy_fd(fd
[0], 1);
383 ret
|= finish_connect(pid
);
387 int cmd_tar_tree(int argc
, const char **argv
, char **envp
)
390 usage(tar_tree_usage
);
391 if (!strncmp("--remote=", argv
[1], 9))
392 return remote_tar(argc
, argv
);
393 return generate_tar(argc
, argv
, envp
);
396 /* ustar header + extended global header content */
397 #define HEADERSIZE (2 * RECORDSIZE)
399 int cmd_get_tar_commit_id(int argc
, const char **argv
, char **envp
)
401 char buffer
[HEADERSIZE
];
402 struct ustar_header
*header
= (struct ustar_header
*)buffer
;
403 char *content
= buffer
+ RECORDSIZE
;
406 n
= xread(0, buffer
, HEADERSIZE
);
408 die("git-get-tar-commit-id: read error");
409 if (header
->typeflag
[0] != 'g')
411 if (memcmp(content
, "52 comment=", 11))
414 n
= xwrite(1, content
+ 11, 41);
416 die("git-get-tar-commit-id: write error");