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
);
50 /* acquire the next record from the buffer; user must call write_if_needed() */
51 static char *get_record(void)
53 char *p
= block
+ offset
;
54 memset(p
, 0, RECORDSIZE
);
60 * The end of tar archives is marked by 1024 nul bytes and after that
61 * follows the rest of the block (if any).
63 static void write_trailer(void)
76 * queues up writes, so that all our write(2) calls write exactly one
77 * full block; pads writes to RECORDSIZE
79 static void write_blocked(void *buf
, unsigned long size
)
84 unsigned long chunk
= BLOCKSIZE
- offset
;
87 memcpy(block
+ offset
, buf
, chunk
);
93 while (size
>= BLOCKSIZE
) {
94 reliable_write(buf
, BLOCKSIZE
);
99 memcpy(block
+ offset
, buf
, size
);
102 tail
= offset
% RECORDSIZE
;
104 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
105 offset
+= RECORDSIZE
- tail
;
110 static void strbuf_append_string(struct strbuf
*sb
, const char *s
)
112 int slen
= strlen(s
);
113 int total
= sb
->len
+ slen
;
114 if (total
> sb
->alloc
) {
115 sb
->buf
= xrealloc(sb
->buf
, total
);
118 memcpy(sb
->buf
+ sb
->len
, s
, slen
);
123 * pax extended header records have the format "%u %s=%s\n". %u contains
124 * the size of the whole string (including the %u), the first %s is the
125 * keyword, the second one is the value. This function constructs such a
126 * string and appends it to a struct strbuf.
128 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
129 const char *value
, unsigned int valuelen
)
135 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
136 for (tmp
= len
; tmp
> 9; tmp
/= 10)
139 total
= sb
->len
+ len
;
140 if (total
> sb
->alloc
) {
141 sb
->buf
= xrealloc(sb
->buf
, total
);
146 p
+= sprintf(p
, "%u %s=", len
, keyword
);
147 memcpy(p
, value
, valuelen
);
153 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
155 char *p
= (char *)header
;
156 unsigned int chksum
= 0;
157 while (p
< header
->chksum
)
159 chksum
+= sizeof(header
->chksum
) * ' ';
160 p
+= sizeof(header
->chksum
);
161 while (p
< (char *)header
+ sizeof(struct ustar_header
))
166 static int get_path_prefix(const struct strbuf
*path
, int maxlen
)
171 while (i
> 0 && path
->buf
[i
] != '/')
176 static void write_entry(const unsigned char *sha1
, struct strbuf
*path
,
177 unsigned int mode
, void *buffer
, unsigned long size
)
179 struct ustar_header header
;
180 struct strbuf ext_header
;
182 memset(&header
, 0, sizeof(header
));
183 ext_header
.buf
= NULL
;
184 ext_header
.len
= ext_header
.alloc
= 0;
187 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
189 strcpy(header
.name
, "pax_global_header");
191 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
193 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
196 *header
.typeflag
= TYPEFLAG_DIR
;
198 } else if (S_ISLNK(mode
)) {
199 *header
.typeflag
= TYPEFLAG_LNK
;
201 } else if (S_ISREG(mode
)) {
202 *header
.typeflag
= TYPEFLAG_REG
;
203 mode
|= (mode
& 0100) ? 0777 : 0666;
205 error("unsupported file mode: 0%o (SHA1: %s)",
206 mode
, sha1_to_hex(sha1
));
209 if (path
->len
> sizeof(header
.name
)) {
210 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
211 int rest
= path
->len
- plen
- 1;
212 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
213 memcpy(header
.prefix
, path
->buf
, plen
);
214 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
216 sprintf(header
.name
, "%s.data",
218 strbuf_append_ext_header(&ext_header
, "path",
219 path
->buf
, path
->len
);
222 memcpy(header
.name
, path
->buf
, path
->len
);
225 if (S_ISLNK(mode
) && buffer
) {
226 if (size
> sizeof(header
.linkname
)) {
227 sprintf(header
.linkname
, "see %s.paxheader",
229 strbuf_append_ext_header(&ext_header
, "linkpath",
232 memcpy(header
.linkname
, buffer
, size
);
235 sprintf(header
.mode
, "%07o", mode
& 07777);
236 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
237 sprintf(header
.mtime
, "%011lo", archive_time
);
239 /* XXX: should we provide more meaningful info here? */
240 sprintf(header
.uid
, "%07o", 0);
241 sprintf(header
.gid
, "%07o", 0);
242 strncpy(header
.uname
, "git", 31);
243 strncpy(header
.gname
, "git", 31);
244 sprintf(header
.devmajor
, "%07o", 0);
245 sprintf(header
.devminor
, "%07o", 0);
247 memcpy(header
.magic
, "ustar", 6);
248 memcpy(header
.version
, "00", 2);
250 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
252 if (ext_header
.len
> 0) {
253 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
254 free(ext_header
.buf
);
256 write_blocked(&header
, sizeof(header
));
257 if (S_ISREG(mode
) && buffer
&& size
> 0)
258 write_blocked(buffer
, size
);
261 static void write_global_extended_header(const unsigned char *sha1
)
263 struct strbuf ext_header
;
264 ext_header
.buf
= NULL
;
265 ext_header
.len
= ext_header
.alloc
= 0;
266 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
267 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
268 free(ext_header
.buf
);
271 static void traverse_tree(struct tree_desc
*tree
, struct strbuf
*path
)
273 int pathlen
= path
->len
;
274 struct name_entry entry
;
276 while (tree_entry(tree
, &entry
)) {
279 unsigned long eltsize
;
281 eltbuf
= read_sha1_file(entry
.sha1
, elttype
, &eltsize
);
283 die("cannot read %s", sha1_to_hex(entry
.sha1
));
286 strbuf_append_string(path
, entry
.path
);
287 if (S_ISDIR(entry
.mode
))
288 strbuf_append_string(path
, "/");
290 write_entry(entry
.sha1
, path
, entry
.mode
, eltbuf
, eltsize
);
292 if (S_ISDIR(entry
.mode
)) {
293 struct tree_desc subtree
;
294 subtree
.buf
= eltbuf
;
295 subtree
.size
= eltsize
;
296 traverse_tree(&subtree
, path
);
302 static int generate_tar(int argc
, const char **argv
, char** envp
)
304 unsigned char sha1
[20], tree_sha1
[20];
305 struct commit
*commit
;
306 struct tree_desc tree
;
307 struct strbuf current_path
;
309 current_path
.buf
= xmalloc(PATH_MAX
);
310 current_path
.alloc
= PATH_MAX
;
311 current_path
.len
= current_path
.eof
= 0;
313 setup_git_directory();
314 git_config(git_default_config
);
318 strbuf_append_string(¤t_path
, argv
[2]);
319 strbuf_append_string(¤t_path
, "/");
322 if (get_sha1(argv
[1], sha1
))
323 die("Not a valid object name %s", argv
[1]);
326 usage(tar_tree_usage
);
329 commit
= lookup_commit_reference_gently(sha1
, 1);
331 write_global_extended_header(commit
->object
.sha1
);
332 archive_time
= commit
->date
;
334 archive_time
= time(NULL
);
336 tree
.buf
= read_object_with_reference(sha1
, tree_type
, &tree
.size
,
339 die("not a reference to a tag, commit or tree object: %s",
342 if (current_path
.len
> 0)
343 write_entry(tree_sha1
, ¤t_path
, 040777, NULL
, 0);
344 traverse_tree(&tree
, ¤t_path
);
346 free(current_path
.buf
);
350 static const char *exec
= "git-upload-tar";
352 static int remote_tar(int argc
, const char **argv
)
359 if (argc
< 3 || 4 < argc
)
360 usage(tar_tree_usage
);
362 /* --remote=<repo> */
363 url
= strdup(argv
[1]+9);
364 pid
= git_connect(fd
, url
, exec
);
368 packet_write(fd
[1], "want %s\n", argv
[2]);
370 packet_write(fd
[1], "base %s\n", argv
[3]);
373 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
375 die("git-tar-tree: expected ACK/NAK, got EOF");
376 if (buf
[len
-1] == '\n')
378 if (strcmp(buf
, "ACK")) {
379 if (5 < len
&& !strncmp(buf
, "NACK ", 5))
380 die("git-tar-tree: NACK %s", buf
+ 5);
381 die("git-tar-tree: protocol error");
384 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
386 die("git-tar-tree: expected a flush");
388 /* Now, start reading from fd[0] and spit it out to stdout */
389 ret
= copy_fd(fd
[0], 1);
392 ret
|= finish_connect(pid
);
396 int cmd_tar_tree(int argc
, const char **argv
, char **envp
)
399 usage(tar_tree_usage
);
400 if (!strncmp("--remote=", argv
[1], 9))
401 return remote_tar(argc
, argv
);
402 return generate_tar(argc
, argv
, envp
);