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