Merge branch 'maint'
[git/dscho.git] / archive-tar.c
blob13029619e5ec34bac4ba61a6fc08800ab36f4a1b
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "tar.h"
6 #include "archive.h"
8 #define RECORDSIZE (512)
9 #define BLOCKSIZE (RECORDSIZE * 20)
11 static char block[BLOCKSIZE];
12 static unsigned long offset;
14 static int tar_umask = 002;
16 /* writes out the whole block, but only if it is full */
17 static void write_if_needed(void)
19 if (offset == BLOCKSIZE) {
20 write_or_die(1, block, BLOCKSIZE);
21 offset = 0;
26 * queues up writes, so that all our write(2) calls write exactly one
27 * full block; pads writes to RECORDSIZE
29 static void write_blocked(const void *data, unsigned long size)
31 const char *buf = data;
32 unsigned long tail;
34 if (offset) {
35 unsigned long chunk = BLOCKSIZE - offset;
36 if (size < chunk)
37 chunk = size;
38 memcpy(block + offset, buf, chunk);
39 size -= chunk;
40 offset += chunk;
41 buf += chunk;
42 write_if_needed();
44 while (size >= BLOCKSIZE) {
45 write_or_die(1, buf, BLOCKSIZE);
46 size -= BLOCKSIZE;
47 buf += BLOCKSIZE;
49 if (size) {
50 memcpy(block + offset, buf, size);
51 offset += size;
53 tail = offset % RECORDSIZE;
54 if (tail) {
55 memset(block + offset, 0, RECORDSIZE - tail);
56 offset += RECORDSIZE - tail;
58 write_if_needed();
62 * The end of tar archives is marked by 2*512 nul bytes and after that
63 * follows the rest of the block (if any).
65 static void write_trailer(void)
67 int tail = BLOCKSIZE - offset;
68 memset(block + offset, 0, tail);
69 write_or_die(1, block, BLOCKSIZE);
70 if (tail < 2 * RECORDSIZE) {
71 memset(block, 0, offset);
72 write_or_die(1, block, BLOCKSIZE);
77 * pax extended header records have the format "%u %s=%s\n". %u contains
78 * the size of the whole string (including the %u), the first %s is the
79 * keyword, the second one is the value. This function constructs such a
80 * string and appends it to a struct strbuf.
82 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
83 const char *value, unsigned int valuelen)
85 int len, tmp;
87 /* "%u %s=%s\n" */
88 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
89 for (tmp = len; tmp > 9; tmp /= 10)
90 len++;
92 strbuf_grow(sb, len);
93 strbuf_addf(sb, "%u %s=", len, keyword);
94 strbuf_add(sb, value, valuelen);
95 strbuf_addch(sb, '\n');
98 static unsigned int ustar_header_chksum(const struct ustar_header *header)
100 char *p = (char *)header;
101 unsigned int chksum = 0;
102 while (p < header->chksum)
103 chksum += *p++;
104 chksum += sizeof(header->chksum) * ' ';
105 p += sizeof(header->chksum);
106 while (p < (char *)header + sizeof(struct ustar_header))
107 chksum += *p++;
108 return chksum;
111 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
113 size_t i = pathlen;
114 if (i > maxlen)
115 i = maxlen;
116 do {
117 i--;
118 } while (i > 0 && path[i] != '/');
119 return i;
122 static int write_tar_entry(struct archiver_args *args,
123 const unsigned char *sha1, const char *path, size_t pathlen,
124 unsigned int mode, void *buffer, unsigned long size)
126 struct ustar_header header;
127 struct strbuf ext_header;
128 int err = 0;
130 memset(&header, 0, sizeof(header));
131 strbuf_init(&ext_header, 0);
133 if (!sha1) {
134 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
135 mode = 0100666;
136 strcpy(header.name, "pax_global_header");
137 } else if (!path) {
138 *header.typeflag = TYPEFLAG_EXT_HEADER;
139 mode = 0100666;
140 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
141 } else {
142 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
143 *header.typeflag = TYPEFLAG_DIR;
144 mode = (mode | 0777) & ~tar_umask;
145 } else if (S_ISLNK(mode)) {
146 *header.typeflag = TYPEFLAG_LNK;
147 mode |= 0777;
148 } else if (S_ISREG(mode)) {
149 *header.typeflag = TYPEFLAG_REG;
150 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
151 } else {
152 return error("unsupported file mode: 0%o (SHA1: %s)",
153 mode, sha1_to_hex(sha1));
155 if (pathlen > sizeof(header.name)) {
156 size_t plen = get_path_prefix(path, pathlen,
157 sizeof(header.prefix));
158 size_t rest = pathlen - plen - 1;
159 if (plen > 0 && rest <= sizeof(header.name)) {
160 memcpy(header.prefix, path, plen);
161 memcpy(header.name, path + plen + 1, rest);
162 } else {
163 sprintf(header.name, "%s.data",
164 sha1_to_hex(sha1));
165 strbuf_append_ext_header(&ext_header, "path",
166 path, pathlen);
168 } else
169 memcpy(header.name, path, pathlen);
172 if (S_ISLNK(mode) && buffer) {
173 if (size > sizeof(header.linkname)) {
174 sprintf(header.linkname, "see %s.paxheader",
175 sha1_to_hex(sha1));
176 strbuf_append_ext_header(&ext_header, "linkpath",
177 buffer, size);
178 } else
179 memcpy(header.linkname, buffer, size);
182 sprintf(header.mode, "%07o", mode & 07777);
183 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
184 sprintf(header.mtime, "%011lo", args->time);
186 sprintf(header.uid, "%07o", 0);
187 sprintf(header.gid, "%07o", 0);
188 strlcpy(header.uname, "root", sizeof(header.uname));
189 strlcpy(header.gname, "root", sizeof(header.gname));
190 sprintf(header.devmajor, "%07o", 0);
191 sprintf(header.devminor, "%07o", 0);
193 memcpy(header.magic, "ustar", 6);
194 memcpy(header.version, "00", 2);
196 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
198 if (ext_header.len > 0) {
199 err = write_tar_entry(args, sha1, NULL, 0, 0, ext_header.buf,
200 ext_header.len);
201 if (err)
202 return err;
204 strbuf_release(&ext_header);
205 write_blocked(&header, sizeof(header));
206 if (S_ISREG(mode) && buffer && size > 0)
207 write_blocked(buffer, size);
208 return err;
211 static int write_global_extended_header(struct archiver_args *args)
213 const unsigned char *sha1 = args->commit_sha1;
214 struct strbuf ext_header;
215 int err;
217 strbuf_init(&ext_header, 0);
218 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
219 err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf,
220 ext_header.len);
221 strbuf_release(&ext_header);
222 return err;
225 static int git_tar_config(const char *var, const char *value, void *cb)
227 if (!strcmp(var, "tar.umask")) {
228 if (value && !strcmp(value, "user")) {
229 tar_umask = umask(0);
230 umask(tar_umask);
231 } else {
232 tar_umask = git_config_int(var, value);
234 return 0;
236 return git_default_config(var, value, cb);
239 int write_tar_archive(struct archiver_args *args)
241 int err = 0;
243 git_config(git_tar_config, NULL);
245 if (args->commit_sha1)
246 err = write_global_extended_header(args);
247 if (!err)
248 err = write_archive_entries(args, write_tar_entry);
249 if (!err)
250 write_trailer();
251 return err;