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;
24 /* tries hard to write, either succeeds or dies in the attempt */
25 static void reliable_write(const void *data, unsigned long size)
27 const char *buf = data;
30 long ret = xwrite(1, buf, size);
34 die("git-tar-tree: %s", strerror(errno));
36 die("git-tar-tree: disk full?");
43 /* writes out the whole block, but only if it is full */
44 static void write_if_needed(void)
46 if (offset == BLOCKSIZE) {
47 reliable_write(block, BLOCKSIZE);
53 * queues up writes, so that all our write(2) calls write exactly one
54 * full block; pads writes to RECORDSIZE
56 static void write_blocked(const void *data, unsigned long size)
58 const char *buf = data;
62 unsigned long chunk = BLOCKSIZE - offset;
65 memcpy(block + offset, buf, chunk);
71 while (size >= BLOCKSIZE) {
72 reliable_write(buf, BLOCKSIZE);
77 memcpy(block + offset, buf, size);
80 tail = offset % RECORDSIZE;
82 memset(block + offset, 0, RECORDSIZE - tail);
83 offset += RECORDSIZE - tail;
89 * The end of tar archives is marked by 2*512 nul bytes and after that
90 * follows the rest of the block (if any).
92 static void write_trailer(void)
94 int tail = BLOCKSIZE - offset;
95 memset(block + offset, 0, tail);
96 reliable_write(block, BLOCKSIZE);
97 if (tail < 2 * RECORDSIZE) {
98 memset(block, 0, offset);
99 reliable_write(block, BLOCKSIZE);
103 static void strbuf_append_string(struct strbuf *sb, const char *s)
105 int slen = strlen(s);
106 int total = sb->len + slen;
107 if (total > sb->alloc) {
108 sb->buf = xrealloc(sb->buf, total);
111 memcpy(sb->buf + sb->len, s, slen);
116 * pax extended header records have the format "%u %s=%s\n". %u contains
117 * the size of the whole string (including the %u), the first %s is the
118 * keyword, the second one is the value. This function constructs such a
119 * string and appends it to a struct strbuf.
121 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
122 const char *value, unsigned int valuelen)
128 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
129 for (tmp = len; tmp > 9; tmp /= 10)
132 total = sb->len + len;
133 if (total > sb->alloc) {
134 sb->buf = xrealloc(sb->buf, total);
139 p += sprintf(p, "%u %s=", len, keyword);
140 memcpy(p, value, valuelen);
146 static unsigned int ustar_header_chksum(const struct ustar_header *header)
148 char *p = (char *)header;
149 unsigned int chksum = 0;
150 while (p < header->chksum)
152 chksum += sizeof(header->chksum) * ' ';
153 p += sizeof(header->chksum);
154 while (p < (char *)header + sizeof(struct ustar_header))
159 static int get_path_prefix(const struct strbuf *path, int maxlen)
166 } while (i > 0 && path->buf[i] != '/');
170 static void write_entry(const unsigned char *sha1, struct strbuf *path,
171 unsigned int mode, void *buffer, unsigned long size)
173 struct ustar_header header;
174 struct strbuf ext_header;
176 memset(&header, 0, sizeof(header));
177 ext_header.buf = NULL;
178 ext_header.len = ext_header.alloc = 0;
181 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
183 strcpy(header.name, "pax_global_header");
185 *header.typeflag = TYPEFLAG_EXT_HEADER;
187 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
190 *header.typeflag = TYPEFLAG_DIR;
192 } else if (S_ISLNK(mode)) {
193 *header.typeflag = TYPEFLAG_LNK;
195 } else if (S_ISREG(mode)) {
196 *header.typeflag = TYPEFLAG_REG;
197 mode |= (mode & 0100) ? 0777 : 0666;
199 error("unsupported file mode: 0%o (SHA1: %s)",
200 mode, sha1_to_hex(sha1));
203 if (path->len > sizeof(header.name)) {
204 int plen = get_path_prefix(path, sizeof(header.prefix));
205 int rest = path->len - plen - 1;
206 if (plen > 0 && rest <= sizeof(header.name)) {
207 memcpy(header.prefix, path->buf, plen);
208 memcpy(header.name, path->buf + plen + 1, rest);
210 sprintf(header.name, "%s.data",
212 strbuf_append_ext_header(&ext_header, "path",
213 path->buf, path->len);
216 memcpy(header.name, path->buf, path->len);
219 if (S_ISLNK(mode) && buffer) {
220 if (size > sizeof(header.linkname)) {
221 sprintf(header.linkname, "see %s.paxheader",
223 strbuf_append_ext_header(&ext_header, "linkpath",
226 memcpy(header.linkname, buffer, size);
229 sprintf(header.mode, "%07o", mode & 07777);
230 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
231 sprintf(header.mtime, "%011lo", archive_time);
233 /* XXX: should we provide more meaningful info here? */
234 sprintf(header.uid, "%07o", 0);
235 sprintf(header.gid, "%07o", 0);
236 strlcpy(header.uname, "git", sizeof(header.uname));
237 strlcpy(header.gname, "git", sizeof(header.gname));
238 sprintf(header.devmajor, "%07o", 0);
239 sprintf(header.devminor, "%07o", 0);
241 memcpy(header.magic, "ustar", 6);
242 memcpy(header.version, "00", 2);
244 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
246 if (ext_header.len > 0) {
247 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
248 free(ext_header.buf);
250 write_blocked(&header, sizeof(header));
251 if (S_ISREG(mode) && buffer && size > 0)
252 write_blocked(buffer, size);
255 static void write_global_extended_header(const unsigned char *sha1)
257 struct strbuf ext_header;
258 ext_header.buf = NULL;
259 ext_header.len = ext_header.alloc = 0;
260 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
261 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
262 free(ext_header.buf);
265 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
267 int pathlen = path->len;
268 struct name_entry entry;
270 while (tree_entry(tree, &entry)) {
273 unsigned long eltsize;
275 eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
277 die("cannot read %s", sha1_to_hex(entry.sha1));
280 strbuf_append_string(path, entry.path);
281 if (S_ISDIR(entry.mode))
282 strbuf_append_string(path, "/");
284 write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
286 if (S_ISDIR(entry.mode)) {
287 struct tree_desc subtree;
288 subtree.buf = eltbuf;
289 subtree.size = eltsize;
290 traverse_tree(&subtree, path);
296 static int generate_tar(int argc, const char **argv, char** envp)
298 unsigned char sha1[20], tree_sha1[20];
299 struct commit *commit;
300 struct tree_desc tree;
301 struct strbuf current_path;
303 current_path.buf = xmalloc(PATH_MAX);
304 current_path.alloc = PATH_MAX;
305 current_path.len = current_path.eof = 0;
307 setup_git_directory();
308 git_config(git_default_config);
312 strbuf_append_string(¤t_path, argv[2]);
313 strbuf_append_string(¤t_path, "/");
316 if (get_sha1(argv[1], sha1))
317 die("Not a valid object name %s", argv[1]);
320 usage(tar_tree_usage);
323 commit = lookup_commit_reference_gently(sha1, 1);
325 write_global_extended_header(commit->object.sha1);
326 archive_time = commit->date;
328 archive_time = time(NULL);
330 tree.buf = read_object_with_reference(sha1, tree_type, &tree.size,
333 die("not a reference to a tag, commit or tree object: %s",
336 if (current_path.len > 0)
337 write_entry(tree_sha1, ¤t_path, 040777, NULL, 0);
338 traverse_tree(&tree, ¤t_path);
340 free(current_path.buf);
344 static const char *exec = "git-upload-tar";
346 static int remote_tar(int argc, const char **argv)
353 if (argc < 3 || 4 < argc)
354 usage(tar_tree_usage);
356 /* --remote=<repo> */
357 url = strdup(argv[1]+9);
358 pid = git_connect(fd, url, exec);
362 packet_write(fd[1], "want %s\n", argv[2]);
364 packet_write(fd[1], "base %s\n", argv[3]);
367 len = packet_read_line(fd[0], buf, sizeof(buf));
369 die("git-tar-tree: expected ACK/NAK, got EOF");
370 if (buf[len-1] == '\n')
372 if (strcmp(buf, "ACK")) {
373 if (5 < len && !strncmp(buf, "NACK ", 5))
374 die("git-tar-tree: NACK %s", buf + 5);
375 die("git-tar-tree: protocol error");
378 len = packet_read_line(fd[0], buf, sizeof(buf));
380 die("git-tar-tree: expected a flush");
382 /* Now, start reading from fd[0] and spit it out to stdout */
383 ret = copy_fd(fd[0], 1);
386 ret |= finish_connect(pid);
390 int cmd_tar_tree(int argc, const char **argv, char **envp)
393 usage(tar_tree_usage);
394 if (!strncmp("--remote=", argv[1], 9))
395 return remote_tar(argc, argv);
396 return generate_tar(argc, argv, envp);
399 /* ustar header + extended global header content */
400 #define HEADERSIZE (2 * RECORDSIZE)
402 int cmd_get_tar_commit_id(int argc, const char **argv, char **envp)
404 char buffer[HEADERSIZE];
405 struct ustar_header *header = (struct ustar_header *)buffer;
406 char *content = buffer + RECORDSIZE;
409 n = xread(0, buffer, HEADERSIZE);
411 die("git-get-tar-commit-id: read error");
412 if (header->typeflag[0] != 'g')
414 if (memcmp(content, "52 comment=", 11))
417 n = xwrite(1, content + 11, 41);
419 die("git-get-tar-commit-id: write error");