gitweb: Use parse_difftree_raw_line in git_difftree_body
[git] / builtin-tar-tree.c
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"
12
13 #define RECORDSIZE      (512)
14 #define BLOCKSIZE       (RECORDSIZE * 20)
15
16 static const char tar_tree_usage[] =
17 "git-tar-tree [--remote=<repo>] <ent> [basedir]";
18
19 static char block[BLOCKSIZE];
20 static unsigned long offset;
21
22 static time_t archive_time;
23 static int tar_umask;
24
25 /* tries hard to write, either succeeds or dies in the attempt */
26 static void reliable_write(const void *data, unsigned long size)
27 {
28         const char *buf = data;
29
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?");
38                 }
39                 size -= ret;
40                 buf += ret;
41         }
42 }
43
44 /* writes out the whole block, but only if it is full */
45 static void write_if_needed(void)
46 {
47         if (offset == BLOCKSIZE) {
48                 reliable_write(block, BLOCKSIZE);
49                 offset = 0;
50         }
51 }
52
53 /*
54  * queues up writes, so that all our write(2) calls write exactly one
55  * full block; pads writes to RECORDSIZE
56  */
57 static void write_blocked(const void *data, unsigned long size)
58 {
59         const char *buf = data;
60         unsigned long tail;
61
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();
71         }
72         while (size >= BLOCKSIZE) {
73                 reliable_write(buf, BLOCKSIZE);
74                 size -= BLOCKSIZE;
75                 buf += BLOCKSIZE;
76         }
77         if (size) {
78                 memcpy(block + offset, buf, size);
79                 offset += size;
80         }
81         tail = offset % RECORDSIZE;
82         if (tail)  {
83                 memset(block + offset, 0, RECORDSIZE - tail);
84                 offset += RECORDSIZE - tail;
85         }
86         write_if_needed();
87 }
88
89 /*
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).
92  */
93 static void write_trailer(void)
94 {
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);
101         }
102 }
103
104 static void strbuf_append_string(struct strbuf *sb, const char *s)
105 {
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;
111         }
112         memcpy(sb->buf + sb->len, s, slen);
113         sb->len = total;
114 }
115
116 /*
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.
121  */
122 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
123                                      const char *value, unsigned int valuelen)
124 {
125         char *p;
126         int len, total, tmp;
127
128         /* "%u %s=%s\n" */
129         len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
130         for (tmp = len; tmp > 9; tmp /= 10)
131                 len++;
132
133         total = sb->len + len;
134         if (total > sb->alloc) {
135                 sb->buf = xrealloc(sb->buf, total);
136                 sb->alloc = total;
137         }
138
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;
145 }
146
147 static unsigned int ustar_header_chksum(const struct ustar_header *header)
148 {
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;
158 }
159
160 static int get_path_prefix(const struct strbuf *path, int maxlen)
161 {
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;
169 }
170
171 static void write_entry(const unsigned char *sha1, struct strbuf *path,
172                         unsigned int mode, void *buffer, unsigned long size)
173 {
174         struct ustar_header header;
175         struct strbuf ext_header;
176
177         memset(&header, 0, sizeof(header));
178         ext_header.buf = NULL;
179         ext_header.len = ext_header.alloc = 0;
180
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;
203                 }
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);
215                         }
216                 } else
217                         memcpy(header.name, path->buf, path->len);
218         }
219
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);
228         }
229
230         sprintf(header.mode, "%07o", mode & 07777);
231         sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
232         sprintf(header.mtime, "%011lo", archive_time);
233
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);
241
242         memcpy(header.magic, "ustar", 6);
243         memcpy(header.version, "00", 2);
244
245         sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
246
247         if (ext_header.len > 0) {
248                 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
249                 free(ext_header.buf);
250         }
251         write_blocked(&header, sizeof(header));
252         if (S_ISREG(mode) && buffer && size > 0)
253                 write_blocked(buffer, size);
254 }
255
256 static void write_global_extended_header(const unsigned char *sha1)
257 {
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);
264 }
265
266 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
267 {
268         int pathlen = path->len;
269         struct name_entry entry;
270
271         while (tree_entry(tree, &entry)) {
272                 void *eltbuf;
273                 char elttype[20];
274                 unsigned long eltsize;
275
276                 eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
277                 if (!eltbuf)
278                         die("cannot read %s", sha1_to_hex(entry.sha1));
279
280                 path->len = pathlen;
281                 strbuf_append_string(path, entry.path);
282                 if (S_ISDIR(entry.mode))
283                         strbuf_append_string(path, "/");
284
285                 write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
286
287                 if (S_ISDIR(entry.mode)) {
288                         struct tree_desc subtree;
289                         subtree.buf = eltbuf;
290                         subtree.size = eltsize;
291                         traverse_tree(&subtree, path);
292                 }
293                 free(eltbuf);
294         }
295 }
296
297 int git_tar_config(const char *var, const char *value)
298 {
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);
305                 }
306                 return 0;
307         }
308         return git_default_config(var, value);
309 }
310
311 static int generate_tar(int argc, const char **argv, const char *prefix)
312 {
313         unsigned char sha1[20], tree_sha1[20];
314         struct commit *commit;
315         struct tree_desc tree;
316         struct strbuf current_path;
317         void *buffer;
318
319         current_path.buf = xmalloc(PATH_MAX);
320         current_path.alloc = PATH_MAX;
321         current_path.len = current_path.eof = 0;
322
323         git_config(git_tar_config);
324
325         switch (argc) {
326         case 3:
327                 strbuf_append_string(&current_path, argv[2]);
328                 strbuf_append_string(&current_path, "/");
329                 /* FALLTHROUGH */
330         case 2:
331                 if (get_sha1(argv[1], sha1))
332                         die("Not a valid object name %s", argv[1]);
333                 break;
334         default:
335                 usage(tar_tree_usage);
336         }
337
338         commit = lookup_commit_reference_gently(sha1, 1);
339         if (commit) {
340                 write_global_extended_header(commit->object.sha1);
341                 archive_time = commit->date;
342         } else
343                 archive_time = time(NULL);
344
345         tree.buf = buffer = read_object_with_reference(sha1, tree_type,
346                                                        &tree.size, tree_sha1);
347         if (!tree.buf)
348                 die("not a reference to a tag, commit or tree object: %s",
349                     sha1_to_hex(sha1));
350
351         if (current_path.len > 0)
352                 write_entry(tree_sha1, &current_path, 040777, NULL, 0);
353         traverse_tree(&tree, &current_path);
354         write_trailer();
355         free(buffer);
356         free(current_path.buf);
357         return 0;
358 }
359
360 static const char *exec = "git-upload-tar";
361
362 static int remote_tar(int argc, const char **argv)
363 {
364         int fd[2], ret, len;
365         pid_t pid;
366         char buf[1024];
367         char *url;
368
369         if (argc < 3 || 4 < argc)
370                 usage(tar_tree_usage);
371
372         /* --remote=<repo> */
373         url = strdup(argv[1]+9);
374         pid = git_connect(fd, url, exec);
375         if (pid < 0)
376                 return 1;
377
378         packet_write(fd[1], "want %s\n", argv[2]);
379         if (argv[3])
380                 packet_write(fd[1], "base %s\n", argv[3]);
381         packet_flush(fd[1]);
382
383         len = packet_read_line(fd[0], buf, sizeof(buf));
384         if (!len)
385                 die("git-tar-tree: expected ACK/NAK, got EOF");
386         if (buf[len-1] == '\n')
387                 buf[--len] = 0;
388         if (strcmp(buf, "ACK")) {
389                 if (5 < len && !strncmp(buf, "NACK ", 5))
390                         die("git-tar-tree: NACK %s", buf + 5);
391                 die("git-tar-tree: protocol error");
392         }
393         /* expect a flush */
394         len = packet_read_line(fd[0], buf, sizeof(buf));
395         if (len)
396                 die("git-tar-tree: expected a flush");
397
398         /* Now, start reading from fd[0] and spit it out to stdout */
399         ret = copy_fd(fd[0], 1);
400         close(fd[0]);
401
402         ret |= finish_connect(pid);
403         return !!ret;
404 }
405
406 int cmd_tar_tree(int argc, const char **argv, const char *prefix)
407 {
408         if (argc < 2)
409                 usage(tar_tree_usage);
410         if (!strncmp("--remote=", argv[1], 9))
411                 return remote_tar(argc, argv);
412         return generate_tar(argc, argv, prefix);
413 }
414
415 /* ustar header + extended global header content */
416 #define HEADERSIZE (2 * RECORDSIZE)
417
418 int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
419 {
420         char buffer[HEADERSIZE];
421         struct ustar_header *header = (struct ustar_header *)buffer;
422         char *content = buffer + RECORDSIZE;
423         ssize_t n;
424
425         n = xread(0, buffer, HEADERSIZE);
426         if (n < HEADERSIZE)
427                 die("git-get-tar-commit-id: read error");
428         if (header->typeflag[0] != 'g')
429                 return 1;
430         if (memcmp(content, "52 comment=", 11))
431                 return 1;
432
433         n = xwrite(1, content + 11, 41);
434         if (n < 41)
435                 die("git-get-tar-commit-id: write error");
436
437         return 0;
438 }