]>
Commit | Line | Data |
---|---|---|
0fcfd160 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | * | |
6 | * This handles basic git sha1 object files - packing, unpacking, | |
7 | * creation etc. | |
8 | */ | |
0fcfd160 | 9 | #include "cache.h" |
1f688557 | 10 | #include "delta.h" |
a733cb60 | 11 | #include "pack.h" |
8e440259 PE |
12 | #include "blob.h" |
13 | #include "commit.h" | |
14 | #include "tag.h" | |
15 | #include "tree.h" | |
f35a6d3b | 16 | #include "refs.h" |
70f5d5d3 | 17 | #include "pack-revindex.h" |
0fcfd160 | 18 | |
144bde78 LT |
19 | #ifndef O_NOATIME |
20 | #if defined(__linux__) && (defined(__i386__) || defined(__PPC__)) | |
21 | #define O_NOATIME 01000000 | |
22 | #else | |
23 | #define O_NOATIME 0 | |
24 | #endif | |
25 | #endif | |
26 | ||
e05db0fd PR |
27 | #ifdef NO_C99_FORMAT |
28 | #define SZ_FMT "lu" | |
9e42d6a1 | 29 | static unsigned long sz_fmt(size_t s) { return (unsigned long)s; } |
e05db0fd PR |
30 | #else |
31 | #define SZ_FMT "zu" | |
9e42d6a1 | 32 | static size_t sz_fmt(size_t s) { return s; } |
e05db0fd PR |
33 | #endif |
34 | ||
96f1e58f | 35 | const unsigned char null_sha1[20]; |
88cd621d | 36 | |
144bde78 LT |
37 | static unsigned int sha1_file_open_flag = O_NOATIME; |
38 | ||
192a6be2 | 39 | const signed char hexval_table[256] = { |
e49521b5 JH |
40 | -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */ |
41 | -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */ | |
42 | -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */ | |
43 | -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */ | |
44 | -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */ | |
45 | -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */ | |
46 | 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */ | |
47 | 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */ | |
48 | -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */ | |
49 | -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */ | |
50 | -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */ | |
51 | -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */ | |
52 | -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */ | |
53 | -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */ | |
54 | -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */ | |
55 | -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */ | |
56 | -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */ | |
57 | -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */ | |
58 | -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */ | |
59 | -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */ | |
60 | -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */ | |
61 | -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */ | |
62 | -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */ | |
63 | -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */ | |
64 | -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */ | |
65 | -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */ | |
66 | -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */ | |
67 | -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */ | |
68 | -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */ | |
69 | -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */ | |
70 | -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */ | |
71 | -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */ | |
72 | }; | |
0fcfd160 LT |
73 | |
74 | int get_sha1_hex(const char *hex, unsigned char *sha1) | |
75 | { | |
76 | int i; | |
77 | for (i = 0; i < 20; i++) { | |
78 | unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); | |
79 | if (val & ~0xff) | |
80 | return -1; | |
81 | *sha1++ = val; | |
82 | hex += 2; | |
83 | } | |
84 | return 0; | |
85 | } | |
86 | ||
b2cb9425 JH |
87 | int safe_create_leading_directories(char *path) |
88 | { | |
89 | char *pos = path; | |
67d42212 JR |
90 | struct stat st; |
91 | ||
85dadc38 | 92 | if (is_absolute_path(path)) |
67ffdf4c | 93 | pos++; |
b2cb9425 JH |
94 | |
95 | while (pos) { | |
96 | pos = strchr(pos, '/'); | |
97 | if (!pos) | |
98 | break; | |
99 | *pos = 0; | |
67d42212 JR |
100 | if (!stat(path, &st)) { |
101 | /* path exists */ | |
102 | if (!S_ISDIR(st.st_mode)) { | |
b2cb9425 | 103 | *pos = '/'; |
67d42212 | 104 | return -3; |
b2cb9425 | 105 | } |
457f06d6 | 106 | } |
67d42212 JR |
107 | else if (mkdir(path, 0777)) { |
108 | *pos = '/'; | |
109 | return -1; | |
110 | } | |
457f06d6 JS |
111 | else if (adjust_shared_perm(path)) { |
112 | *pos = '/'; | |
113 | return -2; | |
114 | } | |
b2cb9425 JH |
115 | *pos++ = '/'; |
116 | } | |
117 | return 0; | |
118 | } | |
723c31fe | 119 | |
0fcfd160 LT |
120 | char * sha1_to_hex(const unsigned char *sha1) |
121 | { | |
dcb3450f LT |
122 | static int bufno; |
123 | static char hexbuffer[4][50]; | |
0fcfd160 | 124 | static const char hex[] = "0123456789abcdef"; |
dcb3450f | 125 | char *buffer = hexbuffer[3 & ++bufno], *buf = buffer; |
0fcfd160 LT |
126 | int i; |
127 | ||
128 | for (i = 0; i < 20; i++) { | |
129 | unsigned int val = *sha1++; | |
130 | *buf++ = hex[val >> 4]; | |
131 | *buf++ = hex[val & 0xf]; | |
132 | } | |
1e80e044 JS |
133 | *buf = '\0'; |
134 | ||
0fcfd160 LT |
135 | return buffer; |
136 | } | |
137 | ||
ace1534d JH |
138 | static void fill_sha1_path(char *pathbuf, const unsigned char *sha1) |
139 | { | |
140 | int i; | |
141 | for (i = 0; i < 20; i++) { | |
142 | static char hex[] = "0123456789abcdef"; | |
143 | unsigned int val = sha1[i]; | |
144 | char *pos = pathbuf + i*2 + (i > 0); | |
145 | *pos++ = hex[val >> 4]; | |
146 | *pos = hex[val & 0xf]; | |
147 | } | |
148 | } | |
149 | ||
0fcfd160 LT |
150 | /* |
151 | * NOTE! This returns a statically allocated buffer, so you have to be | |
790296fd | 152 | * careful about using it. Do an "xstrdup()" if you need to save the |
0fcfd160 | 153 | * filename. |
ace1534d JH |
154 | * |
155 | * Also note that this returns the location for creating. Reading | |
156 | * SHA1 file can happen from any alternate directory listed in the | |
d19938ab | 157 | * DB_ENVIRONMENT environment variable if it is not found in |
ace1534d | 158 | * the primary object database. |
0fcfd160 LT |
159 | */ |
160 | char *sha1_file_name(const unsigned char *sha1) | |
161 | { | |
0fcfd160 LT |
162 | static char *name, *base; |
163 | ||
164 | if (!base) { | |
d19938ab | 165 | const char *sha1_file_directory = get_object_directory(); |
0fcfd160 | 166 | int len = strlen(sha1_file_directory); |
812666c8 | 167 | base = xmalloc(len + 60); |
0fcfd160 LT |
168 | memcpy(base, sha1_file_directory, len); |
169 | memset(base+len, 0, 60); | |
170 | base[len] = '/'; | |
171 | base[len+3] = '/'; | |
172 | name = base + len + 1; | |
173 | } | |
ace1534d | 174 | fill_sha1_path(name, sha1); |
0fcfd160 LT |
175 | return base; |
176 | } | |
177 | ||
bf592c50 DB |
178 | char *sha1_pack_name(const unsigned char *sha1) |
179 | { | |
180 | static const char hex[] = "0123456789abcdef"; | |
181 | static char *name, *base, *buf; | |
182 | int i; | |
183 | ||
184 | if (!base) { | |
185 | const char *sha1_file_directory = get_object_directory(); | |
186 | int len = strlen(sha1_file_directory); | |
187 | base = xmalloc(len + 60); | |
188 | sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory); | |
189 | name = base + len + 11; | |
190 | } | |
191 | ||
192 | buf = name; | |
193 | ||
194 | for (i = 0; i < 20; i++) { | |
195 | unsigned int val = *sha1++; | |
196 | *buf++ = hex[val >> 4]; | |
197 | *buf++ = hex[val & 0xf]; | |
198 | } | |
a6080a0a | 199 | |
bf592c50 DB |
200 | return base; |
201 | } | |
202 | ||
203 | char *sha1_pack_index_name(const unsigned char *sha1) | |
204 | { | |
205 | static const char hex[] = "0123456789abcdef"; | |
206 | static char *name, *base, *buf; | |
207 | int i; | |
208 | ||
209 | if (!base) { | |
210 | const char *sha1_file_directory = get_object_directory(); | |
211 | int len = strlen(sha1_file_directory); | |
212 | base = xmalloc(len + 60); | |
213 | sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory); | |
214 | name = base + len + 11; | |
215 | } | |
216 | ||
217 | buf = name; | |
218 | ||
219 | for (i = 0; i < 20; i++) { | |
220 | unsigned int val = *sha1++; | |
221 | *buf++ = hex[val >> 4]; | |
222 | *buf++ = hex[val & 0xf]; | |
223 | } | |
a6080a0a | 224 | |
bf592c50 DB |
225 | return base; |
226 | } | |
227 | ||
d5a63b99 JH |
228 | struct alternate_object_database *alt_odb_list; |
229 | static struct alternate_object_database **alt_odb_tail; | |
ace1534d | 230 | |
c2f493a4 MW |
231 | static void read_info_alternates(const char * alternates, int depth); |
232 | ||
ddd5d056 JH |
233 | /* |
234 | * Prepare alternate object database registry. | |
d5a63b99 JH |
235 | * |
236 | * The variable alt_odb_list points at the list of struct | |
237 | * alternate_object_database. The elements on this list come from | |
238 | * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT | |
239 | * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates, | |
1494e038 JH |
240 | * whose contents is similar to that environment variable but can be |
241 | * LF separated. Its base points at a statically allocated buffer that | |
d5a63b99 JH |
242 | * contains "/the/directory/corresponding/to/.git/objects/...", while |
243 | * its name points just after the slash at the end of ".git/objects/" | |
244 | * in the example above, and has enough space to hold 40-byte hex | |
245 | * SHA1, an extra slash for the first level indirection, and the | |
246 | * terminating NUL. | |
ddd5d056 | 247 | */ |
c2f493a4 | 248 | static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth) |
ace1534d | 249 | { |
c2f493a4 | 250 | struct stat st; |
1494e038 | 251 | const char *objdir = get_object_directory(); |
c2f493a4 MW |
252 | struct alternate_object_database *ent; |
253 | struct alternate_object_database *alt; | |
254 | /* 43 = 40-byte + 2 '/' + terminating NUL */ | |
255 | int pfxlen = len; | |
256 | int entlen = pfxlen + 43; | |
ccfd3e99 | 257 | int base_len = -1; |
d5a63b99 | 258 | |
85dadc38 | 259 | if (!is_absolute_path(entry) && relative_base) { |
c2f493a4 MW |
260 | /* Relative alt-odb */ |
261 | if (base_len < 0) | |
262 | base_len = strlen(relative_base) + 1; | |
263 | entlen += base_len; | |
264 | pfxlen += base_len; | |
265 | } | |
266 | ent = xmalloc(sizeof(*ent) + entlen); | |
267 | ||
85dadc38 | 268 | if (!is_absolute_path(entry) && relative_base) { |
c2f493a4 MW |
269 | memcpy(ent->base, relative_base, base_len - 1); |
270 | ent->base[base_len - 1] = '/'; | |
271 | memcpy(ent->base + base_len, entry, len); | |
272 | } | |
273 | else | |
274 | memcpy(ent->base, entry, pfxlen); | |
275 | ||
276 | ent->name = ent->base + pfxlen + 1; | |
277 | ent->base[pfxlen + 3] = '/'; | |
278 | ent->base[pfxlen] = ent->base[entlen-1] = 0; | |
279 | ||
280 | /* Detect cases where alternate disappeared */ | |
281 | if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) { | |
282 | error("object directory %s does not exist; " | |
283 | "check .git/objects/info/alternates.", | |
284 | ent->base); | |
285 | free(ent); | |
286 | return -1; | |
287 | } | |
288 | ||
289 | /* Prevent the common mistake of listing the same | |
290 | * thing twice, or object directory itself. | |
291 | */ | |
292 | for (alt = alt_odb_list; alt; alt = alt->next) { | |
293 | if (!memcmp(ent->base, alt->base, pfxlen)) { | |
294 | free(ent); | |
295 | return -1; | |
296 | } | |
297 | } | |
298 | if (!memcmp(ent->base, objdir, pfxlen)) { | |
299 | free(ent); | |
300 | return -1; | |
301 | } | |
302 | ||
303 | /* add the alternate entry */ | |
304 | *alt_odb_tail = ent; | |
305 | alt_odb_tail = &(ent->next); | |
306 | ent->next = NULL; | |
307 | ||
308 | /* recursively add alternates */ | |
309 | read_info_alternates(ent->base, depth + 1); | |
310 | ||
311 | ent->base[pfxlen] = '/'; | |
312 | ||
313 | return 0; | |
314 | } | |
315 | ||
316 | static void link_alt_odb_entries(const char *alt, const char *ep, int sep, | |
317 | const char *relative_base, int depth) | |
318 | { | |
319 | const char *cp, *last; | |
320 | ||
321 | if (depth > 5) { | |
322 | error("%s: ignoring alternate object stores, nesting too deep.", | |
323 | relative_base); | |
324 | return; | |
325 | } | |
326 | ||
d5a63b99 | 327 | last = alt; |
9577e7e3 JH |
328 | while (last < ep) { |
329 | cp = last; | |
330 | if (cp < ep && *cp == '#') { | |
331 | while (cp < ep && *cp != sep) | |
332 | cp++; | |
333 | last = cp + 1; | |
334 | continue; | |
335 | } | |
c2f493a4 MW |
336 | while (cp < ep && *cp != sep) |
337 | cp++; | |
d5a63b99 | 338 | if (last != cp) { |
85dadc38 | 339 | if (!is_absolute_path(last) && depth) { |
c2f493a4 MW |
340 | error("%s: ignoring relative alternate object store %s", |
341 | relative_base, last); | |
342 | } else { | |
343 | link_alt_odb_entry(last, cp - last, | |
344 | relative_base, depth); | |
1494e038 | 345 | } |
d5a63b99 | 346 | } |
9577e7e3 | 347 | while (cp < ep && *cp == sep) |
d5a63b99 JH |
348 | cp++; |
349 | last = cp; | |
9577e7e3 | 350 | } |
d5a63b99 JH |
351 | } |
352 | ||
c2f493a4 | 353 | static void read_info_alternates(const char * relative_base, int depth) |
d5a63b99 | 354 | { |
9577e7e3 | 355 | char *map; |
dc49cd76 | 356 | size_t mapsz; |
d5a63b99 | 357 | struct stat st; |
9cb18f56 JM |
358 | const char alt_file_name[] = "info/alternates"; |
359 | /* Given that relative_base is no longer than PATH_MAX, | |
360 | ensure that "path" has enough space to append "/", the | |
361 | file name, "info/alternates", and a trailing NUL. */ | |
362 | char path[PATH_MAX + 1 + sizeof alt_file_name]; | |
c2f493a4 | 363 | int fd; |
d5a63b99 | 364 | |
9cb18f56 | 365 | sprintf(path, "%s/%s", relative_base, alt_file_name); |
d5a63b99 JH |
366 | fd = open(path, O_RDONLY); |
367 | if (fd < 0) | |
368 | return; | |
369 | if (fstat(fd, &st) || (st.st_size == 0)) { | |
370 | close(fd); | |
9a217f2a | 371 | return; |
ace1534d | 372 | } |
dc49cd76 SP |
373 | mapsz = xsize_t(st.st_size); |
374 | map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0); | |
d5a63b99 | 375 | close(fd); |
d5a63b99 | 376 | |
dc49cd76 | 377 | link_alt_odb_entries(map, map + mapsz, '\n', relative_base, depth); |
c2f493a4 | 378 | |
dc49cd76 | 379 | munmap(map, mapsz); |
ace1534d JH |
380 | } |
381 | ||
c2f493a4 MW |
382 | void prepare_alt_odb(void) |
383 | { | |
554fe20d | 384 | const char *alt; |
c2f493a4 | 385 | |
7dc24aa5 SP |
386 | if (alt_odb_tail) |
387 | return; | |
388 | ||
c2f493a4 MW |
389 | alt = getenv(ALTERNATE_DB_ENVIRONMENT); |
390 | if (!alt) alt = ""; | |
391 | ||
c2f493a4 MW |
392 | alt_odb_tail = &alt_odb_list; |
393 | link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL, 0); | |
394 | ||
395 | read_info_alternates(get_object_directory(), 0); | |
396 | } | |
397 | ||
ace1534d JH |
398 | static char *find_sha1_file(const unsigned char *sha1, struct stat *st) |
399 | { | |
ace1534d | 400 | char *name = sha1_file_name(sha1); |
d5a63b99 | 401 | struct alternate_object_database *alt; |
ace1534d JH |
402 | |
403 | if (!stat(name, st)) | |
404 | return name; | |
9a217f2a | 405 | prepare_alt_odb(); |
d5a63b99 JH |
406 | for (alt = alt_odb_list; alt; alt = alt->next) { |
407 | name = alt->name; | |
ace1534d | 408 | fill_sha1_path(name, sha1); |
d5a63b99 JH |
409 | if (!stat(alt->base, st)) |
410 | return alt->base; | |
ace1534d JH |
411 | } |
412 | return NULL; | |
413 | } | |
414 | ||
60bb8b14 | 415 | static unsigned int pack_used_ctr; |
a53128b6 SP |
416 | static unsigned int pack_mmap_calls; |
417 | static unsigned int peak_pack_open_windows; | |
418 | static unsigned int pack_open_windows; | |
419 | static size_t peak_pack_mapped; | |
60bb8b14 | 420 | static size_t pack_mapped; |
9a217f2a | 421 | struct packed_git *packed_git; |
1f688557 | 422 | |
b79d18c9 | 423 | void pack_report(void) |
a53128b6 SP |
424 | { |
425 | fprintf(stderr, | |
e05db0fd PR |
426 | "pack_report: getpagesize() = %10" SZ_FMT "\n" |
427 | "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n" | |
428 | "pack_report: core.packedGitLimit = %10" SZ_FMT "\n", | |
9e42d6a1 SP |
429 | sz_fmt(getpagesize()), |
430 | sz_fmt(packed_git_window_size), | |
431 | sz_fmt(packed_git_limit)); | |
a53128b6 SP |
432 | fprintf(stderr, |
433 | "pack_report: pack_used_ctr = %10u\n" | |
434 | "pack_report: pack_mmap_calls = %10u\n" | |
435 | "pack_report: pack_open_windows = %10u / %10u\n" | |
e05db0fd PR |
436 | "pack_report: pack_mapped = " |
437 | "%10" SZ_FMT " / %10" SZ_FMT "\n", | |
a53128b6 SP |
438 | pack_used_ctr, |
439 | pack_mmap_calls, | |
440 | pack_open_windows, peak_pack_open_windows, | |
9e42d6a1 | 441 | sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped)); |
a53128b6 SP |
442 | } |
443 | ||
42873078 | 444 | static int check_packed_git_idx(const char *path, struct packed_git *p) |
1f688557 JH |
445 | { |
446 | void *idx_map; | |
42873078 | 447 | struct pack_idx_header *hdr; |
dc49cd76 | 448 | size_t idx_size; |
74e34e1f | 449 | uint32_t version, nr, i, *index; |
e93ec6f9 | 450 | int fd = open(path, O_RDONLY); |
1f688557 | 451 | struct stat st; |
42873078 | 452 | |
1f688557 JH |
453 | if (fd < 0) |
454 | return -1; | |
455 | if (fstat(fd, &st)) { | |
456 | close(fd); | |
457 | return -1; | |
458 | } | |
dc49cd76 | 459 | idx_size = xsize_t(st.st_size); |
2d88451b SP |
460 | if (idx_size < 4 * 256 + 20 + 20) { |
461 | close(fd); | |
462 | return error("index file %s is too small", path); | |
463 | } | |
c4712e45 | 464 | idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); |
1f688557 | 465 | close(fd); |
1f688557 | 466 | |
42873078 NP |
467 | hdr = idx_map; |
468 | if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) { | |
74e34e1f NP |
469 | version = ntohl(hdr->idx_version); |
470 | if (version < 2 || version > 2) { | |
471 | munmap(idx_map, idx_size); | |
472 | return error("index file %s is version %d" | |
473 | " and is not supported by this binary" | |
474 | " (try upgrading GIT to a newer version)", | |
475 | path, version); | |
476 | } | |
477 | } else | |
478 | version = 1; | |
df1b059d | 479 | |
1f688557 | 480 | nr = 0; |
42873078 | 481 | index = idx_map; |
74e34e1f NP |
482 | if (version > 1) |
483 | index += 2; /* skip index header */ | |
1f688557 | 484 | for (i = 0; i < 256; i++) { |
326bf396 | 485 | uint32_t n = ntohl(index[i]); |
2d88451b SP |
486 | if (n < nr) { |
487 | munmap(idx_map, idx_size); | |
df1b059d | 488 | return error("non-monotonic index %s", path); |
2d88451b | 489 | } |
1f688557 JH |
490 | nr = n; |
491 | } | |
492 | ||
74e34e1f NP |
493 | if (version == 1) { |
494 | /* | |
495 | * Total size: | |
496 | * - 256 index entries 4 bytes each | |
497 | * - 24-byte entries * nr (20-byte sha1 + 4-byte offset) | |
498 | * - 20-byte SHA1 of the packfile | |
499 | * - 20-byte SHA1 file checksum | |
500 | */ | |
501 | if (idx_size != 4*256 + nr * 24 + 20 + 20) { | |
502 | munmap(idx_map, idx_size); | |
eef427a0 | 503 | return error("wrong index v1 file size in %s", path); |
74e34e1f NP |
504 | } |
505 | } else if (version == 2) { | |
506 | /* | |
507 | * Minimum size: | |
508 | * - 8 bytes of header | |
509 | * - 256 index entries 4 bytes each | |
510 | * - 20-byte sha1 entry * nr | |
511 | * - 4-byte crc entry * nr | |
512 | * - 4-byte offset entry * nr | |
513 | * - 20-byte SHA1 of the packfile | |
514 | * - 20-byte SHA1 file checksum | |
515 | * And after the 4-byte offset table might be a | |
516 | * variable sized table containing 8-byte entries | |
517 | * for offsets larger than 2^31. | |
518 | */ | |
519 | unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20; | |
1164f1e4 LT |
520 | unsigned long max_size = min_size; |
521 | if (nr) | |
522 | max_size += (nr - 1)*8; | |
523 | if (idx_size < min_size || idx_size > max_size) { | |
74e34e1f | 524 | munmap(idx_map, idx_size); |
eef427a0 | 525 | return error("wrong index v2 file size in %s", path); |
74e34e1f | 526 | } |
7109c889 JH |
527 | if (idx_size != min_size && |
528 | /* | |
529 | * make sure we can deal with large pack offsets. | |
530 | * 31-bit signed offset won't be enough, neither | |
531 | * 32-bit unsigned one will be. | |
532 | */ | |
533 | (sizeof(off_t) <= 4)) { | |
534 | munmap(idx_map, idx_size); | |
535 | return error("pack too large for current definition of off_t in %s", path); | |
74e34e1f | 536 | } |
2d88451b | 537 | } |
1f688557 | 538 | |
74e34e1f | 539 | p->index_version = version; |
42873078 NP |
540 | p->index_data = idx_map; |
541 | p->index_size = idx_size; | |
57059091 | 542 | p->num_objects = nr; |
1f688557 JH |
543 | return 0; |
544 | } | |
545 | ||
bc8e478a | 546 | int open_pack_index(struct packed_git *p) |
d079837e SP |
547 | { |
548 | char *idx_name; | |
549 | int ret; | |
550 | ||
551 | if (p->index_data) | |
552 | return 0; | |
553 | ||
554 | idx_name = xstrdup(p->pack_name); | |
555 | strcpy(idx_name + strlen(idx_name) - strlen(".pack"), ".idx"); | |
556 | ret = check_packed_git_idx(idx_name, p); | |
557 | free(idx_name); | |
558 | return ret; | |
559 | } | |
560 | ||
11daf39b SP |
561 | static void scan_windows(struct packed_git *p, |
562 | struct packed_git **lru_p, | |
563 | struct pack_window **lru_w, | |
564 | struct pack_window **lru_l) | |
1f688557 | 565 | { |
11daf39b SP |
566 | struct pack_window *w, *w_l; |
567 | ||
568 | for (w_l = NULL, w = p->windows; w; w = w->next) { | |
569 | if (!w->inuse_cnt) { | |
570 | if (!*lru_w || w->last_used < (*lru_w)->last_used) { | |
571 | *lru_p = p; | |
572 | *lru_w = w; | |
573 | *lru_l = w_l; | |
54044bf8 | 574 | } |
54044bf8 | 575 | } |
11daf39b | 576 | w_l = w; |
f9253394 | 577 | } |
11daf39b SP |
578 | } |
579 | ||
d1efefa4 | 580 | static int unuse_one_window(struct packed_git *current, int keep_fd) |
11daf39b SP |
581 | { |
582 | struct packed_git *p, *lru_p = NULL; | |
583 | struct pack_window *lru_w = NULL, *lru_l = NULL; | |
584 | ||
585 | if (current) | |
586 | scan_windows(current, &lru_p, &lru_w, &lru_l); | |
587 | for (p = packed_git; p; p = p->next) | |
588 | scan_windows(p, &lru_p, &lru_w, &lru_l); | |
54044bf8 SP |
589 | if (lru_p) { |
590 | munmap(lru_w->base, lru_w->len); | |
591 | pack_mapped -= lru_w->len; | |
592 | if (lru_l) | |
593 | lru_l->next = lru_w->next; | |
594 | else { | |
595 | lru_p->windows = lru_w->next; | |
d1efefa4 | 596 | if (!lru_p->windows && lru_p->pack_fd != keep_fd) { |
54044bf8 SP |
597 | close(lru_p->pack_fd); |
598 | lru_p->pack_fd = -1; | |
599 | } | |
600 | } | |
601 | free(lru_w); | |
a53128b6 | 602 | pack_open_windows--; |
54044bf8 SP |
603 | return 1; |
604 | } | |
605 | return 0; | |
f9253394 JH |
606 | } |
607 | ||
d1efefa4 | 608 | void release_pack_memory(size_t need, int fd) |
97bfeb34 SP |
609 | { |
610 | size_t cur = pack_mapped; | |
d1efefa4 | 611 | while (need >= (cur - pack_mapped) && unuse_one_window(NULL, fd)) |
97bfeb34 SP |
612 | ; /* nothing */ |
613 | } | |
614 | ||
c9ced051 SP |
615 | void close_pack_windows(struct packed_git *p) |
616 | { | |
617 | while (p->windows) { | |
618 | struct pack_window *w = p->windows; | |
619 | ||
620 | if (w->inuse_cnt) | |
621 | die("pack '%s' still has open windows to it", | |
622 | p->pack_name); | |
623 | munmap(w->base, w->len); | |
624 | pack_mapped -= w->len; | |
625 | pack_open_windows--; | |
626 | p->windows = w->next; | |
627 | free(w); | |
628 | } | |
629 | } | |
630 | ||
03e79c88 | 631 | void unuse_pack(struct pack_window **w_cursor) |
f9253394 | 632 | { |
03e79c88 SP |
633 | struct pack_window *w = *w_cursor; |
634 | if (w) { | |
635 | w->inuse_cnt--; | |
636 | *w_cursor = NULL; | |
637 | } | |
1f688557 JH |
638 | } |
639 | ||
3cf8b462 SP |
640 | /* |
641 | * Do not call this directly as this leaks p->pack_fd on error return; | |
642 | * call open_packed_git() instead. | |
643 | */ | |
644 | static int open_packed_git_1(struct packed_git *p) | |
1f688557 | 645 | { |
9bc879c1 SP |
646 | struct stat st; |
647 | struct pack_header hdr; | |
648 | unsigned char sha1[20]; | |
649 | unsigned char *idx_sha1; | |
2c039da8 | 650 | long fd_flag; |
9bc879c1 | 651 | |
d079837e SP |
652 | if (!p->index_data && open_pack_index(p)) |
653 | return error("packfile %s index unavailable", p->pack_name); | |
654 | ||
9bc879c1 SP |
655 | p->pack_fd = open(p->pack_name, O_RDONLY); |
656 | if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) | |
072db278 | 657 | return -1; |
9bc879c1 SP |
658 | |
659 | /* If we created the struct before we had the pack we lack size. */ | |
bf592c50 | 660 | if (!p->pack_size) { |
bf592c50 | 661 | if (!S_ISREG(st.st_mode)) |
072db278 | 662 | return error("packfile %s not a regular file", p->pack_name); |
bf592c50 | 663 | p->pack_size = st.st_size; |
9bc879c1 | 664 | } else if (p->pack_size != st.st_size) |
072db278 | 665 | return error("packfile %s size changed", p->pack_name); |
9bc879c1 | 666 | |
2c039da8 JH |
667 | /* We leave these file descriptors open with sliding mmap; |
668 | * there is no point keeping them open across exec(), though. | |
669 | */ | |
670 | fd_flag = fcntl(p->pack_fd, F_GETFD, 0); | |
671 | if (fd_flag < 0) | |
072db278 | 672 | return error("cannot determine file descriptor flags"); |
2c039da8 JH |
673 | fd_flag |= FD_CLOEXEC; |
674 | if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1) | |
072db278 | 675 | return error("cannot set FD_CLOEXEC"); |
2c039da8 | 676 | |
9bc879c1 | 677 | /* Verify we recognize this pack file format. */ |
e6e2bd62 | 678 | if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
072db278 | 679 | return error("file %s is far too short to be a packfile", p->pack_name); |
9bc879c1 | 680 | if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) |
072db278 | 681 | return error("file %s is not a GIT packfile", p->pack_name); |
9bc879c1 | 682 | if (!pack_version_ok(hdr.hdr_version)) |
072db278 | 683 | return error("packfile %s is version %u and not supported" |
9bc879c1 SP |
684 | " (try upgrading GIT to a newer version)", |
685 | p->pack_name, ntohl(hdr.hdr_version)); | |
686 | ||
687 | /* Verify the pack matches its index. */ | |
57059091 | 688 | if (p->num_objects != ntohl(hdr.hdr_entries)) |
072db278 | 689 | return error("packfile %s claims to have %u objects" |
57059091 NP |
690 | " while index indicates %u objects", |
691 | p->pack_name, ntohl(hdr.hdr_entries), | |
692 | p->num_objects); | |
9bc879c1 | 693 | if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1) |
072db278 | 694 | return error("end of packfile %s is unavailable", p->pack_name); |
e6e2bd62 | 695 | if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1)) |
072db278 | 696 | return error("packfile %s signature is unavailable", p->pack_name); |
42873078 | 697 | idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40; |
9bc879c1 | 698 | if (hashcmp(sha1, idx_sha1)) |
072db278 SP |
699 | return error("packfile %s does not match index", p->pack_name); |
700 | return 0; | |
9bc879c1 SP |
701 | } |
702 | ||
3cf8b462 SP |
703 | static int open_packed_git(struct packed_git *p) |
704 | { | |
705 | if (!open_packed_git_1(p)) | |
706 | return 0; | |
707 | if (p->pack_fd != -1) { | |
708 | close(p->pack_fd); | |
709 | p->pack_fd = -1; | |
710 | } | |
711 | return -1; | |
712 | } | |
713 | ||
c4001d92 | 714 | static int in_window(struct pack_window *win, off_t offset) |
60bb8b14 SP |
715 | { |
716 | /* We must promise at least 20 bytes (one hash) after the | |
717 | * offset is available from this window, otherwise the offset | |
718 | * is not actually in this window and a different window (which | |
719 | * has that one hash excess) must be used. This is to support | |
720 | * the object header and delta base parsing routines below. | |
721 | */ | |
722 | off_t win_off = win->offset; | |
723 | return win_off <= offset | |
724 | && (offset + 20) <= (win_off + win->len); | |
725 | } | |
726 | ||
03e79c88 SP |
727 | unsigned char* use_pack(struct packed_git *p, |
728 | struct pack_window **w_cursor, | |
c4001d92 | 729 | off_t offset, |
03e79c88 | 730 | unsigned int *left) |
9bc879c1 | 731 | { |
60bb8b14 | 732 | struct pack_window *win = *w_cursor; |
03e79c88 | 733 | |
072db278 SP |
734 | if (p->pack_fd == -1 && open_packed_git(p)) |
735 | die("packfile %s cannot be accessed", p->pack_name); | |
60bb8b14 SP |
736 | |
737 | /* Since packfiles end in a hash of their content and its | |
738 | * pointless to ask for an offset into the middle of that | |
739 | * hash, and the in_window function above wouldn't match | |
740 | * don't allow an offset too close to the end of the file. | |
741 | */ | |
742 | if (offset > (p->pack_size - 20)) | |
743 | die("offset beyond end of packfile (truncated pack?)"); | |
744 | ||
745 | if (!win || !in_window(win, offset)) { | |
746 | if (win) | |
747 | win->inuse_cnt--; | |
748 | for (win = p->windows; win; win = win->next) { | |
749 | if (in_window(win, offset)) | |
750 | break; | |
751 | } | |
752 | if (!win) { | |
78a28df9 | 753 | size_t window_align = packed_git_window_size / 2; |
dc49cd76 | 754 | off_t len; |
60bb8b14 | 755 | win = xcalloc(1, sizeof(*win)); |
78a28df9 | 756 | win->offset = (offset / window_align) * window_align; |
dc49cd76 SP |
757 | len = p->pack_size - win->offset; |
758 | if (len > packed_git_window_size) | |
759 | len = packed_git_window_size; | |
760 | win->len = (size_t)len; | |
60bb8b14 | 761 | pack_mapped += win->len; |
11daf39b | 762 | while (packed_git_limit < pack_mapped |
d1efefa4 | 763 | && unuse_one_window(p, p->pack_fd)) |
60bb8b14 | 764 | ; /* nothing */ |
c4712e45 | 765 | win->base = xmmap(NULL, win->len, |
60bb8b14 SP |
766 | PROT_READ, MAP_PRIVATE, |
767 | p->pack_fd, win->offset); | |
768 | if (win->base == MAP_FAILED) | |
73b4e4be SP |
769 | die("packfile %s cannot be mapped: %s", |
770 | p->pack_name, | |
771 | strerror(errno)); | |
a53128b6 SP |
772 | pack_mmap_calls++; |
773 | pack_open_windows++; | |
774 | if (pack_mapped > peak_pack_mapped) | |
775 | peak_pack_mapped = pack_mapped; | |
776 | if (pack_open_windows > peak_pack_open_windows) | |
777 | peak_pack_open_windows = pack_open_windows; | |
60bb8b14 SP |
778 | win->next = p->windows; |
779 | p->windows = win; | |
780 | } | |
1f688557 | 781 | } |
03e79c88 SP |
782 | if (win != *w_cursor) { |
783 | win->last_used = pack_used_ctr++; | |
784 | win->inuse_cnt++; | |
785 | *w_cursor = win; | |
786 | } | |
60bb8b14 | 787 | offset -= win->offset; |
03e79c88 | 788 | if (left) |
dc49cd76 | 789 | *left = win->len - xsize_t(offset); |
03e79c88 | 790 | return win->base + offset; |
1f688557 JH |
791 | } |
792 | ||
42873078 | 793 | struct packed_git *add_packed_git(const char *path, int path_len, int local) |
1f688557 JH |
794 | { |
795 | struct stat st; | |
42873078 | 796 | struct packed_git *p = xmalloc(sizeof(*p) + path_len + 2); |
1f688557 | 797 | |
42873078 NP |
798 | /* |
799 | * Make sure a corresponding .pack file exists and that | |
800 | * the index looks sane. | |
801 | */ | |
802 | path_len -= strlen(".idx"); | |
803 | if (path_len < 1) | |
1f688557 | 804 | return NULL; |
42873078 NP |
805 | memcpy(p->pack_name, path, path_len); |
806 | strcpy(p->pack_name + path_len, ".pack"); | |
d079837e | 807 | if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) { |
42873078 | 808 | free(p); |
1f688557 JH |
809 | return NULL; |
810 | } | |
42873078 | 811 | |
1f688557 JH |
812 | /* ok, it looks sane as far as we can check without |
813 | * actually mapping the pack file. | |
814 | */ | |
d079837e SP |
815 | p->index_version = 0; |
816 | p->index_data = NULL; | |
817 | p->index_size = 0; | |
818 | p->num_objects = 0; | |
1f688557 | 819 | p->pack_size = st.st_size; |
1f688557 | 820 | p->next = NULL; |
c41ee586 | 821 | p->windows = NULL; |
9bc879c1 | 822 | p->pack_fd = -1; |
9d835df2 | 823 | p->pack_local = local; |
b867092f | 824 | p->mtime = st.st_mtime; |
42873078 NP |
825 | if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1)) |
826 | hashclr(p->sha1); | |
1f688557 JH |
827 | return p; |
828 | } | |
829 | ||
bf592c50 | 830 | struct packed_git *parse_pack_index(unsigned char *sha1) |
c508df5e DB |
831 | { |
832 | char *path = sha1_pack_index_name(sha1); | |
833 | return parse_pack_index_file(sha1, path); | |
834 | } | |
835 | ||
42873078 NP |
836 | struct packed_git *parse_pack_index_file(const unsigned char *sha1, |
837 | const char *idx_path) | |
bf592c50 | 838 | { |
42873078 NP |
839 | const char *path = sha1_pack_name(sha1); |
840 | struct packed_git *p = xmalloc(sizeof(*p) + strlen(path) + 2); | |
bf592c50 | 841 | |
42873078 NP |
842 | if (check_packed_git_idx(idx_path, p)) { |
843 | free(p); | |
bf592c50 | 844 | return NULL; |
42873078 | 845 | } |
bf592c50 | 846 | |
bf592c50 | 847 | strcpy(p->pack_name, path); |
bf592c50 | 848 | p->pack_size = 0; |
bf592c50 | 849 | p->next = NULL; |
c41ee586 | 850 | p->windows = NULL; |
9bc879c1 | 851 | p->pack_fd = -1; |
e702496e | 852 | hashcpy(p->sha1, sha1); |
bf592c50 DB |
853 | return p; |
854 | } | |
855 | ||
856 | void install_packed_git(struct packed_git *pack) | |
857 | { | |
858 | pack->next = packed_git; | |
859 | packed_git = pack; | |
860 | } | |
861 | ||
9d835df2 | 862 | static void prepare_packed_git_one(char *objdir, int local) |
1f688557 | 863 | { |
9cb18f56 JM |
864 | /* Ensure that this buffer is large enough so that we can |
865 | append "/pack/" without clobbering the stack even if | |
866 | strlen(objdir) were PATH_MAX. */ | |
867 | char path[PATH_MAX + 1 + 4 + 1 + 1]; | |
1f688557 JH |
868 | int len; |
869 | DIR *dir; | |
870 | struct dirent *de; | |
871 | ||
872 | sprintf(path, "%s/pack", objdir); | |
873 | len = strlen(path); | |
874 | dir = opendir(path); | |
b5b16990 | 875 | if (!dir) { |
26125f6b | 876 | if (errno != ENOENT) |
bd2afde8 | 877 | error("unable to open object pack directory: %s: %s", |
26125f6b | 878 | path, strerror(errno)); |
1f688557 | 879 | return; |
b5b16990 | 880 | } |
1f688557 JH |
881 | path[len++] = '/'; |
882 | while ((de = readdir(dir)) != NULL) { | |
883 | int namelen = strlen(de->d_name); | |
884 | struct packed_git *p; | |
885 | ||
5bb1cda5 | 886 | if (!has_extension(de->d_name, ".idx")) |
1f688557 JH |
887 | continue; |
888 | ||
9cb18f56 JM |
889 | if (len + namelen + 1 > sizeof(path)) |
890 | continue; | |
891 | ||
54a15a8d | 892 | /* Don't reopen a pack we already have. */ |
1f688557 | 893 | strcpy(path + len, de->d_name); |
86f7780c JK |
894 | for (p = packed_git; p; p = p->next) { |
895 | if (!memcmp(path, p->pack_name, len + namelen - 4)) | |
896 | break; | |
897 | } | |
898 | if (p) | |
899 | continue; | |
54a15a8d SP |
900 | /* See if it really is a valid .idx file with corresponding |
901 | * .pack file that we can map. | |
902 | */ | |
9d835df2 | 903 | p = add_packed_git(path, len + namelen, local); |
1f688557 JH |
904 | if (!p) |
905 | continue; | |
625e9421 | 906 | install_packed_git(p); |
1f688557 | 907 | } |
5b35bcd5 | 908 | closedir(dir); |
1f688557 JH |
909 | } |
910 | ||
b867092f JH |
911 | static int sort_pack(const void *a_, const void *b_) |
912 | { | |
913 | struct packed_git *a = *((struct packed_git **)a_); | |
914 | struct packed_git *b = *((struct packed_git **)b_); | |
915 | int st; | |
916 | ||
917 | /* | |
918 | * Local packs tend to contain objects specific to our | |
919 | * variant of the project than remote ones. In addition, | |
920 | * remote ones could be on a network mounted filesystem. | |
921 | * Favor local ones for these reasons. | |
922 | */ | |
923 | st = a->pack_local - b->pack_local; | |
924 | if (st) | |
925 | return -st; | |
926 | ||
927 | /* | |
928 | * Younger packs tend to contain more recent objects, | |
929 | * and more recent objects tend to get accessed more | |
930 | * often. | |
931 | */ | |
932 | if (a->mtime < b->mtime) | |
933 | return 1; | |
934 | else if (a->mtime == b->mtime) | |
935 | return 0; | |
936 | return -1; | |
937 | } | |
938 | ||
939 | static void rearrange_packed_git(void) | |
940 | { | |
941 | struct packed_git **ary, *p; | |
942 | int i, n; | |
943 | ||
944 | for (n = 0, p = packed_git; p; p = p->next) | |
945 | n++; | |
946 | if (n < 2) | |
947 | return; | |
948 | ||
949 | /* prepare an array of packed_git for easier sorting */ | |
950 | ary = xcalloc(n, sizeof(struct packed_git *)); | |
951 | for (n = 0, p = packed_git; p; p = p->next) | |
952 | ary[n++] = p; | |
953 | ||
954 | qsort(ary, n, sizeof(struct packed_git *), sort_pack); | |
955 | ||
956 | /* link them back again */ | |
957 | for (i = 0; i < n - 1; i++) | |
958 | ary[i]->next = ary[i + 1]; | |
959 | ary[n - 1]->next = NULL; | |
960 | packed_git = ary[0]; | |
961 | ||
962 | free(ary); | |
963 | } | |
964 | ||
637cdd9d | 965 | static int prepare_packed_git_run_once = 0; |
9a217f2a | 966 | void prepare_packed_git(void) |
1f688557 | 967 | { |
d5a63b99 | 968 | struct alternate_object_database *alt; |
1f688557 | 969 | |
637cdd9d | 970 | if (prepare_packed_git_run_once) |
1f688557 | 971 | return; |
9d835df2 | 972 | prepare_packed_git_one(get_object_directory(), 1); |
9a217f2a | 973 | prepare_alt_odb(); |
d5a63b99 | 974 | for (alt = alt_odb_list; alt; alt = alt->next) { |
1494e038 | 975 | alt->name[-1] = 0; |
9d835df2 | 976 | prepare_packed_git_one(alt->base, 0); |
1494e038 | 977 | alt->name[-1] = '/'; |
1f688557 | 978 | } |
b867092f | 979 | rearrange_packed_git(); |
637cdd9d JK |
980 | prepare_packed_git_run_once = 1; |
981 | } | |
982 | ||
fc04c412 | 983 | void reprepare_packed_git(void) |
637cdd9d JK |
984 | { |
985 | prepare_packed_git_run_once = 0; | |
986 | prepare_packed_git(); | |
1f688557 JH |
987 | } |
988 | ||
5d6ccf5c | 989 | int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type) |
0fcfd160 LT |
990 | { |
991 | unsigned char real_sha1[20]; | |
7cfb5f36 | 992 | hash_sha1_file(map, size, type, real_sha1); |
a89fccd2 | 993 | return hashcmp(sha1, real_sha1) ? -1 : 0; |
0fcfd160 LT |
994 | } |
995 | ||
4175e9e3 | 996 | static void *map_sha1_file(const unsigned char *sha1, unsigned long *size) |
0fcfd160 | 997 | { |
0fcfd160 LT |
998 | struct stat st; |
999 | void *map; | |
144bde78 | 1000 | int fd; |
ace1534d JH |
1001 | char *filename = find_sha1_file(sha1, &st); |
1002 | ||
1003 | if (!filename) { | |
ace1534d JH |
1004 | return NULL; |
1005 | } | |
0fcfd160 | 1006 | |
144bde78 | 1007 | fd = open(filename, O_RDONLY | sha1_file_open_flag); |
0fcfd160 | 1008 | if (fd < 0) { |
144bde78 LT |
1009 | /* See if it works without O_NOATIME */ |
1010 | switch (sha1_file_open_flag) { | |
1011 | default: | |
1012 | fd = open(filename, O_RDONLY); | |
1013 | if (fd >= 0) | |
1014 | break; | |
1015 | /* Fallthrough */ | |
1016 | case 0: | |
144bde78 LT |
1017 | return NULL; |
1018 | } | |
1019 | ||
1f688557 JH |
1020 | /* If it failed once, it will probably fail again. |
1021 | * Stop using O_NOATIME | |
1022 | */ | |
144bde78 | 1023 | sha1_file_open_flag = 0; |
0fcfd160 | 1024 | } |
dc49cd76 SP |
1025 | *size = xsize_t(st.st_size); |
1026 | map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); | |
0fcfd160 | 1027 | close(fd); |
0fcfd160 LT |
1028 | return map; |
1029 | } | |
1030 | ||
726f852b | 1031 | static int legacy_loose_object(unsigned char *map) |
bb6b8e4f JH |
1032 | { |
1033 | unsigned int word; | |
1034 | ||
1035 | /* | |
1036 | * Is it a zlib-compressed buffer? If so, the first byte | |
1037 | * must be 0x78 (15-bit window size, deflated), and the | |
1038 | * first 16-bit word is evenly divisible by 31 | |
1039 | */ | |
1040 | word = (map[0] << 8) + map[1]; | |
1041 | if (map[0] == 0x78 && !(word % 31)) | |
1042 | return 1; | |
1043 | else | |
1044 | return 0; | |
1045 | } | |
1046 | ||
72518e9c | 1047 | unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep) |
c4483576 | 1048 | { |
ad1ed5ee | 1049 | unsigned shift; |
93821bd9 | 1050 | unsigned char c; |
93821bd9 | 1051 | unsigned long size; |
ad1ed5ee JH |
1052 | unsigned long used = 0; |
1053 | ||
1054 | c = buf[used++]; | |
1055 | *type = (c >> 4) & 7; | |
1056 | size = c & 15; | |
1057 | shift = 4; | |
1058 | while (c & 0x80) { | |
1059 | if (len <= used) | |
1060 | return 0; | |
1061 | if (sizeof(long) * 8 <= shift) | |
1062 | return 0; | |
1063 | c = buf[used++]; | |
1064 | size += (c & 0x7f) << shift; | |
1065 | shift += 7; | |
1066 | } | |
1067 | *sizep = size; | |
1068 | return used; | |
1069 | } | |
1070 | ||
1071 | static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz) | |
1072 | { | |
1073 | unsigned long size, used; | |
1074 | static const char valid_loose_object_type[8] = { | |
1075 | 0, /* OBJ_EXT */ | |
1076 | 1, 1, 1, 1, /* "commit", "tree", "blob", "tag" */ | |
1077 | 0, /* "delta" and others are invalid in a loose object */ | |
93821bd9 | 1078 | }; |
ad1ed5ee | 1079 | enum object_type type; |
93821bd9 | 1080 | |
c4483576 LT |
1081 | /* Get the data stream */ |
1082 | memset(stream, 0, sizeof(*stream)); | |
1083 | stream->next_in = map; | |
1084 | stream->avail_in = mapsize; | |
1085 | stream->next_out = buffer; | |
93821bd9 LT |
1086 | stream->avail_out = bufsiz; |
1087 | ||
bb6b8e4f | 1088 | if (legacy_loose_object(map)) { |
93821bd9 LT |
1089 | inflateInit(stream); |
1090 | return inflate(stream, 0); | |
1091 | } | |
1092 | ||
726f852b NP |
1093 | |
1094 | /* | |
1095 | * There used to be a second loose object header format which | |
1096 | * was meant to mimic the in-pack format, allowing for direct | |
1097 | * copy of the object data. This format turned up not to be | |
1098 | * really worth it and we don't write it any longer. But we | |
1099 | * can still read it. | |
1100 | */ | |
ad1ed5ee JH |
1101 | used = unpack_object_header_gently(map, mapsize, &type, &size); |
1102 | if (!used || !valid_loose_object_type[type]) | |
93821bd9 | 1103 | return -1; |
ad1ed5ee JH |
1104 | map += used; |
1105 | mapsize -= used; | |
93821bd9 LT |
1106 | |
1107 | /* Set up the stream for the rest.. */ | |
1108 | stream->next_in = map; | |
1109 | stream->avail_in = mapsize; | |
c4483576 | 1110 | inflateInit(stream); |
93821bd9 LT |
1111 | |
1112 | /* And generate the fake traditional header */ | |
ad1ed5ee | 1113 | stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu", |
df843662 | 1114 | typename(type), size); |
93821bd9 | 1115 | return 0; |
c4483576 LT |
1116 | } |
1117 | ||
7efbff75 | 1118 | static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1) |
5180cacc LT |
1119 | { |
1120 | int bytes = strlen(buffer) + 1; | |
d565b341 | 1121 | unsigned char *buf = xmalloc(1+size); |
93821bd9 | 1122 | unsigned long n; |
7efbff75 | 1123 | int status = Z_OK; |
5180cacc | 1124 | |
93821bd9 LT |
1125 | n = stream->total_out - bytes; |
1126 | if (n > size) | |
1127 | n = size; | |
1128 | memcpy(buf, (char *) buffer + bytes, n); | |
1129 | bytes = n; | |
456cdf6e LT |
1130 | if (bytes <= size) { |
1131 | /* | |
1132 | * The above condition must be (bytes <= size), not | |
1133 | * (bytes < size). In other words, even though we | |
1134 | * expect no more output and set avail_out to zer0, | |
1135 | * the input zlib stream may have bytes that express | |
1136 | * "this concludes the stream", and we *do* want to | |
1137 | * eat that input. | |
1138 | * | |
1139 | * Otherwise we would not be able to test that we | |
1140 | * consumed all the input to reach the expected size; | |
1141 | * we also want to check that zlib tells us that all | |
1142 | * went well with status == Z_STREAM_END at the end. | |
1143 | */ | |
5180cacc LT |
1144 | stream->next_out = buf + bytes; |
1145 | stream->avail_out = size - bytes; | |
7efbff75 JH |
1146 | while (status == Z_OK) |
1147 | status = inflate(stream, Z_FINISH); | |
5180cacc LT |
1148 | } |
1149 | buf[size] = 0; | |
456cdf6e | 1150 | if (status == Z_STREAM_END && !stream->avail_in) { |
7efbff75 JH |
1151 | inflateEnd(stream); |
1152 | return buf; | |
1153 | } | |
1154 | ||
1155 | if (status < 0) | |
1156 | error("corrupt loose object '%s'", sha1_to_hex(sha1)); | |
1157 | else if (stream->avail_in) | |
1158 | error("garbage at end of loose object '%s'", | |
1159 | sha1_to_hex(sha1)); | |
1160 | free(buf); | |
1161 | return NULL; | |
5180cacc LT |
1162 | } |
1163 | ||
1164 | /* | |
1165 | * We used to just use "sscanf()", but that's actually way | |
1166 | * too permissive for what we want to check. So do an anal | |
1167 | * object header parse by hand. | |
1168 | */ | |
21666f1a | 1169 | static int parse_sha1_header(const char *hdr, unsigned long *sizep) |
5180cacc | 1170 | { |
21666f1a | 1171 | char type[10]; |
5180cacc LT |
1172 | int i; |
1173 | unsigned long size; | |
1174 | ||
1175 | /* | |
a6080a0a | 1176 | * The type can be at most ten bytes (including the |
5180cacc | 1177 | * terminating '\0' that we add), and is followed by |
21666f1a | 1178 | * a space. |
5180cacc | 1179 | */ |
21666f1a | 1180 | i = 0; |
5180cacc LT |
1181 | for (;;) { |
1182 | char c = *hdr++; | |
1183 | if (c == ' ') | |
1184 | break; | |
21666f1a NP |
1185 | type[i++] = c; |
1186 | if (i >= sizeof(type)) | |
5180cacc | 1187 | return -1; |
5180cacc | 1188 | } |
21666f1a | 1189 | type[i] = 0; |
5180cacc LT |
1190 | |
1191 | /* | |
1192 | * The length must follow immediately, and be in canonical | |
1193 | * decimal format (ie "010" is not valid). | |
1194 | */ | |
1195 | size = *hdr++ - '0'; | |
1196 | if (size > 9) | |
1197 | return -1; | |
1198 | if (size) { | |
1199 | for (;;) { | |
1200 | unsigned long c = *hdr - '0'; | |
1201 | if (c > 9) | |
1202 | break; | |
1203 | hdr++; | |
1204 | size = size * 10 + c; | |
1205 | } | |
1206 | } | |
1207 | *sizep = size; | |
1208 | ||
1209 | /* | |
1210 | * The length must be followed by a zero byte | |
1211 | */ | |
21666f1a | 1212 | return *hdr ? -1 : type_from_string(type); |
5180cacc LT |
1213 | } |
1214 | ||
7efbff75 | 1215 | static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1) |
0fcfd160 | 1216 | { |
5180cacc | 1217 | int ret; |
0fcfd160 | 1218 | z_stream stream; |
5180cacc | 1219 | char hdr[8192]; |
0fcfd160 | 1220 | |
5180cacc | 1221 | ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)); |
21666f1a | 1222 | if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0) |
0fcfd160 LT |
1223 | return NULL; |
1224 | ||
7efbff75 | 1225 | return unpack_sha1_rest(&stream, hdr, *size, sha1); |
0fcfd160 LT |
1226 | } |
1227 | ||
54dab52a NP |
1228 | unsigned long get_size_from_delta(struct packed_git *p, |
1229 | struct pack_window **w_curs, | |
1230 | off_t curpos) | |
1231 | { | |
1232 | const unsigned char *data; | |
1233 | unsigned char delta_head[20], *in; | |
1234 | z_stream stream; | |
1235 | int st; | |
1236 | ||
1237 | memset(&stream, 0, sizeof(stream)); | |
1238 | stream.next_out = delta_head; | |
1239 | stream.avail_out = sizeof(delta_head); | |
1240 | ||
1241 | inflateInit(&stream); | |
1242 | do { | |
1243 | in = use_pack(p, w_curs, curpos, &stream.avail_in); | |
1244 | stream.next_in = in; | |
1245 | st = inflate(&stream, Z_FINISH); | |
1246 | curpos += stream.next_in - in; | |
1247 | } while ((st == Z_OK || st == Z_BUF_ERROR) && | |
1248 | stream.total_out < sizeof(delta_head)); | |
1249 | inflateEnd(&stream); | |
1250 | if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) | |
1251 | die("delta data unpack-initial failed"); | |
1252 | ||
1253 | /* Examine the initial part of the delta to figure out | |
1254 | * the result size. | |
1255 | */ | |
1256 | data = delta_head; | |
1257 | ||
1258 | /* ignore base size */ | |
1259 | get_delta_hdr_size(&data, delta_head+sizeof(delta_head)); | |
1260 | ||
1261 | /* Read the result size */ | |
1262 | return get_delta_hdr_size(&data, delta_head+sizeof(delta_head)); | |
1263 | } | |
1264 | ||
c4001d92 | 1265 | static off_t get_delta_base(struct packed_git *p, |
03e79c88 | 1266 | struct pack_window **w_curs, |
c4001d92 | 1267 | off_t *curpos, |
21666f1a | 1268 | enum object_type type, |
c4001d92 | 1269 | off_t delta_obj_offset) |
eb32d236 | 1270 | { |
2b87c45b | 1271 | unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL); |
c4001d92 | 1272 | off_t base_offset; |
eb32d236 | 1273 | |
8d8a4ea5 SP |
1274 | /* use_pack() assured us we have [base_info, base_info + 20) |
1275 | * as a range that we can look at without walking off the | |
1276 | * end of the mapped window. Its actually the hash size | |
1277 | * that is assured. An OFS_DELTA longer than the hash size | |
1278 | * is stupid, as then a REF_DELTA would be smaller to store. | |
1279 | */ | |
21666f1a | 1280 | if (type == OBJ_OFS_DELTA) { |
eb32d236 NP |
1281 | unsigned used = 0; |
1282 | unsigned char c = base_info[used++]; | |
1283 | base_offset = c & 127; | |
1284 | while (c & 128) { | |
1285 | base_offset += 1; | |
8723f216 | 1286 | if (!base_offset || MSB(base_offset, 7)) |
eb32d236 NP |
1287 | die("offset value overflow for delta base object"); |
1288 | c = base_info[used++]; | |
1289 | base_offset = (base_offset << 7) + (c & 127); | |
1290 | } | |
1291 | base_offset = delta_obj_offset - base_offset; | |
1292 | if (base_offset >= delta_obj_offset) | |
1293 | die("delta base offset out of bound"); | |
2b87c45b | 1294 | *curpos += used; |
21666f1a | 1295 | } else if (type == OBJ_REF_DELTA) { |
eb32d236 NP |
1296 | /* The base entry _must_ be in the same pack */ |
1297 | base_offset = find_pack_entry_one(base_info, p); | |
1298 | if (!base_offset) | |
1299 | die("failed to find delta-pack base object %s", | |
1300 | sha1_to_hex(base_info)); | |
2b87c45b | 1301 | *curpos += 20; |
eb32d236 NP |
1302 | } else |
1303 | die("I am totally screwed"); | |
2b87c45b | 1304 | return base_offset; |
eb32d236 NP |
1305 | } |
1306 | ||
f3bf9224 | 1307 | /* forward declaration for a mutually recursive function */ |
c4001d92 | 1308 | static int packed_object_info(struct packed_git *p, off_t offset, |
21666f1a | 1309 | unsigned long *sizep); |
f3bf9224 | 1310 | |
43057304 | 1311 | static int packed_delta_info(struct packed_git *p, |
03e79c88 | 1312 | struct pack_window **w_curs, |
c4001d92 | 1313 | off_t curpos, |
21666f1a | 1314 | enum object_type type, |
c4001d92 | 1315 | off_t obj_offset, |
43057304 | 1316 | unsigned long *sizep) |
5db47c2b | 1317 | { |
c4001d92 | 1318 | off_t base_offset; |
f3bf9224 | 1319 | |
21666f1a NP |
1320 | base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset); |
1321 | type = packed_object_info(p, base_offset, NULL); | |
f3bf9224 | 1322 | |
c62266f3 JH |
1323 | /* We choose to only get the type of the base object and |
1324 | * ignore potentially corrupt pack file that expects the delta | |
1325 | * based on a base with a wrong size. This saves tons of | |
1326 | * inflate() calls. | |
1327 | */ | |
54dab52a NP |
1328 | if (sizep) |
1329 | *sizep = get_size_from_delta(p, w_curs, curpos); | |
21666f1a NP |
1330 | |
1331 | return type; | |
5db47c2b JH |
1332 | } |
1333 | ||
2b87c45b NP |
1334 | static int unpack_object_header(struct packed_git *p, |
1335 | struct pack_window **w_curs, | |
c4001d92 | 1336 | off_t *curpos, |
2b87c45b | 1337 | unsigned long *sizep) |
a733cb60 | 1338 | { |
03e79c88 SP |
1339 | unsigned char *base; |
1340 | unsigned int left; | |
ad1ed5ee | 1341 | unsigned long used; |
2b87c45b | 1342 | enum object_type type; |
a733cb60 | 1343 | |
8d8a4ea5 SP |
1344 | /* use_pack() assures us we have [base, base + 20) available |
1345 | * as a range that we can look at at. (Its actually the hash | |
3dff5379 | 1346 | * size that is assured.) With our object header encoding |
8d8a4ea5 SP |
1347 | * the maximum deflated object size is 2^137, which is just |
1348 | * insane, so we know won't exceed what we have been given. | |
1349 | */ | |
2b87c45b NP |
1350 | base = use_pack(p, w_curs, *curpos, &left); |
1351 | used = unpack_object_header_gently(base, left, &type, sizep); | |
ad1ed5ee JH |
1352 | if (!used) |
1353 | die("object offset outside of pack file"); | |
2b87c45b | 1354 | *curpos += used; |
ad1ed5ee | 1355 | |
2b87c45b | 1356 | return type; |
a733cb60 LT |
1357 | } |
1358 | ||
21666f1a | 1359 | const char *packed_object_info_detail(struct packed_git *p, |
c4001d92 | 1360 | off_t obj_offset, |
21666f1a NP |
1361 | unsigned long *size, |
1362 | unsigned long *store_size, | |
1363 | unsigned int *delta_chain_length, | |
1364 | unsigned char *base_sha1) | |
ad8c80a5 | 1365 | { |
03e79c88 | 1366 | struct pack_window *w_curs = NULL; |
c4001d92 SP |
1367 | off_t curpos; |
1368 | unsigned long dummy; | |
43057304 | 1369 | unsigned char *next_sha1; |
21666f1a | 1370 | enum object_type type; |
70f5d5d3 | 1371 | struct revindex_entry *revidx; |
ad8c80a5 | 1372 | |
43057304 | 1373 | *delta_chain_length = 0; |
2b87c45b | 1374 | curpos = obj_offset; |
21666f1a | 1375 | type = unpack_object_header(p, &w_curs, &curpos, size); |
43057304 | 1376 | |
70f5d5d3 NP |
1377 | revidx = find_pack_revindex(p, obj_offset); |
1378 | *store_size = revidx[1].offset - obj_offset; | |
1379 | ||
43057304 | 1380 | for (;;) { |
21666f1a | 1381 | switch (type) { |
43057304 | 1382 | default: |
08a19d87 | 1383 | die("pack %s contains unknown object type %d", |
21666f1a | 1384 | p->pack_name, type); |
43057304 NP |
1385 | case OBJ_COMMIT: |
1386 | case OBJ_TREE: | |
1387 | case OBJ_BLOB: | |
1388 | case OBJ_TAG: | |
03e79c88 | 1389 | unuse_pack(&w_curs); |
21666f1a | 1390 | return typename(type); |
eb32d236 | 1391 | case OBJ_OFS_DELTA: |
21666f1a | 1392 | obj_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset); |
eb32d236 | 1393 | if (*delta_chain_length == 0) { |
70f5d5d3 NP |
1394 | revidx = find_pack_revindex(p, obj_offset); |
1395 | hashcpy(base_sha1, nth_packed_object_sha1(p, revidx->nr)); | |
eb32d236 NP |
1396 | } |
1397 | break; | |
1398 | case OBJ_REF_DELTA: | |
2b87c45b | 1399 | next_sha1 = use_pack(p, &w_curs, curpos, NULL); |
43057304 NP |
1400 | if (*delta_chain_length == 0) |
1401 | hashcpy(base_sha1, next_sha1); | |
2b87c45b | 1402 | obj_offset = find_pack_entry_one(next_sha1, p); |
43057304 NP |
1403 | break; |
1404 | } | |
43057304 | 1405 | (*delta_chain_length)++; |
2b87c45b | 1406 | curpos = obj_offset; |
21666f1a | 1407 | type = unpack_object_header(p, &w_curs, &curpos, &dummy); |
ad8c80a5 | 1408 | } |
ad8c80a5 JH |
1409 | } |
1410 | ||
c4001d92 | 1411 | static int packed_object_info(struct packed_git *p, off_t obj_offset, |
21666f1a | 1412 | unsigned long *sizep) |
1f688557 | 1413 | { |
03e79c88 | 1414 | struct pack_window *w_curs = NULL; |
c4001d92 SP |
1415 | unsigned long size; |
1416 | off_t curpos = obj_offset; | |
21666f1a | 1417 | enum object_type type; |
5db47c2b | 1418 | |
21666f1a | 1419 | type = unpack_object_header(p, &w_curs, &curpos, &size); |
5db47c2b | 1420 | |
21666f1a | 1421 | switch (type) { |
eb32d236 NP |
1422 | case OBJ_OFS_DELTA: |
1423 | case OBJ_REF_DELTA: | |
21666f1a NP |
1424 | type = packed_delta_info(p, &w_curs, curpos, |
1425 | type, obj_offset, sizep); | |
2b87c45b | 1426 | break; |
a733cb60 | 1427 | case OBJ_COMMIT: |
a733cb60 | 1428 | case OBJ_TREE: |
a733cb60 | 1429 | case OBJ_BLOB: |
a733cb60 | 1430 | case OBJ_TAG: |
2b87c45b NP |
1431 | if (sizep) |
1432 | *sizep = size; | |
a69d0943 | 1433 | break; |
1f688557 | 1434 | default: |
08a19d87 | 1435 | die("pack %s contains unknown object type %d", |
21666f1a | 1436 | p->pack_name, type); |
1f688557 | 1437 | } |
2b87c45b | 1438 | unuse_pack(&w_curs); |
21666f1a | 1439 | return type; |
1f688557 JH |
1440 | } |
1441 | ||
eb950c19 | 1442 | static void *unpack_compressed_entry(struct packed_git *p, |
03e79c88 | 1443 | struct pack_window **w_curs, |
c4001d92 | 1444 | off_t curpos, |
eb950c19 | 1445 | unsigned long size) |
de530aaa SP |
1446 | { |
1447 | int st; | |
1448 | z_stream stream; | |
079afb18 | 1449 | unsigned char *buffer, *in; |
de530aaa SP |
1450 | |
1451 | buffer = xmalloc(size + 1); | |
1452 | buffer[size] = 0; | |
1453 | memset(&stream, 0, sizeof(stream)); | |
de530aaa SP |
1454 | stream.next_out = buffer; |
1455 | stream.avail_out = size; | |
1456 | ||
1457 | inflateInit(&stream); | |
079afb18 | 1458 | do { |
2b87c45b | 1459 | in = use_pack(p, w_curs, curpos, &stream.avail_in); |
079afb18 SP |
1460 | stream.next_in = in; |
1461 | st = inflate(&stream, Z_FINISH); | |
2b87c45b | 1462 | curpos += stream.next_in - in; |
079afb18 | 1463 | } while (st == Z_OK || st == Z_BUF_ERROR); |
de530aaa SP |
1464 | inflateEnd(&stream); |
1465 | if ((st != Z_STREAM_END) || stream.total_out != size) { | |
1466 | free(buffer); | |
1467 | return NULL; | |
1468 | } | |
1469 | ||
1470 | return buffer; | |
1471 | } | |
1472 | ||
e5e01619 LT |
1473 | #define MAX_DELTA_CACHE (256) |
1474 | ||
18bdec11 | 1475 | static size_t delta_base_cached; |
5e08ecbf NP |
1476 | |
1477 | static struct delta_base_cache_lru_list { | |
1478 | struct delta_base_cache_lru_list *prev; | |
1479 | struct delta_base_cache_lru_list *next; | |
1480 | } delta_base_cache_lru = { &delta_base_cache_lru, &delta_base_cache_lru }; | |
1481 | ||
e5e01619 | 1482 | static struct delta_base_cache_entry { |
5e08ecbf NP |
1483 | struct delta_base_cache_lru_list lru; |
1484 | void *data; | |
e5e01619 LT |
1485 | struct packed_git *p; |
1486 | off_t base_offset; | |
1487 | unsigned long size; | |
e5e01619 LT |
1488 | enum object_type type; |
1489 | } delta_base_cache[MAX_DELTA_CACHE]; | |
1490 | ||
1491 | static unsigned long pack_entry_hash(struct packed_git *p, off_t base_offset) | |
1492 | { | |
1493 | unsigned long hash; | |
1494 | ||
1495 | hash = (unsigned long)p + (unsigned long)base_offset; | |
1496 | hash += (hash >> 8) + (hash >> 16); | |
3358004a | 1497 | return hash % MAX_DELTA_CACHE; |
e5e01619 LT |
1498 | } |
1499 | ||
62f255ad | 1500 | static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset, |
a0cba108 | 1501 | unsigned long *base_size, enum object_type *type, int keep_cache) |
62f255ad | 1502 | { |
e5e01619 LT |
1503 | void *ret; |
1504 | unsigned long hash = pack_entry_hash(p, base_offset); | |
1505 | struct delta_base_cache_entry *ent = delta_base_cache + hash; | |
1506 | ||
1507 | ret = ent->data; | |
1508 | if (ret && ent->p == p && ent->base_offset == base_offset) | |
1509 | goto found_cache_entry; | |
62f255ad | 1510 | return unpack_entry(p, base_offset, type, base_size); |
e5e01619 LT |
1511 | |
1512 | found_cache_entry: | |
18bdec11 | 1513 | if (!keep_cache) { |
a0cba108 | 1514 | ent->data = NULL; |
5e08ecbf NP |
1515 | ent->lru.next->prev = ent->lru.prev; |
1516 | ent->lru.prev->next = ent->lru.next; | |
18bdec11 | 1517 | delta_base_cached -= ent->size; |
182af834 PH |
1518 | } else { |
1519 | ret = xmemdupz(ent->data, ent->size); | |
a0cba108 | 1520 | } |
e5e01619 LT |
1521 | *type = ent->type; |
1522 | *base_size = ent->size; | |
1523 | return ret; | |
62f255ad LT |
1524 | } |
1525 | ||
18bdec11 SP |
1526 | static inline void release_delta_base_cache(struct delta_base_cache_entry *ent) |
1527 | { | |
1528 | if (ent->data) { | |
1529 | free(ent->data); | |
1530 | ent->data = NULL; | |
5e08ecbf NP |
1531 | ent->lru.next->prev = ent->lru.prev; |
1532 | ent->lru.prev->next = ent->lru.next; | |
18bdec11 SP |
1533 | delta_base_cached -= ent->size; |
1534 | } | |
1535 | } | |
1536 | ||
62f255ad LT |
1537 | static void add_delta_base_cache(struct packed_git *p, off_t base_offset, |
1538 | void *base, unsigned long base_size, enum object_type type) | |
1539 | { | |
5e08ecbf | 1540 | unsigned long hash = pack_entry_hash(p, base_offset); |
e5e01619 | 1541 | struct delta_base_cache_entry *ent = delta_base_cache + hash; |
5e08ecbf | 1542 | struct delta_base_cache_lru_list *lru; |
e5e01619 | 1543 | |
18bdec11 SP |
1544 | release_delta_base_cache(ent); |
1545 | delta_base_cached += base_size; | |
5e08ecbf NP |
1546 | |
1547 | for (lru = delta_base_cache_lru.next; | |
1548 | delta_base_cached > delta_base_cache_limit | |
1549 | && lru != &delta_base_cache_lru; | |
1550 | lru = lru->next) { | |
1551 | struct delta_base_cache_entry *f = (void *)lru; | |
18bdec11 SP |
1552 | if (f->type == OBJ_BLOB) |
1553 | release_delta_base_cache(f); | |
1554 | } | |
5e08ecbf NP |
1555 | for (lru = delta_base_cache_lru.next; |
1556 | delta_base_cached > delta_base_cache_limit | |
1557 | && lru != &delta_base_cache_lru; | |
1558 | lru = lru->next) { | |
1559 | struct delta_base_cache_entry *f = (void *)lru; | |
1560 | release_delta_base_cache(f); | |
1561 | } | |
18bdec11 | 1562 | |
e5e01619 LT |
1563 | ent->p = p; |
1564 | ent->base_offset = base_offset; | |
1565 | ent->type = type; | |
1566 | ent->data = base; | |
1567 | ent->size = base_size; | |
5e08ecbf NP |
1568 | ent->lru.next = &delta_base_cache_lru; |
1569 | ent->lru.prev = delta_base_cache_lru.prev; | |
1570 | delta_base_cache_lru.prev->next = &ent->lru; | |
1571 | delta_base_cache_lru.prev = &ent->lru; | |
62f255ad LT |
1572 | } |
1573 | ||
eb950c19 | 1574 | static void *unpack_delta_entry(struct packed_git *p, |
03e79c88 | 1575 | struct pack_window **w_curs, |
c4001d92 | 1576 | off_t curpos, |
1f688557 | 1577 | unsigned long delta_size, |
c4001d92 | 1578 | off_t obj_offset, |
21666f1a | 1579 | enum object_type *type, |
eb950c19 | 1580 | unsigned long *sizep) |
1f688557 | 1581 | { |
7c3e8be3 | 1582 | void *delta_data, *result, *base; |
c4001d92 SP |
1583 | unsigned long base_size; |
1584 | off_t base_offset; | |
43057304 | 1585 | |
21666f1a | 1586 | base_offset = get_delta_base(p, w_curs, &curpos, *type, obj_offset); |
a0cba108 | 1587 | base = cache_or_unpack_entry(p, base_offset, &base_size, type, 0); |
67686d95 | 1588 | if (!base) |
c4001d92 SP |
1589 | die("failed to read delta base object" |
1590 | " at %"PRIuMAX" from %s", | |
1591 | (uintmax_t)base_offset, p->pack_name); | |
67686d95 | 1592 | |
2b87c45b | 1593 | delta_data = unpack_compressed_entry(p, w_curs, curpos, delta_size); |
9064d87b SP |
1594 | if (!delta_data) |
1595 | die("failed to unpack compressed delta" | |
1596 | " at %"PRIuMAX" from %s", | |
1597 | (uintmax_t)curpos, p->pack_name); | |
1f688557 JH |
1598 | result = patch_delta(base, base_size, |
1599 | delta_data, delta_size, | |
21666f1a | 1600 | sizep); |
1f688557 JH |
1601 | if (!result) |
1602 | die("failed to apply delta"); | |
1603 | free(delta_data); | |
62f255ad | 1604 | add_delta_base_cache(p, base_offset, base, base_size, *type); |
1f688557 JH |
1605 | return result; |
1606 | } | |
1607 | ||
c4001d92 | 1608 | void *unpack_entry(struct packed_git *p, off_t obj_offset, |
21666f1a | 1609 | enum object_type *type, unsigned long *sizep) |
1f688557 | 1610 | { |
03e79c88 | 1611 | struct pack_window *w_curs = NULL; |
c4001d92 | 1612 | off_t curpos = obj_offset; |
21666f1a | 1613 | void *data; |
1f688557 | 1614 | |
21666f1a NP |
1615 | *type = unpack_object_header(p, &w_curs, &curpos, sizep); |
1616 | switch (*type) { | |
eb32d236 NP |
1617 | case OBJ_OFS_DELTA: |
1618 | case OBJ_REF_DELTA: | |
21666f1a NP |
1619 | data = unpack_delta_entry(p, &w_curs, curpos, *sizep, |
1620 | obj_offset, type, sizep); | |
4d703a1a | 1621 | break; |
a733cb60 | 1622 | case OBJ_COMMIT: |
a733cb60 | 1623 | case OBJ_TREE: |
a733cb60 | 1624 | case OBJ_BLOB: |
a733cb60 | 1625 | case OBJ_TAG: |
21666f1a | 1626 | data = unpack_compressed_entry(p, &w_curs, curpos, *sizep); |
4d703a1a | 1627 | break; |
1f688557 | 1628 | default: |
21666f1a | 1629 | die("unknown object type %i in %s", *type, p->pack_name); |
1f688557 | 1630 | } |
03e79c88 | 1631 | unuse_pack(&w_curs); |
21666f1a | 1632 | return data; |
1f688557 JH |
1633 | } |
1634 | ||
d079837e | 1635 | const unsigned char *nth_packed_object_sha1(struct packed_git *p, |
d72308e0 | 1636 | uint32_t n) |
9a217f2a | 1637 | { |
42873078 | 1638 | const unsigned char *index = p->index_data; |
d079837e SP |
1639 | if (!index) { |
1640 | if (open_pack_index(p)) | |
1641 | return NULL; | |
1642 | index = p->index_data; | |
1643 | } | |
57059091 | 1644 | if (n >= p->num_objects) |
d72308e0 | 1645 | return NULL; |
74e34e1f NP |
1646 | index += 4 * 256; |
1647 | if (p->index_version == 1) { | |
1648 | return index + 24 * n + 4; | |
1649 | } else { | |
1650 | index += 8; | |
1651 | return index + 20 * n; | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | static off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n) | |
1656 | { | |
1657 | const unsigned char *index = p->index_data; | |
1658 | index += 4 * 256; | |
1659 | if (p->index_version == 1) { | |
1660 | return ntohl(*((uint32_t *)(index + 24 * n))); | |
1661 | } else { | |
1662 | uint32_t off; | |
1663 | index += 8 + p->num_objects * (20 + 4); | |
1664 | off = ntohl(*((uint32_t *)(index + 4 * n))); | |
1665 | if (!(off & 0x80000000)) | |
1666 | return off; | |
1667 | index += p->num_objects * 4 + (off & 0x7fffffff) * 8; | |
1668 | return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) | | |
1669 | ntohl(*((uint32_t *)(index + 4))); | |
1670 | } | |
9a217f2a JH |
1671 | } |
1672 | ||
c4001d92 | 1673 | off_t find_pack_entry_one(const unsigned char *sha1, |
43057304 | 1674 | struct packed_git *p) |
1f688557 | 1675 | { |
42873078 | 1676 | const uint32_t *level1_ofs = p->index_data; |
42873078 | 1677 | const unsigned char *index = p->index_data; |
74e34e1f | 1678 | unsigned hi, lo; |
42873078 | 1679 | |
d079837e SP |
1680 | if (!index) { |
1681 | if (open_pack_index(p)) | |
1682 | return 0; | |
1683 | level1_ofs = p->index_data; | |
1684 | index = p->index_data; | |
1685 | } | |
74e34e1f NP |
1686 | if (p->index_version > 1) { |
1687 | level1_ofs += 2; | |
1688 | index += 8; | |
1689 | } | |
42873078 | 1690 | index += 4 * 256; |
74e34e1f NP |
1691 | hi = ntohl(level1_ofs[*sha1]); |
1692 | lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1])); | |
1f688557 JH |
1693 | |
1694 | do { | |
74e34e1f NP |
1695 | unsigned mi = (lo + hi) / 2; |
1696 | unsigned x = (p->index_version > 1) ? (mi * 20) : (mi * 24 + 4); | |
1697 | int cmp = hashcmp(index + x, sha1); | |
43057304 | 1698 | if (!cmp) |
74e34e1f | 1699 | return nth_packed_object_offset(p, mi); |
1f688557 JH |
1700 | if (cmp > 0) |
1701 | hi = mi; | |
1702 | else | |
1703 | lo = mi+1; | |
1704 | } while (lo < hi); | |
1705 | return 0; | |
1706 | } | |
1707 | ||
000dfd3f | 1708 | int matches_pack_name(struct packed_git *p, const char *name) |
d4ff6d92 SP |
1709 | { |
1710 | const char *last_c, *c; | |
1711 | ||
000dfd3f JH |
1712 | if (!strcmp(p->pack_name, name)) |
1713 | return 1; | |
d4ff6d92 SP |
1714 | |
1715 | for (c = p->pack_name, last_c = c; *c;) | |
1716 | if (*c == '/') | |
1717 | last_c = ++c; | |
1718 | else | |
1719 | ++c; | |
000dfd3f JH |
1720 | if (!strcmp(last_c, name)) |
1721 | return 1; | |
d4ff6d92 | 1722 | |
000dfd3f | 1723 | return 0; |
d4ff6d92 SP |
1724 | } |
1725 | ||
106d710b | 1726 | static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, const char **ignore_packed) |
1f688557 | 1727 | { |
f7c22cc6 | 1728 | static struct packed_git *last_found = (void *)1; |
1f688557 | 1729 | struct packed_git *p; |
c4001d92 | 1730 | off_t offset; |
43057304 | 1731 | |
1f688557 | 1732 | prepare_packed_git(); |
f7c22cc6 NP |
1733 | if (!packed_git) |
1734 | return 0; | |
1735 | p = (last_found == (void *)1) ? packed_git : last_found; | |
1f688557 | 1736 | |
f7c22cc6 | 1737 | do { |
106d710b JH |
1738 | if (ignore_packed) { |
1739 | const char **ig; | |
1740 | for (ig = ignore_packed; *ig; ig++) | |
000dfd3f | 1741 | if (matches_pack_name(p, *ig)) |
106d710b JH |
1742 | break; |
1743 | if (*ig) | |
f7c22cc6 | 1744 | goto next; |
106d710b | 1745 | } |
f7c22cc6 | 1746 | |
43057304 NP |
1747 | offset = find_pack_entry_one(sha1, p); |
1748 | if (offset) { | |
c715f783 SP |
1749 | /* |
1750 | * We are about to tell the caller where they can | |
1751 | * locate the requested object. We better make | |
1752 | * sure the packfile is still here and can be | |
1753 | * accessed before supplying that answer, as | |
1754 | * it may have been deleted since the index | |
1755 | * was loaded! | |
1756 | */ | |
1757 | if (p->pack_fd == -1 && open_packed_git(p)) { | |
1758 | error("packfile %s cannot be accessed", p->pack_name); | |
f7c22cc6 | 1759 | goto next; |
c715f783 | 1760 | } |
43057304 NP |
1761 | e->offset = offset; |
1762 | e->p = p; | |
1763 | hashcpy(e->sha1, sha1); | |
f7c22cc6 | 1764 | last_found = p; |
1f688557 | 1765 | return 1; |
43057304 | 1766 | } |
f7c22cc6 NP |
1767 | |
1768 | next: | |
1769 | if (p == last_found) | |
1770 | p = packed_git; | |
1771 | else | |
1772 | p = p->next; | |
1773 | if (p == last_found) | |
1774 | p = p->next; | |
1775 | } while (p); | |
1f688557 JH |
1776 | return 0; |
1777 | } | |
1778 | ||
a6080a0a | 1779 | struct packed_git *find_sha1_pack(const unsigned char *sha1, |
bf592c50 DB |
1780 | struct packed_git *packs) |
1781 | { | |
1782 | struct packed_git *p; | |
bf592c50 DB |
1783 | |
1784 | for (p = packs; p; p = p->next) { | |
43057304 | 1785 | if (find_pack_entry_one(sha1, p)) |
bf592c50 DB |
1786 | return p; |
1787 | } | |
1788 | return NULL; | |
21666f1a | 1789 | |
bf592c50 DB |
1790 | } |
1791 | ||
21666f1a | 1792 | static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *sizep) |
65c2e0c3 | 1793 | { |
36e4d74a | 1794 | int status; |
65c2e0c3 JH |
1795 | unsigned long mapsize, size; |
1796 | void *map; | |
1797 | z_stream stream; | |
d65a16f6 | 1798 | char hdr[32]; |
65c2e0c3 | 1799 | |
bb6b8e4f | 1800 | map = map_sha1_file(sha1, &mapsize); |
f0df4ed5 JS |
1801 | if (!map) |
1802 | return error("unable to find %s", sha1_to_hex(sha1)); | |
36e4d74a JH |
1803 | if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) |
1804 | status = error("unable to unpack %s header", | |
1805 | sha1_to_hex(sha1)); | |
21666f1a | 1806 | else if ((status = parse_sha1_header(hdr, &size)) < 0) |
36e4d74a | 1807 | status = error("unable to parse %s header", sha1_to_hex(sha1)); |
21666f1a NP |
1808 | else if (sizep) |
1809 | *sizep = size; | |
65c2e0c3 JH |
1810 | inflateEnd(&stream); |
1811 | munmap(map, mapsize); | |
1812 | return status; | |
1813 | } | |
1814 | ||
21666f1a | 1815 | int sha1_object_info(const unsigned char *sha1, unsigned long *sizep) |
f0df4ed5 | 1816 | { |
f0df4ed5 JS |
1817 | struct pack_entry e; |
1818 | ||
1819 | if (!find_pack_entry(sha1, &e, NULL)) { | |
1820 | reprepare_packed_git(); | |
1821 | if (!find_pack_entry(sha1, &e, NULL)) | |
21666f1a | 1822 | return sha1_loose_object_info(sha1, sizep); |
f0df4ed5 | 1823 | } |
21666f1a | 1824 | return packed_object_info(e.p, e.offset, sizep); |
f0df4ed5 JS |
1825 | } |
1826 | ||
21666f1a NP |
1827 | static void *read_packed_sha1(const unsigned char *sha1, |
1828 | enum object_type *type, unsigned long *size) | |
1f688557 JH |
1829 | { |
1830 | struct pack_entry e; | |
1831 | ||
8276c007 | 1832 | if (!find_pack_entry(sha1, &e, NULL)) |
1f688557 | 1833 | return NULL; |
8276c007 | 1834 | else |
a0cba108 | 1835 | return cache_or_unpack_entry(e.p, e.offset, size, type, 1); |
1f688557 JH |
1836 | } |
1837 | ||
d66b37bb JH |
1838 | /* |
1839 | * This is meant to hold a *small* number of objects that you would | |
1840 | * want read_sha1_file() to be able to return, but yet you do not want | |
1841 | * to write them into the object store (e.g. a browse-only | |
1842 | * application). | |
1843 | */ | |
1844 | static struct cached_object { | |
1845 | unsigned char sha1[20]; | |
21666f1a | 1846 | enum object_type type; |
d66b37bb JH |
1847 | void *buf; |
1848 | unsigned long size; | |
1849 | } *cached_objects; | |
1850 | static int cached_object_nr, cached_object_alloc; | |
1851 | ||
346245a1 JK |
1852 | static struct cached_object empty_tree = { |
1853 | /* empty tree sha1: 4b825dc642cb6eb9a060e54bf8d69288fbee4904 */ | |
1854 | "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" | |
1855 | "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04", | |
1856 | OBJ_TREE, | |
1857 | "", | |
1858 | 0 | |
1859 | }; | |
1860 | ||
d66b37bb JH |
1861 | static struct cached_object *find_cached_object(const unsigned char *sha1) |
1862 | { | |
1863 | int i; | |
1864 | struct cached_object *co = cached_objects; | |
1865 | ||
1866 | for (i = 0; i < cached_object_nr; i++, co++) { | |
1867 | if (!hashcmp(co->sha1, sha1)) | |
1868 | return co; | |
1869 | } | |
346245a1 JK |
1870 | if (!hashcmp(sha1, empty_tree.sha1)) |
1871 | return &empty_tree; | |
d66b37bb JH |
1872 | return NULL; |
1873 | } | |
1874 | ||
21666f1a NP |
1875 | int pretend_sha1_file(void *buf, unsigned long len, enum object_type type, |
1876 | unsigned char *sha1) | |
d66b37bb JH |
1877 | { |
1878 | struct cached_object *co; | |
1879 | ||
21666f1a | 1880 | hash_sha1_file(buf, len, typename(type), sha1); |
d66b37bb JH |
1881 | if (has_sha1_file(sha1) || find_cached_object(sha1)) |
1882 | return 0; | |
1883 | if (cached_object_alloc <= cached_object_nr) { | |
1884 | cached_object_alloc = alloc_nr(cached_object_alloc); | |
1885 | cached_objects = xrealloc(cached_objects, | |
1886 | sizeof(*cached_objects) * | |
1887 | cached_object_alloc); | |
1888 | } | |
1889 | co = &cached_objects[cached_object_nr++]; | |
1890 | co->size = len; | |
21666f1a | 1891 | co->type = type; |
efa13f7b JH |
1892 | co->buf = xmalloc(len); |
1893 | memcpy(co->buf, buf, len); | |
d66b37bb JH |
1894 | hashcpy(co->sha1, sha1); |
1895 | return 0; | |
1896 | } | |
1897 | ||
21666f1a NP |
1898 | void *read_sha1_file(const unsigned char *sha1, enum object_type *type, |
1899 | unsigned long *size) | |
0fcfd160 LT |
1900 | { |
1901 | unsigned long mapsize; | |
1902 | void *map, *buf; | |
d66b37bb JH |
1903 | struct cached_object *co; |
1904 | ||
1905 | co = find_cached_object(sha1); | |
1906 | if (co) { | |
21666f1a | 1907 | *type = co->type; |
d66b37bb | 1908 | *size = co->size; |
182af834 | 1909 | return xmemdupz(co->buf, co->size); |
d66b37bb | 1910 | } |
0fcfd160 | 1911 | |
8276c007 PE |
1912 | buf = read_packed_sha1(sha1, type, size); |
1913 | if (buf) | |
1914 | return buf; | |
bb6b8e4f | 1915 | map = map_sha1_file(sha1, &mapsize); |
0fcfd160 | 1916 | if (map) { |
7efbff75 | 1917 | buf = unpack_sha1_file(map, mapsize, type, size, sha1); |
0fcfd160 LT |
1918 | munmap(map, mapsize); |
1919 | return buf; | |
1920 | } | |
637cdd9d | 1921 | reprepare_packed_git(); |
8276c007 | 1922 | return read_packed_sha1(sha1, type, size); |
0fcfd160 LT |
1923 | } |
1924 | ||
40469ee9 | 1925 | void *read_object_with_reference(const unsigned char *sha1, |
21666f1a | 1926 | const char *required_type_name, |
40469ee9 JH |
1927 | unsigned long *size, |
1928 | unsigned char *actual_sha1_return) | |
f4913f91 | 1929 | { |
21666f1a | 1930 | enum object_type type, required_type; |
f4913f91 JH |
1931 | void *buffer; |
1932 | unsigned long isize; | |
40469ee9 | 1933 | unsigned char actual_sha1[20]; |
f4913f91 | 1934 | |
21666f1a | 1935 | required_type = type_from_string(required_type_name); |
e702496e | 1936 | hashcpy(actual_sha1, sha1); |
40469ee9 JH |
1937 | while (1) { |
1938 | int ref_length = -1; | |
1939 | const char *ref_type = NULL; | |
f4913f91 | 1940 | |
21666f1a | 1941 | buffer = read_sha1_file(actual_sha1, &type, &isize); |
40469ee9 JH |
1942 | if (!buffer) |
1943 | return NULL; | |
21666f1a | 1944 | if (type == required_type) { |
40469ee9 JH |
1945 | *size = isize; |
1946 | if (actual_sha1_return) | |
e702496e | 1947 | hashcpy(actual_sha1_return, actual_sha1); |
40469ee9 JH |
1948 | return buffer; |
1949 | } | |
1950 | /* Handle references */ | |
21666f1a | 1951 | else if (type == OBJ_COMMIT) |
40469ee9 | 1952 | ref_type = "tree "; |
21666f1a | 1953 | else if (type == OBJ_TAG) |
40469ee9 JH |
1954 | ref_type = "object "; |
1955 | else { | |
1956 | free(buffer); | |
1957 | return NULL; | |
1958 | } | |
1959 | ref_length = strlen(ref_type); | |
f4913f91 | 1960 | |
50974ec9 MK |
1961 | if (ref_length + 40 > isize || |
1962 | memcmp(buffer, ref_type, ref_length) || | |
1d7f171c | 1963 | get_sha1_hex((char *) buffer + ref_length, actual_sha1)) { |
40469ee9 JH |
1964 | free(buffer); |
1965 | return NULL; | |
1966 | } | |
1cf58e72 | 1967 | free(buffer); |
40469ee9 JH |
1968 | /* Now we have the ID of the referred-to object in |
1969 | * actual_sha1. Check again. */ | |
f4913f91 | 1970 | } |
f4913f91 JH |
1971 | } |
1972 | ||
ce9fbf16 | 1973 | static void write_sha1_file_prepare(const void *buf, unsigned long len, |
972a9155 | 1974 | const char *type, unsigned char *sha1, |
d65a16f6 | 1975 | char *hdr, int *hdrlen) |
d410c0f5 JH |
1976 | { |
1977 | SHA_CTX c; | |
1978 | ||
1979 | /* Generate the header */ | |
d65a16f6 | 1980 | *hdrlen = sprintf(hdr, "%s %lu", type, len)+1; |
d410c0f5 JH |
1981 | |
1982 | /* Sha1.. */ | |
1983 | SHA1_Init(&c); | |
1984 | SHA1_Update(&c, hdr, *hdrlen); | |
1985 | SHA1_Update(&c, buf, len); | |
1986 | SHA1_Final(sha1, &c); | |
d410c0f5 JH |
1987 | } |
1988 | ||
230f1322 LT |
1989 | /* |
1990 | * Link the tempfile to the final place, possibly creating the | |
1991 | * last directory level as you do so. | |
1992 | * | |
1993 | * Returns the errno on failure, 0 on success. | |
1994 | */ | |
839837b9 | 1995 | static int link_temp_to_file(const char *tmpfile, const char *filename) |
230f1322 LT |
1996 | { |
1997 | int ret; | |
756aaf4a | 1998 | char *dir; |
230f1322 LT |
1999 | |
2000 | if (!link(tmpfile, filename)) | |
2001 | return 0; | |
2002 | ||
2003 | /* | |
756aaf4a | 2004 | * Try to mkdir the last path component if that failed. |
230f1322 LT |
2005 | * |
2006 | * Re-try the "link()" regardless of whether the mkdir | |
2007 | * succeeds, since a race might mean that somebody | |
2008 | * else succeeded. | |
2009 | */ | |
2010 | ret = errno; | |
756aaf4a SP |
2011 | dir = strrchr(filename, '/'); |
2012 | if (dir) { | |
2013 | *dir = 0; | |
866cae0d | 2014 | if (!mkdir(filename, 0777) && adjust_shared_perm(filename)) { |
91c23e48 | 2015 | *dir = '/'; |
756aaf4a | 2016 | return -2; |
91c23e48 | 2017 | } |
756aaf4a SP |
2018 | *dir = '/'; |
2019 | if (!link(tmpfile, filename)) | |
2020 | return 0; | |
2021 | ret = errno; | |
230f1322 LT |
2022 | } |
2023 | return ret; | |
2024 | } | |
2025 | ||
2026 | /* | |
2027 | * Move the just written object into its final resting place | |
2028 | */ | |
839837b9 | 2029 | int move_temp_to_file(const char *tmpfile, const char *filename) |
230f1322 LT |
2030 | { |
2031 | int ret = link_temp_to_file(tmpfile, filename); | |
7ebb6fca LT |
2032 | |
2033 | /* | |
2034 | * Coda hack - coda doesn't like cross-directory links, | |
2035 | * so we fall back to a rename, which will mean that it | |
2036 | * won't be able to check collisions, but that's not a | |
2037 | * big deal. | |
2038 | * | |
2039 | * The same holds for FAT formatted media. | |
2040 | * | |
2041 | * When this succeeds, we just return 0. We have nothing | |
2042 | * left to unlink. | |
2043 | */ | |
2044 | if (ret && ret != EEXIST) { | |
2045 | if (!rename(tmpfile, filename)) | |
230f1322 | 2046 | return 0; |
9e48b389 | 2047 | ret = errno; |
230f1322 LT |
2048 | } |
2049 | unlink(tmpfile); | |
2050 | if (ret) { | |
2051 | if (ret != EEXIST) { | |
916d081b | 2052 | return error("unable to write sha1 filename %s: %s\n", filename, strerror(ret)); |
230f1322 LT |
2053 | } |
2054 | /* FIXME!!! Collision check here ? */ | |
2055 | } | |
2056 | ||
2057 | return 0; | |
2058 | } | |
2059 | ||
4d548150 LT |
2060 | static int write_buffer(int fd, const void *buf, size_t len) |
2061 | { | |
d34cf19b | 2062 | if (write_in_full(fd, buf, len) < 0) |
93822c22 | 2063 | return error("file write error (%s)", strerror(errno)); |
4d548150 LT |
2064 | return 0; |
2065 | } | |
2066 | ||
ce9fbf16 | 2067 | int hash_sha1_file(const void *buf, unsigned long len, const char *type, |
abdc3fc8 RS |
2068 | unsigned char *sha1) |
2069 | { | |
d65a16f6 | 2070 | char hdr[32]; |
abdc3fc8 RS |
2071 | int hdrlen; |
2072 | write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen); | |
2073 | return 0; | |
2074 | } | |
2075 | ||
bf0f910d | 2076 | int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1) |
0fcfd160 | 2077 | { |
ac54c277 | 2078 | int size, ret; |
bf0f910d | 2079 | unsigned char *compressed; |
0fcfd160 LT |
2080 | z_stream stream; |
2081 | unsigned char sha1[20]; | |
706bc531 | 2082 | char *filename; |
aac17941 | 2083 | static char tmpfile[PATH_MAX]; |
d65a16f6 | 2084 | char hdr[32]; |
230f1322 | 2085 | int fd, hdrlen; |
a44c9a5e | 2086 | |
d410c0f5 JH |
2087 | /* Normally if we have it in the pack then we do not bother writing |
2088 | * it out into .git/objects/??/?{38} file. | |
2089 | */ | |
972a9155 RS |
2090 | write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen); |
2091 | filename = sha1_file_name(sha1); | |
706bc531 | 2092 | if (returnsha1) |
e702496e | 2093 | hashcpy(returnsha1, sha1); |
d410c0f5 JH |
2094 | if (has_sha1_file(sha1)) |
2095 | return 0; | |
aac17941 LT |
2096 | fd = open(filename, O_RDONLY); |
2097 | if (fd >= 0) { | |
706bc531 | 2098 | /* |
aac17941 LT |
2099 | * FIXME!!! We might do collision checking here, but we'd |
2100 | * need to uncompress the old file and check it. Later. | |
706bc531 | 2101 | */ |
aac17941 | 2102 | close(fd); |
706bc531 LT |
2103 | return 0; |
2104 | } | |
2105 | ||
aac17941 | 2106 | if (errno != ENOENT) { |
916d081b | 2107 | return error("sha1 file %s: %s\n", filename, strerror(errno)); |
aac17941 LT |
2108 | } |
2109 | ||
0e55181f | 2110 | snprintf(tmpfile, sizeof(tmpfile), "%s/tmp_obj_XXXXXX", get_object_directory()); |
ace1534d | 2111 | |
aac17941 LT |
2112 | fd = mkstemp(tmpfile); |
2113 | if (fd < 0) { | |
916d081b PB |
2114 | if (errno == EPERM) |
2115 | return error("insufficient permission for adding an object to repository database %s\n", get_object_directory()); | |
2116 | else | |
2117 | return error("unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno)); | |
aac17941 LT |
2118 | } |
2119 | ||
0fcfd160 LT |
2120 | /* Set it up */ |
2121 | memset(&stream, 0, sizeof(stream)); | |
12f6c308 | 2122 | deflateInit(&stream, zlib_compression_level); |
93821bd9 | 2123 | size = 8 + deflateBound(&stream, len+hdrlen); |
812666c8 | 2124 | compressed = xmalloc(size); |
0fcfd160 LT |
2125 | |
2126 | /* Compress it */ | |
0fcfd160 LT |
2127 | stream.next_out = compressed; |
2128 | stream.avail_out = size; | |
a44c9a5e LT |
2129 | |
2130 | /* First header.. */ | |
d65a16f6 | 2131 | stream.next_in = (unsigned char *)hdr; |
a44c9a5e | 2132 | stream.avail_in = hdrlen; |
726f852b NP |
2133 | while (deflate(&stream, 0) == Z_OK) |
2134 | /* nothing */; | |
a44c9a5e LT |
2135 | |
2136 | /* Then the data itself.. */ | |
2137 | stream.next_in = buf; | |
2138 | stream.avail_in = len; | |
ac54c277 LT |
2139 | ret = deflate(&stream, Z_FINISH); |
2140 | if (ret != Z_STREAM_END) | |
2141 | die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret); | |
2142 | ||
2143 | ret = deflateEnd(&stream); | |
2144 | if (ret != Z_OK) | |
2145 | die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret); | |
2146 | ||
0fcfd160 LT |
2147 | size = stream.total_out; |
2148 | ||
4d548150 LT |
2149 | if (write_buffer(fd, compressed, size) < 0) |
2150 | die("unable to write sha1 file"); | |
aac17941 | 2151 | fchmod(fd, 0444); |
e82973cf JH |
2152 | if (close(fd)) |
2153 | die("unable to write sha1 file"); | |
383f85b7 | 2154 | free(compressed); |
0fcfd160 | 2155 | |
230f1322 | 2156 | return move_temp_to_file(tmpfile, filename); |
0fcfd160 | 2157 | } |
8237b185 | 2158 | |
4d548150 LT |
2159 | /* |
2160 | * We need to unpack and recompress the object for writing | |
2161 | * it out to a different file. | |
2162 | */ | |
2163 | static void *repack_object(const unsigned char *sha1, unsigned long *objsize) | |
a5eda52b | 2164 | { |
4d548150 | 2165 | size_t size; |
a5eda52b | 2166 | z_stream stream; |
4d548150 LT |
2167 | unsigned char *unpacked; |
2168 | unsigned long len; | |
21666f1a | 2169 | enum object_type type; |
d65a16f6 | 2170 | char hdr[32]; |
4d548150 LT |
2171 | int hdrlen; |
2172 | void *buf; | |
bfc66daf | 2173 | |
a9486b02 | 2174 | /* need to unpack and recompress it by itself */ |
21666f1a | 2175 | unpacked = read_packed_sha1(sha1, &type, &len); |
8276c007 PE |
2176 | if (!unpacked) |
2177 | error("cannot read sha1_file for %s", sha1_to_hex(sha1)); | |
a5eda52b | 2178 | |
21666f1a | 2179 | hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1; |
a5eda52b | 2180 | |
4d548150 LT |
2181 | /* Set it up */ |
2182 | memset(&stream, 0, sizeof(stream)); | |
12f6c308 | 2183 | deflateInit(&stream, zlib_compression_level); |
4d548150 LT |
2184 | size = deflateBound(&stream, len + hdrlen); |
2185 | buf = xmalloc(size); | |
a5eda52b | 2186 | |
4d548150 LT |
2187 | /* Compress it */ |
2188 | stream.next_out = buf; | |
2189 | stream.avail_out = size; | |
a5eda52b | 2190 | |
4d548150 LT |
2191 | /* First header.. */ |
2192 | stream.next_in = (void *)hdr; | |
2193 | stream.avail_in = hdrlen; | |
2194 | while (deflate(&stream, 0) == Z_OK) | |
2195 | /* nothing */; | |
bfc66daf | 2196 | |
4d548150 LT |
2197 | /* Then the data itself.. */ |
2198 | stream.next_in = unpacked; | |
2199 | stream.avail_in = len; | |
2200 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
2201 | /* nothing */; | |
2202 | deflateEnd(&stream); | |
2203 | free(unpacked); | |
bfc66daf | 2204 | |
4d548150 LT |
2205 | *objsize = stream.total_out; |
2206 | return buf; | |
2207 | } | |
2208 | ||
2209 | int write_sha1_to_fd(int fd, const unsigned char *sha1) | |
2210 | { | |
2211 | int retval; | |
2212 | unsigned long objsize; | |
bb6b8e4f | 2213 | void *buf = map_sha1_file(sha1, &objsize); |
4d548150 LT |
2214 | |
2215 | if (buf) { | |
2216 | retval = write_buffer(fd, buf, objsize); | |
2217 | munmap(buf, objsize); | |
2218 | return retval; | |
2219 | } | |
2220 | ||
2221 | buf = repack_object(sha1, &objsize); | |
2222 | retval = write_buffer(fd, buf, objsize); | |
2223 | free(buf); | |
2224 | return retval; | |
a5eda52b DB |
2225 | } |
2226 | ||
70b9829e DB |
2227 | int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer, |
2228 | size_t bufsize, size_t *bufposn) | |
8237b185 | 2229 | { |
230f1322 | 2230 | char tmpfile[PATH_MAX]; |
8237b185 DB |
2231 | int local; |
2232 | z_stream stream; | |
2233 | unsigned char real_sha1[20]; | |
bf0f910d | 2234 | unsigned char discard[4096]; |
8237b185 DB |
2235 | int ret; |
2236 | SHA_CTX c; | |
2237 | ||
0e55181f | 2238 | snprintf(tmpfile, sizeof(tmpfile), "%s/tmp_obj_XXXXXX", get_object_directory()); |
8237b185 | 2239 | |
230f1322 | 2240 | local = mkstemp(tmpfile); |
916d081b PB |
2241 | if (local < 0) { |
2242 | if (errno == EPERM) | |
2243 | return error("insufficient permission for adding an object to repository database %s\n", get_object_directory()); | |
2244 | else | |
2245 | return error("unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno)); | |
2246 | } | |
8237b185 DB |
2247 | |
2248 | memset(&stream, 0, sizeof(stream)); | |
2249 | ||
2250 | inflateInit(&stream); | |
2251 | ||
2252 | SHA1_Init(&c); | |
2253 | ||
2254 | do { | |
2255 | ssize_t size; | |
70b9829e DB |
2256 | if (*bufposn) { |
2257 | stream.avail_in = *bufposn; | |
96ad15ae | 2258 | stream.next_in = (unsigned char *) buffer; |
70b9829e DB |
2259 | do { |
2260 | stream.next_out = discard; | |
2261 | stream.avail_out = sizeof(discard); | |
2262 | ret = inflate(&stream, Z_SYNC_FLUSH); | |
2263 | SHA1_Update(&c, discard, sizeof(discard) - | |
2264 | stream.avail_out); | |
2265 | } while (stream.avail_in && ret == Z_OK); | |
4d548150 LT |
2266 | if (write_buffer(local, buffer, *bufposn - stream.avail_in) < 0) |
2267 | die("unable to write sha1 file"); | |
70b9829e DB |
2268 | memmove(buffer, buffer + *bufposn - stream.avail_in, |
2269 | stream.avail_in); | |
2270 | *bufposn = stream.avail_in; | |
2271 | if (ret != Z_OK) | |
2272 | break; | |
2273 | } | |
93d26e4c | 2274 | size = xread(fd, buffer + *bufposn, bufsize - *bufposn); |
8237b185 DB |
2275 | if (size <= 0) { |
2276 | close(local); | |
230f1322 | 2277 | unlink(tmpfile); |
8237b185 DB |
2278 | if (!size) |
2279 | return error("Connection closed?"); | |
2280 | perror("Reading from connection"); | |
2281 | return -1; | |
2282 | } | |
70b9829e DB |
2283 | *bufposn += size; |
2284 | } while (1); | |
8237b185 DB |
2285 | inflateEnd(&stream); |
2286 | ||
b5b8d814 | 2287 | fchmod(local, 0444); |
0d315468 JM |
2288 | if (close(local) != 0) |
2289 | die("unable to write sha1 file"); | |
8237b185 DB |
2290 | SHA1_Final(real_sha1, &c); |
2291 | if (ret != Z_STREAM_END) { | |
230f1322 | 2292 | unlink(tmpfile); |
8237b185 DB |
2293 | return error("File %s corrupted", sha1_to_hex(sha1)); |
2294 | } | |
a89fccd2 | 2295 | if (hashcmp(sha1, real_sha1)) { |
230f1322 | 2296 | unlink(tmpfile); |
bd2afde8 | 2297 | return error("File %s has bad hash", sha1_to_hex(sha1)); |
8237b185 | 2298 | } |
230f1322 LT |
2299 | |
2300 | return move_temp_to_file(tmpfile, sha1_file_name(sha1)); | |
8237b185 DB |
2301 | } |
2302 | ||
bf592c50 DB |
2303 | int has_pack_index(const unsigned char *sha1) |
2304 | { | |
2305 | struct stat st; | |
2306 | if (stat(sha1_pack_index_name(sha1), &st)) | |
2307 | return 0; | |
2308 | return 1; | |
2309 | } | |
2310 | ||
2311 | int has_pack_file(const unsigned char *sha1) | |
2312 | { | |
2313 | struct stat st; | |
2314 | if (stat(sha1_pack_name(sha1), &st)) | |
2315 | return 0; | |
2316 | return 1; | |
2317 | } | |
2318 | ||
106d710b | 2319 | int has_sha1_pack(const unsigned char *sha1, const char **ignore_packed) |
dade09c2 LT |
2320 | { |
2321 | struct pack_entry e; | |
106d710b | 2322 | return find_pack_entry(sha1, &e, ignore_packed); |
dade09c2 LT |
2323 | } |
2324 | ||
8237b185 DB |
2325 | int has_sha1_file(const unsigned char *sha1) |
2326 | { | |
8237b185 | 2327 | struct stat st; |
1f688557 JH |
2328 | struct pack_entry e; |
2329 | ||
106d710b | 2330 | if (find_pack_entry(sha1, &e, NULL)) |
1f688557 | 2331 | return 1; |
ab90ea5d | 2332 | return find_sha1_file(sha1, &st) ? 1 : 0; |
8237b185 | 2333 | } |
74400e71 | 2334 | |
e7332f96 BE |
2335 | int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object) |
2336 | { | |
fd17f5b5 | 2337 | struct strbuf buf; |
e7332f96 | 2338 | int ret; |
e7332f96 | 2339 | |
fd17f5b5 PH |
2340 | strbuf_init(&buf, 0); |
2341 | if (strbuf_read(&buf, fd, 4096) < 0) { | |
2342 | strbuf_release(&buf); | |
024510c8 DB |
2343 | return -1; |
2344 | } | |
e7332f96 | 2345 | |
024510c8 | 2346 | if (!type) |
8e440259 | 2347 | type = blob_type; |
024510c8 | 2348 | if (write_object) |
fd17f5b5 | 2349 | ret = write_sha1_file(buf.buf, buf.len, type, sha1); |
abdc3fc8 | 2350 | else |
fd17f5b5 PH |
2351 | ret = hash_sha1_file(buf.buf, buf.len, type, sha1); |
2352 | strbuf_release(&buf); | |
2353 | ||
024510c8 DB |
2354 | return ret; |
2355 | } | |
2356 | ||
edaec3fb | 2357 | int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, |
53bca91a | 2358 | enum object_type type, const char *path) |
74400e71 | 2359 | { |
dc49cd76 | 2360 | size_t size = xsize_t(st->st_size); |
3a55602e | 2361 | void *buf = NULL; |
6c510bee | 2362 | int ret, re_allocated = 0; |
74400e71 | 2363 | |
74400e71 | 2364 | if (size) |
c4712e45 | 2365 | buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
74400e71 | 2366 | close(fd); |
74400e71 | 2367 | |
7672db20 | 2368 | if (!type) |
edaec3fb | 2369 | type = OBJ_BLOB; |
6c510bee LT |
2370 | |
2371 | /* | |
2372 | * Convert blobs to git internal format | |
2373 | */ | |
d0d8e14d | 2374 | if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) { |
5ecd293d PH |
2375 | struct strbuf nbuf; |
2376 | strbuf_init(&nbuf, 0); | |
21e5ad50 SP |
2377 | if (convert_to_git(path, buf, size, &nbuf, |
2378 | write_object ? safe_crlf : 0)) { | |
ac78e548 | 2379 | munmap(buf, size); |
b315c5c0 | 2380 | buf = strbuf_detach(&nbuf, &size); |
6c510bee LT |
2381 | re_allocated = 1; |
2382 | } | |
2383 | } | |
2384 | ||
7672db20 | 2385 | if (write_object) |
edaec3fb | 2386 | ret = write_sha1_file(buf, size, typename(type), sha1); |
abdc3fc8 | 2387 | else |
edaec3fb | 2388 | ret = hash_sha1_file(buf, size, typename(type), sha1); |
6c510bee LT |
2389 | if (re_allocated) { |
2390 | free(buf); | |
2391 | return ret; | |
2392 | } | |
aac17941 LT |
2393 | if (size) |
2394 | munmap(buf, size); | |
2395 | return ret; | |
74400e71 | 2396 | } |
ec1fcc16 JH |
2397 | |
2398 | int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object) | |
2399 | { | |
2400 | int fd; | |
2401 | char *target; | |
dc49cd76 | 2402 | size_t len; |
ec1fcc16 JH |
2403 | |
2404 | switch (st->st_mode & S_IFMT) { | |
2405 | case S_IFREG: | |
2406 | fd = open(path, O_RDONLY); | |
2407 | if (fd < 0) | |
2408 | return error("open(\"%s\"): %s", path, | |
2409 | strerror(errno)); | |
53bca91a | 2410 | if (index_fd(sha1, fd, st, write_object, OBJ_BLOB, path) < 0) |
ec1fcc16 JH |
2411 | return error("%s: failed to insert into database", |
2412 | path); | |
2413 | break; | |
2414 | case S_IFLNK: | |
dc49cd76 SP |
2415 | len = xsize_t(st->st_size); |
2416 | target = xmalloc(len + 1); | |
2417 | if (readlink(path, target, len + 1) != st->st_size) { | |
ec1fcc16 JH |
2418 | char *errstr = strerror(errno); |
2419 | free(target); | |
2420 | return error("readlink(\"%s\"): %s", path, | |
2421 | errstr); | |
2422 | } | |
abdc3fc8 | 2423 | if (!write_object) |
dc49cd76 SP |
2424 | hash_sha1_file(target, len, blob_type, sha1); |
2425 | else if (write_sha1_file(target, len, blob_type, sha1)) | |
ec1fcc16 JH |
2426 | return error("%s: failed to insert into database", |
2427 | path); | |
2428 | free(target); | |
2429 | break; | |
f35a6d3b LT |
2430 | case S_IFDIR: |
2431 | return resolve_gitlink_ref(path, "HEAD", sha1); | |
ec1fcc16 JH |
2432 | default: |
2433 | return error("%s: unsupported file type", path); | |
2434 | } | |
2435 | return 0; | |
2436 | } | |
a69e5429 JH |
2437 | |
2438 | int read_pack_header(int fd, struct pack_header *header) | |
2439 | { | |
2440 | char *c = (char*)header; | |
2441 | ssize_t remaining = sizeof(struct pack_header); | |
2442 | do { | |
2443 | ssize_t r = xread(fd, c, remaining); | |
2444 | if (r <= 0) | |
2445 | /* "eof before pack header was fully read" */ | |
2446 | return PH_ERROR_EOF; | |
2447 | remaining -= r; | |
2448 | c += r; | |
2449 | } while (remaining > 0); | |
2450 | if (header->hdr_signature != htonl(PACK_SIGNATURE)) | |
2451 | /* "protocol error (pack signature mismatch detected)" */ | |
2452 | return PH_ERROR_PACK_SIGNATURE; | |
2453 | if (!pack_version_ok(header->hdr_version)) | |
2454 | /* "protocol error (pack version unsupported)" */ | |
2455 | return PH_ERROR_PROTOCOL; | |
2456 | return 0; | |
2457 | } |