]>
Commit | Line | Data |
---|---|---|
92747a90 | 1 | /* |
ae64bbc1 | 2 | * Copyright (c) 2005, 2006 Rene Scharfe |
92747a90 | 3 | */ |
731ab9cc RS |
4 | #include <time.h> |
5 | #include "cache.h" | |
1b0c7174 | 6 | #include "tree-walk.h" |
c3f92812 | 7 | #include "commit.h" |
ae64bbc1 RS |
8 | #include "strbuf.h" |
9 | #include "tar.h" | |
56d1398a | 10 | #include "builtin.h" |
21754264 | 11 | #include "pkt-line.h" |
731ab9cc RS |
12 | |
13 | #define RECORDSIZE (512) | |
14 | #define BLOCKSIZE (RECORDSIZE * 20) | |
15 | ||
21754264 JH |
16 | static const char tar_tree_usage[] = |
17 | "git-tar-tree [--remote=<repo>] <ent> [basedir]"; | |
731ab9cc RS |
18 | |
19 | static char block[BLOCKSIZE]; | |
20 | static unsigned long offset; | |
21 | ||
731ab9cc | 22 | static time_t archive_time; |
ce1a79b6 | 23 | static int tar_umask; |
731ab9cc | 24 | |
731ab9cc | 25 | /* tries hard to write, either succeeds or dies in the attempt */ |
6698060c | 26 | static void reliable_write(const void *data, unsigned long size) |
731ab9cc | 27 | { |
6698060c RS |
28 | const char *buf = data; |
29 | ||
731ab9cc | 30 | while (size > 0) { |
1c15afb9 | 31 | long ret = xwrite(1, buf, size); |
731ab9cc | 32 | if (ret < 0) { |
731ab9cc RS |
33 | if (errno == EPIPE) |
34 | exit(0); | |
667bb59b | 35 | die("git-tar-tree: %s", strerror(errno)); |
731ab9cc | 36 | } else if (!ret) { |
667bb59b | 37 | die("git-tar-tree: disk full?"); |
731ab9cc RS |
38 | } |
39 | size -= ret; | |
40 | buf += ret; | |
41 | } | |
42 | } | |
43 | ||
44 | /* writes out the whole block, but only if it is full */ | |
45 | static void write_if_needed(void) | |
46 | { | |
47 | if (offset == BLOCKSIZE) { | |
48 | reliable_write(block, BLOCKSIZE); | |
49 | offset = 0; | |
50 | } | |
51 | } | |
52 | ||
731ab9cc RS |
53 | /* |
54 | * queues up writes, so that all our write(2) calls write exactly one | |
55 | * full block; pads writes to RECORDSIZE | |
56 | */ | |
6698060c | 57 | static void write_blocked(const void *data, unsigned long size) |
731ab9cc | 58 | { |
6698060c | 59 | const char *buf = data; |
731ab9cc RS |
60 | unsigned long tail; |
61 | ||
62 | if (offset) { | |
63 | unsigned long chunk = BLOCKSIZE - offset; | |
64 | if (size < chunk) | |
65 | chunk = size; | |
66 | memcpy(block + offset, buf, chunk); | |
67 | size -= chunk; | |
68 | offset += chunk; | |
69 | buf += chunk; | |
70 | write_if_needed(); | |
71 | } | |
72 | while (size >= BLOCKSIZE) { | |
73 | reliable_write(buf, BLOCKSIZE); | |
74 | size -= BLOCKSIZE; | |
75 | buf += BLOCKSIZE; | |
76 | } | |
77 | if (size) { | |
78 | memcpy(block + offset, buf, size); | |
731ab9cc RS |
79 | offset += size; |
80 | } | |
81 | tail = offset % RECORDSIZE; | |
82 | if (tail) { | |
83 | memset(block + offset, 0, RECORDSIZE - tail); | |
84 | offset += RECORDSIZE - tail; | |
85 | } | |
86 | write_if_needed(); | |
87 | } | |
88 | ||
37958be7 RS |
89 | /* |
90 | * The end of tar archives is marked by 2*512 nul bytes and after that | |
91 | * follows the rest of the block (if any). | |
92 | */ | |
93 | static void write_trailer(void) | |
94 | { | |
95 | int tail = BLOCKSIZE - offset; | |
96 | memset(block + offset, 0, tail); | |
97 | reliable_write(block, BLOCKSIZE); | |
98 | if (tail < 2 * RECORDSIZE) { | |
99 | memset(block, 0, offset); | |
100 | reliable_write(block, BLOCKSIZE); | |
101 | } | |
102 | } | |
103 | ||
cb0c6df6 | 104 | static void strbuf_append_string(struct strbuf *sb, const char *s) |
731ab9cc | 105 | { |
cb0c6df6 RS |
106 | int slen = strlen(s); |
107 | int total = sb->len + slen; | |
108 | if (total > sb->alloc) { | |
109 | sb->buf = xrealloc(sb->buf, total); | |
110 | sb->alloc = total; | |
731ab9cc | 111 | } |
cb0c6df6 RS |
112 | memcpy(sb->buf + sb->len, s, slen); |
113 | sb->len = total; | |
731ab9cc RS |
114 | } |
115 | ||
ae64bbc1 RS |
116 | /* |
117 | * pax extended header records have the format "%u %s=%s\n". %u contains | |
118 | * the size of the whole string (including the %u), the first %s is the | |
119 | * keyword, the second one is the value. This function constructs such a | |
120 | * string and appends it to a struct strbuf. | |
121 | */ | |
122 | static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword, | |
123 | const char *value, unsigned int valuelen) | |
71058b1f | 124 | { |
ae64bbc1 RS |
125 | char *p; |
126 | int len, total, tmp; | |
71058b1f | 127 | |
71058b1f | 128 | /* "%u %s=%s\n" */ |
ae64bbc1 RS |
129 | len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1; |
130 | for (tmp = len; tmp > 9; tmp /= 10) | |
71058b1f | 131 | len++; |
71058b1f | 132 | |
ae64bbc1 RS |
133 | total = sb->len + len; |
134 | if (total > sb->alloc) { | |
135 | sb->buf = xrealloc(sb->buf, total); | |
136 | sb->alloc = total; | |
137 | } | |
71058b1f | 138 | |
ae64bbc1 RS |
139 | p = sb->buf; |
140 | p += sprintf(p, "%u %s=", len, keyword); | |
141 | memcpy(p, value, valuelen); | |
142 | p += valuelen; | |
143 | *p = '\n'; | |
144 | sb->len = total; | |
145 | } | |
731ab9cc | 146 | |
ae64bbc1 | 147 | static unsigned int ustar_header_chksum(const struct ustar_header *header) |
731ab9cc | 148 | { |
ae64bbc1 RS |
149 | char *p = (char *)header; |
150 | unsigned int chksum = 0; | |
151 | while (p < header->chksum) | |
152 | chksum += *p++; | |
153 | chksum += sizeof(header->chksum) * ' '; | |
154 | p += sizeof(header->chksum); | |
155 | while (p < (char *)header + sizeof(struct ustar_header)) | |
156 | chksum += *p++; | |
157 | return chksum; | |
731ab9cc RS |
158 | } |
159 | ||
4c691724 | 160 | static int get_path_prefix(const struct strbuf *path, int maxlen) |
87fec8fc | 161 | { |
4c691724 RS |
162 | int i = path->len; |
163 | if (i > maxlen) | |
164 | i = maxlen; | |
17cf250a | 165 | do { |
4c691724 | 166 | i--; |
17cf250a | 167 | } while (i > 0 && path->buf[i] != '/'); |
4c691724 | 168 | return i; |
87fec8fc RS |
169 | } |
170 | ||
ae64bbc1 RS |
171 | static void write_entry(const unsigned char *sha1, struct strbuf *path, |
172 | unsigned int mode, void *buffer, unsigned long size) | |
731ab9cc | 173 | { |
ae64bbc1 RS |
174 | struct ustar_header header; |
175 | struct strbuf ext_header; | |
176 | ||
177 | memset(&header, 0, sizeof(header)); | |
178 | ext_header.buf = NULL; | |
179 | ext_header.len = ext_header.alloc = 0; | |
180 | ||
181 | if (!sha1) { | |
182 | *header.typeflag = TYPEFLAG_GLOBAL_HEADER; | |
183 | mode = 0100666; | |
184 | strcpy(header.name, "pax_global_header"); | |
185 | } else if (!path) { | |
186 | *header.typeflag = TYPEFLAG_EXT_HEADER; | |
187 | mode = 0100666; | |
188 | sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1)); | |
731ab9cc | 189 | } else { |
ae64bbc1 RS |
190 | if (S_ISDIR(mode)) { |
191 | *header.typeflag = TYPEFLAG_DIR; | |
ce1a79b6 | 192 | mode = (mode | 0777) & ~tar_umask; |
ae64bbc1 RS |
193 | } else if (S_ISLNK(mode)) { |
194 | *header.typeflag = TYPEFLAG_LNK; | |
195 | mode |= 0777; | |
196 | } else if (S_ISREG(mode)) { | |
197 | *header.typeflag = TYPEFLAG_REG; | |
ce1a79b6 | 198 | mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask; |
ae64bbc1 RS |
199 | } else { |
200 | error("unsupported file mode: 0%o (SHA1: %s)", | |
201 | mode, sha1_to_hex(sha1)); | |
202 | return; | |
203 | } | |
204 | if (path->len > sizeof(header.name)) { | |
4c691724 RS |
205 | int plen = get_path_prefix(path, sizeof(header.prefix)); |
206 | int rest = path->len - plen - 1; | |
207 | if (plen > 0 && rest <= sizeof(header.name)) { | |
208 | memcpy(header.prefix, path->buf, plen); | |
209 | memcpy(header.name, path->buf + plen + 1, rest); | |
210 | } else { | |
211 | sprintf(header.name, "%s.data", | |
212 | sha1_to_hex(sha1)); | |
213 | strbuf_append_ext_header(&ext_header, "path", | |
214 | path->buf, path->len); | |
215 | } | |
ae64bbc1 RS |
216 | } else |
217 | memcpy(header.name, path->buf, path->len); | |
731ab9cc RS |
218 | } |
219 | ||
ae64bbc1 RS |
220 | if (S_ISLNK(mode) && buffer) { |
221 | if (size > sizeof(header.linkname)) { | |
222 | sprintf(header.linkname, "see %s.paxheader", | |
d5776d50 | 223 | sha1_to_hex(sha1)); |
ae64bbc1 RS |
224 | strbuf_append_ext_header(&ext_header, "linkpath", |
225 | buffer, size); | |
226 | } else | |
227 | memcpy(header.linkname, buffer, size); | |
d5776d50 RS |
228 | } |
229 | ||
ae64bbc1 RS |
230 | sprintf(header.mode, "%07o", mode & 07777); |
231 | sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0); | |
232 | sprintf(header.mtime, "%011lo", archive_time); | |
731ab9cc RS |
233 | |
234 | /* XXX: should we provide more meaningful info here? */ | |
ae64bbc1 RS |
235 | sprintf(header.uid, "%07o", 0); |
236 | sprintf(header.gid, "%07o", 0); | |
817151e6 PE |
237 | strlcpy(header.uname, "git", sizeof(header.uname)); |
238 | strlcpy(header.gname, "git", sizeof(header.gname)); | |
ae64bbc1 RS |
239 | sprintf(header.devmajor, "%07o", 0); |
240 | sprintf(header.devminor, "%07o", 0); | |
731ab9cc | 241 | |
ae64bbc1 RS |
242 | memcpy(header.magic, "ustar", 6); |
243 | memcpy(header.version, "00", 2); | |
731ab9cc | 244 | |
ae64bbc1 | 245 | sprintf(header.chksum, "%07o", ustar_header_chksum(&header)); |
731ab9cc | 246 | |
ae64bbc1 RS |
247 | if (ext_header.len > 0) { |
248 | write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len); | |
249 | free(ext_header.buf); | |
250 | } | |
251 | write_blocked(&header, sizeof(header)); | |
252 | if (S_ISREG(mode) && buffer && size > 0) | |
253 | write_blocked(buffer, size); | |
254 | } | |
731ab9cc | 255 | |
bf0f910d | 256 | static void write_global_extended_header(const unsigned char *sha1) |
87fec8fc | 257 | { |
ae64bbc1 RS |
258 | struct strbuf ext_header; |
259 | ext_header.buf = NULL; | |
260 | ext_header.len = ext_header.alloc = 0; | |
261 | strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40); | |
262 | write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len); | |
263 | free(ext_header.buf); | |
731ab9cc RS |
264 | } |
265 | ||
cb0c6df6 | 266 | static void traverse_tree(struct tree_desc *tree, struct strbuf *path) |
731ab9cc | 267 | { |
cb0c6df6 | 268 | int pathlen = path->len; |
4c068a98 | 269 | struct name_entry entry; |
731ab9cc | 270 | |
4c068a98 | 271 | while (tree_entry(tree, &entry)) { |
731ab9cc RS |
272 | void *eltbuf; |
273 | char elttype[20]; | |
274 | unsigned long eltsize; | |
5207234a | 275 | |
4c068a98 | 276 | eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize); |
731ab9cc | 277 | if (!eltbuf) |
4c068a98 | 278 | die("cannot read %s", sha1_to_hex(entry.sha1)); |
cb0c6df6 RS |
279 | |
280 | path->len = pathlen; | |
4c068a98 LT |
281 | strbuf_append_string(path, entry.path); |
282 | if (S_ISDIR(entry.mode)) | |
cb0c6df6 RS |
283 | strbuf_append_string(path, "/"); |
284 | ||
4c068a98 | 285 | write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize); |
cb0c6df6 | 286 | |
4c068a98 | 287 | if (S_ISDIR(entry.mode)) { |
7ec57556 LT |
288 | struct tree_desc subtree; |
289 | subtree.buf = eltbuf; | |
290 | subtree.size = eltsize; | |
cb0c6df6 | 291 | traverse_tree(&subtree, path); |
731ab9cc RS |
292 | } |
293 | free(eltbuf); | |
294 | } | |
295 | } | |
296 | ||
ce1a79b6 WT |
297 | int git_tar_config(const char *var, const char *value) |
298 | { | |
299 | if (!strcmp(var, "tar.umask")) { | |
300 | if (!strcmp(value, "user")) { | |
301 | tar_umask = umask(0); | |
302 | umask(tar_umask); | |
303 | } else { | |
304 | tar_umask = git_config_int(var, value); | |
305 | } | |
306 | return 0; | |
307 | } | |
308 | return git_default_config(var, value); | |
309 | } | |
310 | ||
1af0d112 | 311 | static int generate_tar(int argc, const char **argv, char** envp) |
731ab9cc | 312 | { |
2c6df2d5 | 313 | unsigned char sha1[20], tree_sha1[20]; |
c3f92812 | 314 | struct commit *commit; |
7ec57556 | 315 | struct tree_desc tree; |
cb0c6df6 RS |
316 | struct strbuf current_path; |
317 | ||
318 | current_path.buf = xmalloc(PATH_MAX); | |
319 | current_path.alloc = PATH_MAX; | |
320 | current_path.len = current_path.eof = 0; | |
731ab9cc | 321 | |
53228a5f | 322 | setup_git_directory(); |
ce1a79b6 | 323 | git_config(git_tar_config); |
53228a5f | 324 | |
731ab9cc RS |
325 | switch (argc) { |
326 | case 3: | |
cb0c6df6 RS |
327 | strbuf_append_string(¤t_path, argv[2]); |
328 | strbuf_append_string(¤t_path, "/"); | |
731ab9cc RS |
329 | /* FALLTHROUGH */ |
330 | case 2: | |
31fff305 DL |
331 | if (get_sha1(argv[1], sha1)) |
332 | die("Not a valid object name %s", argv[1]); | |
731ab9cc RS |
333 | break; |
334 | default: | |
335 | usage(tar_tree_usage); | |
336 | } | |
337 | ||
473d404b | 338 | commit = lookup_commit_reference_gently(sha1, 1); |
c3f92812 DB |
339 | if (commit) { |
340 | write_global_extended_header(commit->object.sha1); | |
341 | archive_time = commit->date; | |
cb0c6df6 RS |
342 | } else |
343 | archive_time = time(NULL); | |
344 | ||
8e440259 | 345 | tree.buf = read_object_with_reference(sha1, tree_type, &tree.size, |
2c6df2d5 | 346 | tree_sha1); |
7ec57556 | 347 | if (!tree.buf) |
db413479 RS |
348 | die("not a reference to a tag, commit or tree object: %s", |
349 | sha1_to_hex(sha1)); | |
cb0c6df6 RS |
350 | |
351 | if (current_path.len > 0) | |
352 | write_entry(tree_sha1, ¤t_path, 040777, NULL, 0); | |
353 | traverse_tree(&tree, ¤t_path); | |
731ab9cc | 354 | write_trailer(); |
cb0c6df6 | 355 | free(current_path.buf); |
731ab9cc RS |
356 | return 0; |
357 | } | |
21754264 JH |
358 | |
359 | static const char *exec = "git-upload-tar"; | |
360 | ||
361 | static int remote_tar(int argc, const char **argv) | |
362 | { | |
363 | int fd[2], ret, len; | |
364 | pid_t pid; | |
365 | char buf[1024]; | |
366 | char *url; | |
367 | ||
368 | if (argc < 3 || 4 < argc) | |
369 | usage(tar_tree_usage); | |
370 | ||
371 | /* --remote=<repo> */ | |
372 | url = strdup(argv[1]+9); | |
373 | pid = git_connect(fd, url, exec); | |
374 | if (pid < 0) | |
375 | return 1; | |
376 | ||
377 | packet_write(fd[1], "want %s\n", argv[2]); | |
378 | if (argv[3]) | |
379 | packet_write(fd[1], "base %s\n", argv[3]); | |
380 | packet_flush(fd[1]); | |
381 | ||
382 | len = packet_read_line(fd[0], buf, sizeof(buf)); | |
383 | if (!len) | |
384 | die("git-tar-tree: expected ACK/NAK, got EOF"); | |
385 | if (buf[len-1] == '\n') | |
386 | buf[--len] = 0; | |
387 | if (strcmp(buf, "ACK")) { | |
388 | if (5 < len && !strncmp(buf, "NACK ", 5)) | |
389 | die("git-tar-tree: NACK %s", buf + 5); | |
390 | die("git-tar-tree: protocol error"); | |
391 | } | |
392 | /* expect a flush */ | |
393 | len = packet_read_line(fd[0], buf, sizeof(buf)); | |
394 | if (len) | |
395 | die("git-tar-tree: expected a flush"); | |
396 | ||
397 | /* Now, start reading from fd[0] and spit it out to stdout */ | |
398 | ret = copy_fd(fd[0], 1); | |
399 | close(fd[0]); | |
400 | ||
401 | ret |= finish_connect(pid); | |
402 | return !!ret; | |
403 | } | |
404 | ||
405 | int cmd_tar_tree(int argc, const char **argv, char **envp) | |
406 | { | |
407 | if (argc < 2) | |
408 | usage(tar_tree_usage); | |
409 | if (!strncmp("--remote=", argv[1], 9)) | |
410 | return remote_tar(argc, argv); | |
1af0d112 | 411 | return generate_tar(argc, argv, envp); |
21754264 | 412 | } |
52ba03cb RS |
413 | |
414 | /* ustar header + extended global header content */ | |
415 | #define HEADERSIZE (2 * RECORDSIZE) | |
416 | ||
417 | int cmd_get_tar_commit_id(int argc, const char **argv, char **envp) | |
418 | { | |
419 | char buffer[HEADERSIZE]; | |
420 | struct ustar_header *header = (struct ustar_header *)buffer; | |
421 | char *content = buffer + RECORDSIZE; | |
422 | ssize_t n; | |
423 | ||
424 | n = xread(0, buffer, HEADERSIZE); | |
425 | if (n < HEADERSIZE) | |
426 | die("git-get-tar-commit-id: read error"); | |
427 | if (header->typeflag[0] != 'g') | |
428 | return 1; | |
429 | if (memcmp(content, "52 comment=", 11)) | |
430 | return 1; | |
431 | ||
432 | n = xwrite(1, content + 11, 41); | |
433 | if (n < 41) | |
434 | die("git-get-tar-commit-id: write error"); | |
435 | ||
436 | return 0; | |
437 | } |