2 * Copyright (c) 2005, 2006 Rene Scharfe
7 #include "run-command.h"
9 #define RECORDSIZE (512)
10 #define BLOCKSIZE (RECORDSIZE * 20)
12 static char block
[BLOCKSIZE
];
13 static unsigned long offset
;
15 static int tar_umask
= 002;
17 static int write_tar_filter_archive(const struct archiver
*ar
,
18 struct archiver_args
*args
);
20 /* writes out the whole block, but only if it is full */
21 static void write_if_needed(void)
23 if (offset
== BLOCKSIZE
) {
24 write_or_die(1, block
, BLOCKSIZE
);
30 * queues up writes, so that all our write(2) calls write exactly one
31 * full block; pads writes to RECORDSIZE
33 static void write_blocked(const void *data
, unsigned long size
)
35 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
);
57 tail
= offset
% RECORDSIZE
;
59 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
60 offset
+= RECORDSIZE
- tail
;
66 * The end of tar archives is marked by 2*512 nul bytes and after that
67 * follows the rest of the block (if any).
69 static void write_trailer(void)
71 int tail
= BLOCKSIZE
- offset
;
72 memset(block
+ offset
, 0, tail
);
73 write_or_die(1, block
, BLOCKSIZE
);
74 if (tail
< 2 * RECORDSIZE
) {
75 memset(block
, 0, offset
);
76 write_or_die(1, block
, BLOCKSIZE
);
81 * pax extended header records have the format "%u %s=%s\n". %u contains
82 * the size of the whole string (including the %u), the first %s is the
83 * keyword, the second one is the value. This function constructs such a
84 * string and appends it to a struct strbuf.
86 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
87 const char *value
, unsigned int valuelen
)
92 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
93 for (tmp
= len
; tmp
> 9; tmp
/= 10)
97 strbuf_addf(sb
, "%u %s=", len
, keyword
);
98 strbuf_add(sb
, value
, valuelen
);
99 strbuf_addch(sb
, '\n');
102 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
104 char *p
= (char *)header
;
105 unsigned int chksum
= 0;
106 while (p
< header
->chksum
)
108 chksum
+= sizeof(header
->chksum
) * ' ';
109 p
+= sizeof(header
->chksum
);
110 while (p
< (char *)header
+ sizeof(struct ustar_header
))
115 static size_t get_path_prefix(const char *path
, size_t pathlen
, size_t maxlen
)
122 } while (i
> 0 && path
[i
] != '/');
126 static void prepare_header(struct archiver_args
*args
,
127 struct ustar_header
*header
,
128 unsigned int mode
, unsigned long size
)
130 sprintf(header
->mode
, "%07o", mode
& 07777);
131 sprintf(header
->size
, "%011lo", S_ISREG(mode
) ? size
: 0);
132 sprintf(header
->mtime
, "%011lo", (unsigned long) args
->time
);
134 sprintf(header
->uid
, "%07o", 0);
135 sprintf(header
->gid
, "%07o", 0);
136 strlcpy(header
->uname
, "root", sizeof(header
->uname
));
137 strlcpy(header
->gname
, "root", sizeof(header
->gname
));
138 sprintf(header
->devmajor
, "%07o", 0);
139 sprintf(header
->devminor
, "%07o", 0);
141 memcpy(header
->magic
, "ustar", 6);
142 memcpy(header
->version
, "00", 2);
144 sprintf(header
->chksum
, "%07o", ustar_header_chksum(header
));
147 static int write_extended_header(struct archiver_args
*args
,
148 const unsigned char *sha1
,
149 const void *buffer
, unsigned long size
)
151 struct ustar_header header
;
153 memset(&header
, 0, sizeof(header
));
154 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
156 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
157 prepare_header(args
, &header
, mode
, size
);
158 write_blocked(&header
, sizeof(header
));
159 write_blocked(buffer
, size
);
163 static int write_tar_entry(struct archiver_args
*args
,
164 const unsigned char *sha1
, const char *path
, size_t pathlen
,
165 unsigned int mode
, void *buffer
, unsigned long size
)
167 struct ustar_header header
;
168 struct strbuf ext_header
= STRBUF_INIT
;
171 memset(&header
, 0, sizeof(header
));
173 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
174 *header
.typeflag
= TYPEFLAG_DIR
;
175 mode
= (mode
| 0777) & ~tar_umask
;
176 } else if (S_ISLNK(mode
)) {
177 *header
.typeflag
= TYPEFLAG_LNK
;
179 } else if (S_ISREG(mode
)) {
180 *header
.typeflag
= TYPEFLAG_REG
;
181 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
183 return error("unsupported file mode: 0%o (SHA1: %s)",
184 mode
, sha1_to_hex(sha1
));
186 if (pathlen
> sizeof(header
.name
)) {
187 size_t plen
= get_path_prefix(path
, pathlen
,
188 sizeof(header
.prefix
));
189 size_t rest
= pathlen
- plen
- 1;
190 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
191 memcpy(header
.prefix
, path
, plen
);
192 memcpy(header
.name
, path
+ plen
+ 1, rest
);
194 sprintf(header
.name
, "%s.data",
196 strbuf_append_ext_header(&ext_header
, "path",
200 memcpy(header
.name
, path
, pathlen
);
202 if (S_ISLNK(mode
) && buffer
) {
203 if (size
> sizeof(header
.linkname
)) {
204 sprintf(header
.linkname
, "see %s.paxheader",
206 strbuf_append_ext_header(&ext_header
, "linkpath",
209 memcpy(header
.linkname
, buffer
, size
);
212 prepare_header(args
, &header
, mode
, size
);
214 if (ext_header
.len
> 0) {
215 err
= write_extended_header(args
, sha1
, ext_header
.buf
,
220 strbuf_release(&ext_header
);
221 write_blocked(&header
, sizeof(header
));
222 if (S_ISREG(mode
) && buffer
&& size
> 0)
223 write_blocked(buffer
, size
);
227 static int write_global_extended_header(struct archiver_args
*args
)
229 const unsigned char *sha1
= args
->commit_sha1
;
230 struct strbuf ext_header
= STRBUF_INIT
;
231 struct ustar_header header
;
235 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
236 memset(&header
, 0, sizeof(header
));
237 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
239 strcpy(header
.name
, "pax_global_header");
240 prepare_header(args
, &header
, mode
, ext_header
.len
);
241 write_blocked(&header
, sizeof(header
));
242 write_blocked(ext_header
.buf
, ext_header
.len
);
243 strbuf_release(&ext_header
);
247 static struct archiver
**tar_filters
;
248 static int nr_tar_filters
;
249 static int alloc_tar_filters
;
251 static struct archiver
*find_tar_filter(const char *name
, int len
)
254 for (i
= 0; i
< nr_tar_filters
; i
++) {
255 struct archiver
*ar
= tar_filters
[i
];
256 if (!strncmp(ar
->name
, name
, len
) && !ar
->name
[len
])
262 static int tar_filter_config(const char *var
, const char *value
, void *data
)
270 if (prefixcmp(var
, "tar."))
272 dot
= strrchr(var
, '.');
277 namelen
= dot
- name
;
280 ar
= find_tar_filter(name
, namelen
);
282 ar
= xcalloc(1, sizeof(*ar
));
283 ar
->name
= xmemdupz(name
, namelen
);
284 ar
->write_archive
= write_tar_filter_archive
;
285 ar
->flags
= ARCHIVER_WANT_COMPRESSION_LEVELS
;
286 ALLOC_GROW(tar_filters
, nr_tar_filters
+ 1, alloc_tar_filters
);
287 tar_filters
[nr_tar_filters
++] = ar
;
290 if (!strcmp(type
, "command")) {
292 return config_error_nonbool(var
);
294 ar
->data
= xstrdup(value
);
297 if (!strcmp(type
, "remote")) {
298 if (git_config_bool(var
, value
))
299 ar
->flags
|= ARCHIVER_REMOTE
;
301 ar
->flags
&= ~ARCHIVER_REMOTE
;
308 static int git_tar_config(const char *var
, const char *value
, void *cb
)
310 if (!strcmp(var
, "tar.umask")) {
311 if (value
&& !strcmp(value
, "user")) {
312 tar_umask
= umask(0);
315 tar_umask
= git_config_int(var
, value
);
320 return tar_filter_config(var
, value
, cb
);
323 static int write_tar_archive(const struct archiver
*ar
,
324 struct archiver_args
*args
)
328 if (args
->commit_sha1
)
329 err
= write_global_extended_header(args
);
331 err
= write_archive_entries(args
, write_tar_entry
);
337 static int write_tar_filter_archive(const struct archiver
*ar
,
338 struct archiver_args
*args
)
340 struct strbuf cmd
= STRBUF_INIT
;
341 struct child_process filter
;
346 die("BUG: tar-filter archiver called with no filter defined");
348 strbuf_addstr(&cmd
, ar
->data
);
349 if (args
->compression_level
>= 0)
350 strbuf_addf(&cmd
, " -%d", args
->compression_level
);
352 memset(&filter
, 0, sizeof(filter
));
356 filter
.use_shell
= 1;
359 if (start_command(&filter
) < 0)
360 die_errno("unable to start '%s' filter", argv
[0]);
362 if (dup2(filter
.in
, 1) < 0)
363 die_errno("unable to redirect descriptor");
366 r
= write_tar_archive(ar
, args
);
369 if (finish_command(&filter
) != 0)
370 die("'%s' filter reported error", argv
[0]);
372 strbuf_release(&cmd
);
376 static struct archiver tar_archiver
= {
382 void init_tar_archiver(void)
385 register_archiver(&tar_archiver
);
387 tar_filter_config("tar.tgz.command", "gzip -cn", NULL
);
388 tar_filter_config("tar.tgz.remote", "true", NULL
);
389 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL
);
390 tar_filter_config("tar.tar.gz.remote", "true", NULL
);
391 git_config(git_tar_config
, NULL
);
392 for (i
= 0; i
< nr_tar_filters
; i
++) {
393 /* omit any filters that never had a command configured */
394 if (tar_filters
[i
]->data
)
395 register_archiver(tar_filters
[i
]);