2 * Copyright (c) 2005, 2006 Rene Scharfe
8 #include "object-store.h"
10 #include "run-command.h"
12 #define RECORDSIZE (512)
13 #define BLOCKSIZE (RECORDSIZE * 20)
15 static char block
[BLOCKSIZE
];
16 static unsigned long offset
;
18 static int tar_umask
= 002;
20 static int write_tar_filter_archive(const struct archiver
*ar
,
21 struct archiver_args
*args
);
24 * This is the max value that a ustar size header can specify, as it is fixed
25 * at 11 octal digits. POSIX specifies that we switch to extended headers at
28 * Likewise for the mtime (which happens to use a buffer of the same size).
30 #if ULONG_MAX == 0xFFFFFFFF
31 #define USTAR_MAX_SIZE ULONG_MAX
33 #define USTAR_MAX_SIZE 077777777777UL
35 #if TIME_MAX == 0xFFFFFFFF
36 #define USTAR_MAX_MTIME TIME_MAX
38 #define USTAR_MAX_MTIME 077777777777ULL
41 static void tar_write_block(const void *buf
)
43 write_or_die(1, buf
, BLOCKSIZE
);
46 static void (*write_block
)(const void *) = tar_write_block
;
48 /* writes out the whole block, but only if it is full */
49 static void write_if_needed(void)
51 if (offset
== BLOCKSIZE
) {
58 * queues up writes, so that all our write(2) calls write exactly one
59 * full block; pads writes to RECORDSIZE
61 static void do_write_blocked(const void *data
, unsigned long size
)
63 const char *buf
= data
;
66 unsigned long chunk
= BLOCKSIZE
- offset
;
69 memcpy(block
+ offset
, buf
, chunk
);
75 while (size
>= BLOCKSIZE
) {
81 memcpy(block
+ offset
, buf
, size
);
86 static void finish_record(void)
89 tail
= offset
% RECORDSIZE
;
91 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
92 offset
+= RECORDSIZE
- tail
;
97 static void write_blocked(const void *data
, unsigned long size
)
99 do_write_blocked(data
, size
);
104 * The end of tar archives is marked by 2*512 nul bytes and after that
105 * follows the rest of the block (if any).
107 static void write_trailer(void)
109 int tail
= BLOCKSIZE
- offset
;
110 memset(block
+ offset
, 0, tail
);
112 if (tail
< 2 * RECORDSIZE
) {
113 memset(block
, 0, offset
);
119 * queues up writes, so that all our write(2) calls write exactly one
120 * full block; pads writes to RECORDSIZE
122 static int stream_blocked(struct repository
*r
, const struct object_id
*oid
)
124 struct git_istream
*st
;
125 enum object_type type
;
130 st
= open_istream(r
, oid
, &type
, &sz
, NULL
);
132 return error(_("cannot stream blob %s"), oid_to_hex(oid
));
134 readlen
= read_istream(st
, buf
, sizeof(buf
));
137 do_write_blocked(buf
, readlen
);
146 * pax extended header records have the format "%u %s=%s\n". %u contains
147 * the size of the whole string (including the %u), the first %s is the
148 * keyword, the second one is the value. This function constructs such a
149 * string and appends it to a struct strbuf.
151 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
152 const char *value
, size_t valuelen
)
154 size_t orig_len
= sb
->len
;
158 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
159 for (tmp
= 1; len
/ 10 >= tmp
; tmp
*= 10)
162 strbuf_grow(sb
, len
);
163 strbuf_addf(sb
, "%"PRIuMAX
" %s=", (uintmax_t)len
, keyword
);
164 strbuf_add(sb
, value
, valuelen
);
165 strbuf_addch(sb
, '\n');
167 if (len
!= sb
->len
- orig_len
)
168 BUG("pax extended header length miscalculated as %"PRIuMAX
169 ", should be %"PRIuMAX
,
170 (uintmax_t)len
, (uintmax_t)(sb
->len
- orig_len
));
174 * Like strbuf_append_ext_header, but for numeric values.
176 static void strbuf_append_ext_header_uint(struct strbuf
*sb
,
180 char buf
[40]; /* big enough for 2^128 in decimal, plus NUL */
183 len
= xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, value
);
184 strbuf_append_ext_header(sb
, keyword
, buf
, len
);
187 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
189 const unsigned char *p
= (const unsigned char *)header
;
190 unsigned int chksum
= 0;
191 while (p
< (const unsigned char *)header
->chksum
)
193 chksum
+= sizeof(header
->chksum
) * ' ';
194 p
+= sizeof(header
->chksum
);
195 while (p
< (const unsigned char *)header
+ sizeof(struct ustar_header
))
200 static size_t get_path_prefix(const char *path
, size_t pathlen
, size_t maxlen
)
203 if (i
> 1 && path
[i
- 1] == '/')
209 } while (i
> 0 && path
[i
] != '/');
213 static void prepare_header(struct archiver_args
*args
,
214 struct ustar_header
*header
,
215 unsigned int mode
, unsigned long size
)
217 xsnprintf(header
->mode
, sizeof(header
->mode
), "%07o", mode
& 07777);
218 xsnprintf(header
->size
, sizeof(header
->size
), "%011"PRIoMAX
, S_ISREG(mode
) ? (uintmax_t)size
: (uintmax_t)0);
219 xsnprintf(header
->mtime
, sizeof(header
->mtime
), "%011lo", (unsigned long) args
->time
);
221 xsnprintf(header
->uid
, sizeof(header
->uid
), "%07o", 0);
222 xsnprintf(header
->gid
, sizeof(header
->gid
), "%07o", 0);
223 strlcpy(header
->uname
, "root", sizeof(header
->uname
));
224 strlcpy(header
->gname
, "root", sizeof(header
->gname
));
225 xsnprintf(header
->devmajor
, sizeof(header
->devmajor
), "%07o", 0);
226 xsnprintf(header
->devminor
, sizeof(header
->devminor
), "%07o", 0);
228 memcpy(header
->magic
, "ustar", 6);
229 memcpy(header
->version
, "00", 2);
231 xsnprintf(header
->chksum
, sizeof(header
->chksum
), "%07o", ustar_header_chksum(header
));
234 static void write_extended_header(struct archiver_args
*args
,
235 const struct object_id
*oid
,
236 const void *buffer
, unsigned long size
)
238 struct ustar_header header
;
240 memset(&header
, 0, sizeof(header
));
241 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
243 xsnprintf(header
.name
, sizeof(header
.name
), "%s.paxheader", oid_to_hex(oid
));
244 prepare_header(args
, &header
, mode
, size
);
245 write_blocked(&header
, sizeof(header
));
246 write_blocked(buffer
, size
);
249 static int write_tar_entry(struct archiver_args
*args
,
250 const struct object_id
*oid
,
251 const char *path
, size_t pathlen
,
253 void *buffer
, unsigned long size
)
255 struct ustar_header header
;
256 struct strbuf ext_header
= STRBUF_INIT
;
257 unsigned long size_in_header
;
260 memset(&header
, 0, sizeof(header
));
262 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
263 *header
.typeflag
= TYPEFLAG_DIR
;
264 mode
= (mode
| 0777) & ~tar_umask
;
265 } else if (S_ISLNK(mode
)) {
266 *header
.typeflag
= TYPEFLAG_LNK
;
268 } else if (S_ISREG(mode
)) {
269 *header
.typeflag
= TYPEFLAG_REG
;
270 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
272 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
273 mode
, oid_to_hex(oid
));
275 if (pathlen
> sizeof(header
.name
)) {
276 size_t plen
= get_path_prefix(path
, pathlen
,
277 sizeof(header
.prefix
));
278 size_t rest
= pathlen
- plen
- 1;
279 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
280 memcpy(header
.prefix
, path
, plen
);
281 memcpy(header
.name
, path
+ plen
+ 1, rest
);
283 xsnprintf(header
.name
, sizeof(header
.name
), "%s.data",
285 strbuf_append_ext_header(&ext_header
, "path",
289 memcpy(header
.name
, path
, pathlen
);
292 if (size
> sizeof(header
.linkname
)) {
293 xsnprintf(header
.linkname
, sizeof(header
.linkname
),
294 "see %s.paxheader", oid_to_hex(oid
));
295 strbuf_append_ext_header(&ext_header
, "linkpath",
298 memcpy(header
.linkname
, buffer
, size
);
301 size_in_header
= size
;
302 if (S_ISREG(mode
) && size
> USTAR_MAX_SIZE
) {
304 strbuf_append_ext_header_uint(&ext_header
, "size", size
);
307 prepare_header(args
, &header
, mode
, size_in_header
);
309 if (ext_header
.len
> 0) {
310 write_extended_header(args
, oid
, ext_header
.buf
,
313 strbuf_release(&ext_header
);
314 write_blocked(&header
, sizeof(header
));
315 if (S_ISREG(mode
) && size
> 0) {
317 write_blocked(buffer
, size
);
319 err
= stream_blocked(args
->repo
, oid
);
324 static void write_global_extended_header(struct archiver_args
*args
)
326 const struct object_id
*oid
= args
->commit_oid
;
327 struct strbuf ext_header
= STRBUF_INIT
;
328 struct ustar_header header
;
332 strbuf_append_ext_header(&ext_header
, "comment",
334 the_hash_algo
->hexsz
);
335 if (args
->time
> USTAR_MAX_MTIME
) {
336 strbuf_append_ext_header_uint(&ext_header
, "mtime",
338 args
->time
= USTAR_MAX_MTIME
;
344 memset(&header
, 0, sizeof(header
));
345 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
347 xsnprintf(header
.name
, sizeof(header
.name
), "pax_global_header");
348 prepare_header(args
, &header
, mode
, ext_header
.len
);
349 write_blocked(&header
, sizeof(header
));
350 write_blocked(ext_header
.buf
, ext_header
.len
);
351 strbuf_release(&ext_header
);
354 static struct archiver
**tar_filters
;
355 static int nr_tar_filters
;
356 static int alloc_tar_filters
;
358 static struct archiver
*find_tar_filter(const char *name
, size_t len
)
361 for (i
= 0; i
< nr_tar_filters
; i
++) {
362 struct archiver
*ar
= tar_filters
[i
];
363 if (!strncmp(ar
->name
, name
, len
) && !ar
->name
[len
])
369 static int tar_filter_config(const char *var
, const char *value
, void *data
)
376 if (parse_config_key(var
, "tar", &name
, &namelen
, &type
) < 0 || !name
)
379 ar
= find_tar_filter(name
, namelen
);
382 ar
->name
= xmemdupz(name
, namelen
);
383 ar
->write_archive
= write_tar_filter_archive
;
384 ar
->flags
= ARCHIVER_WANT_COMPRESSION_LEVELS
|
385 ARCHIVER_HIGH_COMPRESSION_LEVELS
;
386 ALLOC_GROW(tar_filters
, nr_tar_filters
+ 1, alloc_tar_filters
);
387 tar_filters
[nr_tar_filters
++] = ar
;
390 if (!strcmp(type
, "command")) {
392 return config_error_nonbool(var
);
393 free(ar
->filter_command
);
394 ar
->filter_command
= xstrdup(value
);
397 if (!strcmp(type
, "remote")) {
398 if (git_config_bool(var
, value
))
399 ar
->flags
|= ARCHIVER_REMOTE
;
401 ar
->flags
&= ~ARCHIVER_REMOTE
;
408 static int git_tar_config(const char *var
, const char *value
, void *cb
)
410 if (!strcmp(var
, "tar.umask")) {
411 if (value
&& !strcmp(value
, "user")) {
412 tar_umask
= umask(0);
415 tar_umask
= git_config_int(var
, value
);
420 return tar_filter_config(var
, value
, cb
);
423 static int write_tar_archive(const struct archiver
*ar
,
424 struct archiver_args
*args
)
428 write_global_extended_header(args
);
429 err
= write_archive_entries(args
, write_tar_entry
);
435 static git_zstream gzstream
;
436 static unsigned char outbuf
[16384];
438 static void tgz_deflate(int flush
)
440 while (gzstream
.avail_in
|| flush
== Z_FINISH
) {
441 int status
= git_deflate(&gzstream
, flush
);
442 if (!gzstream
.avail_out
|| status
== Z_STREAM_END
) {
443 write_or_die(1, outbuf
, gzstream
.next_out
- outbuf
);
444 gzstream
.next_out
= outbuf
;
445 gzstream
.avail_out
= sizeof(outbuf
);
446 if (status
== Z_STREAM_END
)
449 if (status
!= Z_OK
&& status
!= Z_BUF_ERROR
)
450 die(_("deflate error (%d)"), status
);
454 static void tgz_write_block(const void *data
)
456 gzstream
.next_in
= (void *)data
;
457 gzstream
.avail_in
= BLOCKSIZE
;
458 tgz_deflate(Z_NO_FLUSH
);
461 static const char internal_gzip_command
[] = "git archive gzip";
463 static int write_tar_filter_archive(const struct archiver
*ar
,
464 struct archiver_args
*args
)
466 #if ZLIB_VERNUM >= 0x1221
467 struct gz_header_s gzhead
= { .os
= 3 }; /* Unix, for reproducibility */
469 struct strbuf cmd
= STRBUF_INIT
;
470 struct child_process filter
= CHILD_PROCESS_INIT
;
473 if (!ar
->filter_command
)
474 BUG("tar-filter archiver called with no filter defined");
476 if (!strcmp(ar
->filter_command
, internal_gzip_command
)) {
477 write_block
= tgz_write_block
;
478 git_deflate_init_gzip(&gzstream
, args
->compression_level
);
479 #if ZLIB_VERNUM >= 0x1221
480 if (deflateSetHeader(&gzstream
.z
, &gzhead
) != Z_OK
)
481 BUG("deflateSetHeader() called too late");
483 gzstream
.next_out
= outbuf
;
484 gzstream
.avail_out
= sizeof(outbuf
);
486 r
= write_tar_archive(ar
, args
);
488 tgz_deflate(Z_FINISH
);
489 git_deflate_end(&gzstream
);
493 strbuf_addstr(&cmd
, ar
->filter_command
);
494 if (args
->compression_level
>= 0)
495 strbuf_addf(&cmd
, " -%d", args
->compression_level
);
497 strvec_push(&filter
.args
, cmd
.buf
);
498 filter
.use_shell
= 1;
501 if (start_command(&filter
) < 0)
502 die_errno(_("unable to start '%s' filter"), cmd
.buf
);
504 if (dup2(filter
.in
, 1) < 0)
505 die_errno(_("unable to redirect descriptor"));
508 r
= write_tar_archive(ar
, args
);
511 if (finish_command(&filter
) != 0)
512 die(_("'%s' filter reported error"), cmd
.buf
);
514 strbuf_release(&cmd
);
518 static struct archiver tar_archiver
= {
520 .write_archive
= write_tar_archive
,
521 .flags
= ARCHIVER_REMOTE
,
524 void init_tar_archiver(void)
527 register_archiver(&tar_archiver
);
529 tar_filter_config("tar.tgz.command", internal_gzip_command
, NULL
);
530 tar_filter_config("tar.tgz.remote", "true", NULL
);
531 tar_filter_config("tar.tar.gz.command", internal_gzip_command
, NULL
);
532 tar_filter_config("tar.tar.gz.remote", "true", NULL
);
533 git_config(git_tar_config
, NULL
);
534 for (i
= 0; i
< nr_tar_filters
; i
++) {
535 /* omit any filters that never had a command configured */
536 if (tar_filters
[i
]->filter_command
)
537 register_archiver(tar_filters
[i
]);