2 * Copyright (c) 2005, 2006 Rene Scharfe
4 #include "git-compat-util.h"
12 #include "object-store.h"
13 #include "streaming.h"
14 #include "run-command.h"
15 #include "write-or-die.h"
17 #define RECORDSIZE (512)
18 #define BLOCKSIZE (RECORDSIZE * 20)
20 static char block
[BLOCKSIZE
];
21 static unsigned long offset
;
23 static int tar_umask
= 002;
25 static int write_tar_filter_archive(const struct archiver
*ar
,
26 struct archiver_args
*args
);
29 * This is the max value that a ustar size header can specify, as it is fixed
30 * at 11 octal digits. POSIX specifies that we switch to extended headers at
33 * Likewise for the mtime (which happens to use a buffer of the same size).
35 #if ULONG_MAX == 0xFFFFFFFF
36 #define USTAR_MAX_SIZE ULONG_MAX
38 #define USTAR_MAX_SIZE 077777777777UL
40 #if TIME_MAX == 0xFFFFFFFF
41 #define USTAR_MAX_MTIME TIME_MAX
43 #define USTAR_MAX_MTIME 077777777777ULL
46 static void tar_write_block(const void *buf
)
48 write_or_die(1, buf
, BLOCKSIZE
);
51 static void (*write_block
)(const void *) = tar_write_block
;
53 /* writes out the whole block, but only if it is full */
54 static void write_if_needed(void)
56 if (offset
== BLOCKSIZE
) {
63 * queues up writes, so that all our write(2) calls write exactly one
64 * full block; pads writes to RECORDSIZE
66 static void do_write_blocked(const void *data
, unsigned long size
)
68 const char *buf
= data
;
71 unsigned long chunk
= BLOCKSIZE
- offset
;
74 memcpy(block
+ offset
, buf
, chunk
);
80 while (size
>= BLOCKSIZE
) {
86 memcpy(block
+ offset
, buf
, size
);
91 static void finish_record(void)
94 tail
= offset
% RECORDSIZE
;
96 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
97 offset
+= RECORDSIZE
- tail
;
102 static void write_blocked(const void *data
, unsigned long size
)
104 do_write_blocked(data
, size
);
109 * The end of tar archives is marked by 2*512 nul bytes and after that
110 * follows the rest of the block (if any).
112 static void write_trailer(void)
114 int tail
= BLOCKSIZE
- offset
;
115 memset(block
+ offset
, 0, tail
);
117 if (tail
< 2 * RECORDSIZE
) {
118 memset(block
, 0, offset
);
124 * queues up writes, so that all our write(2) calls write exactly one
125 * full block; pads writes to RECORDSIZE
127 static int stream_blocked(struct repository
*r
, const struct object_id
*oid
)
129 struct git_istream
*st
;
130 enum object_type type
;
135 st
= open_istream(r
, oid
, &type
, &sz
, NULL
);
137 return error(_("cannot stream blob %s"), oid_to_hex(oid
));
139 readlen
= read_istream(st
, buf
, sizeof(buf
));
142 do_write_blocked(buf
, readlen
);
151 * pax extended header records have the format "%u %s=%s\n". %u contains
152 * the size of the whole string (including the %u), the first %s is the
153 * keyword, the second one is the value. This function constructs such a
154 * string and appends it to a struct strbuf.
156 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
157 const char *value
, size_t valuelen
)
159 size_t orig_len
= sb
->len
;
163 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
164 for (tmp
= 1; len
/ 10 >= tmp
; tmp
*= 10)
167 strbuf_grow(sb
, len
);
168 strbuf_addf(sb
, "%"PRIuMAX
" %s=", (uintmax_t)len
, keyword
);
169 strbuf_add(sb
, value
, valuelen
);
170 strbuf_addch(sb
, '\n');
172 if (len
!= sb
->len
- orig_len
)
173 BUG("pax extended header length miscalculated as %"PRIuMAX
174 ", should be %"PRIuMAX
,
175 (uintmax_t)len
, (uintmax_t)(sb
->len
- orig_len
));
179 * Like strbuf_append_ext_header, but for numeric values.
181 static void strbuf_append_ext_header_uint(struct strbuf
*sb
,
185 char buf
[40]; /* big enough for 2^128 in decimal, plus NUL */
188 len
= xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, value
);
189 strbuf_append_ext_header(sb
, keyword
, buf
, len
);
192 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
194 const unsigned char *p
= (const unsigned char *)header
;
195 unsigned int chksum
= 0;
196 while (p
< (const unsigned char *)header
->chksum
)
198 chksum
+= sizeof(header
->chksum
) * ' ';
199 p
+= sizeof(header
->chksum
);
200 while (p
< (const unsigned char *)header
+ sizeof(struct ustar_header
))
205 static size_t get_path_prefix(const char *path
, size_t pathlen
, size_t maxlen
)
208 if (i
> 1 && path
[i
- 1] == '/')
214 } while (i
> 0 && path
[i
] != '/');
218 static void prepare_header(struct archiver_args
*args
,
219 struct ustar_header
*header
,
220 unsigned int mode
, unsigned long size
)
222 xsnprintf(header
->mode
, sizeof(header
->mode
), "%07o", mode
& 07777);
223 xsnprintf(header
->size
, sizeof(header
->size
), "%011"PRIoMAX
, S_ISREG(mode
) ? (uintmax_t)size
: (uintmax_t)0);
224 xsnprintf(header
->mtime
, sizeof(header
->mtime
), "%011lo", (unsigned long) args
->time
);
226 xsnprintf(header
->uid
, sizeof(header
->uid
), "%07o", 0);
227 xsnprintf(header
->gid
, sizeof(header
->gid
), "%07o", 0);
228 strlcpy(header
->uname
, "root", sizeof(header
->uname
));
229 strlcpy(header
->gname
, "root", sizeof(header
->gname
));
230 xsnprintf(header
->devmajor
, sizeof(header
->devmajor
), "%07o", 0);
231 xsnprintf(header
->devminor
, sizeof(header
->devminor
), "%07o", 0);
233 memcpy(header
->magic
, "ustar", 6);
234 memcpy(header
->version
, "00", 2);
236 xsnprintf(header
->chksum
, sizeof(header
->chksum
), "%07o", ustar_header_chksum(header
));
239 static void write_extended_header(struct archiver_args
*args
,
240 const struct object_id
*oid
,
241 const void *buffer
, unsigned long size
)
243 struct ustar_header header
;
245 memset(&header
, 0, sizeof(header
));
246 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
248 xsnprintf(header
.name
, sizeof(header
.name
), "%s.paxheader", oid_to_hex(oid
));
249 prepare_header(args
, &header
, mode
, size
);
250 write_blocked(&header
, sizeof(header
));
251 write_blocked(buffer
, size
);
254 static int write_tar_entry(struct archiver_args
*args
,
255 const struct object_id
*oid
,
256 const char *path
, size_t pathlen
,
258 void *buffer
, unsigned long size
)
260 struct ustar_header header
;
261 struct strbuf ext_header
= STRBUF_INIT
;
262 unsigned long size_in_header
;
265 memset(&header
, 0, sizeof(header
));
267 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
268 *header
.typeflag
= TYPEFLAG_DIR
;
269 mode
= (mode
| 0777) & ~tar_umask
;
270 } else if (S_ISLNK(mode
)) {
271 *header
.typeflag
= TYPEFLAG_LNK
;
273 } else if (S_ISREG(mode
)) {
274 *header
.typeflag
= TYPEFLAG_REG
;
275 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
277 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
278 mode
, oid_to_hex(oid
));
280 if (pathlen
> sizeof(header
.name
)) {
281 size_t plen
= get_path_prefix(path
, pathlen
,
282 sizeof(header
.prefix
));
283 size_t rest
= pathlen
- plen
- 1;
284 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
285 memcpy(header
.prefix
, path
, plen
);
286 memcpy(header
.name
, path
+ plen
+ 1, rest
);
288 xsnprintf(header
.name
, sizeof(header
.name
), "%s.data",
290 strbuf_append_ext_header(&ext_header
, "path",
294 memcpy(header
.name
, path
, pathlen
);
297 if (size
> sizeof(header
.linkname
)) {
298 xsnprintf(header
.linkname
, sizeof(header
.linkname
),
299 "see %s.paxheader", oid_to_hex(oid
));
300 strbuf_append_ext_header(&ext_header
, "linkpath",
303 memcpy(header
.linkname
, buffer
, size
);
306 size_in_header
= size
;
307 if (S_ISREG(mode
) && size
> USTAR_MAX_SIZE
) {
309 strbuf_append_ext_header_uint(&ext_header
, "size", size
);
312 prepare_header(args
, &header
, mode
, size_in_header
);
314 if (ext_header
.len
> 0) {
315 write_extended_header(args
, oid
, ext_header
.buf
,
318 strbuf_release(&ext_header
);
319 write_blocked(&header
, sizeof(header
));
320 if (S_ISREG(mode
) && size
> 0) {
322 write_blocked(buffer
, size
);
324 err
= stream_blocked(args
->repo
, oid
);
329 static void write_global_extended_header(struct archiver_args
*args
)
331 const struct object_id
*oid
= args
->commit_oid
;
332 struct strbuf ext_header
= STRBUF_INIT
;
333 struct ustar_header header
;
337 strbuf_append_ext_header(&ext_header
, "comment",
339 the_hash_algo
->hexsz
);
340 if (args
->time
> USTAR_MAX_MTIME
) {
341 strbuf_append_ext_header_uint(&ext_header
, "mtime",
343 args
->time
= USTAR_MAX_MTIME
;
349 memset(&header
, 0, sizeof(header
));
350 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
352 xsnprintf(header
.name
, sizeof(header
.name
), "pax_global_header");
353 prepare_header(args
, &header
, mode
, ext_header
.len
);
354 write_blocked(&header
, sizeof(header
));
355 write_blocked(ext_header
.buf
, ext_header
.len
);
356 strbuf_release(&ext_header
);
359 static struct archiver
**tar_filters
;
360 static int nr_tar_filters
;
361 static int alloc_tar_filters
;
363 static struct archiver
*find_tar_filter(const char *name
, size_t len
)
366 for (i
= 0; i
< nr_tar_filters
; i
++) {
367 struct archiver
*ar
= tar_filters
[i
];
368 if (!strncmp(ar
->name
, name
, len
) && !ar
->name
[len
])
374 static int tar_filter_config(const char *var
, const char *value
,
382 if (parse_config_key(var
, "tar", &name
, &namelen
, &type
) < 0 || !name
)
385 ar
= find_tar_filter(name
, namelen
);
388 ar
->name
= xmemdupz(name
, namelen
);
389 ar
->write_archive
= write_tar_filter_archive
;
390 ar
->flags
= ARCHIVER_WANT_COMPRESSION_LEVELS
|
391 ARCHIVER_HIGH_COMPRESSION_LEVELS
;
392 ALLOC_GROW(tar_filters
, nr_tar_filters
+ 1, alloc_tar_filters
);
393 tar_filters
[nr_tar_filters
++] = ar
;
396 if (!strcmp(type
, "command")) {
398 return config_error_nonbool(var
);
399 free(ar
->filter_command
);
400 ar
->filter_command
= xstrdup(value
);
403 if (!strcmp(type
, "remote")) {
404 if (git_config_bool(var
, value
))
405 ar
->flags
|= ARCHIVER_REMOTE
;
407 ar
->flags
&= ~ARCHIVER_REMOTE
;
414 static int git_tar_config(const char *var
, const char *value
, 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
);
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
]);