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