]> git.ipfire.org Git - thirdparty/git.git/blame - tar-tree.c
assume unchanged git: diff-index fix.
[thirdparty/git.git] / tar-tree.c
CommitLineData
92747a90 1/*
ae64bbc1 2 * Copyright (c) 2005, 2006 Rene Scharfe
92747a90 3 */
731ab9cc
RS
4#include <time.h>
5#include "cache.h"
7ec57556 6#include "diff.h"
c3f92812 7#include "commit.h"
ae64bbc1
RS
8#include "strbuf.h"
9#include "tar.h"
731ab9cc
RS
10
11#define RECORDSIZE (512)
12#define BLOCKSIZE (RECORDSIZE * 20)
13
4d1f1190 14static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
731ab9cc
RS
15
16static char block[BLOCKSIZE];
17static unsigned long offset;
18
731ab9cc
RS
19static time_t archive_time;
20
731ab9cc
RS
21/* tries hard to write, either succeeds or dies in the attempt */
22static void reliable_write(void *buf, unsigned long size)
23{
24 while (size > 0) {
1c15afb9 25 long ret = xwrite(1, buf, size);
731ab9cc 26 if (ret < 0) {
731ab9cc
RS
27 if (errno == EPIPE)
28 exit(0);
667bb59b 29 die("git-tar-tree: %s", strerror(errno));
731ab9cc 30 } else if (!ret) {
667bb59b 31 die("git-tar-tree: disk full?");
731ab9cc
RS
32 }
33 size -= ret;
34 buf += ret;
35 }
36}
37
38/* writes out the whole block, but only if it is full */
39static void write_if_needed(void)
40{
41 if (offset == BLOCKSIZE) {
42 reliable_write(block, BLOCKSIZE);
43 offset = 0;
44 }
45}
46
a90a6e6a
RS
47/* acquire the next record from the buffer; user must call write_if_needed() */
48static char *get_record(void)
49{
50 char *p = block + offset;
51 memset(p, 0, RECORDSIZE);
52 offset += RECORDSIZE;
53 return p;
54}
55
731ab9cc
RS
56/*
57 * The end of tar archives is marked by 1024 nul bytes and after that
58 * follows the rest of the block (if any).
59 */
60static void write_trailer(void)
61{
9b5b9f39 62 get_record();
731ab9cc 63 write_if_needed();
9b5b9f39 64 get_record();
731ab9cc 65 write_if_needed();
a325a11b 66 while (offset) {
9b5b9f39
RS
67 get_record();
68 write_if_needed();
731ab9cc
RS
69 }
70}
71
72/*
73 * queues up writes, so that all our write(2) calls write exactly one
74 * full block; pads writes to RECORDSIZE
75 */
76static void write_blocked(void *buf, unsigned long size)
77{
78 unsigned long tail;
79
80 if (offset) {
81 unsigned long chunk = BLOCKSIZE - offset;
82 if (size < chunk)
83 chunk = size;
84 memcpy(block + offset, buf, chunk);
85 size -= chunk;
86 offset += chunk;
87 buf += chunk;
88 write_if_needed();
89 }
90 while (size >= BLOCKSIZE) {
91 reliable_write(buf, BLOCKSIZE);
92 size -= BLOCKSIZE;
93 buf += BLOCKSIZE;
94 }
95 if (size) {
96 memcpy(block + offset, buf, size);
97 buf += size;
98 offset += size;
99 }
100 tail = offset % RECORDSIZE;
101 if (tail) {
102 memset(block + offset, 0, RECORDSIZE - tail);
103 offset += RECORDSIZE - tail;
104 }
105 write_if_needed();
106}
107
cb0c6df6 108static void strbuf_append_string(struct strbuf *sb, const char *s)
731ab9cc 109{
cb0c6df6
RS
110 int slen = strlen(s);
111 int total = sb->len + slen;
112 if (total > sb->alloc) {
113 sb->buf = xrealloc(sb->buf, total);
114 sb->alloc = total;
731ab9cc 115 }
cb0c6df6
RS
116 memcpy(sb->buf + sb->len, s, slen);
117 sb->len = total;
731ab9cc
RS
118}
119
ae64bbc1
RS
120/*
121 * pax extended header records have the format "%u %s=%s\n". %u contains
122 * the size of the whole string (including the %u), the first %s is the
123 * keyword, the second one is the value. This function constructs such a
124 * string and appends it to a struct strbuf.
125 */
126static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
127 const char *value, unsigned int valuelen)
71058b1f 128{
ae64bbc1
RS
129 char *p;
130 int len, total, tmp;
71058b1f 131
71058b1f 132 /* "%u %s=%s\n" */
ae64bbc1
RS
133 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
134 for (tmp = len; tmp > 9; tmp /= 10)
71058b1f 135 len++;
71058b1f 136
ae64bbc1
RS
137 total = sb->len + len;
138 if (total > sb->alloc) {
139 sb->buf = xrealloc(sb->buf, total);
140 sb->alloc = total;
141 }
71058b1f 142
ae64bbc1
RS
143 p = sb->buf;
144 p += sprintf(p, "%u %s=", len, keyword);
145 memcpy(p, value, valuelen);
146 p += valuelen;
147 *p = '\n';
148 sb->len = total;
149}
731ab9cc 150
ae64bbc1 151static unsigned int ustar_header_chksum(const struct ustar_header *header)
731ab9cc 152{
ae64bbc1
RS
153 char *p = (char *)header;
154 unsigned int chksum = 0;
155 while (p < header->chksum)
156 chksum += *p++;
157 chksum += sizeof(header->chksum) * ' ';
158 p += sizeof(header->chksum);
159 while (p < (char *)header + sizeof(struct ustar_header))
160 chksum += *p++;
161 return chksum;
731ab9cc
RS
162}
163
4c691724 164static int get_path_prefix(const struct strbuf *path, int maxlen)
87fec8fc 165{
4c691724
RS
166 int i = path->len;
167 if (i > maxlen)
168 i = maxlen;
169 while (i > 0 && path->buf[i] != '/')
170 i--;
171 return i;
87fec8fc
RS
172}
173
ae64bbc1
RS
174static void write_entry(const unsigned char *sha1, struct strbuf *path,
175 unsigned int mode, void *buffer, unsigned long size)
731ab9cc 176{
ae64bbc1
RS
177 struct ustar_header header;
178 struct strbuf ext_header;
179
180 memset(&header, 0, sizeof(header));
181 ext_header.buf = NULL;
182 ext_header.len = ext_header.alloc = 0;
183
184 if (!sha1) {
185 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
186 mode = 0100666;
187 strcpy(header.name, "pax_global_header");
188 } else if (!path) {
189 *header.typeflag = TYPEFLAG_EXT_HEADER;
190 mode = 0100666;
191 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
731ab9cc 192 } else {
ae64bbc1
RS
193 if (S_ISDIR(mode)) {
194 *header.typeflag = TYPEFLAG_DIR;
195 mode |= 0777;
196 } else if (S_ISLNK(mode)) {
197 *header.typeflag = TYPEFLAG_LNK;
198 mode |= 0777;
199 } else if (S_ISREG(mode)) {
200 *header.typeflag = TYPEFLAG_REG;
201 mode |= (mode & 0100) ? 0777 : 0666;
202 } else {
203 error("unsupported file mode: 0%o (SHA1: %s)",
204 mode, sha1_to_hex(sha1));
205 return;
206 }
207 if (path->len > sizeof(header.name)) {
4c691724
RS
208 int plen = get_path_prefix(path, sizeof(header.prefix));
209 int rest = path->len - plen - 1;
210 if (plen > 0 && rest <= sizeof(header.name)) {
211 memcpy(header.prefix, path->buf, plen);
212 memcpy(header.name, path->buf + plen + 1, rest);
213 } else {
214 sprintf(header.name, "%s.data",
215 sha1_to_hex(sha1));
216 strbuf_append_ext_header(&ext_header, "path",
217 path->buf, path->len);
218 }
ae64bbc1
RS
219 } else
220 memcpy(header.name, path->buf, path->len);
731ab9cc
RS
221 }
222
ae64bbc1
RS
223 if (S_ISLNK(mode) && buffer) {
224 if (size > sizeof(header.linkname)) {
225 sprintf(header.linkname, "see %s.paxheader",
d5776d50 226 sha1_to_hex(sha1));
ae64bbc1
RS
227 strbuf_append_ext_header(&ext_header, "linkpath",
228 buffer, size);
229 } else
230 memcpy(header.linkname, buffer, size);
d5776d50
RS
231 }
232
ae64bbc1
RS
233 sprintf(header.mode, "%07o", mode & 07777);
234 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
235 sprintf(header.mtime, "%011lo", archive_time);
731ab9cc
RS
236
237 /* XXX: should we provide more meaningful info here? */
ae64bbc1
RS
238 sprintf(header.uid, "%07o", 0);
239 sprintf(header.gid, "%07o", 0);
240 strncpy(header.uname, "git", 31);
241 strncpy(header.gname, "git", 31);
242 sprintf(header.devmajor, "%07o", 0);
243 sprintf(header.devminor, "%07o", 0);
731ab9cc 244
ae64bbc1
RS
245 memcpy(header.magic, "ustar", 6);
246 memcpy(header.version, "00", 2);
731ab9cc 247
ae64bbc1 248 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
731ab9cc 249
ae64bbc1
RS
250 if (ext_header.len > 0) {
251 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
252 free(ext_header.buf);
253 }
254 write_blocked(&header, sizeof(header));
255 if (S_ISREG(mode) && buffer && size > 0)
256 write_blocked(buffer, size);
257}
731ab9cc 258
bf0f910d 259static void write_global_extended_header(const unsigned char *sha1)
87fec8fc 260{
ae64bbc1
RS
261 struct strbuf ext_header;
262 ext_header.buf = NULL;
263 ext_header.len = ext_header.alloc = 0;
264 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
265 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
266 free(ext_header.buf);
731ab9cc
RS
267}
268
cb0c6df6 269static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
731ab9cc 270{
cb0c6df6 271 int pathlen = path->len;
731ab9cc 272
7ec57556
LT
273 while (tree->size) {
274 const char *name;
275 const unsigned char *sha1;
276 unsigned mode;
731ab9cc
RS
277 void *eltbuf;
278 char elttype[20];
279 unsigned long eltsize;
5207234a 280
7ec57556
LT
281 sha1 = tree_entry_extract(tree, &name, &mode);
282 update_tree_entry(tree);
283
284 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
731ab9cc 285 if (!eltbuf)
7ec57556 286 die("cannot read %s", sha1_to_hex(sha1));
cb0c6df6
RS
287
288 path->len = pathlen;
289 strbuf_append_string(path, name);
290 if (S_ISDIR(mode))
291 strbuf_append_string(path, "/");
292
293 write_entry(sha1, path, mode, eltbuf, eltsize);
294
7ec57556
LT
295 if (S_ISDIR(mode)) {
296 struct tree_desc subtree;
297 subtree.buf = eltbuf;
298 subtree.size = eltsize;
cb0c6df6 299 traverse_tree(&subtree, path);
731ab9cc
RS
300 }
301 free(eltbuf);
302 }
303}
304
731ab9cc
RS
305int main(int argc, char **argv)
306{
2c6df2d5 307 unsigned char sha1[20], tree_sha1[20];
c3f92812 308 struct commit *commit;
7ec57556 309 struct tree_desc tree;
cb0c6df6
RS
310 struct strbuf current_path;
311
312 current_path.buf = xmalloc(PATH_MAX);
313 current_path.alloc = PATH_MAX;
314 current_path.len = current_path.eof = 0;
731ab9cc 315
53228a5f 316 setup_git_directory();
84a9b58c 317 git_config(git_default_config);
53228a5f 318
731ab9cc
RS
319 switch (argc) {
320 case 3:
cb0c6df6
RS
321 strbuf_append_string(&current_path, argv[2]);
322 strbuf_append_string(&current_path, "/");
731ab9cc
RS
323 /* FALLTHROUGH */
324 case 2:
3c249c95 325 if (get_sha1(argv[1], sha1) < 0)
731ab9cc
RS
326 usage(tar_tree_usage);
327 break;
328 default:
329 usage(tar_tree_usage);
330 }
331
473d404b 332 commit = lookup_commit_reference_gently(sha1, 1);
c3f92812
DB
333 if (commit) {
334 write_global_extended_header(commit->object.sha1);
335 archive_time = commit->date;
cb0c6df6
RS
336 } else
337 archive_time = time(NULL);
338
2c6df2d5
RS
339 tree.buf = read_object_with_reference(sha1, "tree", &tree.size,
340 tree_sha1);
7ec57556 341 if (!tree.buf)
db413479
RS
342 die("not a reference to a tag, commit or tree object: %s",
343 sha1_to_hex(sha1));
cb0c6df6
RS
344
345 if (current_path.len > 0)
346 write_entry(tree_sha1, &current_path, 040777, NULL, 0);
347 traverse_tree(&tree, &current_path);
731ab9cc 348 write_trailer();
cb0c6df6 349 free(current_path.buf);
731ab9cc
RS
350 return 0;
351}