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;
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);
82 * pax extended header records have the format "%u %s=%s\n". %u contains
83 * the size of the whole string (including the %u), the first %s is the
84 * keyword, the second one is the value. This function constructs such a
85 * string and appends it to a struct strbuf.
87 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
88 const char *value, unsigned int valuelen)
93 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
94 for (tmp = len; tmp > 9; tmp /= 10)
98 strbuf_addf(sb, "%u %s=", len, keyword);
99 strbuf_add(sb, value, valuelen);
100 strbuf_addch(sb, '\n');
103 static unsigned int ustar_header_chksum(const struct ustar_header *header)
105 char *p = (char *)header;
106 unsigned int chksum = 0;
107 while (p < header->chksum)
109 chksum += sizeof(header->chksum) * ' ';
110 p += sizeof(header->chksum);
111 while (p < (char *)header + sizeof(struct ustar_header))
116 static int get_path_prefix(const struct strbuf *path, int maxlen)
123 } while (i > 0 && path->buf[i] != '/');
127 static void write_entry(const unsigned char *sha1, struct strbuf *path,
128 unsigned int mode, void *buffer, unsigned long size)
130 struct ustar_header header;
131 struct strbuf ext_header;
133 memset(&header, 0, sizeof(header));
134 strbuf_init(&ext_header, 0);
137 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
139 strcpy(header.name, "pax_global_header");
141 *header.typeflag = TYPEFLAG_EXT_HEADER;
143 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
146 fprintf(stderr, "%.*s\n", (int)path->len, path->buf);
147 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
148 *header.typeflag = TYPEFLAG_DIR;
149 mode = (mode | 0777) & ~tar_umask;
150 } else if (S_ISLNK(mode)) {
151 *header.typeflag = TYPEFLAG_LNK;
153 } else if (S_ISREG(mode)) {
154 *header.typeflag = TYPEFLAG_REG;
155 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
157 error("unsupported file mode: 0%o (SHA1: %s)",
158 mode, sha1_to_hex(sha1));
161 if (path->len > sizeof(header.name)) {
162 int plen = get_path_prefix(path, sizeof(header.prefix));
163 int rest = path->len - plen - 1;
164 if (plen > 0 && rest <= sizeof(header.name)) {
165 memcpy(header.prefix, path->buf, plen);
166 memcpy(header.name, path->buf + plen + 1, rest);
168 sprintf(header.name, "%s.data",
170 strbuf_append_ext_header(&ext_header, "path",
171 path->buf, path->len);
174 memcpy(header.name, path->buf, path->len);
177 if (S_ISLNK(mode) && buffer) {
178 if (size > sizeof(header.linkname)) {
179 sprintf(header.linkname, "see %s.paxheader",
181 strbuf_append_ext_header(&ext_header, "linkpath",
184 memcpy(header.linkname, buffer, size);
187 sprintf(header.mode, "%07o", mode & 07777);
188 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
189 sprintf(header.mtime, "%011lo", archive_time);
191 sprintf(header.uid, "%07o", 0);
192 sprintf(header.gid, "%07o", 0);
193 strlcpy(header.uname, "root", sizeof(header.uname));
194 strlcpy(header.gname, "root", sizeof(header.gname));
195 sprintf(header.devmajor, "%07o", 0);
196 sprintf(header.devminor, "%07o", 0);
198 memcpy(header.magic, "ustar", 6);
199 memcpy(header.version, "00", 2);
201 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
203 if (ext_header.len > 0) {
204 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
206 strbuf_release(&ext_header);
207 write_blocked(&header, sizeof(header));
208 if (S_ISREG(mode) && buffer && size > 0)
209 write_blocked(buffer, size);
212 static void write_global_extended_header(const unsigned char *sha1)
214 struct strbuf ext_header;
216 strbuf_init(&ext_header, 0);
217 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
218 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
219 strbuf_release(&ext_header);
222 static int git_tar_config(const char *var, const char *value)
224 if (!strcmp(var, "tar.umask")) {
225 if (!strcmp(value, "user")) {
226 tar_umask = umask(0);
229 tar_umask = git_config_int(var, value);
233 return git_default_config(var, value);
236 static int write_tar_entry(const unsigned char *sha1,
237 const char *base, int baselen,
238 const char *filename, unsigned mode, int stage)
240 static struct strbuf path = STRBUF_INIT;
242 enum object_type type;
246 strbuf_grow(&path, PATH_MAX);
247 strbuf_add(&path, base, baselen);
248 strbuf_addstr(&path, filename);
249 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
250 strbuf_addch(&path, '/');
254 buffer = sha1_file_to_archive(path.buf, sha1, mode, &type,
257 die("cannot read %s", sha1_to_hex(sha1));
260 write_entry(sha1, &path, mode, buffer, size);
263 return READ_TREE_RECURSIVE;
266 int write_tar_archive(struct archiver_args *args)
268 int plen = args->base ? strlen(args->base) : 0;
270 git_config(git_tar_config);
272 archive_time = args->time;
273 verbose = args->verbose;
274 commit = args->commit;
276 if (args->commit_sha1)
277 write_global_extended_header(args->commit_sha1);
279 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
280 char *base = xstrdup(args->base);
281 int baselen = strlen(base);
283 while (baselen > 0 && base[baselen - 1] == '/')
284 base[--baselen] = '\0';
285 write_tar_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
288 read_tree_recursive(args->tree, args->base, plen, 0,
289 args->pathspec, write_tar_entry);