Skip to content
Newer
Older
100644 251 lines (223 sloc) 6.26 KB
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "tar.h"
6 #include "archive.h"
7
8 #define RECORDSIZE (512)
9 #define BLOCKSIZE (RECORDSIZE * 20)
10
11 static char block[BLOCKSIZE];
12 static unsigned long offset;
13
f08b3b0 Set default "tar" umask to 002 and owner.group to root.root
René Scharfe authored Jan 5, 2007
14 static int tar_umask = 002;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
15
16 /* writes out the whole block, but only if it is full */
17 static void write_if_needed(void)
18 {
19 if (offset == BLOCKSIZE) {
20 write_or_die(1, block, BLOCKSIZE);
21 offset = 0;
22 }
23 }
24
25 /*
26 * queues up writes, so that all our write(2) calls write exactly one
27 * full block; pads writes to RECORDSIZE
28 */
29 static void write_blocked(const void *data, unsigned long size)
30 {
31 const char *buf = data;
32 unsigned long tail;
33
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();
43 }
44 while (size >= BLOCKSIZE) {
45 write_or_die(1, buf, BLOCKSIZE);
46 size -= BLOCKSIZE;
47 buf += BLOCKSIZE;
48 }
49 if (size) {
50 memcpy(block + offset, buf, size);
51 offset += size;
52 }
53 tail = offset % RECORDSIZE;
54 if (tail) {
55 memset(block + offset, 0, RECORDSIZE - tail);
56 offset += RECORDSIZE - tail;
57 }
58 write_if_needed();
59 }
60
61 /*
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).
64 */
65 static void write_trailer(void)
66 {
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);
73 }
74 }
75
76 /*
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.
81 */
82 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
83 const char *value, unsigned int valuelen)
84 {
7a604f1 Simplify strbuf uses in archive-tar.c using strbuf API
Pierre Habouzit authored Sep 6, 2007
85 int len, tmp;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
86
87 /* "%u %s=%s\n" */
88 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
89 for (tmp = len; tmp > 9; tmp /= 10)
90 len++;
91
7a604f1 Simplify strbuf uses in archive-tar.c using strbuf API
Pierre Habouzit authored Sep 6, 2007
92 strbuf_grow(sb, len);
93 strbuf_addf(sb, "%u %s=", len, keyword);
94 strbuf_add(sb, value, valuelen);
95 strbuf_addch(sb, '\n');
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
96 }
97
98 static unsigned int ustar_header_chksum(const struct ustar_header *header)
99 {
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;
109 }
110
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
111 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
112 {
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
113 size_t i = pathlen;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
114 if (i > maxlen)
115 i = maxlen;
116 do {
117 i--;
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
118 } while (i > 0 && path[i] != '/');
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
119 return i;
120 }
121
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
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)
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
125 {
126 struct ustar_header header;
f285a2d Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer
Brandon Casey authored Oct 9, 2008
127 struct strbuf ext_header = STRBUF_INIT;
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
128 int err = 0;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
129
130 memset(&header, 0, sizeof(header));
131
132 if (!sha1) {
133 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
134 mode = 0100666;
135 strcpy(header.name, "pax_global_header");
136 } else if (!path) {
137 *header.typeflag = TYPEFLAG_EXT_HEADER;
138 mode = 0100666;
139 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
140 } else {
302b928 @tali rename dirlink to gitlink.
tali authored May 21, 2007
141 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
142 *header.typeflag = TYPEFLAG_DIR;
143 mode = (mode | 0777) & ~tar_umask;
144 } else if (S_ISLNK(mode)) {
145 *header.typeflag = TYPEFLAG_LNK;
146 mode |= 0777;
147 } else if (S_ISREG(mode)) {
148 *header.typeflag = TYPEFLAG_REG;
149 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
150 } else {
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
151 return error("unsupported file mode: 0%o (SHA1: %s)",
152 mode, sha1_to_hex(sha1));
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
153 }
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
154 if (pathlen > sizeof(header.name)) {
155 size_t plen = get_path_prefix(path, pathlen,
156 sizeof(header.prefix));
157 size_t rest = pathlen - plen - 1;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
158 if (plen > 0 && rest <= sizeof(header.name)) {
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
159 memcpy(header.prefix, path, plen);
160 memcpy(header.name, path + plen + 1, rest);
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
161 } else {
162 sprintf(header.name, "%s.data",
163 sha1_to_hex(sha1));
164 strbuf_append_ext_header(&ext_header, "path",
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
165 path, pathlen);
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
166 }
167 } else
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
168 memcpy(header.name, path, pathlen);
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
169 }
170
171 if (S_ISLNK(mode) && buffer) {
172 if (size > sizeof(header.linkname)) {
173 sprintf(header.linkname, "see %s.paxheader",
174 sha1_to_hex(sha1));
175 strbuf_append_ext_header(&ext_header, "linkpath",
176 buffer, size);
177 } else
178 memcpy(header.linkname, buffer, size);
179 }
180
181 sprintf(header.mode, "%07o", mode & 07777);
182 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
3e1629f @gitster archive-tar.c: squelch a type mismatch warning
gitster authored May 7, 2009
183 sprintf(header.mtime, "%011lo", (unsigned long) args->time);
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
184
185 sprintf(header.uid, "%07o", 0);
186 sprintf(header.gid, "%07o", 0);
f08b3b0 Set default "tar" umask to 002 and owner.group to root.root
René Scharfe authored Jan 5, 2007
187 strlcpy(header.uname, "root", sizeof(header.uname));
188 strlcpy(header.gname, "root", sizeof(header.gname));
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
189 sprintf(header.devmajor, "%07o", 0);
190 sprintf(header.devminor, "%07o", 0);
191
192 memcpy(header.magic, "ustar", 6);
193 memcpy(header.version, "00", 2);
194
195 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
196
197 if (ext_header.len > 0) {
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
198 err = write_tar_entry(args, sha1, NULL, 0, 0, ext_header.buf,
199 ext_header.len);
200 if (err)
201 return err;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
202 }
7a604f1 Simplify strbuf uses in archive-tar.c using strbuf API
Pierre Habouzit authored Sep 6, 2007
203 strbuf_release(&ext_header);
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
204 write_blocked(&header, sizeof(header));
205 if (S_ISREG(mode) && buffer && size > 0)
206 write_blocked(buffer, size);
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
207 return err;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
208 }
209
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
210 static int write_global_extended_header(struct archiver_args *args)
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
211 {
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
212 const unsigned char *sha1 = args->commit_sha1;
f285a2d Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer
Brandon Casey authored Oct 9, 2008
213 struct strbuf ext_header = STRBUF_INIT;
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
214 int err;
7a604f1 Simplify strbuf uses in archive-tar.c using strbuf API
Pierre Habouzit authored Sep 6, 2007
215
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
216 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
217 err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf,
218 ext_header.len);
7a604f1 Simplify strbuf uses in archive-tar.c using strbuf API
Pierre Habouzit authored Sep 6, 2007
219 strbuf_release(&ext_header);
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
220 return err;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
221 }
222
ef90d6d @dscho Provide git_config with a callback-data parameter
dscho authored May 14, 2008
223 static int git_tar_config(const char *var, const char *value, void *cb)
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
224 {
225 if (!strcmp(var, "tar.umask")) {
cc1816b @gitster archive-tar.c: guard config parser from value=NULL
gitster authored Feb 8, 2008
226 if (value && !strcmp(value, "user")) {
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
227 tar_umask = umask(0);
228 umask(tar_umask);
229 } else {
230 tar_umask = git_config_int(var, value);
231 }
232 return 0;
233 }
ef90d6d @dscho Provide git_config with a callback-data parameter
dscho authored May 14, 2008
234 return git_default_config(var, value, cb);
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
235 }
236
237 int write_tar_archive(struct archiver_args *args)
238 {
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
239 int err = 0;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
240
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
241 git_config(git_tar_config, NULL);
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
242
243 if (args->commit_sha1)
562e25a archive: centralize archive entry writing
René Scharfe authored Jul 14, 2008
244 err = write_global_extended_header(args);
245 if (!err)
246 err = write_archive_entries(args, write_tar_entry);
247 if (!err)
248 write_trailer();
249 return err;
3d74982 git-tar-tree: Move code for git-archive --format=tar to archive-tar.c
Rene Scharfe authored Sep 24, 2006
250 }
Something went wrong with that request. Please try again.