convert trivial sprintf / strcpy calls to xsnprintf
[git/git.git] / archive-tar.c
CommitLineData
3d74982f
RS
1/*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
3d74982f 4#include "cache.h"
3d74982f 5#include "tar.h"
3d74982f 6#include "archive.h"
5544049d 7#include "streaming.h"
767cf457 8#include "run-command.h"
3d74982f
RS
9
10#define RECORDSIZE (512)
11#define BLOCKSIZE (RECORDSIZE * 20)
12
13static char block[BLOCKSIZE];
14static unsigned long offset;
15
f08b3b0e 16static int tar_umask = 002;
3d74982f 17
767cf457
JK
18static int write_tar_filter_archive(const struct archiver *ar,
19 struct archiver_args *args);
20
3d74982f
RS
21/* writes out the whole block, but only if it is full */
22static void write_if_needed(void)
23{
24 if (offset == BLOCKSIZE) {
25 write_or_die(1, block, BLOCKSIZE);
26 offset = 0;
27 }
28}
29
30/*
31 * queues up writes, so that all our write(2) calls write exactly one
32 * full block; pads writes to RECORDSIZE
33 */
5544049d 34static void do_write_blocked(const void *data, unsigned long size)
3d74982f
RS
35{
36 const char *buf = data;
3d74982f
RS
37
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();
47 }
48 while (size >= BLOCKSIZE) {
49 write_or_die(1, buf, BLOCKSIZE);
50 size -= BLOCKSIZE;
51 buf += BLOCKSIZE;
52 }
53 if (size) {
54 memcpy(block + offset, buf, size);
55 offset += size;
56 }
5544049d
NTND
57}
58
59static void finish_record(void)
60{
61 unsigned long tail;
3d74982f
RS
62 tail = offset % RECORDSIZE;
63 if (tail) {
64 memset(block + offset, 0, RECORDSIZE - tail);
65 offset += RECORDSIZE - tail;
66 }
67 write_if_needed();
68}
69
5544049d
NTND
70static void write_blocked(const void *data, unsigned long size)
71{
72 do_write_blocked(data, size);
73 finish_record();
74}
75
3d74982f
RS
76/*
77 * The end of tar archives is marked by 2*512 nul bytes and after that
78 * follows the rest of the block (if any).
79 */
80static void write_trailer(void)
81{
82 int tail = BLOCKSIZE - offset;
83 memset(block + offset, 0, tail);
84 write_or_die(1, block, BLOCKSIZE);
85 if (tail < 2 * RECORDSIZE) {
86 memset(block, 0, offset);
87 write_or_die(1, block, BLOCKSIZE);
88 }
89}
90
5544049d
NTND
91/*
92 * queues up writes, so that all our write(2) calls write exactly one
93 * full block; pads writes to RECORDSIZE
94 */
95static int stream_blocked(const unsigned char *sha1)
96{
97 struct git_istream *st;
98 enum object_type type;
99 unsigned long sz;
100 char buf[BLOCKSIZE];
101 ssize_t readlen;
102
103 st = open_istream(sha1, &type, &sz, NULL);
104 if (!st)
105 return error("cannot stream blob %s", sha1_to_hex(sha1));
106 for (;;) {
107 readlen = read_istream(st, buf, sizeof(buf));
108 if (readlen <= 0)
109 break;
110 do_write_blocked(buf, readlen);
111 }
112 close_istream(st);
113 if (!readlen)
114 finish_record();
115 return readlen;
116}
117
3d74982f
RS
118/*
119 * pax extended header records have the format "%u %s=%s\n". %u contains
120 * the size of the whole string (including the %u), the first %s is the
121 * keyword, the second one is the value. This function constructs such a
122 * string and appends it to a struct strbuf.
123 */
124static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
125 const char *value, unsigned int valuelen)
126{
7a604f16 127 int len, tmp;
3d74982f
RS
128
129 /* "%u %s=%s\n" */
130 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
131 for (tmp = len; tmp > 9; tmp /= 10)
132 len++;
133
7a604f16
PH
134 strbuf_grow(sb, len);
135 strbuf_addf(sb, "%u %s=", len, keyword);
136 strbuf_add(sb, value, valuelen);
137 strbuf_addch(sb, '\n');
3d74982f
RS
138}
139
140static unsigned int ustar_header_chksum(const struct ustar_header *header)
141{
a5a46eb9 142 const unsigned char *p = (const unsigned char *)header;
3d74982f 143 unsigned int chksum = 0;
a5a46eb9 144 while (p < (const unsigned char *)header->chksum)
3d74982f
RS
145 chksum += *p++;
146 chksum += sizeof(header->chksum) * ' ';
147 p += sizeof(header->chksum);
a5a46eb9 148 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
3d74982f
RS
149 chksum += *p++;
150 return chksum;
151}
152
562e25ab 153static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
3d74982f 154{
562e25ab 155 size_t i = pathlen;
22f0dcd9
RS
156 if (i > 1 && path[i - 1] == '/')
157 i--;
3d74982f
RS
158 if (i > maxlen)
159 i = maxlen;
160 do {
161 i--;
562e25ab 162 } while (i > 0 && path[i] != '/');
3d74982f
RS
163 return i;
164}
165
d240d410
NTND
166static void prepare_header(struct archiver_args *args,
167 struct ustar_header *header,
168 unsigned int mode, unsigned long size)
169{
170 sprintf(header->mode, "%07o", mode & 07777);
171 sprintf(header->size, "%011lo", S_ISREG(mode) ? size : 0);
172 sprintf(header->mtime, "%011lo", (unsigned long) args->time);
173
174 sprintf(header->uid, "%07o", 0);
175 sprintf(header->gid, "%07o", 0);
176 strlcpy(header->uname, "root", sizeof(header->uname));
177 strlcpy(header->gname, "root", sizeof(header->gname));
178 sprintf(header->devmajor, "%07o", 0);
179 sprintf(header->devminor, "%07o", 0);
180
181 memcpy(header->magic, "ustar", 6);
182 memcpy(header->version, "00", 2);
183
184 sprintf(header->chksum, "%07o", ustar_header_chksum(header));
185}
186
187static int write_extended_header(struct archiver_args *args,
188 const unsigned char *sha1,
189 const void *buffer, unsigned long size)
190{
191 struct ustar_header header;
192 unsigned int mode;
193 memset(&header, 0, sizeof(header));
194 *header.typeflag = TYPEFLAG_EXT_HEADER;
15c6ef7b 195 mode = 0100666;
d240d410
NTND
196 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
197 prepare_header(args, &header, mode, size);
198 write_blocked(&header, sizeof(header));
199 write_blocked(buffer, size);
200 return 0;
201}
202
562e25ab 203static int write_tar_entry(struct archiver_args *args,
9cb513b7
NTND
204 const unsigned char *sha1,
205 const char *path, size_t pathlen,
206 unsigned int mode)
3d74982f
RS
207{
208 struct ustar_header header;
f285a2d7 209 struct strbuf ext_header = STRBUF_INIT;
9cb513b7
NTND
210 unsigned int old_mode = mode;
211 unsigned long size;
212 void *buffer;
562e25ab 213 int err = 0;
3d74982f
RS
214
215 memset(&header, 0, sizeof(header));
3d74982f 216
85390709
NTND
217 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
218 *header.typeflag = TYPEFLAG_DIR;
219 mode = (mode | 0777) & ~tar_umask;
220 } else if (S_ISLNK(mode)) {
221 *header.typeflag = TYPEFLAG_LNK;
222 mode |= 0777;
223 } else if (S_ISREG(mode)) {
224 *header.typeflag = TYPEFLAG_REG;
225 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
3d74982f 226 } else {
85390709
NTND
227 return error("unsupported file mode: 0%o (SHA1: %s)",
228 mode, sha1_to_hex(sha1));
229 }
230 if (pathlen > sizeof(header.name)) {
231 size_t plen = get_path_prefix(path, pathlen,
232 sizeof(header.prefix));
233 size_t rest = pathlen - plen - 1;
234 if (plen > 0 && rest <= sizeof(header.name)) {
235 memcpy(header.prefix, path, plen);
108332c7 236 memcpy(header.name, path + plen + 1, rest);
3d74982f 237 } else {
85390709
NTND
238 sprintf(header.name, "%s.data",
239 sha1_to_hex(sha1));
240 strbuf_append_ext_header(&ext_header, "path",
241 path, pathlen);
3d74982f 242 }
85390709
NTND
243 } else
244 memcpy(header.name, path, pathlen);
3d74982f 245
5544049d
NTND
246 if (S_ISREG(mode) && !args->convert &&
247 sha1_object_info(sha1, &size) == OBJ_BLOB &&
248 size > big_file_threshold)
249 buffer = NULL;
250 else if (S_ISLNK(mode) || S_ISREG(mode)) {
9cb513b7
NTND
251 enum object_type type;
252 buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
253 if (!buffer)
254 return error("cannot read %s", sha1_to_hex(sha1));
255 } else {
256 buffer = NULL;
257 size = 0;
258 }
259
260 if (S_ISLNK(mode)) {
3d74982f
RS
261 if (size > sizeof(header.linkname)) {
262 sprintf(header.linkname, "see %s.paxheader",
263 sha1_to_hex(sha1));
264 strbuf_append_ext_header(&ext_header, "linkpath",
265 buffer, size);
266 } else
267 memcpy(header.linkname, buffer, size);
268 }
269
d240d410 270 prepare_header(args, &header, mode, size);
3d74982f
RS
271
272 if (ext_header.len > 0) {
d240d410
NTND
273 err = write_extended_header(args, sha1, ext_header.buf,
274 ext_header.len);
9cb513b7
NTND
275 if (err) {
276 free(buffer);
562e25ab 277 return err;
9cb513b7 278 }
3d74982f 279 }
7a604f16 280 strbuf_release(&ext_header);
3d74982f 281 write_blocked(&header, sizeof(header));
5544049d
NTND
282 if (S_ISREG(mode) && size > 0) {
283 if (buffer)
284 write_blocked(buffer, size);
285 else
286 err = stream_blocked(sha1);
287 }
9cb513b7 288 free(buffer);
562e25ab 289 return err;
3d74982f
RS
290}
291
562e25ab 292static int write_global_extended_header(struct archiver_args *args)
3d74982f 293{
562e25ab 294 const unsigned char *sha1 = args->commit_sha1;
f285a2d7 295 struct strbuf ext_header = STRBUF_INIT;
d240d410
NTND
296 struct ustar_header header;
297 unsigned int mode;
298 int err = 0;
7a604f16 299
3d74982f 300 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
d240d410
NTND
301 memset(&header, 0, sizeof(header));
302 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
15c6ef7b 303 mode = 0100666;
5096d490 304 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
d240d410
NTND
305 prepare_header(args, &header, mode, ext_header.len);
306 write_blocked(&header, sizeof(header));
307 write_blocked(ext_header.buf, ext_header.len);
7a604f16 308 strbuf_release(&ext_header);
562e25ab 309 return err;
3d74982f
RS
310}
311
767cf457
JK
312static struct archiver **tar_filters;
313static int nr_tar_filters;
314static int alloc_tar_filters;
315
316static struct archiver *find_tar_filter(const char *name, int len)
317{
318 int i;
319 for (i = 0; i < nr_tar_filters; i++) {
320 struct archiver *ar = tar_filters[i];
321 if (!strncmp(ar->name, name, len) && !ar->name[len])
322 return ar;
323 }
324 return NULL;
325}
326
327static int tar_filter_config(const char *var, const char *value, void *data)
328{
329 struct archiver *ar;
767cf457
JK
330 const char *name;
331 const char *type;
332 int namelen;
333
785a0429 334 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
767cf457 335 return 0;
767cf457
JK
336
337 ar = find_tar_filter(name, namelen);
338 if (!ar) {
339 ar = xcalloc(1, sizeof(*ar));
340 ar->name = xmemdupz(name, namelen);
341 ar->write_archive = write_tar_filter_archive;
342 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
343 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
344 tar_filters[nr_tar_filters++] = ar;
345 }
346
347 if (!strcmp(type, "command")) {
348 if (!value)
349 return config_error_nonbool(var);
350 free(ar->data);
351 ar->data = xstrdup(value);
352 return 0;
353 }
7b97730b
JK
354 if (!strcmp(type, "remote")) {
355 if (git_config_bool(var, value))
356 ar->flags |= ARCHIVER_REMOTE;
357 else
358 ar->flags &= ~ARCHIVER_REMOTE;
359 return 0;
360 }
767cf457
JK
361
362 return 0;
363}
364
ef90d6d4 365static int git_tar_config(const char *var, const char *value, void *cb)
3d74982f
RS
366{
367 if (!strcmp(var, "tar.umask")) {
cc1816b0 368 if (value && !strcmp(value, "user")) {
3d74982f
RS
369 tar_umask = umask(0);
370 umask(tar_umask);
371 } else {
372 tar_umask = git_config_int(var, value);
373 }
374 return 0;
375 }
767cf457
JK
376
377 return tar_filter_config(var, value, cb);
3d74982f
RS
378}
379
4d7c9898
JK
380static int write_tar_archive(const struct archiver *ar,
381 struct archiver_args *args)
3d74982f 382{
562e25ab 383 int err = 0;
3d74982f 384
3d74982f 385 if (args->commit_sha1)
562e25ab
RS
386 err = write_global_extended_header(args);
387 if (!err)
388 err = write_archive_entries(args, write_tar_entry);
389 if (!err)
390 write_trailer();
391 return err;
3d74982f 392}
13e0f88d 393
767cf457
JK
394static int write_tar_filter_archive(const struct archiver *ar,
395 struct archiver_args *args)
396{
397 struct strbuf cmd = STRBUF_INIT;
d3180279 398 struct child_process filter = CHILD_PROCESS_INIT;
767cf457
JK
399 const char *argv[2];
400 int r;
401
402 if (!ar->data)
403 die("BUG: tar-filter archiver called with no filter defined");
404
405 strbuf_addstr(&cmd, ar->data);
406 if (args->compression_level >= 0)
407 strbuf_addf(&cmd, " -%d", args->compression_level);
408
767cf457
JK
409 argv[0] = cmd.buf;
410 argv[1] = NULL;
411 filter.argv = argv;
412 filter.use_shell = 1;
413 filter.in = -1;
414
415 if (start_command(&filter) < 0)
416 die_errno("unable to start '%s' filter", argv[0]);
417 close(1);
418 if (dup2(filter.in, 1) < 0)
419 die_errno("unable to redirect descriptor");
420 close(filter.in);
421
422 r = write_tar_archive(ar, args);
423
424 close(1);
425 if (finish_command(&filter) != 0)
426 die("'%s' filter reported error", argv[0]);
427
428 strbuf_release(&cmd);
429 return r;
430}
431
13e0f88d
JK
432static struct archiver tar_archiver = {
433 "tar",
434 write_tar_archive,
7b97730b 435 ARCHIVER_REMOTE
13e0f88d
JK
436};
437
438void init_tar_archiver(void)
439{
767cf457 440 int i;
13e0f88d 441 register_archiver(&tar_archiver);
767cf457 442
0e804e09 443 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
7b97730b 444 tar_filter_config("tar.tgz.remote", "true", NULL);
0e804e09 445 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
7b97730b 446 tar_filter_config("tar.tar.gz.remote", "true", NULL);
13e0f88d 447 git_config(git_tar_config, NULL);
767cf457
JK
448 for (i = 0; i < nr_tar_filters; i++) {
449 /* omit any filters that never had a command configured */
450 if (tar_filters[i]->data)
451 register_archiver(tar_filters[i]);
452 }
13e0f88d 453}