1/*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4#include <time.h>
5#include "cache.h"
6#include "tree-walk.h"
7#include "commit.h"
8#include "strbuf.h"
9#include "tar.h"
10
11#define RECORDSIZE (512)
12#define BLOCKSIZE (RECORDSIZE * 20)
13
14static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
15
16static char block[BLOCKSIZE];
17static unsigned long offset;
18
19static time_t archive_time;
20
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) {
25 long ret = xwrite(1, buf, size);
26 if (ret < 0) {
27 if (errno == EPIPE)
28 exit(0);
29 die("git-tar-tree: %s", strerror(errno));
30 } else if (!ret) {
31 die("git-tar-tree: disk full?");
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
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
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{
62 get_record();
63 write_if_needed();
64 get_record();
65 write_if_needed();
66 while (offset) {
67 get_record();
68 write_if_needed();
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 offset += size;
98 }
99 tail = offset % RECORDSIZE;
100 if (tail) {
101 memset(block + offset, 0, RECORDSIZE - tail);
102 offset += RECORDSIZE - tail;
103 }
104 write_if_needed();
105}
106
107static void strbuf_append_string(struct strbuf *sb, const char *s)
108{
109 int slen = strlen(s);
110 int total = sb->len + slen;
111 if (total > sb->alloc) {
112 sb->buf = xrealloc(sb->buf, total);
113 sb->alloc = total;
114 }
115 memcpy(sb->buf + sb->len, s, slen);
116 sb->len = total;
117}
118
119/*
120 * pax extended header records have the format "%u %s=%s\n". %u contains
121 * the size of the whole string (including the %u), the first %s is the
122 * keyword, the second one is the value. This function constructs such a
123 * string and appends it to a struct strbuf.
124 */
125static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
126 const char *value, unsigned int valuelen)
127{
128 char *p;
129 int len, total, tmp;
130
131 /* "%u %s=%s\n" */
132 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
133 for (tmp = len; tmp > 9; tmp /= 10)
134 len++;
135
136 total = sb->len + len;
137 if (total > sb->alloc) {
138 sb->buf = xrealloc(sb->buf, total);
139 sb->alloc = total;
140 }
141
142 p = sb->buf;
143 p += sprintf(p, "%u %s=", len, keyword);
144 memcpy(p, value, valuelen);
145 p += valuelen;
146 *p = '\n';
147 sb->len = total;
148}
149
150static unsigned int ustar_header_chksum(const struct ustar_header *header)
151{
152 char *p = (char *)header;
153 unsigned int chksum = 0;
154 while (p < header->chksum)
155 chksum += *p++;
156 chksum += sizeof(header->chksum) * ' ';
157 p += sizeof(header->chksum);
158 while (p < (char *)header + sizeof(struct ustar_header))
159 chksum += *p++;
160 return chksum;
161}
162
163static int get_path_prefix(const struct strbuf *path, int maxlen)
164{
165 int i = path->len;
166 if (i > maxlen)
167 i = maxlen;
168 while (i > 0 && path->buf[i] != '/')
169 i--;
170 return i;
171}
172
173static void write_entry(const unsigned char *sha1, struct strbuf *path,
174 unsigned int mode, void *buffer, unsigned long size)
175{
176 struct ustar_header header;
177 struct strbuf ext_header;
178
179 memset(&header, 0, sizeof(header));
180 ext_header.buf = NULL;
181 ext_header.len = ext_header.alloc = 0;
182
183 if (!sha1) {
184 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
185 mode = 0100666;
186 strcpy(header.name, "pax_global_header");
187 } else if (!path) {
188 *header.typeflag = TYPEFLAG_EXT_HEADER;
189 mode = 0100666;
190 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
191 } else {
192 if (S_ISDIR(mode)) {
193 *header.typeflag = TYPEFLAG_DIR;
194 mode |= 0777;
195 } else if (S_ISLNK(mode)) {
196 *header.typeflag = TYPEFLAG_LNK;
197 mode |= 0777;
198 } else if (S_ISREG(mode)) {
199 *header.typeflag = TYPEFLAG_REG;
200 mode |= (mode & 0100) ? 0777 : 0666;
201 } else {
202 error("unsupported file mode: 0%o (SHA1: %s)",
203 mode, sha1_to_hex(sha1));
204 return;
205 }
206 if (path->len > sizeof(header.name)) {
207 int plen = get_path_prefix(path, sizeof(header.prefix));
208 int rest = path->len - plen - 1;
209 if (plen > 0 && rest <= sizeof(header.name)) {
210 memcpy(header.prefix, path->buf, plen);
211 memcpy(header.name, path->buf + plen + 1, rest);
212 } else {
213 sprintf(header.name, "%s.data",
214 sha1_to_hex(sha1));
215 strbuf_append_ext_header(&ext_header, "path",
216 path->buf, path->len);
217 }
218 } else
219 memcpy(header.name, path->buf, path->len);
220 }
221
222 if (S_ISLNK(mode) && buffer) {
223 if (size > sizeof(header.linkname)) {
224 sprintf(header.linkname, "see %s.paxheader",
225 sha1_to_hex(sha1));
226 strbuf_append_ext_header(&ext_header, "linkpath",
227 buffer, size);
228 } else
229 memcpy(header.linkname, buffer, size);
230 }
231
232 sprintf(header.mode, "%07o", mode & 07777);
233 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
234 sprintf(header.mtime, "%011lo", archive_time);
235
236 /* XXX: should we provide more meaningful info here? */
237 sprintf(header.uid, "%07o", 0);
238 sprintf(header.gid, "%07o", 0);
239 strncpy(header.uname, "git", 31);
240 strncpy(header.gname, "git", 31);
241 sprintf(header.devmajor, "%07o", 0);
242 sprintf(header.devminor, "%07o", 0);
243
244 memcpy(header.magic, "ustar", 6);
245 memcpy(header.version, "00", 2);
246
247 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
248
249 if (ext_header.len > 0) {
250 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
251 free(ext_header.buf);
252 }
253 write_blocked(&header, sizeof(header));
254 if (S_ISREG(mode) && buffer && size > 0)
255 write_blocked(buffer, size);
256}
257
258static void write_global_extended_header(const unsigned char *sha1)
259{
260 struct strbuf ext_header;
261 ext_header.buf = NULL;
262 ext_header.len = ext_header.alloc = 0;
263 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
264 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
265 free(ext_header.buf);
266}
267
268static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
269{
270 int pathlen = path->len;
271
272 while (tree->size) {
273 const char *name;
274 const unsigned char *sha1;
275 unsigned mode;
276 void *eltbuf;
277 char elttype[20];
278 unsigned long eltsize;
279
280 sha1 = tree_entry_extract(tree, &name, &mode);
281 update_tree_entry(tree);
282
283 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
284 if (!eltbuf)
285 die("cannot read %s", sha1_to_hex(sha1));
286
287 path->len = pathlen;
288 strbuf_append_string(path, name);
289 if (S_ISDIR(mode))
290 strbuf_append_string(path, "/");
291
292 write_entry(sha1, path, mode, eltbuf, eltsize);
293
294 if (S_ISDIR(mode)) {
295 struct tree_desc subtree;
296 subtree.buf = eltbuf;
297 subtree.size = eltsize;
298 traverse_tree(&subtree, path);
299 }
300 free(eltbuf);
301 }
302}
303
304int main(int argc, char **argv)
305{
306 unsigned char sha1[20], tree_sha1[20];
307 struct commit *commit;
308 struct tree_desc tree;
309 struct strbuf current_path;
310
311 current_path.buf = xmalloc(PATH_MAX);
312 current_path.alloc = PATH_MAX;
313 current_path.len = current_path.eof = 0;
314
315 setup_git_directory();
316 git_config(git_default_config);
317
318 switch (argc) {
319 case 3:
320 strbuf_append_string(¤t_path, argv[2]);
321 strbuf_append_string(¤t_path, "/");
322 /* FALLTHROUGH */
323 case 2:
324 if (get_sha1(argv[1], sha1) < 0)
325 usage(tar_tree_usage);
326 break;
327 default:
328 usage(tar_tree_usage);
329 }
330
331 commit = lookup_commit_reference_gently(sha1, 1);
332 if (commit) {
333 write_global_extended_header(commit->object.sha1);
334 archive_time = commit->date;
335 } else
336 archive_time = time(NULL);
337
338 tree.buf = read_object_with_reference(sha1, tree_type, &tree.size,
339 tree_sha1);
340 if (!tree.buf)
341 die("not a reference to a tag, commit or tree object: %s",
342 sha1_to_hex(sha1));
343
344 if (current_path.len > 0)
345 write_entry(tree_sha1, ¤t_path, 040777, NULL, 0);
346 traverse_tree(&tree, ¤t_path);
347 write_trailer();
348 free(current_path.buf);
349 return 0;
350}