archive-tar: split long paths more carefully
[git/debian.git] / archive-tar.c
blob9d4eec2c71ed703fcb5e6df4cfed200b219e1e2e
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "tar.h"
6 #include "archive.h"
7 #include "run-command.h"
9 #define RECORDSIZE (512)
10 #define BLOCKSIZE (RECORDSIZE * 20)
12 static char block[BLOCKSIZE];
13 static unsigned long offset;
15 static int tar_umask = 002;
17 static int write_tar_filter_archive(const struct archiver *ar,
18 struct archiver_args *args);
20 /* writes out the whole block, but only if it is full */
21 static void write_if_needed(void)
23 if (offset == BLOCKSIZE) {
24 write_or_die(1, block, BLOCKSIZE);
25 offset = 0;
30 * queues up writes, so that all our write(2) calls write exactly one
31 * full block; pads writes to RECORDSIZE
33 static void write_blocked(const void *data, unsigned long size)
35 const char *buf = data;
36 unsigned long tail;
38 if (offset) {
39 unsigned long chunk = BLOCKSIZE - offset;
40 if (size < chunk)
41 chunk = size;
42 memcpy(block + offset, buf, chunk);
43 size -= chunk;
44 offset += chunk;
45 buf += chunk;
46 write_if_needed();
48 while (size >= BLOCKSIZE) {
49 write_or_die(1, buf, BLOCKSIZE);
50 size -= BLOCKSIZE;
51 buf += BLOCKSIZE;
53 if (size) {
54 memcpy(block + offset, buf, size);
55 offset += size;
57 tail = offset % RECORDSIZE;
58 if (tail) {
59 memset(block + offset, 0, RECORDSIZE - tail);
60 offset += RECORDSIZE - tail;
62 write_if_needed();
66 * The end of tar archives is marked by 2*512 nul bytes and after that
67 * follows the rest of the block (if any).
69 static void write_trailer(void)
71 int tail = BLOCKSIZE - offset;
72 memset(block + offset, 0, tail);
73 write_or_die(1, block, BLOCKSIZE);
74 if (tail < 2 * RECORDSIZE) {
75 memset(block, 0, offset);
76 write_or_die(1, block, BLOCKSIZE);
81 * pax extended header records have the format "%u %s=%s\n". %u contains
82 * the size of the whole string (including the %u), the first %s is the
83 * keyword, the second one is the value. This function constructs such a
84 * string and appends it to a struct strbuf.
86 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
87 const char *value, unsigned int valuelen)
89 int len, tmp;
91 /* "%u %s=%s\n" */
92 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
93 for (tmp = len; tmp > 9; tmp /= 10)
94 len++;
96 strbuf_grow(sb, len);
97 strbuf_addf(sb, "%u %s=", len, keyword);
98 strbuf_add(sb, value, valuelen);
99 strbuf_addch(sb, '\n');
102 static unsigned int ustar_header_chksum(const struct ustar_header *header)
104 char *p = (char *)header;
105 unsigned int chksum = 0;
106 while (p < header->chksum)
107 chksum += *p++;
108 chksum += sizeof(header->chksum) * ' ';
109 p += sizeof(header->chksum);
110 while (p < (char *)header + sizeof(struct ustar_header))
111 chksum += *p++;
112 return chksum;
115 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
117 size_t i = pathlen;
118 if (i > 1 && path[i - 1] == '/')
119 i--;
120 if (i > maxlen)
121 i = maxlen;
122 do {
123 i--;
124 } while (i > 0 && path[i] != '/');
125 return i;
128 static int write_tar_entry(struct archiver_args *args,
129 const unsigned char *sha1, const char *path, size_t pathlen,
130 unsigned int mode, void *buffer, unsigned long size)
132 struct ustar_header header;
133 struct strbuf ext_header = STRBUF_INIT;
134 int err = 0;
136 memset(&header, 0, sizeof(header));
138 if (!sha1) {
139 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
140 mode = 0100666;
141 strcpy(header.name, "pax_global_header");
142 } else if (!path) {
143 *header.typeflag = TYPEFLAG_EXT_HEADER;
144 mode = 0100666;
145 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
146 } else {
147 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
148 *header.typeflag = TYPEFLAG_DIR;
149 mode = (mode | 0777) & ~tar_umask;
150 } else if (S_ISLNK(mode)) {
151 *header.typeflag = TYPEFLAG_LNK;
152 mode |= 0777;
153 } else if (S_ISREG(mode)) {
154 *header.typeflag = TYPEFLAG_REG;
155 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
156 } else {
157 return error("unsupported file mode: 0%o (SHA1: %s)",
158 mode, sha1_to_hex(sha1));
160 if (pathlen > sizeof(header.name)) {
161 size_t plen = get_path_prefix(path, pathlen,
162 sizeof(header.prefix));
163 size_t rest = pathlen - plen - 1;
164 if (plen > 0 && rest <= sizeof(header.name)) {
165 memcpy(header.prefix, path, plen);
166 memcpy(header.name, path + plen + 1, rest);
167 } else {
168 sprintf(header.name, "%s.data",
169 sha1_to_hex(sha1));
170 strbuf_append_ext_header(&ext_header, "path",
171 path, pathlen);
173 } else
174 memcpy(header.name, path, pathlen);
177 if (S_ISLNK(mode) && buffer) {
178 if (size > sizeof(header.linkname)) {
179 sprintf(header.linkname, "see %s.paxheader",
180 sha1_to_hex(sha1));
181 strbuf_append_ext_header(&ext_header, "linkpath",
182 buffer, size);
183 } else
184 memcpy(header.linkname, buffer, size);
187 sprintf(header.mode, "%07o", mode & 07777);
188 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
189 sprintf(header.mtime, "%011lo", (unsigned long) args->time);
191 sprintf(header.uid, "%07o", 0);
192 sprintf(header.gid, "%07o", 0);
193 strlcpy(header.uname, "root", sizeof(header.uname));
194 strlcpy(header.gname, "root", sizeof(header.gname));
195 sprintf(header.devmajor, "%07o", 0);
196 sprintf(header.devminor, "%07o", 0);
198 memcpy(header.magic, "ustar", 6);
199 memcpy(header.version, "00", 2);
201 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
203 if (ext_header.len > 0) {
204 err = write_tar_entry(args, sha1, NULL, 0, 0, ext_header.buf,
205 ext_header.len);
206 if (err)
207 return err;
209 strbuf_release(&ext_header);
210 write_blocked(&header, sizeof(header));
211 if (S_ISREG(mode) && buffer && size > 0)
212 write_blocked(buffer, size);
213 return err;
216 static int write_global_extended_header(struct archiver_args *args)
218 const unsigned char *sha1 = args->commit_sha1;
219 struct strbuf ext_header = STRBUF_INIT;
220 int err;
222 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
223 err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf,
224 ext_header.len);
225 strbuf_release(&ext_header);
226 return err;
229 static struct archiver **tar_filters;
230 static int nr_tar_filters;
231 static int alloc_tar_filters;
233 static struct archiver *find_tar_filter(const char *name, int len)
235 int i;
236 for (i = 0; i < nr_tar_filters; i++) {
237 struct archiver *ar = tar_filters[i];
238 if (!strncmp(ar->name, name, len) && !ar->name[len])
239 return ar;
241 return NULL;
244 static int tar_filter_config(const char *var, const char *value, void *data)
246 struct archiver *ar;
247 const char *dot;
248 const char *name;
249 const char *type;
250 int namelen;
252 if (prefixcmp(var, "tar."))
253 return 0;
254 dot = strrchr(var, '.');
255 if (dot == var + 9)
256 return 0;
258 name = var + 4;
259 namelen = dot - name;
260 type = dot + 1;
262 ar = find_tar_filter(name, namelen);
263 if (!ar) {
264 ar = xcalloc(1, sizeof(*ar));
265 ar->name = xmemdupz(name, namelen);
266 ar->write_archive = write_tar_filter_archive;
267 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
268 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
269 tar_filters[nr_tar_filters++] = ar;
272 if (!strcmp(type, "command")) {
273 if (!value)
274 return config_error_nonbool(var);
275 free(ar->data);
276 ar->data = xstrdup(value);
277 return 0;
279 if (!strcmp(type, "remote")) {
280 if (git_config_bool(var, value))
281 ar->flags |= ARCHIVER_REMOTE;
282 else
283 ar->flags &= ~ARCHIVER_REMOTE;
284 return 0;
287 return 0;
290 static int git_tar_config(const char *var, const char *value, void *cb)
292 if (!strcmp(var, "tar.umask")) {
293 if (value && !strcmp(value, "user")) {
294 tar_umask = umask(0);
295 umask(tar_umask);
296 } else {
297 tar_umask = git_config_int(var, value);
299 return 0;
302 return tar_filter_config(var, value, cb);
305 static int write_tar_archive(const struct archiver *ar,
306 struct archiver_args *args)
308 int err = 0;
310 if (args->commit_sha1)
311 err = write_global_extended_header(args);
312 if (!err)
313 err = write_archive_entries(args, write_tar_entry);
314 if (!err)
315 write_trailer();
316 return err;
319 static int write_tar_filter_archive(const struct archiver *ar,
320 struct archiver_args *args)
322 struct strbuf cmd = STRBUF_INIT;
323 struct child_process filter;
324 const char *argv[2];
325 int r;
327 if (!ar->data)
328 die("BUG: tar-filter archiver called with no filter defined");
330 strbuf_addstr(&cmd, ar->data);
331 if (args->compression_level >= 0)
332 strbuf_addf(&cmd, " -%d", args->compression_level);
334 memset(&filter, 0, sizeof(filter));
335 argv[0] = cmd.buf;
336 argv[1] = NULL;
337 filter.argv = argv;
338 filter.use_shell = 1;
339 filter.in = -1;
341 if (start_command(&filter) < 0)
342 die_errno("unable to start '%s' filter", argv[0]);
343 close(1);
344 if (dup2(filter.in, 1) < 0)
345 die_errno("unable to redirect descriptor");
346 close(filter.in);
348 r = write_tar_archive(ar, args);
350 close(1);
351 if (finish_command(&filter) != 0)
352 die("'%s' filter reported error", argv[0]);
354 strbuf_release(&cmd);
355 return r;
358 static struct archiver tar_archiver = {
359 "tar",
360 write_tar_archive,
361 ARCHIVER_REMOTE
364 void init_tar_archiver(void)
366 int i;
367 register_archiver(&tar_archiver);
369 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
370 tar_filter_config("tar.tgz.remote", "true", NULL);
371 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
372 tar_filter_config("tar.tar.gz.remote", "true", NULL);
373 git_config(git_tar_config, NULL);
374 for (i = 0; i < nr_tar_filters; i++) {
375 /* omit any filters that never had a command configured */
376 if (tar_filters[i]->data)
377 register_archiver(tar_filters[i]);