|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2005 Rene Scharfe |
| 2 | + * Copyright (c) 2005, 2006 Rene Scharfe |
3 | 3 | */
|
4 | 4 | #include <time.h>
|
5 | 5 | #include "cache.h"
|
6 | 6 | #include "diff.h"
|
7 | 7 | #include "commit.h"
|
| 8 | +#include "strbuf.h" |
| 9 | +#include "tar.h" |
8 | 10 |
|
9 | 11 | #define RECORDSIZE (512)
|
10 | 12 | #define BLOCKSIZE (RECORDSIZE * 20)
|
11 | 13 |
|
12 |
| -#define TYPEFLAG_AUTO '\0' |
13 |
| -#define TYPEFLAG_REG '0' |
14 |
| -#define TYPEFLAG_LNK '2' |
15 |
| -#define TYPEFLAG_DIR '5' |
16 |
| -#define TYPEFLAG_GLOBAL_HEADER 'g' |
17 |
| -#define TYPEFLAG_EXT_HEADER 'x' |
18 |
| - |
19 | 14 | #define EXT_HEADER_PATH 1
|
20 | 15 | #define EXT_HEADER_LINKPATH 2
|
21 | 16 |
|
@@ -119,6 +114,127 @@ static void write_blocked(void *buf, unsigned long size)
|
119 | 114 | write_if_needed();
|
120 | 115 | }
|
121 | 116 |
|
| 117 | +/* |
| 118 | + * pax extended header records have the format "%u %s=%s\n". %u contains |
| 119 | + * the size of the whole string (including the %u), the first %s is the |
| 120 | + * keyword, the second one is the value. This function constructs such a |
| 121 | + * string and appends it to a struct strbuf. |
| 122 | + */ |
| 123 | +static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword, |
| 124 | + const char *value, unsigned int valuelen) |
| 125 | +{ |
| 126 | + char *p; |
| 127 | + int len, total, tmp; |
| 128 | + |
| 129 | + /* "%u %s=%s\n" */ |
| 130 | + len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1; |
| 131 | + for (tmp = len; tmp > 9; tmp /= 10) |
| 132 | + len++; |
| 133 | + |
| 134 | + total = sb->len + len; |
| 135 | + if (total > sb->alloc) { |
| 136 | + sb->buf = xrealloc(sb->buf, total); |
| 137 | + sb->alloc = total; |
| 138 | + } |
| 139 | + |
| 140 | + p = sb->buf; |
| 141 | + p += sprintf(p, "%u %s=", len, keyword); |
| 142 | + memcpy(p, value, valuelen); |
| 143 | + p += valuelen; |
| 144 | + *p = '\n'; |
| 145 | + sb->len = total; |
| 146 | +} |
| 147 | + |
| 148 | +static unsigned int ustar_header_chksum(const struct ustar_header *header) |
| 149 | +{ |
| 150 | + char *p = (char *)header; |
| 151 | + unsigned int chksum = 0; |
| 152 | + while (p < header->chksum) |
| 153 | + chksum += *p++; |
| 154 | + chksum += sizeof(header->chksum) * ' '; |
| 155 | + p += sizeof(header->chksum); |
| 156 | + while (p < (char *)header + sizeof(struct ustar_header)) |
| 157 | + chksum += *p++; |
| 158 | + return chksum; |
| 159 | +} |
| 160 | + |
| 161 | +static void write_entry(const unsigned char *sha1, struct strbuf *path, |
| 162 | + unsigned int mode, void *buffer, unsigned long size) |
| 163 | +{ |
| 164 | + struct ustar_header header; |
| 165 | + struct strbuf ext_header; |
| 166 | + |
| 167 | + memset(&header, 0, sizeof(header)); |
| 168 | + ext_header.buf = NULL; |
| 169 | + ext_header.len = ext_header.alloc = 0; |
| 170 | + |
| 171 | + if (!sha1) { |
| 172 | + *header.typeflag = TYPEFLAG_GLOBAL_HEADER; |
| 173 | + mode = 0100666; |
| 174 | + strcpy(header.name, "pax_global_header"); |
| 175 | + } else if (!path) { |
| 176 | + *header.typeflag = TYPEFLAG_EXT_HEADER; |
| 177 | + mode = 0100666; |
| 178 | + sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1)); |
| 179 | + } else { |
| 180 | + if (S_ISDIR(mode)) { |
| 181 | + *header.typeflag = TYPEFLAG_DIR; |
| 182 | + mode |= 0777; |
| 183 | + } else if (S_ISLNK(mode)) { |
| 184 | + *header.typeflag = TYPEFLAG_LNK; |
| 185 | + mode |= 0777; |
| 186 | + } else if (S_ISREG(mode)) { |
| 187 | + *header.typeflag = TYPEFLAG_REG; |
| 188 | + mode |= (mode & 0100) ? 0777 : 0666; |
| 189 | + } else { |
| 190 | + error("unsupported file mode: 0%o (SHA1: %s)", |
| 191 | + mode, sha1_to_hex(sha1)); |
| 192 | + return; |
| 193 | + } |
| 194 | + if (path->len > sizeof(header.name)) { |
| 195 | + sprintf(header.name, "%s.data", sha1_to_hex(sha1)); |
| 196 | + strbuf_append_ext_header(&ext_header, "path", |
| 197 | + path->buf, path->len); |
| 198 | + } else |
| 199 | + memcpy(header.name, path->buf, path->len); |
| 200 | + } |
| 201 | + |
| 202 | + if (S_ISLNK(mode) && buffer) { |
| 203 | + if (size > sizeof(header.linkname)) { |
| 204 | + sprintf(header.linkname, "see %s.paxheader", |
| 205 | + sha1_to_hex(sha1)); |
| 206 | + strbuf_append_ext_header(&ext_header, "linkpath", |
| 207 | + buffer, size); |
| 208 | + } else |
| 209 | + memcpy(header.linkname, buffer, size); |
| 210 | + } |
| 211 | + |
| 212 | + sprintf(header.mode, "%07o", mode & 07777); |
| 213 | + sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0); |
| 214 | + sprintf(header.mtime, "%011lo", archive_time); |
| 215 | + |
| 216 | + /* XXX: should we provide more meaningful info here? */ |
| 217 | + sprintf(header.uid, "%07o", 0); |
| 218 | + sprintf(header.gid, "%07o", 0); |
| 219 | + strncpy(header.uname, "git", 31); |
| 220 | + strncpy(header.gname, "git", 31); |
| 221 | + sprintf(header.devmajor, "%07o", 0); |
| 222 | + sprintf(header.devminor, "%07o", 0); |
| 223 | + |
| 224 | + memcpy(header.magic, "ustar", 6); |
| 225 | + memcpy(header.version, "00", 2); |
| 226 | + |
| 227 | + sprintf(header.chksum, "%07o", ustar_header_chksum(&header)); |
| 228 | + |
| 229 | + if (ext_header.len > 0) { |
| 230 | + write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len); |
| 231 | + free(ext_header.buf); |
| 232 | + } |
| 233 | + write_blocked(&header, sizeof(header)); |
| 234 | + if (S_ISREG(mode) && buffer && size > 0) |
| 235 | + write_blocked(buffer, size); |
| 236 | +} |
| 237 | + |
122 | 238 | static void append_string(char **p, const char *s)
|
123 | 239 | {
|
124 | 240 | unsigned int len = strlen(s);
|
@@ -237,16 +353,12 @@ static void write_extended_header(const char *headerfilename, int is_dir,
|
237 | 353 |
|
238 | 354 | static void write_global_extended_header(const unsigned char *sha1)
|
239 | 355 | {
|
240 |
| - char *p; |
241 |
| - unsigned int size; |
242 |
| - |
243 |
| - size = extended_header_len("comment", 40); |
244 |
| - write_header(NULL, TYPEFLAG_GLOBAL_HEADER, NULL, NULL, |
245 |
| - "pax_global_header", 0100600, NULL, size); |
246 |
| - |
247 |
| - p = get_record(); |
248 |
| - append_extended_header(&p, "comment", sha1_to_hex(sha1), 40); |
249 |
| - write_if_needed(); |
| 356 | + struct strbuf ext_header; |
| 357 | + ext_header.buf = NULL; |
| 358 | + ext_header.len = ext_header.alloc = 0; |
| 359 | + strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40); |
| 360 | + write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len); |
| 361 | + free(ext_header.buf); |
250 | 362 | }
|
251 | 363 |
|
252 | 364 | /* stores a ustar header directly in the block buffer */
|
|
0 commit comments