When the patch tries to create a new file and the file exists, abort.
[alt-git.git] / tar-tree.c
blob2c64f690e52093af13585521de4df478ac6e85e5
1 #include <time.h>
2 #include "cache.h"
4 #define RECORDSIZE (512)
5 #define BLOCKSIZE (RECORDSIZE * 20)
7 static const char *tar_tree_usage = "tar-tree <key> [basedir]";
9 static char block[BLOCKSIZE];
10 static unsigned long offset;
12 static const char *basedir;
13 static time_t archive_time;
15 struct path_prefix {
16 struct path_prefix *prev;
17 const char *name;
20 /* tries hard to write, either succeeds or dies in the attempt */
21 static void reliable_write(void *buf, unsigned long size)
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?");
34 size -= ret;
35 buf += ret;
39 /* writes out the whole block, but only if it is full */
40 static void write_if_needed(void)
42 if (offset == BLOCKSIZE) {
43 reliable_write(block, BLOCKSIZE);
44 offset = 0;
49 * The end of tar archives is marked by 1024 nul bytes and after that
50 * follows the rest of the block (if any).
52 static void write_trailer(void)
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;
68 * queues up writes, so that all our write(2) calls write exactly one
69 * full block; pads writes to RECORDSIZE
71 static void write_blocked(void *buf, unsigned long size)
73 unsigned long tail;
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();
85 while (size >= BLOCKSIZE) {
86 reliable_write(buf, BLOCKSIZE);
87 size -= BLOCKSIZE;
88 buf += BLOCKSIZE;
90 if (size) {
91 memcpy(block + offset, buf, size);
92 buf += size;
93 offset += size;
95 tail = offset % RECORDSIZE;
96 if (tail) {
97 memset(block + offset, 0, RECORDSIZE - tail);
98 offset += RECORDSIZE - tail;
100 write_if_needed();
103 static void append_string(char **p, const char *s)
105 unsigned int len = strlen(s);
106 memcpy(*p, s, len);
107 *p += len;
110 static void append_char(char **p, char c)
112 **p = c;
113 *p += 1;
116 static void append_long(char **p, long n)
118 int len = sprintf(*p, "%ld", n);
119 *p += len;
122 static void append_path_prefix(char **buffer, struct path_prefix *prefix)
124 if (!prefix)
125 return;
126 append_path_prefix(buffer, prefix->prev);
127 append_string(buffer, prefix->name);
128 append_char(buffer, '/');
131 static unsigned int path_prefix_len(struct path_prefix *prefix)
133 if (!prefix)
134 return 0;
135 return path_prefix_len(prefix->prev) + strlen(prefix->name) + 1;
138 static void append_path(char **p, int is_dir, const char *basepath,
139 struct path_prefix *prefix, const char *path)
141 if (basepath) {
142 append_string(p, basepath);
143 append_char(p, '/');
145 append_path_prefix(p, prefix);
146 append_string(p, path);
147 if (is_dir)
148 append_char(p, '/');
151 static unsigned int path_len(int is_dir, const char *basepath,
152 struct path_prefix *prefix, const char *path)
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;
163 static void write_header(const char *, char, const char *, struct path_prefix *,
164 const char *, unsigned int, unsigned long);
166 /* stores a pax extended header directly in the block buffer */
167 static 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)
172 char *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, 'x', NULL, NULL, headerfilename, 0100600, size);
181 p = block + offset;
182 memset(p, 0, RECORDSIZE);
183 offset += RECORDSIZE;
184 append_long(&p, size);
185 append_string(&p, " path=");
186 append_path(&p, is_dir, basepath, prefix, path);
187 append_char(&p, '\n');
188 write_if_needed();
191 static void write_global_extended_header(const char *sha1)
193 char *p;
194 write_header(NULL, 'g', NULL, NULL, "pax_global_header", 0, 52);
195 p = block + offset;
196 memset(p, 0, RECORDSIZE);
197 offset += RECORDSIZE;
198 append_long(&p, 52); /* 2 + 9 + 40 + 1 */
199 append_string(&p, " comment=");
200 append_string(&p, sha1_to_hex(sha1));
201 append_char(&p, '\n');
202 write_if_needed();
205 /* stores a ustar header directly in the block buffer */
206 static void write_header(const char *sha1, char typeflag, const char *basepath,
207 struct path_prefix *prefix, const char *path,
208 unsigned int mode, unsigned long size)
210 unsigned int namelen;
211 char *p, *header = NULL;
212 unsigned int checksum = 0;
213 int i;
215 namelen = path_len(S_ISDIR(mode), basepath, prefix, path);
216 if (namelen > 500) {
217 die("tar-tree: name too log of object %s\n", sha1_to_hex(sha1));
218 } else if (namelen > 100) {
219 char *sha1_hex = sha1_to_hex(sha1);
220 char headerfilename[51];
221 sprintf(headerfilename, "%s.paxheader", sha1_hex);
222 /* the extended header must be written before the normal one */
223 write_extended_header(headerfilename, S_ISDIR(mode), basepath,
224 prefix, path, namelen);
226 header = block + offset;
227 memset(header, 0, RECORDSIZE);
228 offset += RECORDSIZE;
229 sprintf(header, "%s.data", sha1_hex);
230 } else {
231 header = block + offset;
232 memset(header, 0, RECORDSIZE);
233 offset += RECORDSIZE;
234 p = header;
235 append_path(&p, S_ISDIR(mode), basepath, prefix, path);
238 if (S_ISDIR(mode))
239 mode |= 0755; /* GIT doesn't store permissions of dirs */
240 sprintf(&header[100], "%07o", mode & 07777);
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 */
248 sprintf(&header[124], "%011lo", S_ISDIR(mode) ? 0 : size);
249 sprintf(&header[136], "%011lo", archive_time);
251 header[156] = typeflag;
253 memcpy(&header[257], "ustar", 6);
254 memcpy(&header[263], "00", 2);
256 printf(&header[329], "%07o", 0); /* devmajor */
257 printf(&header[337], "%07o", 0); /* devminor */
259 memset(&header[148], ' ', 8);
260 for (i = 0; i < RECORDSIZE; i++)
261 checksum += header[i];
262 sprintf(&header[148], "%07o", checksum & 0x1fffff);
264 write_if_needed();
267 static void traverse_tree(void *buffer, unsigned long size,
268 struct path_prefix *prefix)
270 struct path_prefix this_prefix;
271 this_prefix.prev = prefix;
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;
282 if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
283 die("corrupt 'tree' file");
284 buffer = sha1 + 20;
285 size -= namelen + 20;
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);
298 free(eltbuf);
302 /* get commit time from committer line of commit object */
303 time_t commit_time(void * buffer, unsigned long size)
305 time_t result = 0;
306 char *p = buffer;
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;
323 size -= endp - p - 1;
324 p = endp + 1;
326 return result;
329 int main(int argc, char **argv)
331 unsigned char sha1[20];
332 unsigned char commit_sha1[20];
333 void *buffer;
334 unsigned long size;
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);
348 sha1_file_directory = getenv(DB_ENVIRONMENT);
349 if (!sha1_file_directory)
350 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
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);
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;