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