2 * Copyright (c) 2005, 2006 Rene Scharfe
4 #include "git-compat-util.h"
11 #include "object-store-ll.h"
12 #include "streaming.h"
13 #include "run-command.h"
14 #include "write-or-die.h"
16 #define RECORDSIZE (512)
17 #define BLOCKSIZE (RECORDSIZE * 20)
19 static char block
[BLOCKSIZE
];
20 static unsigned long offset
;
22 static int tar_umask
= 002;
24 static int write_tar_filter_archive(const struct archiver
*ar
,
25 struct archiver_args
*args
);
28 * This is the max value that a ustar size header can specify, as it is fixed
29 * at 11 octal digits. POSIX specifies that we switch to extended headers at
32 * Likewise for the mtime (which happens to use a buffer of the same size).
34 #if ULONG_MAX == 0xFFFFFFFF
35 #define USTAR_MAX_SIZE ULONG_MAX
37 #define USTAR_MAX_SIZE 077777777777UL
39 #if TIME_MAX == 0xFFFFFFFF
40 #define USTAR_MAX_MTIME TIME_MAX
42 #define USTAR_MAX_MTIME 077777777777ULL
45 static void tar_write_block(const void *buf
)
47 write_or_die(1, buf
, BLOCKSIZE
);
50 static void (*write_block
)(const void *) = tar_write_block
;
52 /* writes out the whole block, but only if it is full */
53 static void write_if_needed(void)
55 if (offset
== BLOCKSIZE
) {
62 * queues up writes, so that all our write(2) calls write exactly one
63 * full block; pads writes to RECORDSIZE
65 static void do_write_blocked(const void *data
, unsigned long size
)
67 const char *buf
= data
;
70 unsigned long chunk
= BLOCKSIZE
- offset
;
73 memcpy(block
+ offset
, buf
, chunk
);
79 while (size
>= BLOCKSIZE
) {
85 memcpy(block
+ offset
, buf
, size
);
90 static void finish_record(void)
93 tail
= offset
% RECORDSIZE
;
95 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
96 offset
+= RECORDSIZE
- tail
;
101 static void write_blocked(const void *data
, unsigned long size
)
103 do_write_blocked(data
, size
);
108 * The end of tar archives is marked by 2*512 nul bytes and after that
109 * follows the rest of the block (if any).
111 static void write_trailer(void)
113 int tail
= BLOCKSIZE
- offset
;
114 memset(block
+ offset
, 0, tail
);
116 if (tail
< 2 * RECORDSIZE
) {
117 memset(block
, 0, offset
);
123 * queues up writes, so that all our write(2) calls write exactly one
124 * full block; pads writes to RECORDSIZE
126 static int stream_blocked(struct repository
*r
, const struct object_id
*oid
)
128 struct git_istream
*st
;
129 enum object_type type
;
134 st
= open_istream(r
, oid
, &type
, &sz
, NULL
);
136 return error(_("cannot stream blob %s"), oid_to_hex(oid
));
138 readlen
= read_istream(st
, buf
, sizeof(buf
));
141 do_write_blocked(buf
, readlen
);
150 * pax extended header records have the format "%u %s=%s\n". %u contains
151 * the size of the whole string (including the %u), the first %s is the
152 * keyword, the second one is the value. This function constructs such a
153 * string and appends it to a struct strbuf.
155 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
156 const char *value
, size_t valuelen
)
158 size_t orig_len
= sb
->len
;
162 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
163 for (tmp
= 1; len
/ 10 >= tmp
; tmp
*= 10)
166 strbuf_grow(sb
, len
);
167 strbuf_addf(sb
, "%"PRIuMAX
" %s=", (uintmax_t)len
, keyword
);
168 strbuf_add(sb
, value
, valuelen
);
169 strbuf_addch(sb
, '\n');
171 if (len
!= sb
->len
- orig_len
)
172 BUG("pax extended header length miscalculated as %"PRIuMAX
173 ", should be %"PRIuMAX
,
174 (uintmax_t)len
, (uintmax_t)(sb
->len
- orig_len
));
178 * Like strbuf_append_ext_header, but for numeric values.
180 static void strbuf_append_ext_header_uint(struct strbuf
*sb
,
184 char buf
[40]; /* big enough for 2^128 in decimal, plus NUL */
187 len
= xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, value
);
188 strbuf_append_ext_header(sb
, keyword
, buf
, len
);
191 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
193 const unsigned char *p
= (const unsigned char *)header
;
194 unsigned int chksum
= 0;
195 while (p
< (const unsigned char *)header
->chksum
)
197 chksum
+= sizeof(header
->chksum
) * ' ';
198 p
+= sizeof(header
->chksum
);
199 while (p
< (const unsigned char *)header
+ sizeof(struct ustar_header
))
204 static size_t get_path_prefix(const char *path
, size_t pathlen
, size_t maxlen
)
207 if (i
> 1 && path
[i
- 1] == '/')
213 } while (i
> 0 && path
[i
] != '/');
217 static void prepare_header(struct archiver_args
*args
,
218 struct ustar_header
*header
,
219 unsigned int mode
, unsigned long size
)
221 xsnprintf(header
->mode
, sizeof(header
->mode
), "%07o", mode
& 07777);
222 xsnprintf(header
->size
, sizeof(header
->size
), "%011"PRIoMAX
, S_ISREG(mode
) ? (uintmax_t)size
: (uintmax_t)0);
223 xsnprintf(header
->mtime
, sizeof(header
->mtime
), "%011lo", (unsigned long) args
->time
);
225 xsnprintf(header
->uid
, sizeof(header
->uid
), "%07o", 0);
226 xsnprintf(header
->gid
, sizeof(header
->gid
), "%07o", 0);
227 strlcpy(header
->uname
, "root", sizeof(header
->uname
));
228 strlcpy(header
->gname
, "root", sizeof(header
->gname
));
229 xsnprintf(header
->devmajor
, sizeof(header
->devmajor
), "%07o", 0);
230 xsnprintf(header
->devminor
, sizeof(header
->devminor
), "%07o", 0);
232 memcpy(header
->magic
, "ustar", 6);
233 memcpy(header
->version
, "00", 2);
235 xsnprintf(header
->chksum
, sizeof(header
->chksum
), "%07o", ustar_header_chksum(header
));
238 static void write_extended_header(struct archiver_args
*args
,
239 const struct object_id
*oid
,
240 const void *buffer
, unsigned long size
)
242 struct ustar_header header
;
244 memset(&header
, 0, sizeof(header
));
245 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
247 xsnprintf(header
.name
, sizeof(header
.name
), "%s.paxheader", oid_to_hex(oid
));
248 prepare_header(args
, &header
, mode
, size
);
249 write_blocked(&header
, sizeof(header
));
250 write_blocked(buffer
, size
);
253 static int write_tar_entry(struct archiver_args
*args
,
254 const struct object_id
*oid
,
255 const char *path
, size_t pathlen
,
257 void *buffer
, unsigned long size
)
259 struct ustar_header header
;
260 struct strbuf ext_header
= STRBUF_INIT
;
261 unsigned long size_in_header
;
264 memset(&header
, 0, sizeof(header
));
266 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
267 *header
.typeflag
= TYPEFLAG_DIR
;
268 mode
= (mode
| 0777) & ~tar_umask
;
269 } else if (S_ISLNK(mode
)) {
270 *header
.typeflag
= TYPEFLAG_LNK
;
272 } else if (S_ISREG(mode
)) {
273 *header
.typeflag
= TYPEFLAG_REG
;
274 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
276 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
277 mode
, oid_to_hex(oid
));
279 if (pathlen
> sizeof(header
.name
)) {
280 size_t plen
= get_path_prefix(path
, pathlen
,
281 sizeof(header
.prefix
));
282 size_t rest
= pathlen
- plen
- 1;
283 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
284 memcpy(header
.prefix
, path
, plen
);
285 memcpy(header
.name
, path
+ plen
+ 1, rest
);
287 xsnprintf(header
.name
, sizeof(header
.name
), "%s.data",
289 strbuf_append_ext_header(&ext_header
, "path",
293 memcpy(header
.name
, path
, pathlen
);
296 if (size
> sizeof(header
.linkname
)) {
297 xsnprintf(header
.linkname
, sizeof(header
.linkname
),
298 "see %s.paxheader", oid_to_hex(oid
));
299 strbuf_append_ext_header(&ext_header
, "linkpath",
302 memcpy(header
.linkname
, buffer
, size
);
305 size_in_header
= size
;
306 if (S_ISREG(mode
) && size
> USTAR_MAX_SIZE
) {
308 strbuf_append_ext_header_uint(&ext_header
, "size", size
);
311 prepare_header(args
, &header
, mode
, size_in_header
);
313 if (ext_header
.len
> 0) {
314 write_extended_header(args
, oid
, ext_header
.buf
,
317 strbuf_release(&ext_header
);
318 write_blocked(&header
, sizeof(header
));
319 if (S_ISREG(mode
) && size
> 0) {
321 write_blocked(buffer
, size
);
323 err
= stream_blocked(args
->repo
, oid
);
328 static void write_global_extended_header(struct archiver_args
*args
)
330 const struct object_id
*oid
= args
->commit_oid
;
331 struct strbuf ext_header
= STRBUF_INIT
;
332 struct ustar_header header
;
336 strbuf_append_ext_header(&ext_header
, "comment",
338 the_hash_algo
->hexsz
);
339 if (args
->time
> USTAR_MAX_MTIME
) {
340 strbuf_append_ext_header_uint(&ext_header
, "mtime",
342 args
->time
= USTAR_MAX_MTIME
;
348 memset(&header
, 0, sizeof(header
));
349 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
351 xsnprintf(header
.name
, sizeof(header
.name
), "pax_global_header");
352 prepare_header(args
, &header
, mode
, ext_header
.len
);
353 write_blocked(&header
, sizeof(header
));
354 write_blocked(ext_header
.buf
, ext_header
.len
);
355 strbuf_release(&ext_header
);
358 static struct archiver
**tar_filters
;
359 static int nr_tar_filters
;
360 static int alloc_tar_filters
;
362 static struct archiver
*find_tar_filter(const char *name
, size_t len
)
365 for (i
= 0; i
< nr_tar_filters
; i
++) {
366 struct archiver
*ar
= tar_filters
[i
];
367 if (!strncmp(ar
->name
, name
, len
) && !ar
->name
[len
])
373 static int tar_filter_config(const char *var
, const char *value
,
381 if (parse_config_key(var
, "tar", &name
, &namelen
, &type
) < 0 || !name
)
384 ar
= find_tar_filter(name
, namelen
);
387 ar
->name
= xmemdupz(name
, namelen
);
388 ar
->write_archive
= write_tar_filter_archive
;
389 ar
->flags
= ARCHIVER_WANT_COMPRESSION_LEVELS
|
390 ARCHIVER_HIGH_COMPRESSION_LEVELS
;
391 ALLOC_GROW(tar_filters
, nr_tar_filters
+ 1, alloc_tar_filters
);
392 tar_filters
[nr_tar_filters
++] = ar
;
395 if (!strcmp(type
, "command")) {
397 return config_error_nonbool(var
);
398 free(ar
->filter_command
);
399 ar
->filter_command
= xstrdup(value
);
402 if (!strcmp(type
, "remote")) {
403 if (git_config_bool(var
, value
))
404 ar
->flags
|= ARCHIVER_REMOTE
;
406 ar
->flags
&= ~ARCHIVER_REMOTE
;
413 static int git_tar_config(const char *var
, const char *value
,
414 const struct config_context
*ctx
, void *cb
)
416 if (!strcmp(var
, "tar.umask")) {
417 if (value
&& !strcmp(value
, "user")) {
418 tar_umask
= umask(0);
421 tar_umask
= git_config_int(var
, value
, ctx
->kvi
);
426 return tar_filter_config(var
, value
, cb
);
429 static int write_tar_archive(const struct archiver
*ar UNUSED
,
430 struct archiver_args
*args
)
434 write_global_extended_header(args
);
435 err
= write_archive_entries(args
, write_tar_entry
);
441 static git_zstream gzstream
;
442 static unsigned char outbuf
[16384];
444 static void tgz_deflate(int flush
)
446 while (gzstream
.avail_in
|| flush
== Z_FINISH
) {
447 int status
= git_deflate(&gzstream
, flush
);
448 if (!gzstream
.avail_out
|| status
== Z_STREAM_END
) {
449 write_or_die(1, outbuf
, gzstream
.next_out
- outbuf
);
450 gzstream
.next_out
= outbuf
;
451 gzstream
.avail_out
= sizeof(outbuf
);
452 if (status
== Z_STREAM_END
)
455 if (status
!= Z_OK
&& status
!= Z_BUF_ERROR
)
456 die(_("deflate error (%d)"), status
);
460 static void tgz_write_block(const void *data
)
462 gzstream
.next_in
= (void *)data
;
463 gzstream
.avail_in
= BLOCKSIZE
;
464 tgz_deflate(Z_NO_FLUSH
);
467 static const char internal_gzip_command
[] = "git archive gzip";
469 static int write_tar_filter_archive(const struct archiver
*ar
,
470 struct archiver_args
*args
)
472 #if ZLIB_VERNUM >= 0x1221
473 struct gz_header_s gzhead
= { .os
= 3 }; /* Unix, for reproducibility */
475 struct strbuf cmd
= STRBUF_INIT
;
476 struct child_process filter
= CHILD_PROCESS_INIT
;
479 if (!ar
->filter_command
)
480 BUG("tar-filter archiver called with no filter defined");
482 if (!strcmp(ar
->filter_command
, internal_gzip_command
)) {
483 write_block
= tgz_write_block
;
484 git_deflate_init_gzip(&gzstream
, args
->compression_level
);
485 #if ZLIB_VERNUM >= 0x1221
486 if (deflateSetHeader(&gzstream
.z
, &gzhead
) != Z_OK
)
487 BUG("deflateSetHeader() called too late");
489 gzstream
.next_out
= outbuf
;
490 gzstream
.avail_out
= sizeof(outbuf
);
492 r
= write_tar_archive(ar
, args
);
494 tgz_deflate(Z_FINISH
);
495 git_deflate_end(&gzstream
);
499 strbuf_addstr(&cmd
, ar
->filter_command
);
500 if (args
->compression_level
>= 0)
501 strbuf_addf(&cmd
, " -%d", args
->compression_level
);
503 strvec_push(&filter
.args
, cmd
.buf
);
504 filter
.use_shell
= 1;
506 filter
.silent_exec_failure
= 1;
508 if (start_command(&filter
) < 0)
509 die_errno(_("unable to start '%s' filter"), cmd
.buf
);
511 if (dup2(filter
.in
, 1) < 0)
512 die_errno(_("unable to redirect descriptor"));
515 r
= write_tar_archive(ar
, args
);
518 if (finish_command(&filter
) != 0)
519 die(_("'%s' filter reported error"), cmd
.buf
);
521 strbuf_release(&cmd
);
525 static struct archiver tar_archiver
= {
527 .write_archive
= write_tar_archive
,
528 .flags
= ARCHIVER_REMOTE
,
531 void init_tar_archiver(void)
534 register_archiver(&tar_archiver
);
536 tar_filter_config("tar.tgz.command", internal_gzip_command
, NULL
);
537 tar_filter_config("tar.tgz.remote", "true", NULL
);
538 tar_filter_config("tar.tar.gz.command", internal_gzip_command
, NULL
);
539 tar_filter_config("tar.tar.gz.remote", "true", NULL
);
540 git_config(git_tar_config
, NULL
);
541 for (i
= 0; i
< nr_tar_filters
; i
++) {
542 /* omit any filters that never had a command configured */
543 if (tar_filters
[i
]->filter_command
)
544 register_archiver(tar_filters
[i
]);