]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | static char block[BLOCKSIZE]; | |
14 | static unsigned long offset; | |
15 | ||
f08b3b0e | 16 | static int tar_umask = 002; |
3d74982f RS |
17 | |
18 | /* writes out the whole block, but only if it is full */ | |
19 | static void write_if_needed(void) | |
20 | { | |
21 | if (offset == BLOCKSIZE) { | |
22 | write_or_die(1, block, BLOCKSIZE); | |
23 | offset = 0; | |
24 | } | |
25 | } | |
26 | ||
27 | /* | |
28 | * queues up writes, so that all our write(2) calls write exactly one | |
29 | * full block; pads writes to RECORDSIZE | |
30 | */ | |
31 | static void write_blocked(const void *data, unsigned long size) | |
32 | { | |
33 | const char *buf = data; | |
34 | unsigned long tail; | |
35 | ||
36 | if (offset) { | |
37 | unsigned long chunk = BLOCKSIZE - offset; | |
38 | if (size < chunk) | |
39 | chunk = size; | |
40 | memcpy(block + offset, buf, chunk); | |
41 | size -= chunk; | |
42 | offset += chunk; | |
43 | buf += chunk; | |
44 | write_if_needed(); | |
45 | } | |
46 | while (size >= BLOCKSIZE) { | |
47 | write_or_die(1, buf, BLOCKSIZE); | |
48 | size -= BLOCKSIZE; | |
49 | buf += BLOCKSIZE; | |
50 | } | |
51 | if (size) { | |
52 | memcpy(block + offset, buf, size); | |
53 | offset += size; | |
54 | } | |
55 | tail = offset % RECORDSIZE; | |
56 | if (tail) { | |
57 | memset(block + offset, 0, RECORDSIZE - tail); | |
58 | offset += RECORDSIZE - tail; | |
59 | } | |
60 | write_if_needed(); | |
61 | } | |
62 | ||
63 | /* | |
64 | * The end of tar archives is marked by 2*512 nul bytes and after that | |
65 | * follows the rest of the block (if any). | |
66 | */ | |
67 | static void write_trailer(void) | |
68 | { | |
69 | int tail = BLOCKSIZE - offset; | |
70 | memset(block + offset, 0, tail); | |
71 | write_or_die(1, block, BLOCKSIZE); | |
72 | if (tail < 2 * RECORDSIZE) { | |
73 | memset(block, 0, offset); | |
74 | write_or_die(1, block, BLOCKSIZE); | |
75 | } | |
76 | } | |
77 | ||
3d74982f RS |
78 | /* |
79 | * pax extended header records have the format "%u %s=%s\n". %u contains | |
80 | * the size of the whole string (including the %u), the first %s is the | |
81 | * keyword, the second one is the value. This function constructs such a | |
82 | * string and appends it to a struct strbuf. | |
83 | */ | |
84 | static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword, | |
85 | const char *value, unsigned int valuelen) | |
86 | { | |
7a604f16 | 87 | int len, tmp; |
3d74982f RS |
88 | |
89 | /* "%u %s=%s\n" */ | |
90 | len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1; | |
91 | for (tmp = len; tmp > 9; tmp /= 10) | |
92 | len++; | |
93 | ||
7a604f16 PH |
94 | strbuf_grow(sb, len); |
95 | strbuf_addf(sb, "%u %s=", len, keyword); | |
96 | strbuf_add(sb, value, valuelen); | |
97 | strbuf_addch(sb, '\n'); | |
3d74982f RS |
98 | } |
99 | ||
100 | static unsigned int ustar_header_chksum(const struct ustar_header *header) | |
101 | { | |
102 | char *p = (char *)header; | |
103 | unsigned int chksum = 0; | |
104 | while (p < header->chksum) | |
105 | chksum += *p++; | |
106 | chksum += sizeof(header->chksum) * ' '; | |
107 | p += sizeof(header->chksum); | |
108 | while (p < (char *)header + sizeof(struct ustar_header)) | |
109 | chksum += *p++; | |
110 | return chksum; | |
111 | } | |
112 | ||
562e25ab | 113 | static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen) |
3d74982f | 114 | { |
562e25ab | 115 | size_t i = pathlen; |
3d74982f RS |
116 | if (i > maxlen) |
117 | i = maxlen; | |
118 | do { | |
119 | i--; | |
562e25ab | 120 | } while (i > 0 && path[i] != '/'); |
3d74982f RS |
121 | return i; |
122 | } | |
123 | ||
562e25ab RS |
124 | static int write_tar_entry(struct archiver_args *args, |
125 | const unsigned char *sha1, const char *path, size_t pathlen, | |
126 | unsigned int mode, void *buffer, unsigned long size) | |
3d74982f RS |
127 | { |
128 | struct ustar_header header; | |
129 | struct strbuf ext_header; | |
562e25ab | 130 | int err = 0; |
3d74982f RS |
131 | |
132 | memset(&header, 0, sizeof(header)); | |
f1696ee3 | 133 | strbuf_init(&ext_header, 0); |
3d74982f RS |
134 | |
135 | if (!sha1) { | |
136 | *header.typeflag = TYPEFLAG_GLOBAL_HEADER; | |
137 | mode = 0100666; | |
138 | strcpy(header.name, "pax_global_header"); | |
139 | } else if (!path) { | |
140 | *header.typeflag = TYPEFLAG_EXT_HEADER; | |
141 | mode = 0100666; | |
142 | sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1)); | |
143 | } else { | |
302b9282 | 144 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
3d74982f RS |
145 | *header.typeflag = TYPEFLAG_DIR; |
146 | mode = (mode | 0777) & ~tar_umask; | |
147 | } else if (S_ISLNK(mode)) { | |
148 | *header.typeflag = TYPEFLAG_LNK; | |
149 | mode |= 0777; | |
150 | } else if (S_ISREG(mode)) { | |
151 | *header.typeflag = TYPEFLAG_REG; | |
152 | mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask; | |
153 | } else { | |
562e25ab RS |
154 | return error("unsupported file mode: 0%o (SHA1: %s)", |
155 | mode, sha1_to_hex(sha1)); | |
3d74982f | 156 | } |
562e25ab RS |
157 | if (pathlen > sizeof(header.name)) { |
158 | size_t plen = get_path_prefix(path, pathlen, | |
159 | sizeof(header.prefix)); | |
160 | size_t rest = pathlen - plen - 1; | |
3d74982f | 161 | if (plen > 0 && rest <= sizeof(header.name)) { |
562e25ab RS |
162 | memcpy(header.prefix, path, plen); |
163 | memcpy(header.name, path + plen + 1, rest); | |
3d74982f RS |
164 | } else { |
165 | sprintf(header.name, "%s.data", | |
166 | sha1_to_hex(sha1)); | |
167 | strbuf_append_ext_header(&ext_header, "path", | |
562e25ab | 168 | path, pathlen); |
3d74982f RS |
169 | } |
170 | } else | |
562e25ab | 171 | memcpy(header.name, path, pathlen); |
3d74982f RS |
172 | } |
173 | ||
174 | if (S_ISLNK(mode) && buffer) { | |
175 | if (size > sizeof(header.linkname)) { | |
176 | sprintf(header.linkname, "see %s.paxheader", | |
177 | sha1_to_hex(sha1)); | |
178 | strbuf_append_ext_header(&ext_header, "linkpath", | |
179 | buffer, size); | |
180 | } else | |
181 | memcpy(header.linkname, buffer, size); | |
182 | } | |
183 | ||
184 | sprintf(header.mode, "%07o", mode & 07777); | |
185 | sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0); | |
562e25ab | 186 | sprintf(header.mtime, "%011lo", args->time); |
3d74982f | 187 | |
3d74982f RS |
188 | sprintf(header.uid, "%07o", 0); |
189 | sprintf(header.gid, "%07o", 0); | |
f08b3b0e RS |
190 | strlcpy(header.uname, "root", sizeof(header.uname)); |
191 | strlcpy(header.gname, "root", sizeof(header.gname)); | |
3d74982f RS |
192 | sprintf(header.devmajor, "%07o", 0); |
193 | sprintf(header.devminor, "%07o", 0); | |
194 | ||
195 | memcpy(header.magic, "ustar", 6); | |
196 | memcpy(header.version, "00", 2); | |
197 | ||
198 | sprintf(header.chksum, "%07o", ustar_header_chksum(&header)); | |
199 | ||
200 | if (ext_header.len > 0) { | |
562e25ab RS |
201 | err = write_tar_entry(args, sha1, NULL, 0, 0, ext_header.buf, |
202 | ext_header.len); | |
203 | if (err) | |
204 | return err; | |
3d74982f | 205 | } |
7a604f16 | 206 | strbuf_release(&ext_header); |
3d74982f RS |
207 | write_blocked(&header, sizeof(header)); |
208 | if (S_ISREG(mode) && buffer && size > 0) | |
209 | write_blocked(buffer, size); | |
562e25ab | 210 | return err; |
3d74982f RS |
211 | } |
212 | ||
562e25ab | 213 | static int write_global_extended_header(struct archiver_args *args) |
3d74982f | 214 | { |
562e25ab | 215 | const unsigned char *sha1 = args->commit_sha1; |
3d74982f | 216 | struct strbuf ext_header; |
562e25ab | 217 | int err; |
7a604f16 | 218 | |
f1696ee3 | 219 | strbuf_init(&ext_header, 0); |
3d74982f | 220 | strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40); |
562e25ab RS |
221 | err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf, |
222 | ext_header.len); | |
7a604f16 | 223 | strbuf_release(&ext_header); |
562e25ab | 224 | return err; |
3d74982f RS |
225 | } |
226 | ||
ef90d6d4 | 227 | static int git_tar_config(const char *var, const char *value, void *cb) |
3d74982f RS |
228 | { |
229 | if (!strcmp(var, "tar.umask")) { | |
cc1816b0 | 230 | if (value && !strcmp(value, "user")) { |
3d74982f RS |
231 | tar_umask = umask(0); |
232 | umask(tar_umask); | |
233 | } else { | |
234 | tar_umask = git_config_int(var, value); | |
235 | } | |
236 | return 0; | |
237 | } | |
ef90d6d4 | 238 | return git_default_config(var, value, cb); |
3d74982f RS |
239 | } |
240 | ||
3d74982f RS |
241 | int write_tar_archive(struct archiver_args *args) |
242 | { | |
562e25ab | 243 | int err = 0; |
3d74982f | 244 | |
562e25ab | 245 | git_config(git_tar_config, NULL); |
3d74982f RS |
246 | |
247 | if (args->commit_sha1) | |
562e25ab RS |
248 | err = write_global_extended_header(args); |
249 | if (!err) | |
250 | err = write_archive_entries(args, write_tar_entry); | |
251 | if (!err) | |
252 | write_trailer(); | |
253 | return err; | |
3d74982f | 254 | } |