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 /* writes out the whole block, but only if it is full */
42 static void write_if_needed(void)
44 if (offset
== BLOCKSIZE
) {
45 write_or_die(1, block
, BLOCKSIZE
);
51 * queues up writes, so that all our write(2) calls write exactly one
52 * full block; pads writes to RECORDSIZE
54 static void do_write_blocked(const void *data
, unsigned long size
)
56 const char *buf
= data
;
59 unsigned long chunk
= BLOCKSIZE
- offset
;
62 memcpy(block
+ offset
, buf
, chunk
);
68 while (size
>= BLOCKSIZE
) {
69 write_or_die(1, buf
, BLOCKSIZE
);
74 memcpy(block
+ offset
, buf
, size
);
79 static void finish_record(void)
82 tail
= offset
% RECORDSIZE
;
84 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
85 offset
+= RECORDSIZE
- tail
;
90 static void write_blocked(const void *data
, unsigned long size
)
92 do_write_blocked(data
, size
);
97 * The end of tar archives is marked by 2*512 nul bytes and after that
98 * follows the rest of the block (if any).
100 static void write_trailer(void)
102 int tail
= BLOCKSIZE
- offset
;
103 memset(block
+ offset
, 0, tail
);
104 write_or_die(1, block
, BLOCKSIZE
);
105 if (tail
< 2 * RECORDSIZE
) {
106 memset(block
, 0, offset
);
107 write_or_die(1, block
, BLOCKSIZE
);
112 * queues up writes, so that all our write(2) calls write exactly one
113 * full block; pads writes to RECORDSIZE
115 static int stream_blocked(const struct object_id
*oid
)
117 struct git_istream
*st
;
118 enum object_type type
;
123 st
= open_istream(oid
, &type
, &sz
, NULL
);
125 return error(_("cannot stream blob %s"), oid_to_hex(oid
));
127 readlen
= read_istream(st
, buf
, sizeof(buf
));
130 do_write_blocked(buf
, readlen
);
139 * pax extended header records have the format "%u %s=%s\n". %u contains
140 * the size of the whole string (including the %u), the first %s is the
141 * keyword, the second one is the value. This function constructs such a
142 * string and appends it to a struct strbuf.
144 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
145 const char *value
, size_t valuelen
)
147 size_t orig_len
= sb
->len
;
151 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
152 for (tmp
= 1; len
/ 10 >= tmp
; tmp
*= 10)
155 strbuf_grow(sb
, len
);
156 strbuf_addf(sb
, "%"PRIuMAX
" %s=", (uintmax_t)len
, keyword
);
157 strbuf_add(sb
, value
, valuelen
);
158 strbuf_addch(sb
, '\n');
160 if (len
!= sb
->len
- orig_len
)
161 BUG("pax extended header length miscalculated as %"PRIuMAX
162 ", should be %"PRIuMAX
,
163 (uintmax_t)len
, (uintmax_t)(sb
->len
- orig_len
));
167 * Like strbuf_append_ext_header, but for numeric values.
169 static void strbuf_append_ext_header_uint(struct strbuf
*sb
,
173 char buf
[40]; /* big enough for 2^128 in decimal, plus NUL */
176 len
= xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
, value
);
177 strbuf_append_ext_header(sb
, keyword
, buf
, len
);
180 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
182 const unsigned char *p
= (const unsigned char *)header
;
183 unsigned int chksum
= 0;
184 while (p
< (const unsigned char *)header
->chksum
)
186 chksum
+= sizeof(header
->chksum
) * ' ';
187 p
+= sizeof(header
->chksum
);
188 while (p
< (const unsigned char *)header
+ sizeof(struct ustar_header
))
193 static size_t get_path_prefix(const char *path
, size_t pathlen
, size_t maxlen
)
196 if (i
> 1 && path
[i
- 1] == '/')
202 } while (i
> 0 && path
[i
] != '/');
206 static void prepare_header(struct archiver_args
*args
,
207 struct ustar_header
*header
,
208 unsigned int mode
, unsigned long size
)
210 xsnprintf(header
->mode
, sizeof(header
->mode
), "%07o", mode
& 07777);
211 xsnprintf(header
->size
, sizeof(header
->size
), "%011"PRIoMAX
, S_ISREG(mode
) ? (uintmax_t)size
: (uintmax_t)0);
212 xsnprintf(header
->mtime
, sizeof(header
->mtime
), "%011lo", (unsigned long) args
->time
);
214 xsnprintf(header
->uid
, sizeof(header
->uid
), "%07o", 0);
215 xsnprintf(header
->gid
, sizeof(header
->gid
), "%07o", 0);
216 strlcpy(header
->uname
, "root", sizeof(header
->uname
));
217 strlcpy(header
->gname
, "root", sizeof(header
->gname
));
218 xsnprintf(header
->devmajor
, sizeof(header
->devmajor
), "%07o", 0);
219 xsnprintf(header
->devminor
, sizeof(header
->devminor
), "%07o", 0);
221 memcpy(header
->magic
, "ustar", 6);
222 memcpy(header
->version
, "00", 2);
224 xsnprintf(header
->chksum
, sizeof(header
->chksum
), "%07o", ustar_header_chksum(header
));
227 static void write_extended_header(struct archiver_args
*args
,
228 const struct object_id
*oid
,
229 const void *buffer
, unsigned long size
)
231 struct ustar_header header
;
233 memset(&header
, 0, sizeof(header
));
234 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
236 xsnprintf(header
.name
, sizeof(header
.name
), "%s.paxheader", oid_to_hex(oid
));
237 prepare_header(args
, &header
, mode
, size
);
238 write_blocked(&header
, sizeof(header
));
239 write_blocked(buffer
, size
);
242 static int write_tar_entry(struct archiver_args
*args
,
243 const struct object_id
*oid
,
244 const char *path
, size_t pathlen
,
247 struct ustar_header header
;
248 struct strbuf ext_header
= STRBUF_INIT
;
249 unsigned int old_mode
= mode
;
250 unsigned long size
, size_in_header
;
254 memset(&header
, 0, sizeof(header
));
256 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
257 *header
.typeflag
= TYPEFLAG_DIR
;
258 mode
= (mode
| 0777) & ~tar_umask
;
259 } else if (S_ISLNK(mode
)) {
260 *header
.typeflag
= TYPEFLAG_LNK
;
262 } else if (S_ISREG(mode
)) {
263 *header
.typeflag
= TYPEFLAG_REG
;
264 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
266 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
267 mode
, oid_to_hex(oid
));
269 if (pathlen
> sizeof(header
.name
)) {
270 size_t plen
= get_path_prefix(path
, pathlen
,
271 sizeof(header
.prefix
));
272 size_t rest
= pathlen
- plen
- 1;
273 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
274 memcpy(header
.prefix
, path
, plen
);
275 memcpy(header
.name
, path
+ plen
+ 1, rest
);
277 xsnprintf(header
.name
, sizeof(header
.name
), "%s.data",
279 strbuf_append_ext_header(&ext_header
, "path",
283 memcpy(header
.name
, path
, pathlen
);
285 if (S_ISREG(mode
) && !args
->convert
&&
286 oid_object_info(args
->repo
, oid
, &size
) == OBJ_BLOB
&&
287 size
> big_file_threshold
)
289 else if (S_ISLNK(mode
) || S_ISREG(mode
)) {
290 enum object_type type
;
291 buffer
= object_file_to_archive(args
, path
, oid
, old_mode
, &type
, &size
);
293 return error(_("cannot read %s"), oid_to_hex(oid
));
300 if (size
> sizeof(header
.linkname
)) {
301 xsnprintf(header
.linkname
, sizeof(header
.linkname
),
302 "see %s.paxheader", oid_to_hex(oid
));
303 strbuf_append_ext_header(&ext_header
, "linkpath",
306 memcpy(header
.linkname
, buffer
, size
);
309 size_in_header
= size
;
310 if (S_ISREG(mode
) && size
> USTAR_MAX_SIZE
) {
312 strbuf_append_ext_header_uint(&ext_header
, "size", size
);
315 prepare_header(args
, &header
, mode
, size_in_header
);
317 if (ext_header
.len
> 0) {
318 write_extended_header(args
, oid
, ext_header
.buf
,
321 strbuf_release(&ext_header
);
322 write_blocked(&header
, sizeof(header
));
323 if (S_ISREG(mode
) && size
> 0) {
325 write_blocked(buffer
, size
);
327 err
= stream_blocked(oid
);
333 static void write_global_extended_header(struct archiver_args
*args
)
335 const struct object_id
*oid
= args
->commit_oid
;
336 struct strbuf ext_header
= STRBUF_INIT
;
337 struct ustar_header header
;
341 strbuf_append_ext_header(&ext_header
, "comment",
343 the_hash_algo
->hexsz
);
344 if (args
->time
> USTAR_MAX_MTIME
) {
345 strbuf_append_ext_header_uint(&ext_header
, "mtime",
347 args
->time
= USTAR_MAX_MTIME
;
353 memset(&header
, 0, sizeof(header
));
354 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
356 xsnprintf(header
.name
, sizeof(header
.name
), "pax_global_header");
357 prepare_header(args
, &header
, mode
, ext_header
.len
);
358 write_blocked(&header
, sizeof(header
));
359 write_blocked(ext_header
.buf
, ext_header
.len
);
360 strbuf_release(&ext_header
);
363 static struct archiver
**tar_filters
;
364 static int nr_tar_filters
;
365 static int alloc_tar_filters
;
367 static struct archiver
*find_tar_filter(const char *name
, int len
)
370 for (i
= 0; i
< nr_tar_filters
; i
++) {
371 struct archiver
*ar
= tar_filters
[i
];
372 if (!strncmp(ar
->name
, name
, len
) && !ar
->name
[len
])
378 static int tar_filter_config(const char *var
, const char *value
, void *data
)
385 if (parse_config_key(var
, "tar", &name
, &namelen
, &type
) < 0 || !name
)
388 ar
= find_tar_filter(name
, namelen
);
390 ar
= xcalloc(1, sizeof(*ar
));
391 ar
->name
= xmemdupz(name
, namelen
);
392 ar
->write_archive
= write_tar_filter_archive
;
393 ar
->flags
= ARCHIVER_WANT_COMPRESSION_LEVELS
;
394 ALLOC_GROW(tar_filters
, nr_tar_filters
+ 1, alloc_tar_filters
);
395 tar_filters
[nr_tar_filters
++] = ar
;
398 if (!strcmp(type
, "command")) {
400 return config_error_nonbool(var
);
402 ar
->data
= xstrdup(value
);
405 if (!strcmp(type
, "remote")) {
406 if (git_config_bool(var
, value
))
407 ar
->flags
|= ARCHIVER_REMOTE
;
409 ar
->flags
&= ~ARCHIVER_REMOTE
;
416 static int git_tar_config(const char *var
, const char *value
, void *cb
)
418 if (!strcmp(var
, "tar.umask")) {
419 if (value
&& !strcmp(value
, "user")) {
420 tar_umask
= umask(0);
423 tar_umask
= git_config_int(var
, value
);
428 return tar_filter_config(var
, value
, cb
);
431 static int write_tar_archive(const struct archiver
*ar
,
432 struct archiver_args
*args
)
436 write_global_extended_header(args
);
437 err
= write_archive_entries(args
, write_tar_entry
);
443 static int write_tar_filter_archive(const struct archiver
*ar
,
444 struct archiver_args
*args
)
446 struct strbuf cmd
= STRBUF_INIT
;
447 struct child_process filter
= CHILD_PROCESS_INIT
;
452 BUG("tar-filter archiver called with no filter defined");
454 strbuf_addstr(&cmd
, ar
->data
);
455 if (args
->compression_level
>= 0)
456 strbuf_addf(&cmd
, " -%d", args
->compression_level
);
461 filter
.use_shell
= 1;
464 if (start_command(&filter
) < 0)
465 die_errno(_("unable to start '%s' filter"), argv
[0]);
467 if (dup2(filter
.in
, 1) < 0)
468 die_errno(_("unable to redirect descriptor"));
471 r
= write_tar_archive(ar
, args
);
474 if (finish_command(&filter
) != 0)
475 die(_("'%s' filter reported error"), argv
[0]);
477 strbuf_release(&cmd
);
481 static struct archiver tar_archiver
= {
487 void init_tar_archiver(void)
490 register_archiver(&tar_archiver
);
492 tar_filter_config("tar.tgz.command", "gzip -cn", NULL
);
493 tar_filter_config("tar.tgz.remote", "true", NULL
);
494 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL
);
495 tar_filter_config("tar.tar.gz.remote", "true", NULL
);
496 git_config(git_tar_config
, NULL
);
497 for (i
= 0; i
< nr_tar_filters
; i
++) {
498 /* omit any filters that never had a command configured */
499 if (tar_filters
[i
]->data
)
500 register_archiver(tar_filters
[i
]);