]>
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" | |
731ab9cc RS |
10 | |
11 | #define RECORDSIZE (512) | |
12 | #define BLOCKSIZE (RECORDSIZE * 20) | |
13 | ||
4d1f1190 | 14 | static const char tar_tree_usage[] = "git-tar-tree <key> [basedir]"; |
731ab9cc RS |
15 | |
16 | static char block[BLOCKSIZE]; | |
17 | static unsigned long offset; | |
18 | ||
731ab9cc RS |
19 | static time_t archive_time; |
20 | ||
731ab9cc RS |
21 | /* tries hard to write, either succeeds or dies in the attempt */ |
22 | static void reliable_write(void *buf, unsigned long size) | |
23 | { | |
24 | while (size > 0) { | |
1c15afb9 | 25 | long ret = xwrite(1, buf, size); |
731ab9cc | 26 | if (ret < 0) { |
731ab9cc RS |
27 | if (errno == EPIPE) |
28 | exit(0); | |
667bb59b | 29 | die("git-tar-tree: %s", strerror(errno)); |
731ab9cc | 30 | } else if (!ret) { |
667bb59b | 31 | die("git-tar-tree: disk full?"); |
731ab9cc RS |
32 | } |
33 | size -= ret; | |
34 | buf += ret; | |
35 | } | |
36 | } | |
37 | ||
38 | /* writes out the whole block, but only if it is full */ | |
39 | static void write_if_needed(void) | |
40 | { | |
41 | if (offset == BLOCKSIZE) { | |
42 | reliable_write(block, BLOCKSIZE); | |
43 | offset = 0; | |
44 | } | |
45 | } | |
46 | ||
a90a6e6a RS |
47 | /* acquire the next record from the buffer; user must call write_if_needed() */ |
48 | static char *get_record(void) | |
49 | { | |
50 | char *p = block + offset; | |
51 | memset(p, 0, RECORDSIZE); | |
52 | offset += RECORDSIZE; | |
53 | return p; | |
54 | } | |
55 | ||
731ab9cc RS |
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 | */ | |
60 | static void write_trailer(void) | |
61 | { | |
9b5b9f39 | 62 | get_record(); |
731ab9cc | 63 | write_if_needed(); |
9b5b9f39 | 64 | get_record(); |
731ab9cc | 65 | write_if_needed(); |
a325a11b | 66 | while (offset) { |
9b5b9f39 RS |
67 | get_record(); |
68 | write_if_needed(); | |
731ab9cc RS |
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 | */ | |
76 | static 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); | |
731ab9cc RS |
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 | ||
cb0c6df6 | 107 | static void strbuf_append_string(struct strbuf *sb, const char *s) |
731ab9cc | 108 | { |
cb0c6df6 RS |
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; | |
731ab9cc | 114 | } |
cb0c6df6 RS |
115 | memcpy(sb->buf + sb->len, s, slen); |
116 | sb->len = total; | |
731ab9cc RS |
117 | } |
118 | ||
ae64bbc1 RS |
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 | */ | |
125 | static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword, | |
126 | const char *value, unsigned int valuelen) | |
71058b1f | 127 | { |
ae64bbc1 RS |
128 | char *p; |
129 | int len, total, tmp; | |
71058b1f | 130 | |
71058b1f | 131 | /* "%u %s=%s\n" */ |
ae64bbc1 RS |
132 | len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1; |
133 | for (tmp = len; tmp > 9; tmp /= 10) | |
71058b1f | 134 | len++; |
71058b1f | 135 | |
ae64bbc1 RS |
136 | total = sb->len + len; |
137 | if (total > sb->alloc) { | |
138 | sb->buf = xrealloc(sb->buf, total); | |
139 | sb->alloc = total; | |
140 | } | |
71058b1f | 141 | |
ae64bbc1 RS |
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 | } | |
731ab9cc | 149 | |
ae64bbc1 | 150 | static unsigned int ustar_header_chksum(const struct ustar_header *header) |
731ab9cc | 151 | { |
ae64bbc1 RS |
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; | |
731ab9cc RS |
161 | } |
162 | ||
4c691724 | 163 | static int get_path_prefix(const struct strbuf *path, int maxlen) |
87fec8fc | 164 | { |
4c691724 RS |
165 | int i = path->len; |
166 | if (i > maxlen) | |
167 | i = maxlen; | |
168 | while (i > 0 && path->buf[i] != '/') | |
169 | i--; | |
170 | return i; | |
87fec8fc RS |
171 | } |
172 | ||
ae64bbc1 RS |
173 | static void write_entry(const unsigned char *sha1, struct strbuf *path, |
174 | unsigned int mode, void *buffer, unsigned long size) | |
731ab9cc | 175 | { |
ae64bbc1 RS |
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)); | |
731ab9cc | 191 | } else { |
ae64bbc1 RS |
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)) { | |
4c691724 RS |
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 | } | |
ae64bbc1 RS |
218 | } else |
219 | memcpy(header.name, path->buf, path->len); | |
731ab9cc RS |
220 | } |
221 | ||
ae64bbc1 RS |
222 | if (S_ISLNK(mode) && buffer) { |
223 | if (size > sizeof(header.linkname)) { | |
224 | sprintf(header.linkname, "see %s.paxheader", | |
d5776d50 | 225 | sha1_to_hex(sha1)); |
ae64bbc1 RS |
226 | strbuf_append_ext_header(&ext_header, "linkpath", |
227 | buffer, size); | |
228 | } else | |
229 | memcpy(header.linkname, buffer, size); | |
d5776d50 RS |
230 | } |
231 | ||
ae64bbc1 RS |
232 | sprintf(header.mode, "%07o", mode & 07777); |
233 | sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0); | |
234 | sprintf(header.mtime, "%011lo", archive_time); | |
731ab9cc RS |
235 | |
236 | /* XXX: should we provide more meaningful info here? */ | |
ae64bbc1 RS |
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); | |
731ab9cc | 243 | |
ae64bbc1 RS |
244 | memcpy(header.magic, "ustar", 6); |
245 | memcpy(header.version, "00", 2); | |
731ab9cc | 246 | |
ae64bbc1 | 247 | sprintf(header.chksum, "%07o", ustar_header_chksum(&header)); |
731ab9cc | 248 | |
ae64bbc1 RS |
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 | } | |
731ab9cc | 257 | |
bf0f910d | 258 | static void write_global_extended_header(const unsigned char *sha1) |
87fec8fc | 259 | { |
ae64bbc1 RS |
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); | |
731ab9cc RS |
266 | } |
267 | ||
cb0c6df6 | 268 | static void traverse_tree(struct tree_desc *tree, struct strbuf *path) |
731ab9cc | 269 | { |
cb0c6df6 | 270 | int pathlen = path->len; |
731ab9cc | 271 | |
7ec57556 LT |
272 | while (tree->size) { |
273 | const char *name; | |
274 | const unsigned char *sha1; | |
275 | unsigned mode; | |
731ab9cc RS |
276 | void *eltbuf; |
277 | char elttype[20]; | |
278 | unsigned long eltsize; | |
5207234a | 279 | |
7ec57556 LT |
280 | sha1 = tree_entry_extract(tree, &name, &mode); |
281 | update_tree_entry(tree); | |
282 | ||
283 | eltbuf = read_sha1_file(sha1, elttype, &eltsize); | |
731ab9cc | 284 | if (!eltbuf) |
7ec57556 | 285 | die("cannot read %s", sha1_to_hex(sha1)); |
cb0c6df6 RS |
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 | ||
7ec57556 LT |
294 | if (S_ISDIR(mode)) { |
295 | struct tree_desc subtree; | |
296 | subtree.buf = eltbuf; | |
297 | subtree.size = eltsize; | |
cb0c6df6 | 298 | traverse_tree(&subtree, path); |
731ab9cc RS |
299 | } |
300 | free(eltbuf); | |
301 | } | |
302 | } | |
303 | ||
731ab9cc RS |
304 | int main(int argc, char **argv) |
305 | { | |
2c6df2d5 | 306 | unsigned char sha1[20], tree_sha1[20]; |
c3f92812 | 307 | struct commit *commit; |
7ec57556 | 308 | struct tree_desc tree; |
cb0c6df6 RS |
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; | |
731ab9cc | 314 | |
53228a5f | 315 | setup_git_directory(); |
84a9b58c | 316 | git_config(git_default_config); |
53228a5f | 317 | |
731ab9cc RS |
318 | switch (argc) { |
319 | case 3: | |
cb0c6df6 RS |
320 | strbuf_append_string(¤t_path, argv[2]); |
321 | strbuf_append_string(¤t_path, "/"); | |
731ab9cc RS |
322 | /* FALLTHROUGH */ |
323 | case 2: | |
3c249c95 | 324 | if (get_sha1(argv[1], sha1) < 0) |
731ab9cc RS |
325 | usage(tar_tree_usage); |
326 | break; | |
327 | default: | |
328 | usage(tar_tree_usage); | |
329 | } | |
330 | ||
473d404b | 331 | commit = lookup_commit_reference_gently(sha1, 1); |
c3f92812 DB |
332 | if (commit) { |
333 | write_global_extended_header(commit->object.sha1); | |
334 | archive_time = commit->date; | |
cb0c6df6 RS |
335 | } else |
336 | archive_time = time(NULL); | |
337 | ||
8e440259 | 338 | tree.buf = read_object_with_reference(sha1, tree_type, &tree.size, |
2c6df2d5 | 339 | tree_sha1); |
7ec57556 | 340 | if (!tree.buf) |
db413479 RS |
341 | die("not a reference to a tag, commit or tree object: %s", |
342 | sha1_to_hex(sha1)); | |
cb0c6df6 RS |
343 | |
344 | if (current_path.len > 0) | |
345 | write_entry(tree_sha1, ¤t_path, 040777, NULL, 0); | |
346 | traverse_tree(&tree, ¤t_path); | |
731ab9cc | 347 | write_trailer(); |
cb0c6df6 | 348 | free(current_path.buf); |
731ab9cc RS |
349 | return 0; |
350 | } |