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