tar-tree: Remove obsolete code
[git/dscho.git] / tar-tree.c
blob11bbc81e36d6e3ac16ff7f113b01d9d651f7edd3
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include <time.h>
5 #include "cache.h"
6 #include "diff.h"
7 #include "commit.h"
8 #include "strbuf.h"
9 #include "tar.h"
11 #define RECORDSIZE (512)
12 #define BLOCKSIZE (RECORDSIZE * 20)
14 static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]";
16 static char block[BLOCKSIZE];
17 static unsigned long offset;
19 static time_t archive_time;
21 /* tries hard to write, either succeeds or dies in the attempt */
22 static void reliable_write(void *buf, unsigned long size)
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?");
33 size -= ret;
34 buf += ret;
38 /* writes out the whole block, but only if it is full */
39 static void write_if_needed(void)
41 if (offset == BLOCKSIZE) {
42 reliable_write(block, BLOCKSIZE);
43 offset = 0;
47 /* acquire the next record from the buffer; user must call write_if_needed() */
48 static char *get_record(void)
50 char *p = block + offset;
51 memset(p, 0, RECORDSIZE);
52 offset += RECORDSIZE;
53 return p;
57 * The end of tar archives is marked by 1024 nul bytes and after that
58 * follows the rest of the block (if any).
60 static void write_trailer(void)
62 get_record();
63 write_if_needed();
64 get_record();
65 write_if_needed();
66 while (offset) {
67 get_record();
68 write_if_needed();
73 * queues up writes, so that all our write(2) calls write exactly one
74 * full block; pads writes to RECORDSIZE
76 static void write_blocked(void *buf, unsigned long size)
78 unsigned long tail;
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();
90 while (size >= BLOCKSIZE) {
91 reliable_write(buf, BLOCKSIZE);
92 size -= BLOCKSIZE;
93 buf += BLOCKSIZE;
95 if (size) {
96 memcpy(block + offset, buf, size);
97 buf += size;
98 offset += size;
100 tail = offset % RECORDSIZE;
101 if (tail) {
102 memset(block + offset, 0, RECORDSIZE - tail);
103 offset += RECORDSIZE - tail;
105 write_if_needed();
108 static void strbuf_append_string(struct strbuf *sb, const char *s)
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;
116 memcpy(sb->buf + sb->len, s, slen);
117 sb->len = total;
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.
126 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
127 const char *value, unsigned int valuelen)
129 char *p;
130 int len, total, tmp;
132 /* "%u %s=%s\n" */
133 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
134 for (tmp = len; tmp > 9; tmp /= 10)
135 len++;
137 total = sb->len + len;
138 if (total > sb->alloc) {
139 sb->buf = xrealloc(sb->buf, total);
140 sb->alloc = total;
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;
151 static unsigned int ustar_header_chksum(const struct ustar_header *header)
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;
164 static void write_entry(const unsigned char *sha1, struct strbuf *path,
165 unsigned int mode, void *buffer, unsigned long size)
167 struct ustar_header header;
168 struct strbuf ext_header;
170 memset(&header, 0, sizeof(header));
171 ext_header.buf = NULL;
172 ext_header.len = ext_header.alloc = 0;
174 if (!sha1) {
175 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
176 mode = 0100666;
177 strcpy(header.name, "pax_global_header");
178 } else if (!path) {
179 *header.typeflag = TYPEFLAG_EXT_HEADER;
180 mode = 0100666;
181 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
182 } else {
183 if (S_ISDIR(mode)) {
184 *header.typeflag = TYPEFLAG_DIR;
185 mode |= 0777;
186 } else if (S_ISLNK(mode)) {
187 *header.typeflag = TYPEFLAG_LNK;
188 mode |= 0777;
189 } else if (S_ISREG(mode)) {
190 *header.typeflag = TYPEFLAG_REG;
191 mode |= (mode & 0100) ? 0777 : 0666;
192 } else {
193 error("unsupported file mode: 0%o (SHA1: %s)",
194 mode, sha1_to_hex(sha1));
195 return;
197 if (path->len > sizeof(header.name)) {
198 sprintf(header.name, "%s.data", sha1_to_hex(sha1));
199 strbuf_append_ext_header(&ext_header, "path",
200 path->buf, path->len);
201 } else
202 memcpy(header.name, path->buf, path->len);
205 if (S_ISLNK(mode) && buffer) {
206 if (size > sizeof(header.linkname)) {
207 sprintf(header.linkname, "see %s.paxheader",
208 sha1_to_hex(sha1));
209 strbuf_append_ext_header(&ext_header, "linkpath",
210 buffer, size);
211 } else
212 memcpy(header.linkname, buffer, size);
215 sprintf(header.mode, "%07o", mode & 07777);
216 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
217 sprintf(header.mtime, "%011lo", archive_time);
219 /* XXX: should we provide more meaningful info here? */
220 sprintf(header.uid, "%07o", 0);
221 sprintf(header.gid, "%07o", 0);
222 strncpy(header.uname, "git", 31);
223 strncpy(header.gname, "git", 31);
224 sprintf(header.devmajor, "%07o", 0);
225 sprintf(header.devminor, "%07o", 0);
227 memcpy(header.magic, "ustar", 6);
228 memcpy(header.version, "00", 2);
230 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
232 if (ext_header.len > 0) {
233 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
234 free(ext_header.buf);
236 write_blocked(&header, sizeof(header));
237 if (S_ISREG(mode) && buffer && size > 0)
238 write_blocked(buffer, size);
241 static void write_global_extended_header(const unsigned char *sha1)
243 struct strbuf ext_header;
244 ext_header.buf = NULL;
245 ext_header.len = ext_header.alloc = 0;
246 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
247 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
248 free(ext_header.buf);
251 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
253 int pathlen = path->len;
255 while (tree->size) {
256 const char *name;
257 const unsigned char *sha1;
258 unsigned mode;
259 void *eltbuf;
260 char elttype[20];
261 unsigned long eltsize;
263 sha1 = tree_entry_extract(tree, &name, &mode);
264 update_tree_entry(tree);
266 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
267 if (!eltbuf)
268 die("cannot read %s", sha1_to_hex(sha1));
270 path->len = pathlen;
271 strbuf_append_string(path, name);
272 if (S_ISDIR(mode))
273 strbuf_append_string(path, "/");
275 write_entry(sha1, path, mode, eltbuf, eltsize);
277 if (S_ISDIR(mode)) {
278 struct tree_desc subtree;
279 subtree.buf = eltbuf;
280 subtree.size = eltsize;
281 traverse_tree(&subtree, path);
283 free(eltbuf);
287 int main(int argc, char **argv)
289 unsigned char sha1[20], tree_sha1[20];
290 struct commit *commit;
291 struct tree_desc tree;
292 struct strbuf current_path;
294 current_path.buf = xmalloc(PATH_MAX);
295 current_path.alloc = PATH_MAX;
296 current_path.len = current_path.eof = 0;
298 setup_git_directory();
300 switch (argc) {
301 case 3:
302 strbuf_append_string(&current_path, argv[2]);
303 strbuf_append_string(&current_path, "/");
304 /* FALLTHROUGH */
305 case 2:
306 if (get_sha1(argv[1], sha1) < 0)
307 usage(tar_tree_usage);
308 break;
309 default:
310 usage(tar_tree_usage);
313 commit = lookup_commit_reference_gently(sha1, 1);
314 if (commit) {
315 write_global_extended_header(commit->object.sha1);
316 archive_time = commit->date;
317 } else
318 archive_time = time(NULL);
320 tree.buf = read_object_with_reference(sha1, "tree", &tree.size,
321 tree_sha1);
322 if (!tree.buf)
323 die("not a reference to a tag, commit or tree object: %s",
324 sha1_to_hex(sha1));
326 if (current_path.len > 0)
327 write_entry(tree_sha1, &current_path, 040777, NULL, 0);
328 traverse_tree(&tree, &current_path);
329 write_trailer();
330 free(current_path.buf);
331 return 0;