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