2 * Copyright (c) 2005, 2006 Rene Scharfe
13 #define RECORDSIZE (512)
14 #define BLOCKSIZE (RECORDSIZE * 20)
16 static const char tar_tree_usage[] =
17 "git-tar-tree [--remote=<repo>] <ent> [basedir]";
19 static char block[BLOCKSIZE];
20 static unsigned long offset;
22 static time_t archive_time;
25 /* tries hard to write, either succeeds or dies in the attempt */
26 static void reliable_write(const void *data, unsigned long size)
28 const char *buf = data;
31 long ret = xwrite(1, buf, size);
35 die("git-tar-tree: %s", strerror(errno));
37 die("git-tar-tree: disk full?");
44 /* writes out the whole block, but only if it is full */
45 static void write_if_needed(void)
47 if (offset == BLOCKSIZE) {
48 reliable_write(block, BLOCKSIZE);
54 * queues up writes, so that all our write(2) calls write exactly one
55 * full block; pads writes to RECORDSIZE
57 static void write_blocked(const void *data, unsigned long size)
59 const char *buf = data;
63 unsigned long chunk = BLOCKSIZE - offset;
66 memcpy(block + offset, buf, chunk);
72 while (size >= BLOCKSIZE) {
73 reliable_write(buf, BLOCKSIZE);
78 memcpy(block + offset, buf, size);
81 tail = offset % RECORDSIZE;
83 memset(block + offset, 0, RECORDSIZE - tail);
84 offset += RECORDSIZE - tail;
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).
93 static void write_trailer(void)
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);
104 static void strbuf_append_string(struct strbuf *sb, const char *s)
106 int slen = strlen(s);
107 int total = sb->len + slen;
108 if (total > sb->alloc) {
109 sb->buf = xrealloc(sb->buf, total);
112 memcpy(sb->buf + sb->len, s, slen);
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.
122 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
123 const char *value, unsigned int valuelen)
129 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
130 for (tmp = len; tmp > 9; tmp /= 10)
133 total = sb->len + len;
134 if (total > sb->alloc) {
135 sb->buf = xrealloc(sb->buf, total);
140 p += sprintf(p, "%u %s=", len, keyword);
141 memcpy(p, value, valuelen);
147 static unsigned int ustar_header_chksum(const struct ustar_header *header)
149 char *p = (char *)header;
150 unsigned int chksum = 0;
151 while (p < header->chksum)
153 chksum += sizeof(header->chksum) * ' ';
154 p += sizeof(header->chksum);
155 while (p < (char *)header + sizeof(struct ustar_header))
160 static int get_path_prefix(const struct strbuf *path, int maxlen)
167 } while (i > 0 && path->buf[i] != '/');
171 static void write_entry(const unsigned char *sha1, struct strbuf *path,
172 unsigned int mode, void *buffer, unsigned long size)
174 struct ustar_header header;
175 struct strbuf ext_header;
177 memset(&header, 0, sizeof(header));
178 ext_header.buf = NULL;
179 ext_header.len = ext_header.alloc = 0;
182 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
184 strcpy(header.name, "pax_global_header");
186 *header.typeflag = TYPEFLAG_EXT_HEADER;
188 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
191 *header.typeflag = TYPEFLAG_DIR;
192 mode = (mode | 0777) & ~tar_umask;
193 } else if (S_ISLNK(mode)) {
194 *header.typeflag = TYPEFLAG_LNK;
196 } else if (S_ISREG(mode)) {
197 *header.typeflag = TYPEFLAG_REG;
198 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
200 error("unsupported file mode: 0%o (SHA1: %s)",
201 mode, sha1_to_hex(sha1));
204 if (path->len > sizeof(header.name)) {
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);
211 sprintf(header.name, "%s.data",
213 strbuf_append_ext_header(&ext_header, "path",
214 path->buf, path->len);
217 memcpy(header.name, path->buf, path->len);
220 if (S_ISLNK(mode) && buffer) {
221 if (size > sizeof(header.linkname)) {
222 sprintf(header.linkname, "see %s.paxheader",
224 strbuf_append_ext_header(&ext_header, "linkpath",
227 memcpy(header.linkname, buffer, size);
230 sprintf(header.mode, "%07o", mode & 07777);
231 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
232 sprintf(header.mtime, "%011lo", archive_time);
234 /* XXX: should we provide more meaningful info here? */
235 sprintf(header.uid, "%07o", 0);
236 sprintf(header.gid, "%07o", 0);
237 strlcpy(header.uname, "git", sizeof(header.uname));
238 strlcpy(header.gname, "git", sizeof(header.gname));
239 sprintf(header.devmajor, "%07o", 0);
240 sprintf(header.devminor, "%07o", 0);
242 memcpy(header.magic, "ustar", 6);
243 memcpy(header.version, "00", 2);
245 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
247 if (ext_header.len > 0) {
248 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
249 free(ext_header.buf);
251 write_blocked(&header, sizeof(header));
252 if (S_ISREG(mode) && buffer && size > 0)
253 write_blocked(buffer, size);
256 static void write_global_extended_header(const unsigned char *sha1)
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);
266 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
268 int pathlen = path->len;
269 struct name_entry entry;
271 while (tree_entry(tree, &entry)) {
274 unsigned long eltsize;
276 eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
278 die("cannot read %s", sha1_to_hex(entry.sha1));
281 strbuf_append_string(path, entry.path);
282 if (S_ISDIR(entry.mode))
283 strbuf_append_string(path, "/");
285 write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
287 if (S_ISDIR(entry.mode)) {
288 struct tree_desc subtree;
289 subtree.buf = eltbuf;
290 subtree.size = eltsize;
291 traverse_tree(&subtree, path);
297 int git_tar_config(const char *var, const char *value)
299 if (!strcmp(var, "tar.umask")) {
300 if (!strcmp(value, "user")) {
301 tar_umask = umask(0);
304 tar_umask = git_config_int(var, value);
308 return git_default_config(var, value);
311 static int generate_tar(int argc, const char **argv, const char *prefix)
313 unsigned char sha1[20], tree_sha1[20];
314 struct commit *commit;
315 struct tree_desc tree;
316 struct strbuf current_path;
319 current_path.buf = xmalloc(PATH_MAX);
320 current_path.alloc = PATH_MAX;
321 current_path.len = current_path.eof = 0;
323 git_config(git_tar_config);
327 strbuf_append_string(¤t_path, argv[2]);
328 strbuf_append_string(¤t_path, "/");
331 if (get_sha1(argv[1], sha1))
332 die("Not a valid object name %s", argv[1]);
335 usage(tar_tree_usage);
338 commit = lookup_commit_reference_gently(sha1, 1);
340 write_global_extended_header(commit->object.sha1);
341 archive_time = commit->date;
343 archive_time = time(NULL);
345 tree.buf = buffer = read_object_with_reference(sha1, tree_type,
346 &tree.size, tree_sha1);
348 die("not a reference to a tag, commit or tree object: %s",
351 if (current_path.len > 0)
352 write_entry(tree_sha1, ¤t_path, 040777, NULL, 0);
353 traverse_tree(&tree, ¤t_path);
356 free(current_path.buf);
360 static const char *exec = "git-upload-tar";
362 static int remote_tar(int argc, const char **argv)
369 if (argc < 3 || 4 < argc)
370 usage(tar_tree_usage);
372 /* --remote=<repo> */
373 url = strdup(argv[1]+9);
374 pid = git_connect(fd, url, exec);
378 packet_write(fd[1], "want %s\n", argv[2]);
380 packet_write(fd[1], "base %s\n", argv[3]);
383 len = packet_read_line(fd[0], buf, sizeof(buf));
385 die("git-tar-tree: expected ACK/NAK, got EOF");
386 if (buf[len-1] == '\n')
388 if (strcmp(buf, "ACK")) {
389 if (5 < len && !strncmp(buf, "NACK ", 5))
390 die("git-tar-tree: NACK %s", buf + 5);
391 die("git-tar-tree: protocol error");
394 len = packet_read_line(fd[0], buf, sizeof(buf));
396 die("git-tar-tree: expected a flush");
398 /* Now, start reading from fd[0] and spit it out to stdout */
399 ret = copy_fd(fd[0], 1);
402 ret |= finish_connect(pid);
406 int cmd_tar_tree(int argc, const char **argv, const char *prefix)
409 usage(tar_tree_usage);
410 if (!strncmp("--remote=", argv[1], 9))
411 return remote_tar(argc, argv);
412 return generate_tar(argc, argv, prefix);
415 /* ustar header + extended global header content */
416 #define HEADERSIZE (2 * RECORDSIZE)
418 int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
420 char buffer[HEADERSIZE];
421 struct ustar_header *header = (struct ustar_header *)buffer;
422 char *content = buffer + RECORDSIZE;
425 n = xread(0, buffer, HEADERSIZE);
427 die("git-get-tar-commit-id: read error");
428 if (header->typeflag[0] != 'g')
430 if (memcmp(content, "52 comment=", 11))
433 n = xwrite(1, content + 11, 41);
435 die("git-get-tar-commit-id: write error");