archive: add baselen member to struct archiver_args
[git/git.git] / archive-tar.c
CommitLineData
3d74982f
RS
1/*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
3d74982f
RS
4#include "cache.h"
5#include "commit.h"
3d74982f
RS
6#include "tar.h"
7#include "builtin.h"
8#include "archive.h"
9
10#define RECORDSIZE (512)
11#define BLOCKSIZE (RECORDSIZE * 20)
12
13static char block[BLOCKSIZE];
14static unsigned long offset;
15
16static time_t archive_time;
f08b3b0e 17static int tar_umask = 002;
3d74982f 18static int verbose;
8460b2fc 19static const struct commit *commit;
ac7fa277 20static size_t base_len;
3d74982f
RS
21
22/* writes out the whole block, but only if it is full */
23static void write_if_needed(void)
24{
25 if (offset == BLOCKSIZE) {
26 write_or_die(1, block, BLOCKSIZE);
27 offset = 0;
28 }
29}
30
31/*
32 * queues up writes, so that all our write(2) calls write exactly one
33 * full block; pads writes to RECORDSIZE
34 */
35static void write_blocked(const void *data, unsigned long size)
36{
37 const char *buf = data;
38 unsigned long tail;
39
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();
49 }
50 while (size >= BLOCKSIZE) {
51 write_or_die(1, buf, BLOCKSIZE);
52 size -= BLOCKSIZE;
53 buf += BLOCKSIZE;
54 }
55 if (size) {
56 memcpy(block + offset, buf, size);
57 offset += size;
58 }
59 tail = offset % RECORDSIZE;
60 if (tail) {
61 memset(block + offset, 0, RECORDSIZE - tail);
62 offset += RECORDSIZE - tail;
63 }
64 write_if_needed();
65}
66
67/*
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).
70 */
71static void write_trailer(void)
72{
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);
79 }
80}
81
3d74982f
RS
82/*
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.
87 */
88static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
89 const char *value, unsigned int valuelen)
90{
7a604f16 91 int len, tmp;
3d74982f
RS
92
93 /* "%u %s=%s\n" */
94 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
95 for (tmp = len; tmp > 9; tmp /= 10)
96 len++;
97
7a604f16
PH
98 strbuf_grow(sb, len);
99 strbuf_addf(sb, "%u %s=", len, keyword);
100 strbuf_add(sb, value, valuelen);
101 strbuf_addch(sb, '\n');
3d74982f
RS
102}
103
104static unsigned int ustar_header_chksum(const struct ustar_header *header)
105{
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;
115}
116
117static int get_path_prefix(const struct strbuf *path, int maxlen)
118{
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;
126}
127
128static void write_entry(const unsigned char *sha1, struct strbuf *path,
129 unsigned int mode, void *buffer, unsigned long size)
130{
131 struct ustar_header header;
132 struct strbuf ext_header;
133
134 memset(&header, 0, sizeof(header));
f1696ee3 135 strbuf_init(&ext_header, 0);
3d74982f
RS
136
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)
b449f4cf 147 fprintf(stderr, "%.*s\n", (int)path->len, path->buf);
302b9282 148 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
3d74982f
RS
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;
161 }
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);
173 }
174 } else
175 memcpy(header.name, path->buf, path->len);
176 }
177
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);
186 }
187
188 sprintf(header.mode, "%07o", mode & 07777);
189 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
190 sprintf(header.mtime, "%011lo", archive_time);
191
3d74982f
RS
192 sprintf(header.uid, "%07o", 0);
193 sprintf(header.gid, "%07o", 0);
f08b3b0e
RS
194 strlcpy(header.uname, "root", sizeof(header.uname));
195 strlcpy(header.gname, "root", sizeof(header.gname));
3d74982f
RS
196 sprintf(header.devmajor, "%07o", 0);
197 sprintf(header.devminor, "%07o", 0);
198
199 memcpy(header.magic, "ustar", 6);
200 memcpy(header.version, "00", 2);
201
202 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
203
204 if (ext_header.len > 0) {
205 write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
3d74982f 206 }
7a604f16 207 strbuf_release(&ext_header);
3d74982f
RS
208 write_blocked(&header, sizeof(header));
209 if (S_ISREG(mode) && buffer && size > 0)
210 write_blocked(buffer, size);
211}
212
213static void write_global_extended_header(const unsigned char *sha1)
214{
215 struct strbuf ext_header;
7a604f16 216
f1696ee3 217 strbuf_init(&ext_header, 0);
3d74982f
RS
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);
7a604f16 220 strbuf_release(&ext_header);
3d74982f
RS
221}
222
ef90d6d4 223static int git_tar_config(const char *var, const char *value, void *cb)
3d74982f
RS
224{
225 if (!strcmp(var, "tar.umask")) {
cc1816b0 226 if (value && !strcmp(value, "user")) {
3d74982f
RS
227 tar_umask = umask(0);
228 umask(tar_umask);
229 } else {
230 tar_umask = git_config_int(var, value);
231 }
232 return 0;
233 }
ef90d6d4 234 return git_default_config(var, value, cb);
3d74982f
RS
235}
236
671f0707
RS
237static int write_tar_entry(const unsigned char *sha1, const char *base,
238 int baselen, const char *filename, unsigned mode, int stage,
239 void *context)
3d74982f 240{
7a604f16 241 static struct strbuf path = STRBUF_INIT;
3d74982f 242 void *buffer;
21666f1a 243 enum object_type type;
3d74982f
RS
244 unsigned long size;
245
7a604f16 246 strbuf_reset(&path);
e03e05ff 247 strbuf_grow(&path, PATH_MAX);
7a604f16 248 strbuf_add(&path, base, baselen);
e03e05ff 249 strbuf_addstr(&path, filename);
008d896d
RS
250 if (is_archive_path_ignored(path.buf + base_len))
251 return 0;
302b9282 252 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
7a604f16 253 strbuf_addch(&path, '/');
3d74982f
RS
254 buffer = NULL;
255 size = 0;
256 } else {
ac7fa277
RS
257 buffer = sha1_file_to_archive(path.buf + base_len, sha1, mode,
258 &type, &size, commit);
3d74982f
RS
259 if (!buffer)
260 die("cannot read %s", sha1_to_hex(sha1));
261 }
262
263 write_entry(sha1, &path, mode, buffer, size);
264 free(buffer);
265
266 return READ_TREE_RECURSIVE;
267}
268
269int write_tar_archive(struct archiver_args *args)
270{
ef90d6d4 271 git_config(git_tar_config, NULL);
3d74982f
RS
272
273 archive_time = args->time;
274 verbose = args->verbose;
8460b2fc 275 commit = args->commit;
d53fe818 276 base_len = args->baselen;
3d74982f
RS
277
278 if (args->commit_sha1)
279 write_global_extended_header(args->commit_sha1);
280
d53fe818 281 if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
3d74982f
RS
282 char *base = xstrdup(args->base);
283 int baselen = strlen(base);
284
285 while (baselen > 0 && base[baselen - 1] == '/')
286 base[--baselen] = '\0';
671f0707
RS
287 write_tar_entry(args->tree->object.sha1, "", 0, base, 040777,
288 0, NULL);
3d74982f
RS
289 free(base);
290 }
d53fe818 291 read_tree_recursive(args->tree, args->base, args->baselen, 0,
671f0707 292 args->pathspec, write_tar_entry, NULL);
3d74982f
RS
293 write_trailer();
294
295 return 0;
296}