Merge commit 'junio/next' into next
[git/platforms/storm.git] / archive-tar.c
blob4add80284e4570d1da44861e9025fa75eeb775d6
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "commit.h"
6 #include "tar.h"
7 #include "builtin.h"
8 #include "archive.h"
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;
18 static int verbose;
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);
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);
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)
91 int len, tmp;
93 /* "%u %s=%s\n" */
94 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
95 for (tmp = len; tmp > 9; tmp /= 10)
96 len++;
98 strbuf_grow(sb, len);
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)
109 chksum += *p++;
110 chksum += sizeof(header->chksum) * ' ';
111 p += sizeof(header->chksum);
112 while (p < (char *)header + sizeof(struct ustar_header))
113 chksum += *p++;
114 return chksum;
117 static int get_path_prefix(const struct strbuf *path, int maxlen)
119 int i = path->len;
120 if (i > maxlen)
121 i = maxlen;
122 do {
123 i--;
124 } while (i > 0 && path->buf[i] != '/');
125 return 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);
137 if (!sha1) {
138 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
139 mode = 0100666;
140 strcpy(header.name, "pax_global_header");
141 } else if (!path) {
142 *header.typeflag = TYPEFLAG_EXT_HEADER;
143 mode = 0100666;
144 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
145 } else {
146 if (verbose)
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;
153 mode |= 0777;
154 } else if (S_ISREG(mode)) {
155 *header.typeflag = TYPEFLAG_REG;
156 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
157 } else {
158 error("unsupported file mode: 0%o (SHA1: %s)",
159 mode, sha1_to_hex(sha1));
160 return;
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);
168 } else {
169 sprintf(header.name, "%s.data",
170 sha1_to_hex(sha1));
171 strbuf_append_ext_header(&ext_header, "path",
172 path->buf, path->len);
174 } else
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",
181 sha1_to_hex(sha1));
182 strbuf_append_ext_header(&ext_header, "linkpath",
183 buffer, size);
184 } else
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);
228 umask(tar_umask);
229 } else {
230 tar_umask = git_config_int(var, value);
232 return 0;
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;
242 void *buffer;
243 enum object_type type;
244 unsigned long size;
246 strbuf_reset(&path);
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, '/');
252 buffer = NULL;
253 size = 0;
254 } else {
255 buffer = sha1_file_to_archive(path.buf + base_len, sha1, mode,
256 &type, &size, commit);
257 if (!buffer)
258 die("cannot read %s", sha1_to_hex(sha1));
261 write_entry(sha1, &path, mode, buffer, size);
262 free(buffer);
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);
288 free(base);
290 read_tree_recursive(args->tree, args->base, plen, 0,
291 args->pathspec, write_tar_entry);
292 write_trailer();
294 return 0;