git-merge: no reason to use cpio anymore
[git/jrn.git] / archive-tar.c
blobc0d95dab0d965ff0ae961d72d7eae4c021c5c05a
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "commit.h"
6 #include "strbuf.h"
7 #include "tar.h"
8 #include "builtin.h"
9 #include "archive.h"
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;
19 static int verbose;
20 static const struct commit *commit;
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);
27 offset = 0;
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;
38 unsigned long tail;
40 if (offset) {
41 unsigned long chunk = BLOCKSIZE - offset;
42 if (size < chunk)
43 chunk = size;
44 memcpy(block + offset, buf, chunk);
45 size -= chunk;
46 offset += chunk;
47 buf += chunk;
48 write_if_needed();
50 while (size >= BLOCKSIZE) {
51 write_or_die(1, buf, BLOCKSIZE);
52 size -= BLOCKSIZE;
53 buf += BLOCKSIZE;
55 if (size) {
56 memcpy(block + offset, buf, size);
57 offset += size;
59 tail = offset % RECORDSIZE;
60 if (tail) {
61 memset(block + offset, 0, RECORDSIZE - tail);
62 offset += RECORDSIZE - tail;
64 write_if_needed();
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);
82 static void strbuf_append_string(struct strbuf *sb, const char *s)
84 int slen = strlen(s);
85 int total = sb->len + slen;
86 if (total + 1 > sb->alloc) {
87 sb->buf = xrealloc(sb->buf, total + 1);
88 sb->alloc = total + 1;
90 memcpy(sb->buf + sb->len, s, slen);
91 sb->len = total;
92 sb->buf[total] = '\0';
96 * pax extended header records have the format "%u %s=%s\n". %u contains
97 * the size of the whole string (including the %u), the first %s is the
98 * keyword, the second one is the value. This function constructs such a
99 * string and appends it to a struct strbuf.
101 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
102 const char *value, unsigned int valuelen)
104 char *p;
105 int len, total, tmp;
107 /* "%u %s=%s\n" */
108 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
109 for (tmp = len; tmp > 9; tmp /= 10)
110 len++;
112 total = sb->len + len;
113 if (total > sb->alloc) {
114 sb->buf = xrealloc(sb->buf, total);
115 sb->alloc = total;
118 p = sb->buf;
119 p += sprintf(p, "%u %s=", len, keyword);
120 memcpy(p, value, valuelen);
121 p += valuelen;
122 *p = '\n';
123 sb->len = total;
126 static unsigned int ustar_header_chksum(const struct ustar_header *header)
128 char *p = (char *)header;
129 unsigned int chksum = 0;
130 while (p < header->chksum)
131 chksum += *p++;
132 chksum += sizeof(header->chksum) * ' ';
133 p += sizeof(header->chksum);
134 while (p < (char *)header + sizeof(struct ustar_header))
135 chksum += *p++;
136 return chksum;
139 static int get_path_prefix(const struct strbuf *path, int maxlen)
141 int i = path->len;
142 if (i > maxlen)
143 i = maxlen;
144 do {
145 i--;
146 } while (i > 0 && path->buf[i] != '/');
147 return i;
150 static void write_entry(const unsigned char *sha1, struct strbuf *path,
151 unsigned int mode, void *buffer, unsigned long size)
153 struct ustar_header header;
154 struct strbuf ext_header;
156 memset(&header, 0, sizeof(header));
157 ext_header.buf = NULL;
158 ext_header.len = ext_header.alloc = 0;
160 if (!sha1) {
161 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
162 mode = 0100666;
163 strcpy(header.name, "pax_global_header");
164 } else if (!path) {
165 *header.typeflag = TYPEFLAG_EXT_HEADER;
166 mode = 0100666;
167 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
168 } else {
169 if (verbose)
170 fprintf(stderr, "%.*s\n", path->len, path->buf);
171 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
172 *header.typeflag = TYPEFLAG_DIR;
173 mode = (mode | 0777) & ~tar_umask;
174 } else if (S_ISLNK(mode)) {
175 *header.typeflag = TYPEFLAG_LNK;
176 mode |= 0777;
177 } else if (S_ISREG(mode)) {
178 *header.typeflag = TYPEFLAG_REG;
179 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
180 } else {
181 error("unsupported file mode: 0%o (SHA1: %s)",
182 mode, sha1_to_hex(sha1));
183 return;
185 if (path->len > sizeof(header.name)) {
186 int plen = get_path_prefix(path, sizeof(header.prefix));
187 int rest = path->len - plen - 1;
188 if (plen > 0 && rest <= sizeof(header.name)) {
189 memcpy(header.prefix, path->buf, plen);
190 memcpy(header.name, path->buf + plen + 1, rest);
191 } else {
192 sprintf(header.name, "%s.data",
193 sha1_to_hex(sha1));
194 strbuf_append_ext_header(&ext_header, "path",
195 path->buf, path->len);
197 } else
198 memcpy(header.name, path->buf, path->len);
201 if (S_ISLNK(mode) && buffer) {
202 if (size > sizeof(header.linkname)) {
203 sprintf(header.linkname, "see %s.paxheader",
204 sha1_to_hex(sha1));
205 strbuf_append_ext_header(&ext_header, "linkpath",
206 buffer, size);
207 } else
208 memcpy(header.linkname, buffer, size);
211 sprintf(header.mode, "%07o", mode & 07777);
212 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
213 sprintf(header.mtime, "%011lo", archive_time);
215 sprintf(header.uid, "%07o", 0);
216 sprintf(header.gid, "%07o", 0);
217 strlcpy(header.uname, "root", sizeof(header.uname));
218 strlcpy(header.gname, "root", sizeof(header.gname));
219 sprintf(header.devmajor, "%07o", 0);
220 sprintf(header.devminor, "%07o", 0);
222 memcpy(header.magic, "ustar", 6);
223 memcpy(header.version, "00", 2);
225 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
227 if (ext_header.len > 0) {
228 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
229 free(ext_header.buf);
231 write_blocked(&header, sizeof(header));
232 if (S_ISREG(mode) && buffer && size > 0)
233 write_blocked(buffer, size);
236 static void write_global_extended_header(const unsigned char *sha1)
238 struct strbuf ext_header;
239 ext_header.buf = NULL;
240 ext_header.len = ext_header.alloc = 0;
241 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
242 write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
243 free(ext_header.buf);
246 static int git_tar_config(const char *var, const char *value)
248 if (!strcmp(var, "tar.umask")) {
249 if (!strcmp(value, "user")) {
250 tar_umask = umask(0);
251 umask(tar_umask);
252 } else {
253 tar_umask = git_config_int(var, value);
255 return 0;
257 return git_default_config(var, value);
260 static int write_tar_entry(const unsigned char *sha1,
261 const char *base, int baselen,
262 const char *filename, unsigned mode, int stage)
264 static struct strbuf path;
265 int filenamelen = strlen(filename);
266 void *buffer;
267 enum object_type type;
268 unsigned long size;
270 if (!path.alloc) {
271 path.buf = xmalloc(PATH_MAX);
272 path.alloc = PATH_MAX;
273 path.len = path.eof = 0;
275 if (path.alloc < baselen + filenamelen + 1) {
276 free(path.buf);
277 path.buf = xmalloc(baselen + filenamelen + 1);
278 path.alloc = baselen + filenamelen + 1;
280 memcpy(path.buf, base, baselen);
281 memcpy(path.buf + baselen, filename, filenamelen);
282 path.len = baselen + filenamelen;
283 path.buf[path.len] = '\0';
284 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
285 strbuf_append_string(&path, "/");
286 buffer = NULL;
287 size = 0;
288 } else {
289 buffer = sha1_file_to_archive(path.buf, sha1, mode, &type,
290 &size, commit);
291 if (!buffer)
292 die("cannot read %s", sha1_to_hex(sha1));
295 write_entry(sha1, &path, mode, buffer, size);
296 free(buffer);
298 return READ_TREE_RECURSIVE;
301 int write_tar_archive(struct archiver_args *args)
303 int plen = args->base ? strlen(args->base) : 0;
305 git_config(git_tar_config);
307 archive_time = args->time;
308 verbose = args->verbose;
309 commit = args->commit;
311 if (args->commit_sha1)
312 write_global_extended_header(args->commit_sha1);
314 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
315 char *base = xstrdup(args->base);
316 int baselen = strlen(base);
318 while (baselen > 0 && base[baselen - 1] == '/')
319 base[--baselen] = '\0';
320 write_tar_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
321 free(base);
323 read_tree_recursive(args->tree, args->base, plen, 0,
324 args->pathspec, write_tar_entry);
325 write_trailer();
327 return 0;