git-describe: use find_unique_abbrev()
[git.git] / tar-tree.c
1 /*
2  * Copyright (c) 2005 Rene Scharfe
3  */
4 #include <time.h>
5 #include "cache.h"
6
7 #define RECORDSIZE      (512)
8 #define BLOCKSIZE       (RECORDSIZE * 20)
9
10 #define TYPEFLAG_AUTO           '\0'
11 #define TYPEFLAG_REG            '0'
12 #define TYPEFLAG_LNK            '2'
13 #define TYPEFLAG_DIR            '5'
14 #define TYPEFLAG_GLOBAL_HEADER  'g'
15 #define TYPEFLAG_EXT_HEADER     'x'
16
17 #define EXT_HEADER_PATH         1
18 #define EXT_HEADER_LINKPATH     2
19
20 static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
21
22 static char block[BLOCKSIZE];
23 static unsigned long offset;
24
25 static const char *basedir;
26 static time_t archive_time;
27
28 struct path_prefix {
29         struct path_prefix *prev;
30         const char *name;
31 };
32
33 /* tries hard to write, either succeeds or dies in the attempt */
34 static void reliable_write(void *buf, unsigned long size)
35 {
36         while (size > 0) {
37                 long ret = xwrite(1, buf, size);
38                 if (ret < 0) {
39                         if (errno == EPIPE)
40                                 exit(0);
41                         die("git-tar-tree: %s", strerror(errno));
42                 } else if (!ret) {
43                         die("git-tar-tree: disk full?");
44                 }
45                 size -= ret;
46                 buf += ret;
47         }
48 }
49
50 /* writes out the whole block, but only if it is full */
51 static void write_if_needed(void)
52 {
53         if (offset == BLOCKSIZE) {
54                 reliable_write(block, BLOCKSIZE);
55                 offset = 0;
56         }
57 }
58
59 /* acquire the next record from the buffer; user must call write_if_needed() */
60 static char *get_record(void)
61 {
62         char *p = block + offset;
63         memset(p, 0, RECORDSIZE);
64         offset += RECORDSIZE;
65         return p;
66 }
67
68 /*
69  * The end of tar archives is marked by 1024 nul bytes and after that
70  * follows the rest of the block (if any).
71  */
72 static void write_trailer(void)
73 {
74         get_record();
75         write_if_needed();
76         get_record();
77         write_if_needed();
78         while (offset) {
79                 get_record();
80                 write_if_needed();
81         }
82 }
83
84 /*
85  * queues up writes, so that all our write(2) calls write exactly one
86  * full block; pads writes to RECORDSIZE
87  */
88 static void write_blocked(void *buf, unsigned long size)
89 {
90         unsigned long tail;
91
92         if (offset) {
93                 unsigned long chunk = BLOCKSIZE - offset;
94                 if (size < chunk)
95                         chunk = size;
96                 memcpy(block + offset, buf, chunk);
97                 size -= chunk;
98                 offset += chunk;
99                 buf += chunk;
100                 write_if_needed();
101         }
102         while (size >= BLOCKSIZE) {
103                 reliable_write(buf, BLOCKSIZE);
104                 size -= BLOCKSIZE;
105                 buf += BLOCKSIZE;
106         }
107         if (size) {
108                 memcpy(block + offset, buf, size);
109                 buf += size;
110                 offset += size;
111         }
112         tail = offset % RECORDSIZE;
113         if (tail)  {
114                 memset(block + offset, 0, RECORDSIZE - tail);
115                 offset += RECORDSIZE - tail;
116         }
117         write_if_needed();
118 }
119
120 static void append_string(char **p, const char *s)
121 {
122         unsigned int len = strlen(s);
123         memcpy(*p, s, len);
124         *p += len;
125 }
126
127 static void append_char(char **p, char c)
128 {
129         **p = c;
130         *p += 1;
131 }
132
133 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
134 {
135         if (!prefix)
136                 return;
137         append_path_prefix(buffer, prefix->prev);
138         append_string(buffer, prefix->name);
139         append_char(buffer, '/');
140 }
141
142 static unsigned int path_prefix_len(struct path_prefix *prefix)
143 {
144         if (!prefix)
145                 return 0;
146         return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
147 }
148
149 static void append_path(char **p, int is_dir, const char *basepath,
150                         struct path_prefix *prefix, const char *path)
151 {
152         if (basepath) {
153                 append_string(p, basepath);
154                 append_char(p, '/');
155         }
156         append_path_prefix(p, prefix);
157         append_string(p, path);
158         if (is_dir)
159                 append_char(p, '/');
160 }
161
162 static unsigned int path_len(int is_dir, const char *basepath,
163                              struct path_prefix *prefix, const char *path)
164 {
165         unsigned int len = 0;
166         if (basepath)
167                 len += strlen(basepath) + 1;
168         len += path_prefix_len(prefix) + strlen(path);
169         if (is_dir)
170                 len++;
171         return len;
172 }
173
174 static void append_extended_header_prefix(char **p, unsigned int size,
175                                           const char *keyword)
176 {
177         int len = sprintf(*p, "%u %s=", size, keyword);
178         *p += len;
179 }
180
181 static unsigned int extended_header_len(const char *keyword,
182                                         unsigned int valuelen)
183 {
184         /* "%u %s=%s\n" */
185         unsigned int len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
186         if (len > 9)
187                 len++;
188         if (len > 99)
189                 len++;
190         return len;
191 }
192
193 static void append_extended_header(char **p, const char *keyword,
194                                    const char *value, unsigned int len)
195 {
196         unsigned int size = extended_header_len(keyword, len);
197         append_extended_header_prefix(p, size, keyword);
198         memcpy(*p, value, len);
199         *p += len;
200         append_char(p, '\n');
201 }
202
203 static void write_header(const unsigned char *, char, const char *, struct path_prefix *,
204                          const char *, unsigned int, void *, unsigned long);
205
206 /* stores a pax extended header directly in the block buffer */
207 static void write_extended_header(const char *headerfilename, int is_dir,
208                                   unsigned int flags, const char *basepath,
209                                   struct path_prefix *prefix,
210                                   const char *path, unsigned int namelen,
211                                   void *content, unsigned int contentsize)
212 {
213         char *buffer, *p;
214         unsigned int pathlen, size, linkpathlen = 0;
215
216         size = pathlen = extended_header_len("path", namelen);
217         if (flags & EXT_HEADER_LINKPATH) {
218                 linkpathlen = extended_header_len("linkpath", contentsize);
219                 size += linkpathlen;
220         }
221         write_header(NULL, TYPEFLAG_EXT_HEADER, NULL, NULL, headerfilename,
222                      0100600, NULL, size);
223
224         buffer = p = malloc(size);
225         if (!buffer)
226                 die("git-tar-tree: %s", strerror(errno));
227         append_extended_header_prefix(&p, pathlen, "path");
228         append_path(&p, is_dir, basepath, prefix, path);
229         append_char(&p, '\n');
230         if (flags & EXT_HEADER_LINKPATH)
231                 append_extended_header(&p, "linkpath", content, contentsize);
232         write_blocked(buffer, size);
233         free(buffer);
234 }
235
236 static void write_global_extended_header(const unsigned char *sha1)
237 {
238         char *p;
239         unsigned int size;
240
241         size = extended_header_len("comment", 40);
242         write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL,
243                      "pax_global_header", 0100600, NULL, size);
244
245         p = get_record();
246         append_extended_header(&p, "comment", sha1_to_hex(sha1), 40);
247         write_if_needed();
248 }
249
250 /* stores a ustar header directly in the block buffer */
251 static void write_header(const unsigned char *sha1, char typeflag, const char *basepath,
252                          struct path_prefix *prefix, const char *path,
253                          unsigned int mode, void *buffer, unsigned long size)
254 {
255         unsigned int namelen; 
256         char *header = NULL;
257         unsigned int checksum = 0;
258         int i;
259         unsigned int ext_header = 0;
260
261         if (typeflag == TYPEFLAG_AUTO) {
262                 if (S_ISDIR(mode))
263                         typeflag = TYPEFLAG_DIR;
264                 else if (S_ISLNK(mode))
265                         typeflag = TYPEFLAG_LNK;
266                 else
267                         typeflag = TYPEFLAG_REG;
268         }
269
270         namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
271         if (namelen > 100)
272                 ext_header |= EXT_HEADER_PATH;
273         if (typeflag == TYPEFLAG_LNK && size > 100)
274                 ext_header |= EXT_HEADER_LINKPATH;
275
276         /* the extended header must be written before the normal one */
277         if (ext_header) {
278                 char headerfilename[51];
279                 sprintf(headerfilename, "%s.paxheader", sha1_to_hex(sha1));
280                 write_extended_header(headerfilename, S_ISDIR(mode),
281                                       ext_header, basepath, prefix, path,
282                                       namelen, buffer, size);
283         }
284
285         header = get_record();
286
287         if (ext_header) {
288                 sprintf(header, "%s.data", sha1_to_hex(sha1));
289         } else {
290                 char *p = header;
291                 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
292         }
293
294         if (typeflag == TYPEFLAG_LNK) {
295                 if (ext_header & EXT_HEADER_LINKPATH) {
296                         sprintf(&header[157], "see %s.paxheader",
297                                 sha1_to_hex(sha1));
298                 } else {
299                         if (buffer)
300                                 strncpy(&header[157], buffer, size);
301                 }
302         }
303
304         if (S_ISDIR(mode))
305                 mode |= 0755;   /* GIT doesn't store permissions of dirs */
306         if (S_ISLNK(mode))
307                 mode |= 0777;   /* ... nor of symlinks */
308         sprintf(&header[100], "%07o", mode & 07777);
309
310         /* XXX: should we provide more meaningful info here? */
311         sprintf(&header[108], "%07o", 0);       /* uid */
312         sprintf(&header[116], "%07o", 0);       /* gid */
313         strncpy(&header[265], "git", 31);       /* uname */
314         strncpy(&header[297], "git", 31);       /* gname */
315
316         if (S_ISDIR(mode) || S_ISLNK(mode))
317                 size = 0;
318         sprintf(&header[124], "%011lo", size);
319         sprintf(&header[136], "%011lo", archive_time);
320
321         header[156] = typeflag;
322
323         memcpy(&header[257], "ustar", 6);
324         memcpy(&header[263], "00", 2);
325
326         sprintf(&header[329], "%07o", 0);       /* devmajor */
327         sprintf(&header[337], "%07o", 0);       /* devminor */
328
329         memset(&header[148], ' ', 8);
330         for (i = 0; i < RECORDSIZE; i++)
331                 checksum += header[i];
332         sprintf(&header[148], "%07o", checksum & 0x1fffff);
333
334         write_if_needed();
335 }
336
337 static void traverse_tree(void *buffer, unsigned long size,
338                           struct path_prefix *prefix)
339 {
340         struct path_prefix this_prefix;
341         this_prefix.prev = prefix;
342
343         while (size) {
344                 int namelen = strlen(buffer)+1;
345                 void *eltbuf;
346                 char elttype[20];
347                 unsigned long eltsize;
348                 unsigned char *sha1 = buffer + namelen;
349                 char *path = strchr(buffer, ' ') + 1;
350                 unsigned int mode;
351
352                 if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
353                         die("corrupt 'tree' file");
354                 if (S_ISDIR(mode) || S_ISREG(mode))
355                         mode |= (mode & 0100) ? 0777 : 0666;
356                 buffer = sha1 + 20;
357                 size -= namelen + 20;
358
359                 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
360                 if (!eltbuf)
361                         die("cannot read %s", sha1_to_hex(sha1));
362                 write_header(sha1, TYPEFLAG_AUTO, basedir, prefix, path,
363                              mode, eltbuf, eltsize);
364                 if (!strcmp(elttype, "tree")) {
365                         this_prefix.name = path;
366                         traverse_tree(eltbuf, eltsize, &this_prefix);
367                 } else if (!strcmp(elttype, "blob") && !S_ISLNK(mode)) {
368                         write_blocked(eltbuf, eltsize);
369                 }
370                 free(eltbuf);
371         }
372 }
373
374 /* get commit time from committer line of commit object */
375 static time_t commit_time(void * buffer, unsigned long size)
376 {
377         time_t result = 0;
378         char *p = buffer;
379
380         while (size > 0) {
381                 char *endp = memchr(p, '\n', size);
382                 if (!endp || endp == p)
383                         break;
384                 *endp = '\0';
385                 if (endp - p > 10 && !memcmp(p, "committer ", 10)) {
386                         char *nump = strrchr(p, '>');
387                         if (!nump)
388                                 break;
389                         nump++;
390                         result = strtoul(nump, &endp, 10);
391                         if (*endp != ' ')
392                                 result = 0;
393                         break;
394                 }
395                 size -= endp - p - 1;
396                 p = endp + 1;
397         }
398         return result;
399 }
400
401 int main(int argc, char **argv)
402 {
403         unsigned char sha1[20];
404         unsigned char commit_sha1[20];
405         void *buffer;
406         unsigned long size;
407
408         setup_git_directory();
409
410         switch (argc) {
411         case 3:
412                 basedir = argv[2];
413                 /* FALLTHROUGH */
414         case 2:
415                 if (get_sha1(argv[1], sha1) < 0)
416                         usage(tar_tree_usage);
417                 break;
418         default:
419                 usage(tar_tree_usage);
420         }
421
422         buffer = read_object_with_reference(sha1, "commit", &size, commit_sha1);
423         if (buffer) {
424                 write_global_extended_header(commit_sha1);
425                 archive_time = commit_time(buffer, size);
426                 free(buffer);
427         }
428         buffer = read_object_with_reference(sha1, "tree", &size, NULL);
429         if (!buffer)
430                 die("not a reference to a tag, commit or tree object: %s",
431                     sha1_to_hex(sha1));
432         if (!archive_time)
433                 archive_time = time(NULL);
434         if (basedir)
435                 write_header((unsigned char *)"0", TYPEFLAG_DIR, NULL, NULL,
436                         basedir, 040755, NULL, 0);
437         traverse_tree(buffer, size, NULL);
438         free(buffer);
439         write_trailer();
440         return 0;
441 }