2 * Copyright (c) 2005, 2006 Rene Scharfe
11 #define RECORDSIZE (512)
12 #define BLOCKSIZE (RECORDSIZE * 20)
14 static char block
[BLOCKSIZE
];
15 static unsigned long offset
;
17 static time_t archive_time
;
18 static int tar_umask
= 002;
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 write_blocked(const void *data
, unsigned long size
)
36 const char *buf
= data
;
40 unsigned long chunk
= BLOCKSIZE
- offset
;
43 memcpy(block
+ offset
, buf
, chunk
);
49 while (size
>= BLOCKSIZE
) {
50 write_or_die(1, buf
, BLOCKSIZE
);
55 memcpy(block
+ offset
, buf
, size
);
58 tail
= offset
% RECORDSIZE
;
60 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
61 offset
+= RECORDSIZE
- tail
;
67 * The end of tar archives is marked by 2*512 nul bytes and after that
68 * follows the rest of the block (if any).
70 static void write_trailer(void)
72 int tail
= BLOCKSIZE
- offset
;
73 memset(block
+ offset
, 0, tail
);
74 write_or_die(1, block
, BLOCKSIZE
);
75 if (tail
< 2 * RECORDSIZE
) {
76 memset(block
, 0, offset
);
77 write_or_die(1, block
, BLOCKSIZE
);
81 static void strbuf_append_string(struct strbuf
*sb
, const char *s
)
84 int total
= sb
->len
+ slen
;
85 if (total
+ 1 > sb
->alloc
) {
86 sb
->buf
= xrealloc(sb
->buf
, total
+ 1);
87 sb
->alloc
= total
+ 1;
89 memcpy(sb
->buf
+ sb
->len
, s
, slen
);
91 sb
->buf
[total
] = '\0';
95 * pax extended header records have the format "%u %s=%s\n". %u contains
96 * the size of the whole string (including the %u), the first %s is the
97 * keyword, the second one is the value. This function constructs such a
98 * string and appends it to a struct strbuf.
100 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
101 const char *value
, unsigned int valuelen
)
107 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
108 for (tmp
= len
; tmp
> 9; tmp
/= 10)
111 total
= sb
->len
+ len
;
112 if (total
> sb
->alloc
) {
113 sb
->buf
= xrealloc(sb
->buf
, total
);
118 p
+= sprintf(p
, "%u %s=", len
, keyword
);
119 memcpy(p
, value
, valuelen
);
125 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
127 char *p
= (char *)header
;
128 unsigned int chksum
= 0;
129 while (p
< header
->chksum
)
131 chksum
+= sizeof(header
->chksum
) * ' ';
132 p
+= sizeof(header
->chksum
);
133 while (p
< (char *)header
+ sizeof(struct ustar_header
))
138 static int get_path_prefix(const struct strbuf
*path
, int maxlen
)
145 } while (i
> 0 && path
->buf
[i
] != '/');
149 static void write_entry(const unsigned char *sha1
, struct strbuf
*path
,
150 unsigned int mode
, void *buffer
, unsigned long size
)
152 struct ustar_header header
;
153 struct strbuf ext_header
;
155 memset(&header
, 0, sizeof(header
));
156 ext_header
.buf
= NULL
;
157 ext_header
.len
= ext_header
.alloc
= 0;
160 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
162 strcpy(header
.name
, "pax_global_header");
164 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
166 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
169 fprintf(stderr
, "%.*s\n", path
->len
, path
->buf
);
170 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
171 *header
.typeflag
= TYPEFLAG_DIR
;
172 mode
= (mode
| 0777) & ~tar_umask
;
173 } else if (S_ISLNK(mode
)) {
174 *header
.typeflag
= TYPEFLAG_LNK
;
176 } else if (S_ISREG(mode
)) {
177 *header
.typeflag
= TYPEFLAG_REG
;
178 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
180 error("unsupported file mode: 0%o (SHA1: %s)",
181 mode
, sha1_to_hex(sha1
));
184 if (path
->len
> sizeof(header
.name
)) {
185 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
186 int rest
= path
->len
- plen
- 1;
187 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
188 memcpy(header
.prefix
, path
->buf
, plen
);
189 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
191 sprintf(header
.name
, "%s.data",
193 strbuf_append_ext_header(&ext_header
, "path",
194 path
->buf
, path
->len
);
197 memcpy(header
.name
, path
->buf
, path
->len
);
200 if (S_ISLNK(mode
) && buffer
) {
201 if (size
> sizeof(header
.linkname
)) {
202 sprintf(header
.linkname
, "see %s.paxheader",
204 strbuf_append_ext_header(&ext_header
, "linkpath",
207 memcpy(header
.linkname
, buffer
, size
);
210 sprintf(header
.mode
, "%07o", mode
& 07777);
211 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
212 sprintf(header
.mtime
, "%011lo", archive_time
);
214 sprintf(header
.uid
, "%07o", 0);
215 sprintf(header
.gid
, "%07o", 0);
216 strlcpy(header
.uname
, "root", sizeof(header
.uname
));
217 strlcpy(header
.gname
, "root", sizeof(header
.gname
));
218 sprintf(header
.devmajor
, "%07o", 0);
219 sprintf(header
.devminor
, "%07o", 0);
221 memcpy(header
.magic
, "ustar", 6);
222 memcpy(header
.version
, "00", 2);
224 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
226 if (ext_header
.len
> 0) {
227 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
228 free(ext_header
.buf
);
230 write_blocked(&header
, sizeof(header
));
231 if (S_ISREG(mode
) && buffer
&& size
> 0)
232 write_blocked(buffer
, size
);
235 static void write_global_extended_header(const unsigned char *sha1
)
237 struct strbuf ext_header
;
238 ext_header
.buf
= NULL
;
239 ext_header
.len
= ext_header
.alloc
= 0;
240 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
241 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
242 free(ext_header
.buf
);
245 static int git_tar_config(const char *var
, const char *value
)
247 if (!strcmp(var
, "tar.umask")) {
248 if (!strcmp(value
, "user")) {
249 tar_umask
= umask(0);
252 tar_umask
= git_config_int(var
, value
);
256 return git_default_config(var
, value
);
259 static int write_tar_entry(const unsigned char *sha1
,
260 const char *base
, int baselen
,
261 const char *filename
, unsigned mode
, int stage
)
263 static struct strbuf path
;
264 int filenamelen
= strlen(filename
);
266 enum object_type type
;
270 path
.buf
= xmalloc(PATH_MAX
);
271 path
.alloc
= PATH_MAX
;
272 path
.len
= path
.eof
= 0;
274 if (path
.alloc
< baselen
+ filenamelen
+ 1) {
276 path
.buf
= xmalloc(baselen
+ filenamelen
+ 1);
277 path
.alloc
= baselen
+ filenamelen
+ 1;
279 memcpy(path
.buf
, base
, baselen
);
280 memcpy(path
.buf
+ baselen
, filename
, filenamelen
);
281 path
.len
= baselen
+ filenamelen
;
282 path
.buf
[path
.len
] = '\0';
283 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
284 strbuf_append_string(&path
, "/");
288 buffer
= convert_sha1_file(path
.buf
, sha1
, mode
, &type
, &size
);
290 die("cannot read %s", sha1_to_hex(sha1
));
293 write_entry(sha1
, &path
, mode
, buffer
, size
);
296 return READ_TREE_RECURSIVE
;
299 int write_tar_archive(struct archiver_args
*args
)
301 int plen
= args
->base
? strlen(args
->base
) : 0;
303 git_config(git_tar_config
);
305 archive_time
= args
->time
;
306 verbose
= args
->verbose
;
308 if (args
->commit_sha1
)
309 write_global_extended_header(args
->commit_sha1
);
311 if (args
->base
&& plen
> 0 && args
->base
[plen
- 1] == '/') {
312 char *base
= xstrdup(args
->base
);
313 int baselen
= strlen(base
);
315 while (baselen
> 0 && base
[baselen
- 1] == '/')
316 base
[--baselen
] = '\0';
317 write_tar_entry(args
->tree
->object
.sha1
, "", 0, base
, 040777, 0);
320 read_tree_recursive(args
->tree
, args
->base
, plen
, 0,
321 args
->pathspec
, write_tar_entry
);