gitweb: Use list for of open for running git commands, thorougly.
[git.git] / builtin-tar-tree.c
blob7c48db9ec85adb1b8866b2cc1dc0eccd2d1816c7
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include <time.h>
5 #include "cache.h"
6 #include "tree-walk.h"
7 #include "commit.h"
8 #include "strbuf.h"
9 #include "tar.h"
10 #include "builtin.h"
11 #include "pkt-line.h"
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;
23 static int tar_umask;
25 /* tries hard to write, either succeeds or dies in the attempt */
26 static void reliable_write(const void *data, unsigned long size)
28 const char *buf = data;
30 while (size > 0) {
31 long ret = xwrite(1, buf, size);
32 if (ret < 0) {
33 if (errno == EPIPE)
34 exit(0);
35 die("git-tar-tree: %s", strerror(errno));
36 } else if (!ret) {
37 die("git-tar-tree: disk full?");
39 size -= ret;
40 buf += ret;
44 /* writes out the whole block, but only if it is full */
45 static void write_if_needed(void)
47 if (offset == BLOCKSIZE) {
48 reliable_write(block, BLOCKSIZE);
49 offset = 0;
54 * queues up writes, so that all our write(2) calls write exactly one
55 * full block; pads writes to RECORDSIZE
57 static void write_blocked(const void *data, unsigned long size)
59 const char *buf = data;
60 unsigned long tail;
62 if (offset) {
63 unsigned long chunk = BLOCKSIZE - offset;
64 if (size < chunk)
65 chunk = size;
66 memcpy(block + offset, buf, chunk);
67 size -= chunk;
68 offset += chunk;
69 buf += chunk;
70 write_if_needed();
72 while (size >= BLOCKSIZE) {
73 reliable_write(buf, BLOCKSIZE);
74 size -= BLOCKSIZE;
75 buf += BLOCKSIZE;
77 if (size) {
78 memcpy(block + offset, buf, size);
79 offset += size;
81 tail = offset % RECORDSIZE;
82 if (tail) {
83 memset(block + offset, 0, RECORDSIZE - tail);
84 offset += RECORDSIZE - tail;
86 write_if_needed();
90 * The end of tar archives is marked by 2*512 nul bytes and after that
91 * follows the rest of the block (if any).
93 static void write_trailer(void)
95 int tail = BLOCKSIZE - offset;
96 memset(block + offset, 0, tail);
97 reliable_write(block, BLOCKSIZE);
98 if (tail < 2 * RECORDSIZE) {
99 memset(block, 0, offset);
100 reliable_write(block, BLOCKSIZE);
104 static void strbuf_append_string(struct strbuf *sb, const char *s)
106 int slen = strlen(s);
107 int total = sb->len + slen;
108 if (total > sb->alloc) {
109 sb->buf = xrealloc(sb->buf, total);
110 sb->alloc = total;
112 memcpy(sb->buf + sb->len, s, slen);
113 sb->len = total;
117 * pax extended header records have the format "%u %s=%s\n". %u contains
118 * the size of the whole string (including the %u), the first %s is the
119 * keyword, the second one is the value. This function constructs such a
120 * string and appends it to a struct strbuf.
122 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
123 const char *value, unsigned int valuelen)
125 char *p;
126 int len, total, tmp;
128 /* "%u %s=%s\n" */
129 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
130 for (tmp = len; tmp > 9; tmp /= 10)
131 len++;
133 total = sb->len + len;
134 if (total > sb->alloc) {
135 sb->buf = xrealloc(sb->buf, total);
136 sb->alloc = total;
139 p = sb->buf;
140 p += sprintf(p, "%u %s=", len, keyword);
141 memcpy(p, value, valuelen);
142 p += valuelen;
143 *p = '\n';
144 sb->len = total;
147 static unsigned int ustar_header_chksum(const struct ustar_header *header)
149 char *p = (char *)header;
150 unsigned int chksum = 0;
151 while (p < header->chksum)
152 chksum += *p++;
153 chksum += sizeof(header->chksum) * ' ';
154 p += sizeof(header->chksum);
155 while (p < (char *)header + sizeof(struct ustar_header))
156 chksum += *p++;
157 return chksum;
160 static int get_path_prefix(const struct strbuf *path, int maxlen)
162 int i = path->len;
163 if (i > maxlen)
164 i = maxlen;
165 do {
166 i--;
167 } while (i > 0 && path->buf[i] != '/');
168 return i;
171 static void write_entry(const unsigned char *sha1, struct strbuf *path,
172 unsigned int mode, void *buffer, unsigned long size)
174 struct ustar_header header;
175 struct strbuf ext_header;
177 memset(&header, 0, sizeof(header));
178 ext_header.buf = NULL;
179 ext_header.len = ext_header.alloc = 0;
181 if (!sha1) {
182 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
183 mode = 0100666;
184 strcpy(header.name, "pax_global_header");
185 } else if (!path) {
186 *header.typeflag = TYPEFLAG_EXT_HEADER;
187 mode = 0100666;
188 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
189 } else {
190 if (S_ISDIR(mode)) {
191 *header.typeflag = TYPEFLAG_DIR;
192 mode = (mode | 0777) & ~tar_umask;
193 } else if (S_ISLNK(mode)) {
194 *header.typeflag = TYPEFLAG_LNK;
195 mode |= 0777;
196 } else if (S_ISREG(mode)) {
197 *header.typeflag = TYPEFLAG_REG;
198 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
199 } else {
200 error("unsupported file mode: 0%o (SHA1: %s)",
201 mode, sha1_to_hex(sha1));
202 return;
204 if (path->len > sizeof(header.name)) {
205 int plen = get_path_prefix(path, sizeof(header.prefix));
206 int rest = path->len - plen - 1;
207 if (plen > 0 && rest <= sizeof(header.name)) {
208 memcpy(header.prefix, path->buf, plen);
209 memcpy(header.name, path->buf + plen + 1, rest);
210 } else {
211 sprintf(header.name, "%s.data",
212 sha1_to_hex(sha1));
213 strbuf_append_ext_header(&ext_header, "path",
214 path->buf, path->len);
216 } else
217 memcpy(header.name, path->buf, path->len);
220 if (S_ISLNK(mode) && buffer) {
221 if (size > sizeof(header.linkname)) {
222 sprintf(header.linkname, "see %s.paxheader",
223 sha1_to_hex(sha1));
224 strbuf_append_ext_header(&ext_header, "linkpath",
225 buffer, size);
226 } else
227 memcpy(header.linkname, buffer, size);
230 sprintf(header.mode, "%07o", mode & 07777);
231 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
232 sprintf(header.mtime, "%011lo", archive_time);
234 /* XXX: should we provide more meaningful info here? */
235 sprintf(header.uid, "%07o", 0);
236 sprintf(header.gid, "%07o", 0);
237 strlcpy(header.uname, "git", sizeof(header.uname));
238 strlcpy(header.gname, "git", sizeof(header.gname));
239 sprintf(header.devmajor, "%07o", 0);
240 sprintf(header.devminor, "%07o", 0);
242 memcpy(header.magic, "ustar", 6);
243 memcpy(header.version, "00", 2);
245 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
247 if (ext_header.len > 0) {
248 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
249 free(ext_header.buf);
251 write_blocked(&header, sizeof(header));
252 if (S_ISREG(mode) && buffer && size > 0)
253 write_blocked(buffer, size);
256 static void write_global_extended_header(const unsigned char *sha1)
258 struct strbuf ext_header;
259 ext_header.buf = NULL;
260 ext_header.len = ext_header.alloc = 0;
261 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
262 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
263 free(ext_header.buf);
266 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
268 int pathlen = path->len;
269 struct name_entry entry;
271 while (tree_entry(tree, &entry)) {
272 void *eltbuf;
273 char elttype[20];
274 unsigned long eltsize;
276 eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
277 if (!eltbuf)
278 die("cannot read %s", sha1_to_hex(entry.sha1));
280 path->len = pathlen;
281 strbuf_append_string(path, entry.path);
282 if (S_ISDIR(entry.mode))
283 strbuf_append_string(path, "/");
285 write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
287 if (S_ISDIR(entry.mode)) {
288 struct tree_desc subtree;
289 subtree.buf = eltbuf;
290 subtree.size = eltsize;
291 traverse_tree(&subtree, path);
293 free(eltbuf);
297 int git_tar_config(const char *var, const char *value)
299 if (!strcmp(var, "tar.umask")) {
300 if (!strcmp(value, "user")) {
301 tar_umask = umask(0);
302 umask(tar_umask);
303 } else {
304 tar_umask = git_config_int(var, value);
306 return 0;
308 return git_default_config(var, value);
311 static int generate_tar(int argc, const char **argv, const char *prefix)
313 unsigned char sha1[20], tree_sha1[20];
314 struct commit *commit;
315 struct tree_desc tree;
316 struct strbuf current_path;
318 current_path.buf = xmalloc(PATH_MAX);
319 current_path.alloc = PATH_MAX;
320 current_path.len = current_path.eof = 0;
322 git_config(git_tar_config);
324 switch (argc) {
325 case 3:
326 strbuf_append_string(&current_path, argv[2]);
327 strbuf_append_string(&current_path, "/");
328 /* FALLTHROUGH */
329 case 2:
330 if (get_sha1(argv[1], sha1))
331 die("Not a valid object name %s", argv[1]);
332 break;
333 default:
334 usage(tar_tree_usage);
337 commit = lookup_commit_reference_gently(sha1, 1);
338 if (commit) {
339 write_global_extended_header(commit->object.sha1);
340 archive_time = commit->date;
341 } else
342 archive_time = time(NULL);
344 tree.buf = read_object_with_reference(sha1, tree_type, &tree.size,
345 tree_sha1);
346 if (!tree.buf)
347 die("not a reference to a tag, commit or tree object: %s",
348 sha1_to_hex(sha1));
350 if (current_path.len > 0)
351 write_entry(tree_sha1, &current_path, 040777, NULL, 0);
352 traverse_tree(&tree, &current_path);
353 write_trailer();
354 free(current_path.buf);
355 return 0;
358 static const char *exec = "git-upload-tar";
360 static int remote_tar(int argc, const char **argv)
362 int fd[2], ret, len;
363 pid_t pid;
364 char buf[1024];
365 char *url;
367 if (argc < 3 || 4 < argc)
368 usage(tar_tree_usage);
370 /* --remote=<repo> */
371 url = strdup(argv[1]+9);
372 pid = git_connect(fd, url, exec);
373 if (pid < 0)
374 return 1;
376 packet_write(fd[1], "want %s\n", argv[2]);
377 if (argv[3])
378 packet_write(fd[1], "base %s\n", argv[3]);
379 packet_flush(fd[1]);
381 len = packet_read_line(fd[0], buf, sizeof(buf));
382 if (!len)
383 die("git-tar-tree: expected ACK/NAK, got EOF");
384 if (buf[len-1] == '\n')
385 buf[--len] = 0;
386 if (strcmp(buf, "ACK")) {
387 if (5 < len && !strncmp(buf, "NACK ", 5))
388 die("git-tar-tree: NACK %s", buf + 5);
389 die("git-tar-tree: protocol error");
391 /* expect a flush */
392 len = packet_read_line(fd[0], buf, sizeof(buf));
393 if (len)
394 die("git-tar-tree: expected a flush");
396 /* Now, start reading from fd[0] and spit it out to stdout */
397 ret = copy_fd(fd[0], 1);
398 close(fd[0]);
400 ret |= finish_connect(pid);
401 return !!ret;
404 int cmd_tar_tree(int argc, const char **argv, const char *prefix)
406 if (argc < 2)
407 usage(tar_tree_usage);
408 if (!strncmp("--remote=", argv[1], 9))
409 return remote_tar(argc, argv);
410 return generate_tar(argc, argv, prefix);
413 /* ustar header + extended global header content */
414 #define HEADERSIZE (2 * RECORDSIZE)
416 int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
418 char buffer[HEADERSIZE];
419 struct ustar_header *header = (struct ustar_header *)buffer;
420 char *content = buffer + RECORDSIZE;
421 ssize_t n;
423 n = xread(0, buffer, HEADERSIZE);
424 if (n < HEADERSIZE)
425 die("git-get-tar-commit-id: read error");
426 if (header->typeflag[0] != 'g')
427 return 1;
428 if (memcmp(content, "52 comment=", 11))
429 return 1;
431 n = xwrite(1, content + 11, 41);
432 if (n < 41)
433 die("git-get-tar-commit-id: write error");
435 return 0;