2 * Copyright (c) 2005, 2006 Rene Scharfe
8 #include "run-command.h"
10 #define RECORDSIZE (512)
11 #define BLOCKSIZE (RECORDSIZE * 20)
13 static char block
[BLOCKSIZE
];
14 static unsigned long offset
;
16 static int tar_umask
= 002;
18 static int write_tar_filter_archive(const struct archiver
*ar
,
19 struct archiver_args
*args
);
21 /* writes out the whole block, but only if it is full */
22 static void write_if_needed(void)
24 if (offset
== BLOCKSIZE
) {
25 write_or_die(1, block
, BLOCKSIZE
);
31 * queues up writes, so that all our write(2) calls write exactly one
32 * full block; pads writes to RECORDSIZE
34 static void do_write_blocked(const void *data
, unsigned long size
)
36 const char *buf
= data
;
39 unsigned long chunk
= BLOCKSIZE
- offset
;
42 memcpy(block
+ offset
, buf
, chunk
);
48 while (size
>= BLOCKSIZE
) {
49 write_or_die(1, buf
, BLOCKSIZE
);
54 memcpy(block
+ offset
, buf
, size
);
59 static void finish_record(void)
62 tail
= offset
% RECORDSIZE
;
64 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
65 offset
+= RECORDSIZE
- tail
;
70 static void write_blocked(const void *data
, unsigned long size
)
72 do_write_blocked(data
, size
);
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).
80 static void write_trailer(void)
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
);
92 * queues up writes, so that all our write(2) calls write exactly one
93 * full block; pads writes to RECORDSIZE
95 static int stream_blocked(const unsigned char *sha1
)
97 struct git_istream
*st
;
98 enum object_type type
;
103 st
= open_istream(sha1
, &type
, &sz
, NULL
);
105 return error("cannot stream blob %s", sha1_to_hex(sha1
));
107 readlen
= read_istream(st
, buf
, sizeof(buf
));
110 do_write_blocked(buf
, readlen
);
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.
124 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
125 const char *value
, unsigned int valuelen
)
130 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
131 for (tmp
= len
; tmp
> 9; tmp
/= 10)
134 strbuf_grow(sb
, len
);
135 strbuf_addf(sb
, "%u %s=", len
, keyword
);
136 strbuf_add(sb
, value
, valuelen
);
137 strbuf_addch(sb
, '\n');
140 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
142 const unsigned char *p
= (const unsigned char *)header
;
143 unsigned int chksum
= 0;
144 while (p
< (const unsigned char *)header
->chksum
)
146 chksum
+= sizeof(header
->chksum
) * ' ';
147 p
+= sizeof(header
->chksum
);
148 while (p
< (const unsigned char *)header
+ sizeof(struct ustar_header
))
153 static size_t get_path_prefix(const char *path
, size_t pathlen
, size_t maxlen
)
160 } while (i
> 0 && path
[i
] != '/');
164 static void prepare_header(struct archiver_args
*args
,
165 struct ustar_header
*header
,
166 unsigned int mode
, unsigned long size
)
168 sprintf(header
->mode
, "%07o", mode
& 07777);
169 sprintf(header
->size
, "%011lo", S_ISREG(mode
) ? size
: 0);
170 sprintf(header
->mtime
, "%011lo", (unsigned long) args
->time
);
172 sprintf(header
->uid
, "%07o", 0);
173 sprintf(header
->gid
, "%07o", 0);
174 strlcpy(header
->uname
, "root", sizeof(header
->uname
));
175 strlcpy(header
->gname
, "root", sizeof(header
->gname
));
176 sprintf(header
->devmajor
, "%07o", 0);
177 sprintf(header
->devminor
, "%07o", 0);
179 memcpy(header
->magic
, "ustar", 6);
180 memcpy(header
->version
, "00", 2);
182 sprintf(header
->chksum
, "%07o", ustar_header_chksum(header
));
185 static int write_extended_header(struct archiver_args
*args
,
186 const unsigned char *sha1
,
187 const void *buffer
, unsigned long size
)
189 struct ustar_header header
;
191 memset(&header
, 0, sizeof(header
));
192 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
194 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
195 prepare_header(args
, &header
, mode
, size
);
196 write_blocked(&header
, sizeof(header
));
197 write_blocked(buffer
, size
);
201 static int write_tar_entry(struct archiver_args
*args
,
202 const unsigned char *sha1
,
203 const char *path
, size_t pathlen
,
206 struct ustar_header header
;
207 struct strbuf ext_header
= STRBUF_INIT
;
208 unsigned int old_mode
= mode
;
213 memset(&header
, 0, sizeof(header
));
215 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
216 *header
.typeflag
= TYPEFLAG_DIR
;
217 mode
= (mode
| 0777) & ~tar_umask
;
218 } else if (S_ISLNK(mode
)) {
219 *header
.typeflag
= TYPEFLAG_LNK
;
221 } else if (S_ISREG(mode
)) {
222 *header
.typeflag
= TYPEFLAG_REG
;
223 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
225 return error("unsupported file mode: 0%o (SHA1: %s)",
226 mode
, sha1_to_hex(sha1
));
228 if (pathlen
> sizeof(header
.name
)) {
229 size_t plen
= get_path_prefix(path
, pathlen
,
230 sizeof(header
.prefix
));
231 size_t rest
= pathlen
- plen
- 1;
232 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
233 memcpy(header
.prefix
, path
, plen
);
234 memcpy(header
.name
, path
+ plen
+ 1, rest
);
236 sprintf(header
.name
, "%s.data",
238 strbuf_append_ext_header(&ext_header
, "path",
242 memcpy(header
.name
, path
, pathlen
);
244 if (S_ISREG(mode
) && !args
->convert
&&
245 sha1_object_info(sha1
, &size
) == OBJ_BLOB
&&
246 size
> big_file_threshold
)
248 else if (S_ISLNK(mode
) || S_ISREG(mode
)) {
249 enum object_type type
;
250 buffer
= sha1_file_to_archive(args
, path
, sha1
, old_mode
, &type
, &size
);
252 return error("cannot read %s", sha1_to_hex(sha1
));
259 if (size
> sizeof(header
.linkname
)) {
260 sprintf(header
.linkname
, "see %s.paxheader",
262 strbuf_append_ext_header(&ext_header
, "linkpath",
265 memcpy(header
.linkname
, buffer
, size
);
268 prepare_header(args
, &header
, mode
, size
);
270 if (ext_header
.len
> 0) {
271 err
= write_extended_header(args
, sha1
, ext_header
.buf
,
278 strbuf_release(&ext_header
);
279 write_blocked(&header
, sizeof(header
));
280 if (S_ISREG(mode
) && size
> 0) {
282 write_blocked(buffer
, size
);
284 err
= stream_blocked(sha1
);
290 static int write_global_extended_header(struct archiver_args
*args
)
292 const unsigned char *sha1
= args
->commit_sha1
;
293 struct strbuf ext_header
= STRBUF_INIT
;
294 struct ustar_header header
;
298 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
299 memset(&header
, 0, sizeof(header
));
300 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
302 strcpy(header
.name
, "pax_global_header");
303 prepare_header(args
, &header
, mode
, ext_header
.len
);
304 write_blocked(&header
, sizeof(header
));
305 write_blocked(ext_header
.buf
, ext_header
.len
);
306 strbuf_release(&ext_header
);
310 static struct archiver
**tar_filters
;
311 static int nr_tar_filters
;
312 static int alloc_tar_filters
;
314 static struct archiver
*find_tar_filter(const char *name
, int len
)
317 for (i
= 0; i
< nr_tar_filters
; i
++) {
318 struct archiver
*ar
= tar_filters
[i
];
319 if (!strncmp(ar
->name
, name
, len
) && !ar
->name
[len
])
325 static int tar_filter_config(const char *var
, const char *value
, void *data
)
333 if (prefixcmp(var
, "tar."))
335 dot
= strrchr(var
, '.');
340 namelen
= dot
- name
;
343 ar
= find_tar_filter(name
, namelen
);
345 ar
= xcalloc(1, sizeof(*ar
));
346 ar
->name
= xmemdupz(name
, namelen
);
347 ar
->write_archive
= write_tar_filter_archive
;
348 ar
->flags
= ARCHIVER_WANT_COMPRESSION_LEVELS
;
349 ALLOC_GROW(tar_filters
, nr_tar_filters
+ 1, alloc_tar_filters
);
350 tar_filters
[nr_tar_filters
++] = ar
;
353 if (!strcmp(type
, "command")) {
355 return config_error_nonbool(var
);
357 ar
->data
= xstrdup(value
);
360 if (!strcmp(type
, "remote")) {
361 if (git_config_bool(var
, value
))
362 ar
->flags
|= ARCHIVER_REMOTE
;
364 ar
->flags
&= ~ARCHIVER_REMOTE
;
371 static int git_tar_config(const char *var
, const char *value
, void *cb
)
373 if (!strcmp(var
, "tar.umask")) {
374 if (value
&& !strcmp(value
, "user")) {
375 tar_umask
= umask(0);
378 tar_umask
= git_config_int(var
, value
);
383 return tar_filter_config(var
, value
, cb
);
386 static int write_tar_archive(const struct archiver
*ar
,
387 struct archiver_args
*args
)
391 if (args
->commit_sha1
)
392 err
= write_global_extended_header(args
);
394 err
= write_archive_entries(args
, write_tar_entry
);
400 static int write_tar_filter_archive(const struct archiver
*ar
,
401 struct archiver_args
*args
)
403 struct strbuf cmd
= STRBUF_INIT
;
404 struct child_process filter
;
409 die("BUG: tar-filter archiver called with no filter defined");
411 strbuf_addstr(&cmd
, ar
->data
);
412 if (args
->compression_level
>= 0)
413 strbuf_addf(&cmd
, " -%d", args
->compression_level
);
415 memset(&filter
, 0, sizeof(filter
));
419 filter
.use_shell
= 1;
422 if (start_command(&filter
) < 0)
423 die_errno("unable to start '%s' filter", argv
[0]);
425 if (dup2(filter
.in
, 1) < 0)
426 die_errno("unable to redirect descriptor");
429 r
= write_tar_archive(ar
, args
);
432 if (finish_command(&filter
) != 0)
433 die("'%s' filter reported error", argv
[0]);
435 strbuf_release(&cmd
);
439 static struct archiver tar_archiver
= {
445 void init_tar_archiver(void)
448 register_archiver(&tar_archiver
);
450 tar_filter_config("tar.tgz.command", "gzip -cn", NULL
);
451 tar_filter_config("tar.tgz.remote", "true", NULL
);
452 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL
);
453 tar_filter_config("tar.tar.gz.remote", "true", NULL
);
454 git_config(git_tar_config
, NULL
);
455 for (i
= 0; i
< nr_tar_filters
; i
++) {
456 /* omit any filters that never had a command configured */
457 if (tar_filters
[i
]->data
)
458 register_archiver(tar_filters
[i
]);