2 * Copyright (c) 2005, 2006 Rene Scharfe
10 #define RECORDSIZE (512)
11 #define BLOCKSIZE (RECORDSIZE * 20)
13 static char block
[BLOCKSIZE
];
14 static unsigned long offset
;
16 static time_t archive_time
;
17 static int tar_umask
= 002;
19 static const struct commit
*commit
;
20 static size_t base_len
;
22 /* writes out the whole block, but only if it is full */
23 static void write_if_needed(void)
25 if (offset
== BLOCKSIZE
) {
26 write_or_die(1, block
, BLOCKSIZE
);
32 * queues up writes, so that all our write(2) calls write exactly one
33 * full block; pads writes to RECORDSIZE
35 static void write_blocked(const void *data
, unsigned long size
)
37 const char *buf
= data
;
41 unsigned long chunk
= BLOCKSIZE
- offset
;
44 memcpy(block
+ offset
, buf
, chunk
);
50 while (size
>= BLOCKSIZE
) {
51 write_or_die(1, buf
, BLOCKSIZE
);
56 memcpy(block
+ offset
, buf
, size
);
59 tail
= offset
% RECORDSIZE
;
61 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
62 offset
+= RECORDSIZE
- tail
;
68 * The end of tar archives is marked by 2*512 nul bytes and after that
69 * follows the rest of the block (if any).
71 static void write_trailer(void)
73 int tail
= BLOCKSIZE
- offset
;
74 memset(block
+ offset
, 0, tail
);
75 write_or_die(1, block
, BLOCKSIZE
);
76 if (tail
< 2 * RECORDSIZE
) {
77 memset(block
, 0, offset
);
78 write_or_die(1, block
, BLOCKSIZE
);
83 * pax extended header records have the format "%u %s=%s\n". %u contains
84 * the size of the whole string (including the %u), the first %s is the
85 * keyword, the second one is the value. This function constructs such a
86 * string and appends it to a struct strbuf.
88 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
89 const char *value
, unsigned int valuelen
)
94 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
95 for (tmp
= len
; tmp
> 9; tmp
/= 10)
99 strbuf_addf(sb
, "%u %s=", len
, keyword
);
100 strbuf_add(sb
, value
, valuelen
);
101 strbuf_addch(sb
, '\n');
104 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
106 char *p
= (char *)header
;
107 unsigned int chksum
= 0;
108 while (p
< header
->chksum
)
110 chksum
+= sizeof(header
->chksum
) * ' ';
111 p
+= sizeof(header
->chksum
);
112 while (p
< (char *)header
+ sizeof(struct ustar_header
))
117 static int get_path_prefix(const struct strbuf
*path
, int maxlen
)
124 } while (i
> 0 && path
->buf
[i
] != '/');
128 static void write_entry(const unsigned char *sha1
, struct strbuf
*path
,
129 unsigned int mode
, void *buffer
, unsigned long size
)
131 struct ustar_header header
;
132 struct strbuf ext_header
;
134 memset(&header
, 0, sizeof(header
));
135 strbuf_init(&ext_header
, 0);
138 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
140 strcpy(header
.name
, "pax_global_header");
142 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
144 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
147 fprintf(stderr
, "%.*s\n", (int)path
->len
, path
->buf
);
148 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
149 *header
.typeflag
= TYPEFLAG_DIR
;
150 mode
= (mode
| 0777) & ~tar_umask
;
151 } else if (S_ISLNK(mode
)) {
152 *header
.typeflag
= TYPEFLAG_LNK
;
154 } else if (S_ISREG(mode
)) {
155 *header
.typeflag
= TYPEFLAG_REG
;
156 mode
= (mode
| ((mode
& 0100) ? 0777 : 0666)) & ~tar_umask
;
158 error("unsupported file mode: 0%o (SHA1: %s)",
159 mode
, sha1_to_hex(sha1
));
162 if (path
->len
> sizeof(header
.name
)) {
163 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
164 int rest
= path
->len
- plen
- 1;
165 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
166 memcpy(header
.prefix
, path
->buf
, plen
);
167 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
169 sprintf(header
.name
, "%s.data",
171 strbuf_append_ext_header(&ext_header
, "path",
172 path
->buf
, path
->len
);
175 memcpy(header
.name
, path
->buf
, path
->len
);
178 if (S_ISLNK(mode
) && buffer
) {
179 if (size
> sizeof(header
.linkname
)) {
180 sprintf(header
.linkname
, "see %s.paxheader",
182 strbuf_append_ext_header(&ext_header
, "linkpath",
185 memcpy(header
.linkname
, buffer
, size
);
188 sprintf(header
.mode
, "%07o", mode
& 07777);
189 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
190 sprintf(header
.mtime
, "%011lo", archive_time
);
192 sprintf(header
.uid
, "%07o", 0);
193 sprintf(header
.gid
, "%07o", 0);
194 strlcpy(header
.uname
, "root", sizeof(header
.uname
));
195 strlcpy(header
.gname
, "root", sizeof(header
.gname
));
196 sprintf(header
.devmajor
, "%07o", 0);
197 sprintf(header
.devminor
, "%07o", 0);
199 memcpy(header
.magic
, "ustar", 6);
200 memcpy(header
.version
, "00", 2);
202 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
204 if (ext_header
.len
> 0) {
205 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
207 strbuf_release(&ext_header
);
208 write_blocked(&header
, sizeof(header
));
209 if (S_ISREG(mode
) && buffer
&& size
> 0)
210 write_blocked(buffer
, size
);
213 static void write_global_extended_header(const unsigned char *sha1
)
215 struct strbuf ext_header
;
217 strbuf_init(&ext_header
, 0);
218 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
219 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
220 strbuf_release(&ext_header
);
223 static int git_tar_config(const char *var
, const char *value
)
225 if (!strcmp(var
, "tar.umask")) {
226 if (value
&& !strcmp(value
, "user")) {
227 tar_umask
= umask(0);
230 tar_umask
= git_config_int(var
, value
);
234 return git_default_config(var
, value
);
237 static int write_tar_entry(const unsigned char *sha1
,
238 const char *base
, int baselen
,
239 const char *filename
, unsigned mode
, int stage
)
241 static struct strbuf path
= STRBUF_INIT
;
243 enum object_type type
;
247 strbuf_grow(&path
, PATH_MAX
);
248 strbuf_add(&path
, base
, baselen
);
249 strbuf_addstr(&path
, filename
);
250 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
251 strbuf_addch(&path
, '/');
255 buffer
= sha1_file_to_archive(path
.buf
+ base_len
, sha1
, mode
,
256 &type
, &size
, commit
);
258 die("cannot read %s", sha1_to_hex(sha1
));
261 write_entry(sha1
, &path
, mode
, buffer
, size
);
264 return READ_TREE_RECURSIVE
;
267 int write_tar_archive(struct archiver_args
*args
)
269 int plen
= args
->base
? strlen(args
->base
) : 0;
271 git_config(git_tar_config
);
273 archive_time
= args
->time
;
274 verbose
= args
->verbose
;
275 commit
= args
->commit
;
276 base_len
= args
->base
? strlen(args
->base
) : 0;
278 if (args
->commit_sha1
)
279 write_global_extended_header(args
->commit_sha1
);
281 if (args
->base
&& plen
> 0 && args
->base
[plen
- 1] == '/') {
282 char *base
= xstrdup(args
->base
);
283 int baselen
= strlen(base
);
285 while (baselen
> 0 && base
[baselen
- 1] == '/')
286 base
[--baselen
] = '\0';
287 write_tar_entry(args
->tree
->object
.sha1
, "", 0, base
, 040777, 0);
290 read_tree_recursive(args
->tree
, args
->base
, plen
, 0,
291 args
->pathspec
, write_tar_entry
);