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