]>
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 | */ | |
1f688557 JH |
9 | #include <sys/types.h> |
10 | #include <dirent.h> | |
0fcfd160 | 11 | #include "cache.h" |
1f688557 | 12 | #include "delta.h" |
a733cb60 | 13 | #include "pack.h" |
0fcfd160 | 14 | |
144bde78 LT |
15 | #ifndef O_NOATIME |
16 | #if defined(__linux__) && (defined(__i386__) || defined(__PPC__)) | |
17 | #define O_NOATIME 01000000 | |
18 | #else | |
19 | #define O_NOATIME 0 | |
20 | #endif | |
21 | #endif | |
22 | ||
23 | static unsigned int sha1_file_open_flag = O_NOATIME; | |
24 | ||
0fcfd160 LT |
25 | static unsigned hexval(char c) |
26 | { | |
27 | if (c >= '0' && c <= '9') | |
28 | return c - '0'; | |
29 | if (c >= 'a' && c <= 'f') | |
30 | return c - 'a' + 10; | |
31 | if (c >= 'A' && c <= 'F') | |
32 | return c - 'A' + 10; | |
33 | return ~0; | |
34 | } | |
35 | ||
36 | int get_sha1_hex(const char *hex, unsigned char *sha1) | |
37 | { | |
38 | int i; | |
39 | for (i = 0; i < 20; i++) { | |
40 | unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); | |
41 | if (val & ~0xff) | |
42 | return -1; | |
43 | *sha1++ = val; | |
44 | hex += 2; | |
45 | } | |
46 | return 0; | |
47 | } | |
48 | ||
5da5c8f4 JH |
49 | static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir, |
50 | *git_graft_file; | |
8ac069ac JH |
51 | static void setup_git_env(void) |
52 | { | |
a9ab586a | 53 | git_dir = getenv(GIT_DIR_ENVIRONMENT); |
8ac069ac JH |
54 | if (!git_dir) |
55 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; | |
a9ab586a | 56 | git_object_dir = getenv(DB_ENVIRONMENT); |
8ac069ac JH |
57 | if (!git_object_dir) { |
58 | git_object_dir = xmalloc(strlen(git_dir) + 9); | |
59 | sprintf(git_object_dir, "%s/objects", git_dir); | |
60 | } | |
95fc7512 DB |
61 | git_refs_dir = xmalloc(strlen(git_dir) + 6); |
62 | sprintf(git_refs_dir, "%s/refs", git_dir); | |
a9ab586a | 63 | git_index_file = getenv(INDEX_ENVIRONMENT); |
8ac069ac JH |
64 | if (!git_index_file) { |
65 | git_index_file = xmalloc(strlen(git_dir) + 7); | |
66 | sprintf(git_index_file, "%s/index", git_dir); | |
67 | } | |
a9ab586a | 68 | git_graft_file = getenv(GRAFT_ENVIRONMENT); |
5da5c8f4 JH |
69 | if (!git_graft_file) |
70 | git_graft_file = strdup(git_path("info/grafts")); | |
8ac069ac JH |
71 | } |
72 | ||
5da1606d SV |
73 | char *get_git_dir(void) |
74 | { | |
75 | if (!git_dir) | |
76 | setup_git_env(); | |
77 | return git_dir; | |
78 | } | |
79 | ||
8ac069ac JH |
80 | char *get_object_directory(void) |
81 | { | |
82 | if (!git_object_dir) | |
83 | setup_git_env(); | |
84 | return git_object_dir; | |
85 | } | |
86 | ||
95fc7512 DB |
87 | char *get_refs_directory(void) |
88 | { | |
89 | if (!git_refs_dir) | |
90 | setup_git_env(); | |
91 | return git_refs_dir; | |
92 | } | |
93 | ||
8ac069ac JH |
94 | char *get_index_file(void) |
95 | { | |
96 | if (!git_index_file) | |
97 | setup_git_env(); | |
98 | return git_index_file; | |
99 | } | |
100 | ||
5da5c8f4 JH |
101 | char *get_graft_file(void) |
102 | { | |
103 | if (!git_graft_file) | |
104 | setup_git_env(); | |
105 | return git_graft_file; | |
106 | } | |
107 | ||
b2cb9425 JH |
108 | int safe_create_leading_directories(char *path) |
109 | { | |
110 | char *pos = path; | |
111 | ||
112 | while (pos) { | |
113 | pos = strchr(pos, '/'); | |
114 | if (!pos) | |
115 | break; | |
116 | *pos = 0; | |
117 | if (mkdir(path, 0777) < 0) | |
118 | if (errno != EEXIST) { | |
119 | *pos = '/'; | |
120 | return -1; | |
121 | } | |
122 | *pos++ = '/'; | |
123 | } | |
124 | return 0; | |
125 | } | |
723c31fe | 126 | |
0fcfd160 LT |
127 | char * sha1_to_hex(const unsigned char *sha1) |
128 | { | |
129 | static char buffer[50]; | |
130 | static const char hex[] = "0123456789abcdef"; | |
131 | char *buf = buffer; | |
132 | int i; | |
133 | ||
134 | for (i = 0; i < 20; i++) { | |
135 | unsigned int val = *sha1++; | |
136 | *buf++ = hex[val >> 4]; | |
137 | *buf++ = hex[val & 0xf]; | |
138 | } | |
139 | return buffer; | |
140 | } | |
141 | ||
ace1534d JH |
142 | static void fill_sha1_path(char *pathbuf, const unsigned char *sha1) |
143 | { | |
144 | int i; | |
145 | for (i = 0; i < 20; i++) { | |
146 | static char hex[] = "0123456789abcdef"; | |
147 | unsigned int val = sha1[i]; | |
148 | char *pos = pathbuf + i*2 + (i > 0); | |
149 | *pos++ = hex[val >> 4]; | |
150 | *pos = hex[val & 0xf]; | |
151 | } | |
152 | } | |
153 | ||
0fcfd160 LT |
154 | /* |
155 | * NOTE! This returns a statically allocated buffer, so you have to be | |
156 | * careful about using it. Do a "strdup()" if you need to save the | |
157 | * filename. | |
ace1534d JH |
158 | * |
159 | * Also note that this returns the location for creating. Reading | |
160 | * SHA1 file can happen from any alternate directory listed in the | |
d19938ab | 161 | * DB_ENVIRONMENT environment variable if it is not found in |
ace1534d | 162 | * the primary object database. |
0fcfd160 LT |
163 | */ |
164 | char *sha1_file_name(const unsigned char *sha1) | |
165 | { | |
0fcfd160 LT |
166 | static char *name, *base; |
167 | ||
168 | if (!base) { | |
d19938ab | 169 | const char *sha1_file_directory = get_object_directory(); |
0fcfd160 | 170 | int len = strlen(sha1_file_directory); |
812666c8 | 171 | base = xmalloc(len + 60); |
0fcfd160 LT |
172 | memcpy(base, sha1_file_directory, len); |
173 | memset(base+len, 0, 60); | |
174 | base[len] = '/'; | |
175 | base[len+3] = '/'; | |
176 | name = base + len + 1; | |
177 | } | |
ace1534d | 178 | fill_sha1_path(name, sha1); |
0fcfd160 LT |
179 | return base; |
180 | } | |
181 | ||
bf592c50 DB |
182 | char *sha1_pack_name(const unsigned char *sha1) |
183 | { | |
184 | static const char hex[] = "0123456789abcdef"; | |
185 | static char *name, *base, *buf; | |
186 | int i; | |
187 | ||
188 | if (!base) { | |
189 | const char *sha1_file_directory = get_object_directory(); | |
190 | int len = strlen(sha1_file_directory); | |
191 | base = xmalloc(len + 60); | |
192 | sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory); | |
193 | name = base + len + 11; | |
194 | } | |
195 | ||
196 | buf = name; | |
197 | ||
198 | for (i = 0; i < 20; i++) { | |
199 | unsigned int val = *sha1++; | |
200 | *buf++ = hex[val >> 4]; | |
201 | *buf++ = hex[val & 0xf]; | |
202 | } | |
203 | ||
204 | return base; | |
205 | } | |
206 | ||
207 | char *sha1_pack_index_name(const unsigned char *sha1) | |
208 | { | |
209 | static const char hex[] = "0123456789abcdef"; | |
210 | static char *name, *base, *buf; | |
211 | int i; | |
212 | ||
213 | if (!base) { | |
214 | const char *sha1_file_directory = get_object_directory(); | |
215 | int len = strlen(sha1_file_directory); | |
216 | base = xmalloc(len + 60); | |
217 | sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory); | |
218 | name = base + len + 11; | |
219 | } | |
220 | ||
221 | buf = name; | |
222 | ||
223 | for (i = 0; i < 20; i++) { | |
224 | unsigned int val = *sha1++; | |
225 | *buf++ = hex[val >> 4]; | |
226 | *buf++ = hex[val & 0xf]; | |
227 | } | |
228 | ||
229 | return base; | |
230 | } | |
231 | ||
d5a63b99 JH |
232 | struct alternate_object_database *alt_odb_list; |
233 | static struct alternate_object_database **alt_odb_tail; | |
ace1534d | 234 | |
ddd5d056 JH |
235 | /* |
236 | * Prepare alternate object database registry. | |
d5a63b99 JH |
237 | * |
238 | * The variable alt_odb_list points at the list of struct | |
239 | * alternate_object_database. The elements on this list come from | |
240 | * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT | |
241 | * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates, | |
242 | * whose contents is exactly in the same format as that environment | |
243 | * variable. Its base points at a statically allocated buffer that | |
244 | * contains "/the/directory/corresponding/to/.git/objects/...", while | |
245 | * its name points just after the slash at the end of ".git/objects/" | |
246 | * in the example above, and has enough space to hold 40-byte hex | |
247 | * SHA1, an extra slash for the first level indirection, and the | |
248 | * terminating NUL. | |
ddd5d056 | 249 | */ |
ccfd3e99 JH |
250 | static void link_alt_odb_entries(const char *alt, const char *ep, int sep, |
251 | const char *relative_base) | |
ace1534d | 252 | { |
ace1534d | 253 | const char *cp, *last; |
d5a63b99 | 254 | struct alternate_object_database *ent; |
ccfd3e99 | 255 | int base_len = -1; |
d5a63b99 JH |
256 | |
257 | last = alt; | |
9577e7e3 JH |
258 | while (last < ep) { |
259 | cp = last; | |
260 | if (cp < ep && *cp == '#') { | |
261 | while (cp < ep && *cp != sep) | |
262 | cp++; | |
263 | last = cp + 1; | |
264 | continue; | |
265 | } | |
266 | for ( ; cp < ep && *cp != sep; cp++) | |
d5a63b99 JH |
267 | ; |
268 | if (last != cp) { | |
269 | /* 43 = 40-byte + 2 '/' + terminating NUL */ | |
270 | int pfxlen = cp - last; | |
271 | int entlen = pfxlen + 43; | |
272 | ||
ccfd3e99 JH |
273 | if (*last != '/' && relative_base) { |
274 | /* Relative alt-odb */ | |
275 | if (base_len < 0) | |
276 | base_len = strlen(relative_base) + 1; | |
277 | entlen += base_len; | |
278 | pfxlen += base_len; | |
279 | } | |
d5a63b99 JH |
280 | ent = xmalloc(sizeof(*ent) + entlen); |
281 | *alt_odb_tail = ent; | |
282 | alt_odb_tail = &(ent->next); | |
283 | ent->next = NULL; | |
ccfd3e99 JH |
284 | if (*last != '/' && relative_base) { |
285 | memcpy(ent->base, relative_base, base_len - 1); | |
286 | ent->base[base_len - 1] = '/'; | |
287 | memcpy(ent->base + base_len, | |
288 | last, cp - last); | |
289 | } | |
290 | else | |
291 | memcpy(ent->base, last, pfxlen); | |
d5a63b99 JH |
292 | ent->name = ent->base + pfxlen + 1; |
293 | ent->base[pfxlen] = ent->base[pfxlen + 3] = '/'; | |
294 | ent->base[entlen-1] = 0; | |
295 | } | |
9577e7e3 | 296 | while (cp < ep && *cp == sep) |
d5a63b99 JH |
297 | cp++; |
298 | last = cp; | |
9577e7e3 | 299 | } |
d5a63b99 JH |
300 | } |
301 | ||
302 | void prepare_alt_odb(void) | |
303 | { | |
304 | char path[PATH_MAX]; | |
9577e7e3 | 305 | char *map; |
d5a63b99 JH |
306 | int fd; |
307 | struct stat st; | |
c7c81b3a JR |
308 | char *alt; |
309 | ||
a9ab586a | 310 | alt = getenv(ALTERNATE_DB_ENVIRONMENT); |
c7c81b3a | 311 | if (!alt) alt = ""; |
ace1534d | 312 | |
d5a63b99 JH |
313 | if (alt_odb_tail) |
314 | return; | |
315 | alt_odb_tail = &alt_odb_list; | |
ccfd3e99 | 316 | link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL); |
d5a63b99 | 317 | |
ccfd3e99 | 318 | sprintf(path, "%s/info/alternates", get_object_directory()); |
d5a63b99 JH |
319 | fd = open(path, O_RDONLY); |
320 | if (fd < 0) | |
321 | return; | |
322 | if (fstat(fd, &st) || (st.st_size == 0)) { | |
323 | close(fd); | |
9a217f2a | 324 | return; |
ace1534d | 325 | } |
d5a63b99 JH |
326 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
327 | close(fd); | |
328 | if (map == MAP_FAILED) | |
329 | return; | |
330 | ||
ccfd3e99 JH |
331 | link_alt_odb_entries(map, map + st.st_size, '\n', |
332 | get_object_directory()); | |
d5a63b99 | 333 | munmap(map, st.st_size); |
ace1534d JH |
334 | } |
335 | ||
336 | static char *find_sha1_file(const unsigned char *sha1, struct stat *st) | |
337 | { | |
ace1534d | 338 | char *name = sha1_file_name(sha1); |
d5a63b99 | 339 | struct alternate_object_database *alt; |
ace1534d JH |
340 | |
341 | if (!stat(name, st)) | |
342 | return name; | |
9a217f2a | 343 | prepare_alt_odb(); |
d5a63b99 JH |
344 | for (alt = alt_odb_list; alt; alt = alt->next) { |
345 | name = alt->name; | |
ace1534d | 346 | fill_sha1_path(name, sha1); |
d5a63b99 JH |
347 | if (!stat(alt->base, st)) |
348 | return alt->base; | |
ace1534d JH |
349 | } |
350 | return NULL; | |
351 | } | |
352 | ||
1f688557 JH |
353 | #define PACK_MAX_SZ (1<<26) |
354 | static int pack_used_ctr; | |
355 | static unsigned long pack_mapped; | |
9a217f2a | 356 | struct packed_git *packed_git; |
1f688557 | 357 | |
1f688557 JH |
358 | static int check_packed_git_idx(const char *path, unsigned long *idx_size_, |
359 | void **idx_map_) | |
360 | { | |
361 | void *idx_map; | |
362 | unsigned int *index; | |
363 | unsigned long idx_size; | |
364 | int nr, i; | |
365 | int fd = open(path, O_RDONLY); | |
366 | struct stat st; | |
367 | if (fd < 0) | |
368 | return -1; | |
369 | if (fstat(fd, &st)) { | |
370 | close(fd); | |
371 | return -1; | |
372 | } | |
373 | idx_size = st.st_size; | |
374 | idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
375 | close(fd); | |
376 | if (idx_map == MAP_FAILED) | |
377 | return -1; | |
378 | ||
379 | index = idx_map; | |
4d235c80 LT |
380 | *idx_map_ = idx_map; |
381 | *idx_size_ = idx_size; | |
1f688557 JH |
382 | |
383 | /* check index map */ | |
f9253394 | 384 | if (idx_size < 4*256 + 20 + 20) |
1f688557 JH |
385 | return error("index file too small"); |
386 | nr = 0; | |
387 | for (i = 0; i < 256; i++) { | |
388 | unsigned int n = ntohl(index[i]); | |
389 | if (n < nr) | |
390 | return error("non-monotonic index"); | |
391 | nr = n; | |
392 | } | |
393 | ||
394 | /* | |
395 | * Total size: | |
396 | * - 256 index entries 4 bytes each | |
397 | * - 24-byte entries * nr (20-byte sha1 + 4-byte offset) | |
398 | * - 20-byte SHA1 of the packfile | |
399 | * - 20-byte SHA1 file checksum | |
400 | */ | |
401 | if (idx_size != 4*256 + nr * 24 + 20 + 20) | |
402 | return error("wrong index file size"); | |
403 | ||
1f688557 JH |
404 | return 0; |
405 | } | |
406 | ||
f9253394 | 407 | static int unuse_one_packed_git(void) |
1f688557 | 408 | { |
f9253394 JH |
409 | struct packed_git *p, *lru = NULL; |
410 | ||
411 | for (p = packed_git; p; p = p->next) { | |
412 | if (p->pack_use_cnt || !p->pack_base) | |
413 | continue; | |
414 | if (!lru || p->pack_last_used < lru->pack_last_used) | |
415 | lru = p; | |
416 | } | |
417 | if (!lru) | |
418 | return 0; | |
419 | munmap(lru->pack_base, lru->pack_size); | |
420 | lru->pack_base = NULL; | |
421 | return 1; | |
422 | } | |
423 | ||
424 | void unuse_packed_git(struct packed_git *p) | |
425 | { | |
426 | p->pack_use_cnt--; | |
1f688557 JH |
427 | } |
428 | ||
f9253394 | 429 | int use_packed_git(struct packed_git *p) |
1f688557 | 430 | { |
bf592c50 DB |
431 | if (!p->pack_size) { |
432 | struct stat st; | |
433 | // We created the struct before we had the pack | |
434 | stat(p->pack_name, &st); | |
435 | if (!S_ISREG(st.st_mode)) | |
436 | die("packfile %s not a regular file", p->pack_name); | |
437 | p->pack_size = st.st_size; | |
438 | } | |
1f688557 JH |
439 | if (!p->pack_base) { |
440 | int fd; | |
441 | struct stat st; | |
442 | void *map; | |
443 | ||
444 | pack_mapped += p->pack_size; | |
f9253394 JH |
445 | while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git()) |
446 | ; /* nothing */ | |
1f688557 JH |
447 | fd = open(p->pack_name, O_RDONLY); |
448 | if (fd < 0) | |
f9253394 | 449 | die("packfile %s cannot be opened", p->pack_name); |
1f688557 JH |
450 | if (fstat(fd, &st)) { |
451 | close(fd); | |
f9253394 | 452 | die("packfile %s cannot be opened", p->pack_name); |
1f688557 JH |
453 | } |
454 | if (st.st_size != p->pack_size) | |
f9253394 | 455 | die("packfile %s size mismatch.", p->pack_name); |
1f688557 JH |
456 | map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0); |
457 | close(fd); | |
458 | if (map == MAP_FAILED) | |
f9253394 | 459 | die("packfile %s cannot be mapped.", p->pack_name); |
1f688557 | 460 | p->pack_base = map; |
f9253394 JH |
461 | |
462 | /* Check if the pack file matches with the index file. | |
463 | * this is cheap. | |
464 | */ | |
465 | if (memcmp((char*)(p->index_base) + p->index_size - 40, | |
bf592c50 DB |
466 | p->pack_base + p->pack_size - 20, 20)) { |
467 | ||
f9253394 | 468 | die("packfile %s does not match index.", p->pack_name); |
bf592c50 | 469 | } |
1f688557 JH |
470 | } |
471 | p->pack_last_used = pack_used_ctr++; | |
f9253394 | 472 | p->pack_use_cnt++; |
1f688557 JH |
473 | return 0; |
474 | } | |
475 | ||
f9253394 | 476 | struct packed_git *add_packed_git(char *path, int path_len) |
1f688557 JH |
477 | { |
478 | struct stat st; | |
479 | struct packed_git *p; | |
480 | unsigned long idx_size; | |
481 | void *idx_map; | |
482 | ||
483 | if (check_packed_git_idx(path, &idx_size, &idx_map)) | |
484 | return NULL; | |
485 | ||
486 | /* do we have a corresponding .pack file? */ | |
487 | strcpy(path + path_len - 4, ".pack"); | |
488 | if (stat(path, &st) || !S_ISREG(st.st_mode)) { | |
489 | munmap(idx_map, idx_size); | |
490 | return NULL; | |
491 | } | |
492 | /* ok, it looks sane as far as we can check without | |
493 | * actually mapping the pack file. | |
494 | */ | |
495 | p = xmalloc(sizeof(*p) + path_len + 2); | |
496 | strcpy(p->pack_name, path); | |
497 | p->index_size = idx_size; | |
498 | p->pack_size = st.st_size; | |
499 | p->index_base = idx_map; | |
500 | p->next = NULL; | |
d85a4fee | 501 | p->pack_base = NULL; |
1f688557 | 502 | p->pack_last_used = 0; |
f9253394 | 503 | p->pack_use_cnt = 0; |
1f688557 JH |
504 | return p; |
505 | } | |
506 | ||
bf592c50 | 507 | struct packed_git *parse_pack_index(unsigned char *sha1) |
c508df5e DB |
508 | { |
509 | char *path = sha1_pack_index_name(sha1); | |
510 | return parse_pack_index_file(sha1, path); | |
511 | } | |
512 | ||
2ab141a2 | 513 | struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path) |
bf592c50 DB |
514 | { |
515 | struct packed_git *p; | |
516 | unsigned long idx_size; | |
517 | void *idx_map; | |
c508df5e | 518 | char *path; |
bf592c50 | 519 | |
c508df5e | 520 | if (check_packed_git_idx(idx_path, &idx_size, &idx_map)) |
bf592c50 DB |
521 | return NULL; |
522 | ||
523 | path = sha1_pack_name(sha1); | |
524 | ||
525 | p = xmalloc(sizeof(*p) + strlen(path) + 2); | |
526 | strcpy(p->pack_name, path); | |
527 | p->index_size = idx_size; | |
528 | p->pack_size = 0; | |
529 | p->index_base = idx_map; | |
530 | p->next = NULL; | |
531 | p->pack_base = NULL; | |
532 | p->pack_last_used = 0; | |
533 | p->pack_use_cnt = 0; | |
534 | memcpy(p->sha1, sha1, 20); | |
535 | return p; | |
536 | } | |
537 | ||
538 | void install_packed_git(struct packed_git *pack) | |
539 | { | |
540 | pack->next = packed_git; | |
541 | packed_git = pack; | |
542 | } | |
543 | ||
1f688557 JH |
544 | static void prepare_packed_git_one(char *objdir) |
545 | { | |
546 | char path[PATH_MAX]; | |
547 | int len; | |
548 | DIR *dir; | |
549 | struct dirent *de; | |
550 | ||
551 | sprintf(path, "%s/pack", objdir); | |
552 | len = strlen(path); | |
553 | dir = opendir(path); | |
554 | if (!dir) | |
555 | return; | |
556 | path[len++] = '/'; | |
557 | while ((de = readdir(dir)) != NULL) { | |
558 | int namelen = strlen(de->d_name); | |
559 | struct packed_git *p; | |
560 | ||
561 | if (strcmp(de->d_name + namelen - 4, ".idx")) | |
562 | continue; | |
563 | ||
564 | /* we have .idx. Is it a file we can map? */ | |
565 | strcpy(path + len, de->d_name); | |
566 | p = add_packed_git(path, len + namelen); | |
567 | if (!p) | |
568 | continue; | |
569 | p->next = packed_git; | |
570 | packed_git = p; | |
571 | } | |
5b35bcd5 | 572 | closedir(dir); |
1f688557 JH |
573 | } |
574 | ||
9a217f2a | 575 | void prepare_packed_git(void) |
1f688557 | 576 | { |
1f688557 | 577 | static int run_once = 0; |
d5a63b99 | 578 | struct alternate_object_database *alt; |
1f688557 | 579 | |
d5a63b99 | 580 | if (run_once) |
1f688557 | 581 | return; |
1f688557 | 582 | prepare_packed_git_one(get_object_directory()); |
9a217f2a | 583 | prepare_alt_odb(); |
d5a63b99 JH |
584 | for (alt = alt_odb_list; alt; alt = alt->next) { |
585 | alt->name[0] = 0; | |
586 | prepare_packed_git_one(alt->base); | |
1f688557 | 587 | } |
d5a63b99 | 588 | run_once = 1; |
1f688557 JH |
589 | } |
590 | ||
5d6ccf5c | 591 | int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type) |
0fcfd160 | 592 | { |
d98b46f8 | 593 | char header[100]; |
0fcfd160 LT |
594 | unsigned char real_sha1[20]; |
595 | SHA_CTX c; | |
596 | ||
597 | SHA1_Init(&c); | |
d98b46f8 | 598 | SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size)); |
0fcfd160 LT |
599 | SHA1_Update(&c, map, size); |
600 | SHA1_Final(real_sha1, &c); | |
601 | return memcmp(sha1, real_sha1, 20) ? -1 : 0; | |
602 | } | |
603 | ||
1f688557 | 604 | static void *map_sha1_file_internal(const unsigned char *sha1, |
d5f1befc | 605 | unsigned long *size) |
0fcfd160 | 606 | { |
0fcfd160 LT |
607 | struct stat st; |
608 | void *map; | |
144bde78 | 609 | int fd; |
ace1534d JH |
610 | char *filename = find_sha1_file(sha1, &st); |
611 | ||
612 | if (!filename) { | |
ace1534d JH |
613 | return NULL; |
614 | } | |
0fcfd160 | 615 | |
144bde78 | 616 | fd = open(filename, O_RDONLY | sha1_file_open_flag); |
0fcfd160 | 617 | if (fd < 0) { |
144bde78 LT |
618 | /* See if it works without O_NOATIME */ |
619 | switch (sha1_file_open_flag) { | |
620 | default: | |
621 | fd = open(filename, O_RDONLY); | |
622 | if (fd >= 0) | |
623 | break; | |
624 | /* Fallthrough */ | |
625 | case 0: | |
144bde78 LT |
626 | return NULL; |
627 | } | |
628 | ||
1f688557 JH |
629 | /* If it failed once, it will probably fail again. |
630 | * Stop using O_NOATIME | |
631 | */ | |
144bde78 | 632 | sha1_file_open_flag = 0; |
0fcfd160 | 633 | } |
0fcfd160 LT |
634 | map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
635 | close(fd); | |
e35f9824 | 636 | if (map == MAP_FAILED) |
0fcfd160 LT |
637 | return NULL; |
638 | *size = st.st_size; | |
639 | return map; | |
640 | } | |
641 | ||
c4483576 LT |
642 | int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size) |
643 | { | |
644 | /* Get the data stream */ | |
645 | memset(stream, 0, sizeof(*stream)); | |
646 | stream->next_in = map; | |
647 | stream->avail_in = mapsize; | |
648 | stream->next_out = buffer; | |
649 | stream->avail_out = size; | |
650 | ||
651 | inflateInit(stream); | |
652 | return inflate(stream, 0); | |
653 | } | |
654 | ||
6da4016a | 655 | static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size) |
5180cacc LT |
656 | { |
657 | int bytes = strlen(buffer) + 1; | |
d565b341 | 658 | unsigned char *buf = xmalloc(1+size); |
5180cacc LT |
659 | |
660 | memcpy(buf, buffer + bytes, stream->total_out - bytes); | |
661 | bytes = stream->total_out - bytes; | |
662 | if (bytes < size) { | |
663 | stream->next_out = buf + bytes; | |
664 | stream->avail_out = size - bytes; | |
665 | while (inflate(stream, Z_FINISH) == Z_OK) | |
666 | /* nothing */; | |
667 | } | |
668 | buf[size] = 0; | |
669 | inflateEnd(stream); | |
670 | return buf; | |
671 | } | |
672 | ||
673 | /* | |
674 | * We used to just use "sscanf()", but that's actually way | |
675 | * too permissive for what we want to check. So do an anal | |
676 | * object header parse by hand. | |
677 | */ | |
678 | int parse_sha1_header(char *hdr, char *type, unsigned long *sizep) | |
679 | { | |
680 | int i; | |
681 | unsigned long size; | |
682 | ||
683 | /* | |
684 | * The type can be at most ten bytes (including the | |
685 | * terminating '\0' that we add), and is followed by | |
686 | * a space. | |
687 | */ | |
688 | i = 10; | |
689 | for (;;) { | |
690 | char c = *hdr++; | |
691 | if (c == ' ') | |
692 | break; | |
693 | if (!--i) | |
694 | return -1; | |
695 | *type++ = c; | |
696 | } | |
697 | *type = 0; | |
698 | ||
699 | /* | |
700 | * The length must follow immediately, and be in canonical | |
701 | * decimal format (ie "010" is not valid). | |
702 | */ | |
703 | size = *hdr++ - '0'; | |
704 | if (size > 9) | |
705 | return -1; | |
706 | if (size) { | |
707 | for (;;) { | |
708 | unsigned long c = *hdr - '0'; | |
709 | if (c > 9) | |
710 | break; | |
711 | hdr++; | |
712 | size = size * 10 + c; | |
713 | } | |
714 | } | |
715 | *sizep = size; | |
716 | ||
717 | /* | |
718 | * The length must be followed by a zero byte | |
719 | */ | |
720 | return *hdr ? -1 : 0; | |
721 | } | |
722 | ||
0fcfd160 LT |
723 | void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size) |
724 | { | |
5180cacc | 725 | int ret; |
0fcfd160 | 726 | z_stream stream; |
5180cacc | 727 | char hdr[8192]; |
0fcfd160 | 728 | |
5180cacc LT |
729 | ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)); |
730 | if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0) | |
0fcfd160 LT |
731 | return NULL; |
732 | ||
5180cacc | 733 | return unpack_sha1_rest(&stream, hdr, *size); |
0fcfd160 LT |
734 | } |
735 | ||
f3bf9224 JH |
736 | /* forward declaration for a mutually recursive function */ |
737 | static int packed_object_info(struct pack_entry *entry, | |
738 | char *type, unsigned long *sizep); | |
739 | ||
5db47c2b JH |
740 | static int packed_delta_info(unsigned char *base_sha1, |
741 | unsigned long delta_size, | |
742 | unsigned long left, | |
743 | char *type, | |
f3bf9224 JH |
744 | unsigned long *sizep, |
745 | struct packed_git *p) | |
5db47c2b | 746 | { |
f3bf9224 JH |
747 | struct pack_entry base_ent; |
748 | ||
5db47c2b JH |
749 | if (left < 20) |
750 | die("truncated pack file"); | |
5db47c2b | 751 | |
f3bf9224 JH |
752 | /* The base entry _must_ be in the same pack */ |
753 | if (!find_pack_entry_one(base_sha1, &base_ent, p)) | |
754 | die("failed to find delta-pack base object %s", | |
755 | sha1_to_hex(base_sha1)); | |
756 | ||
c62266f3 JH |
757 | /* We choose to only get the type of the base object and |
758 | * ignore potentially corrupt pack file that expects the delta | |
759 | * based on a base with a wrong size. This saves tons of | |
760 | * inflate() calls. | |
761 | */ | |
5db47c2b | 762 | |
f3bf9224 | 763 | if (packed_object_info(&base_ent, type, NULL)) |
c62266f3 | 764 | die("cannot get info for delta-pack base"); |
5db47c2b | 765 | |
c62266f3 JH |
766 | if (sizep) { |
767 | const unsigned char *data; | |
768 | unsigned char delta_head[64]; | |
769 | unsigned long result_size; | |
770 | z_stream stream; | |
771 | int st; | |
772 | ||
773 | memset(&stream, 0, sizeof(stream)); | |
774 | ||
775 | data = stream.next_in = base_sha1 + 20; | |
776 | stream.avail_in = left - 20; | |
777 | stream.next_out = delta_head; | |
778 | stream.avail_out = sizeof(delta_head); | |
779 | ||
780 | inflateInit(&stream); | |
781 | st = inflate(&stream, Z_FINISH); | |
782 | inflateEnd(&stream); | |
783 | if ((st != Z_STREAM_END) && | |
784 | stream.total_out != sizeof(delta_head)) | |
785 | die("delta data unpack-initial failed"); | |
786 | ||
787 | /* Examine the initial part of the delta to figure out | |
788 | * the result size. | |
789 | */ | |
790 | data = delta_head; | |
791 | get_delta_hdr_size(&data); /* ignore base size */ | |
5db47c2b | 792 | |
c62266f3 JH |
793 | /* Read the result size */ |
794 | result_size = get_delta_hdr_size(&data); | |
795 | *sizep = result_size; | |
796 | } | |
5db47c2b JH |
797 | return 0; |
798 | } | |
799 | ||
a733cb60 LT |
800 | static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset, |
801 | enum object_type *type, unsigned long *sizep) | |
802 | { | |
01247d87 | 803 | unsigned shift; |
a733cb60 LT |
804 | unsigned char *pack, c; |
805 | unsigned long size; | |
806 | ||
807 | if (offset >= p->pack_size) | |
808 | die("object offset outside of pack file"); | |
809 | ||
810 | pack = p->pack_base + offset; | |
811 | c = *pack++; | |
812 | offset++; | |
813 | *type = (c >> 4) & 7; | |
814 | size = c & 15; | |
01247d87 | 815 | shift = 4; |
a733cb60 LT |
816 | while (c & 0x80) { |
817 | if (offset >= p->pack_size) | |
818 | die("object offset outside of pack file"); | |
819 | c = *pack++; | |
820 | offset++; | |
01247d87 LT |
821 | size += (c & 0x7f) << shift; |
822 | shift += 7; | |
a733cb60 LT |
823 | } |
824 | *sizep = size; | |
825 | return offset; | |
826 | } | |
827 | ||
ad8c80a5 JH |
828 | void packed_object_info_detail(struct pack_entry *e, |
829 | char *type, | |
830 | unsigned long *size, | |
831 | unsigned long *store_size, | |
832 | int *delta_chain_length, | |
833 | unsigned char *base_sha1) | |
834 | { | |
835 | struct packed_git *p = e->p; | |
836 | unsigned long offset, left; | |
837 | unsigned char *pack; | |
838 | enum object_type kind; | |
839 | ||
840 | offset = unpack_object_header(p, e->offset, &kind, size); | |
841 | pack = p->pack_base + offset; | |
842 | left = p->pack_size - offset; | |
843 | if (kind != OBJ_DELTA) | |
844 | *delta_chain_length = 0; | |
845 | else { | |
846 | int chain_length = 0; | |
847 | memcpy(base_sha1, pack, 20); | |
848 | do { | |
849 | struct pack_entry base_ent; | |
850 | unsigned long junk; | |
851 | ||
852 | find_pack_entry_one(pack, &base_ent, p); | |
853 | offset = unpack_object_header(p, base_ent.offset, | |
854 | &kind, &junk); | |
855 | pack = p->pack_base + offset; | |
856 | chain_length++; | |
857 | } while (kind == OBJ_DELTA); | |
858 | *delta_chain_length = chain_length; | |
859 | } | |
860 | switch (kind) { | |
861 | case OBJ_COMMIT: | |
862 | strcpy(type, "commit"); | |
863 | break; | |
864 | case OBJ_TREE: | |
865 | strcpy(type, "tree"); | |
866 | break; | |
867 | case OBJ_BLOB: | |
868 | strcpy(type, "blob"); | |
869 | break; | |
870 | case OBJ_TAG: | |
871 | strcpy(type, "tag"); | |
872 | break; | |
873 | default: | |
874 | die("corrupted pack file"); | |
875 | } | |
876 | *store_size = 0; /* notyet */ | |
877 | } | |
878 | ||
1f688557 JH |
879 | static int packed_object_info(struct pack_entry *entry, |
880 | char *type, unsigned long *sizep) | |
881 | { | |
882 | struct packed_git *p = entry->p; | |
883 | unsigned long offset, size, left; | |
884 | unsigned char *pack; | |
a733cb60 | 885 | enum object_type kind; |
f9253394 | 886 | int retval; |
5db47c2b JH |
887 | |
888 | if (use_packed_git(p)) | |
889 | die("cannot map packed file"); | |
890 | ||
a733cb60 | 891 | offset = unpack_object_header(p, entry->offset, &kind, &size); |
1f688557 | 892 | pack = p->pack_base + offset; |
a733cb60 LT |
893 | left = p->pack_size - offset; |
894 | ||
895 | switch (kind) { | |
896 | case OBJ_DELTA: | |
f3bf9224 | 897 | retval = packed_delta_info(pack, size, left, type, sizep, p); |
f9253394 JH |
898 | unuse_packed_git(p); |
899 | return retval; | |
a733cb60 | 900 | case OBJ_COMMIT: |
1f688557 JH |
901 | strcpy(type, "commit"); |
902 | break; | |
a733cb60 | 903 | case OBJ_TREE: |
1f688557 JH |
904 | strcpy(type, "tree"); |
905 | break; | |
a733cb60 | 906 | case OBJ_BLOB: |
1f688557 JH |
907 | strcpy(type, "blob"); |
908 | break; | |
a733cb60 | 909 | case OBJ_TAG: |
a69d0943 LT |
910 | strcpy(type, "tag"); |
911 | break; | |
1f688557 JH |
912 | default: |
913 | die("corrupted pack file"); | |
914 | } | |
c62266f3 JH |
915 | if (sizep) |
916 | *sizep = size; | |
f9253394 | 917 | unuse_packed_git(p); |
1f688557 JH |
918 | return 0; |
919 | } | |
920 | ||
921 | /* forward declaration for a mutually recursive function */ | |
922 | static void *unpack_entry(struct pack_entry *, char *, unsigned long *); | |
923 | ||
924 | static void *unpack_delta_entry(unsigned char *base_sha1, | |
925 | unsigned long delta_size, | |
926 | unsigned long left, | |
927 | char *type, | |
f3bf9224 JH |
928 | unsigned long *sizep, |
929 | struct packed_git *p) | |
1f688557 | 930 | { |
f3bf9224 | 931 | struct pack_entry base_ent; |
1f688557 JH |
932 | void *data, *delta_data, *result, *base; |
933 | unsigned long data_size, result_size, base_size; | |
934 | z_stream stream; | |
935 | int st; | |
936 | ||
937 | if (left < 20) | |
938 | die("truncated pack file"); | |
939 | data = base_sha1 + 20; | |
940 | data_size = left - 20; | |
941 | delta_data = xmalloc(delta_size); | |
942 | ||
943 | memset(&stream, 0, sizeof(stream)); | |
944 | ||
945 | stream.next_in = data; | |
946 | stream.avail_in = data_size; | |
947 | stream.next_out = delta_data; | |
948 | stream.avail_out = delta_size; | |
949 | ||
950 | inflateInit(&stream); | |
951 | st = inflate(&stream, Z_FINISH); | |
952 | inflateEnd(&stream); | |
953 | if ((st != Z_STREAM_END) || stream.total_out != delta_size) | |
954 | die("delta data unpack failed"); | |
955 | ||
f3bf9224 JH |
956 | /* The base entry _must_ be in the same pack */ |
957 | if (!find_pack_entry_one(base_sha1, &base_ent, p)) | |
958 | die("failed to find delta-pack base object %s", | |
959 | sha1_to_hex(base_sha1)); | |
960 | base = unpack_entry_gently(&base_ent, type, &base_size); | |
1f688557 JH |
961 | if (!base) |
962 | die("failed to read delta-pack base object %s", | |
963 | sha1_to_hex(base_sha1)); | |
964 | result = patch_delta(base, base_size, | |
965 | delta_data, delta_size, | |
966 | &result_size); | |
967 | if (!result) | |
968 | die("failed to apply delta"); | |
969 | free(delta_data); | |
970 | free(base); | |
971 | *sizep = result_size; | |
972 | return result; | |
973 | } | |
974 | ||
975 | static void *unpack_non_delta_entry(unsigned char *data, | |
976 | unsigned long size, | |
977 | unsigned long left) | |
978 | { | |
979 | int st; | |
980 | z_stream stream; | |
4d235c80 | 981 | unsigned char *buffer; |
1f688557 JH |
982 | |
983 | buffer = xmalloc(size + 1); | |
984 | buffer[size] = 0; | |
985 | memset(&stream, 0, sizeof(stream)); | |
986 | stream.next_in = data; | |
987 | stream.avail_in = left; | |
988 | stream.next_out = buffer; | |
989 | stream.avail_out = size; | |
990 | ||
991 | inflateInit(&stream); | |
992 | st = inflate(&stream, Z_FINISH); | |
993 | inflateEnd(&stream); | |
994 | if ((st != Z_STREAM_END) || stream.total_out != size) { | |
995 | free(buffer); | |
996 | return NULL; | |
997 | } | |
998 | ||
999 | return buffer; | |
1000 | } | |
1001 | ||
1002 | static void *unpack_entry(struct pack_entry *entry, | |
1003 | char *type, unsigned long *sizep) | |
1004 | { | |
1005 | struct packed_git *p = entry->p; | |
f9253394 | 1006 | void *retval; |
1f688557 JH |
1007 | |
1008 | if (use_packed_git(p)) | |
1009 | die("cannot map packed file"); | |
f3bf9224 JH |
1010 | retval = unpack_entry_gently(entry, type, sizep); |
1011 | unuse_packed_git(p); | |
1012 | if (!retval) | |
1013 | die("corrupted pack file"); | |
1014 | return retval; | |
1015 | } | |
1016 | ||
1017 | /* The caller is responsible for use_packed_git()/unuse_packed_git() pair */ | |
1018 | void *unpack_entry_gently(struct pack_entry *entry, | |
1019 | char *type, unsigned long *sizep) | |
1020 | { | |
1021 | struct packed_git *p = entry->p; | |
1022 | unsigned long offset, size, left; | |
1023 | unsigned char *pack; | |
1024 | enum object_type kind; | |
1025 | void *retval; | |
1f688557 | 1026 | |
a733cb60 | 1027 | offset = unpack_object_header(p, entry->offset, &kind, &size); |
1f688557 | 1028 | pack = p->pack_base + offset; |
a733cb60 LT |
1029 | left = p->pack_size - offset; |
1030 | switch (kind) { | |
1031 | case OBJ_DELTA: | |
f3bf9224 | 1032 | retval = unpack_delta_entry(pack, size, left, type, sizep, p); |
f9253394 | 1033 | return retval; |
a733cb60 | 1034 | case OBJ_COMMIT: |
1f688557 JH |
1035 | strcpy(type, "commit"); |
1036 | break; | |
a733cb60 | 1037 | case OBJ_TREE: |
1f688557 JH |
1038 | strcpy(type, "tree"); |
1039 | break; | |
a733cb60 | 1040 | case OBJ_BLOB: |
1f688557 JH |
1041 | strcpy(type, "blob"); |
1042 | break; | |
a733cb60 | 1043 | case OBJ_TAG: |
a69d0943 LT |
1044 | strcpy(type, "tag"); |
1045 | break; | |
1f688557 | 1046 | default: |
f3bf9224 | 1047 | return NULL; |
1f688557 JH |
1048 | } |
1049 | *sizep = size; | |
f9253394 | 1050 | retval = unpack_non_delta_entry(pack, size, left); |
f9253394 | 1051 | return retval; |
1f688557 JH |
1052 | } |
1053 | ||
9a217f2a JH |
1054 | int num_packed_objects(const struct packed_git *p) |
1055 | { | |
f9253394 | 1056 | /* See check_packed_git_idx() */ |
9a217f2a JH |
1057 | return (p->index_size - 20 - 20 - 4*256) / 24; |
1058 | } | |
1059 | ||
1060 | int nth_packed_object_sha1(const struct packed_git *p, int n, | |
1061 | unsigned char* sha1) | |
1062 | { | |
1063 | void *index = p->index_base + 256; | |
1064 | if (n < 0 || num_packed_objects(p) <= n) | |
1065 | return -1; | |
1066 | memcpy(sha1, (index + 24 * n + 4), 20); | |
1067 | return 0; | |
1068 | } | |
1069 | ||
f3bf9224 JH |
1070 | int find_pack_entry_one(const unsigned char *sha1, |
1071 | struct pack_entry *e, struct packed_git *p) | |
1f688557 | 1072 | { |
4d235c80 | 1073 | unsigned int *level1_ofs = p->index_base; |
1f688557 JH |
1074 | int hi = ntohl(level1_ofs[*sha1]); |
1075 | int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1])); | |
1076 | void *index = p->index_base + 256; | |
1077 | ||
1078 | do { | |
1079 | int mi = (lo + hi) / 2; | |
1080 | int cmp = memcmp(index + 24 * mi + 4, sha1, 20); | |
1081 | if (!cmp) { | |
1082 | e->offset = ntohl(*((int*)(index + 24 * mi))); | |
1083 | memcpy(e->sha1, sha1, 20); | |
1084 | e->p = p; | |
1085 | return 1; | |
1086 | } | |
1087 | if (cmp > 0) | |
1088 | hi = mi; | |
1089 | else | |
1090 | lo = mi+1; | |
1091 | } while (lo < hi); | |
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e) | |
1096 | { | |
1097 | struct packed_git *p; | |
1098 | prepare_packed_git(); | |
1099 | ||
1100 | for (p = packed_git; p; p = p->next) { | |
f3bf9224 | 1101 | if (find_pack_entry_one(sha1, e, p)) |
1f688557 JH |
1102 | return 1; |
1103 | } | |
1104 | return 0; | |
1105 | } | |
1106 | ||
bf592c50 DB |
1107 | struct packed_git *find_sha1_pack(const unsigned char *sha1, |
1108 | struct packed_git *packs) | |
1109 | { | |
1110 | struct packed_git *p; | |
1111 | struct pack_entry e; | |
1112 | ||
1113 | for (p = packs; p; p = p->next) { | |
1114 | if (find_pack_entry_one(sha1, &e, p)) | |
1115 | return p; | |
1116 | } | |
1117 | return NULL; | |
1118 | ||
1119 | } | |
1120 | ||
36e4d74a | 1121 | int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep) |
65c2e0c3 | 1122 | { |
36e4d74a | 1123 | int status; |
65c2e0c3 JH |
1124 | unsigned long mapsize, size; |
1125 | void *map; | |
1126 | z_stream stream; | |
36e4d74a | 1127 | char hdr[128]; |
65c2e0c3 | 1128 | |
d5f1befc | 1129 | map = map_sha1_file_internal(sha1, &mapsize); |
1f688557 JH |
1130 | if (!map) { |
1131 | struct pack_entry e; | |
1132 | ||
1133 | if (!find_pack_entry(sha1, &e)) | |
1134 | return error("unable to find %s", sha1_to_hex(sha1)); | |
c62266f3 | 1135 | return packed_object_info(&e, type, sizep); |
1f688557 | 1136 | } |
36e4d74a JH |
1137 | if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) |
1138 | status = error("unable to unpack %s header", | |
1139 | sha1_to_hex(sha1)); | |
1140 | if (parse_sha1_header(hdr, type, &size) < 0) | |
1141 | status = error("unable to parse %s header", sha1_to_hex(sha1)); | |
c4584ae3 | 1142 | else { |
65c2e0c3 | 1143 | status = 0; |
c62266f3 JH |
1144 | if (sizep) |
1145 | *sizep = size; | |
65c2e0c3 | 1146 | } |
65c2e0c3 JH |
1147 | inflateEnd(&stream); |
1148 | munmap(map, mapsize); | |
1149 | return status; | |
1150 | } | |
1151 | ||
1f688557 JH |
1152 | static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size) |
1153 | { | |
1154 | struct pack_entry e; | |
1155 | ||
1156 | if (!find_pack_entry(sha1, &e)) { | |
1157 | error("cannot read sha1_file for %s", sha1_to_hex(sha1)); | |
1158 | return NULL; | |
1159 | } | |
1160 | return unpack_entry(&e, type, size); | |
1161 | } | |
1162 | ||
0fcfd160 LT |
1163 | void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size) |
1164 | { | |
1165 | unsigned long mapsize; | |
1166 | void *map, *buf; | |
ab90ea5d | 1167 | struct pack_entry e; |
0fcfd160 | 1168 | |
ab90ea5d JH |
1169 | if (find_pack_entry(sha1, &e)) |
1170 | return read_packed_sha1(sha1, type, size); | |
d5f1befc | 1171 | map = map_sha1_file_internal(sha1, &mapsize); |
0fcfd160 LT |
1172 | if (map) { |
1173 | buf = unpack_sha1_file(map, mapsize, type, size); | |
1174 | munmap(map, mapsize); | |
1175 | return buf; | |
1176 | } | |
ab90ea5d | 1177 | return NULL; |
0fcfd160 LT |
1178 | } |
1179 | ||
40469ee9 | 1180 | void *read_object_with_reference(const unsigned char *sha1, |
bf0f910d | 1181 | const char *required_type, |
40469ee9 JH |
1182 | unsigned long *size, |
1183 | unsigned char *actual_sha1_return) | |
f4913f91 JH |
1184 | { |
1185 | char type[20]; | |
1186 | void *buffer; | |
1187 | unsigned long isize; | |
40469ee9 | 1188 | unsigned char actual_sha1[20]; |
f4913f91 | 1189 | |
40469ee9 JH |
1190 | memcpy(actual_sha1, sha1, 20); |
1191 | while (1) { | |
1192 | int ref_length = -1; | |
1193 | const char *ref_type = NULL; | |
f4913f91 | 1194 | |
40469ee9 JH |
1195 | buffer = read_sha1_file(actual_sha1, type, &isize); |
1196 | if (!buffer) | |
1197 | return NULL; | |
1198 | if (!strcmp(type, required_type)) { | |
1199 | *size = isize; | |
1200 | if (actual_sha1_return) | |
1201 | memcpy(actual_sha1_return, actual_sha1, 20); | |
1202 | return buffer; | |
1203 | } | |
1204 | /* Handle references */ | |
1205 | else if (!strcmp(type, "commit")) | |
1206 | ref_type = "tree "; | |
1207 | else if (!strcmp(type, "tag")) | |
1208 | ref_type = "object "; | |
1209 | else { | |
1210 | free(buffer); | |
1211 | return NULL; | |
1212 | } | |
1213 | ref_length = strlen(ref_type); | |
f4913f91 | 1214 | |
40469ee9 JH |
1215 | if (memcmp(buffer, ref_type, ref_length) || |
1216 | get_sha1_hex(buffer + ref_length, actual_sha1)) { | |
1217 | free(buffer); | |
1218 | return NULL; | |
1219 | } | |
1cf58e72 | 1220 | free(buffer); |
40469ee9 JH |
1221 | /* Now we have the ID of the referred-to object in |
1222 | * actual_sha1. Check again. */ | |
f4913f91 | 1223 | } |
f4913f91 JH |
1224 | } |
1225 | ||
7672db20 BL |
1226 | char *write_sha1_file_prepare(void *buf, |
1227 | unsigned long len, | |
1228 | const char *type, | |
1229 | unsigned char *sha1, | |
1230 | unsigned char *hdr, | |
1231 | int *hdrlen) | |
d410c0f5 JH |
1232 | { |
1233 | SHA_CTX c; | |
1234 | ||
1235 | /* Generate the header */ | |
1236 | *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1; | |
1237 | ||
1238 | /* Sha1.. */ | |
1239 | SHA1_Init(&c); | |
1240 | SHA1_Update(&c, hdr, *hdrlen); | |
1241 | SHA1_Update(&c, buf, len); | |
1242 | SHA1_Final(sha1, &c); | |
1243 | ||
1244 | return sha1_file_name(sha1); | |
1245 | } | |
1246 | ||
bf0f910d | 1247 | int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1) |
0fcfd160 LT |
1248 | { |
1249 | int size; | |
bf0f910d | 1250 | unsigned char *compressed; |
0fcfd160 LT |
1251 | z_stream stream; |
1252 | unsigned char sha1[20]; | |
706bc531 | 1253 | char *filename; |
aac17941 | 1254 | static char tmpfile[PATH_MAX]; |
bf0f910d | 1255 | unsigned char hdr[50]; |
aac17941 | 1256 | int fd, hdrlen, ret; |
a44c9a5e | 1257 | |
d410c0f5 JH |
1258 | /* Normally if we have it in the pack then we do not bother writing |
1259 | * it out into .git/objects/??/?{38} file. | |
1260 | */ | |
1261 | filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen); | |
706bc531 LT |
1262 | if (returnsha1) |
1263 | memcpy(returnsha1, sha1, 20); | |
d410c0f5 JH |
1264 | if (has_sha1_file(sha1)) |
1265 | return 0; | |
aac17941 LT |
1266 | fd = open(filename, O_RDONLY); |
1267 | if (fd >= 0) { | |
706bc531 | 1268 | /* |
aac17941 LT |
1269 | * FIXME!!! We might do collision checking here, but we'd |
1270 | * need to uncompress the old file and check it. Later. | |
706bc531 | 1271 | */ |
aac17941 | 1272 | close(fd); |
706bc531 LT |
1273 | return 0; |
1274 | } | |
1275 | ||
aac17941 LT |
1276 | if (errno != ENOENT) { |
1277 | fprintf(stderr, "sha1 file %s: %s", filename, strerror(errno)); | |
1278 | return -1; | |
1279 | } | |
1280 | ||
1281 | snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory()); | |
ace1534d | 1282 | |
aac17941 LT |
1283 | fd = mkstemp(tmpfile); |
1284 | if (fd < 0) { | |
1285 | fprintf(stderr, "unable to create temporary sha1 filename %s: %s", tmpfile, strerror(errno)); | |
1286 | return -1; | |
1287 | } | |
1288 | ||
0fcfd160 LT |
1289 | /* Set it up */ |
1290 | memset(&stream, 0, sizeof(stream)); | |
1291 | deflateInit(&stream, Z_BEST_COMPRESSION); | |
a44c9a5e | 1292 | size = deflateBound(&stream, len+hdrlen); |
812666c8 | 1293 | compressed = xmalloc(size); |
0fcfd160 LT |
1294 | |
1295 | /* Compress it */ | |
0fcfd160 LT |
1296 | stream.next_out = compressed; |
1297 | stream.avail_out = size; | |
a44c9a5e LT |
1298 | |
1299 | /* First header.. */ | |
1300 | stream.next_in = hdr; | |
1301 | stream.avail_in = hdrlen; | |
1302 | while (deflate(&stream, 0) == Z_OK) | |
6ffcee88 | 1303 | /* nothing */; |
a44c9a5e LT |
1304 | |
1305 | /* Then the data itself.. */ | |
1306 | stream.next_in = buf; | |
1307 | stream.avail_in = len; | |
0fcfd160 LT |
1308 | while (deflate(&stream, Z_FINISH) == Z_OK) |
1309 | /* nothing */; | |
1310 | deflateEnd(&stream); | |
1311 | size = stream.total_out; | |
1312 | ||
706bc531 LT |
1313 | if (write(fd, compressed, size) != size) |
1314 | die("unable to write file"); | |
aac17941 | 1315 | fchmod(fd, 0444); |
706bc531 | 1316 | close(fd); |
383f85b7 | 1317 | free(compressed); |
0fcfd160 | 1318 | |
aac17941 | 1319 | ret = link(tmpfile, filename); |
a31c6d02 | 1320 | if (ret < 0) { |
aac17941 | 1321 | ret = errno; |
a31c6d02 LT |
1322 | |
1323 | /* | |
1324 | * Coda hack - coda doesn't like cross-directory links, | |
1325 | * so we fall back to a rename, which will mean that it | |
1326 | * won't be able to check collisions, but that's not a | |
1327 | * big deal. | |
1328 | * | |
1329 | * When this succeeds, we just return 0. We have nothing | |
1330 | * left to unlink. | |
1331 | */ | |
1332 | if (ret == EXDEV && !rename(tmpfile, filename)) | |
1333 | return 0; | |
1334 | } | |
aac17941 LT |
1335 | unlink(tmpfile); |
1336 | if (ret) { | |
1337 | if (ret != EEXIST) { | |
1338 | fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret)); | |
0fcfd160 | 1339 | return -1; |
aac17941 LT |
1340 | } |
1341 | /* FIXME!!! Collision check here ? */ | |
0fcfd160 | 1342 | } |
aac17941 | 1343 | |
0fcfd160 LT |
1344 | return 0; |
1345 | } | |
8237b185 | 1346 | |
a5eda52b DB |
1347 | int write_sha1_to_fd(int fd, const unsigned char *sha1) |
1348 | { | |
1349 | ssize_t size; | |
1350 | unsigned long objsize; | |
1351 | int posn = 0; | |
bfc66daf SV |
1352 | void *map = map_sha1_file_internal(sha1, &objsize); |
1353 | void *buf = map; | |
1354 | void *temp_obj = NULL; | |
a5eda52b | 1355 | z_stream stream; |
bfc66daf | 1356 | |
a5eda52b DB |
1357 | if (!buf) { |
1358 | unsigned char *unpacked; | |
1359 | unsigned long len; | |
1360 | char type[20]; | |
1361 | char hdr[50]; | |
1362 | int hdrlen; | |
1363 | // need to unpack and recompress it by itself | |
1364 | unpacked = read_packed_sha1(sha1, type, &len); | |
1365 | ||
1366 | hdrlen = sprintf(hdr, "%s %lu", type, len) + 1; | |
1367 | ||
1368 | /* Set it up */ | |
1369 | memset(&stream, 0, sizeof(stream)); | |
1370 | deflateInit(&stream, Z_BEST_COMPRESSION); | |
1371 | size = deflateBound(&stream, len + hdrlen); | |
bfc66daf | 1372 | temp_obj = buf = xmalloc(size); |
a5eda52b DB |
1373 | |
1374 | /* Compress it */ | |
1375 | stream.next_out = buf; | |
1376 | stream.avail_out = size; | |
1377 | ||
1378 | /* First header.. */ | |
0ee19dce | 1379 | stream.next_in = (void *)hdr; |
a5eda52b DB |
1380 | stream.avail_in = hdrlen; |
1381 | while (deflate(&stream, 0) == Z_OK) | |
1382 | /* nothing */; | |
1383 | ||
1384 | /* Then the data itself.. */ | |
1385 | stream.next_in = unpacked; | |
1386 | stream.avail_in = len; | |
1387 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
1388 | /* nothing */; | |
1389 | deflateEnd(&stream); | |
bfc66daf | 1390 | free(unpacked); |
a5eda52b DB |
1391 | |
1392 | objsize = stream.total_out; | |
1393 | } | |
1394 | ||
1395 | do { | |
1396 | size = write(fd, buf + posn, objsize - posn); | |
1397 | if (size <= 0) { | |
1398 | if (!size) { | |
1399 | fprintf(stderr, "write closed"); | |
1400 | } else { | |
1401 | perror("write "); | |
1402 | } | |
1403 | return -1; | |
1404 | } | |
1405 | posn += size; | |
1406 | } while (posn < objsize); | |
bfc66daf SV |
1407 | |
1408 | if (map) | |
1409 | munmap(map, objsize); | |
1410 | if (temp_obj) | |
1411 | free(temp_obj); | |
1412 | ||
a5eda52b DB |
1413 | return 0; |
1414 | } | |
1415 | ||
70b9829e DB |
1416 | int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer, |
1417 | size_t bufsize, size_t *bufposn) | |
8237b185 DB |
1418 | { |
1419 | char *filename = sha1_file_name(sha1); | |
1420 | ||
1421 | int local; | |
1422 | z_stream stream; | |
1423 | unsigned char real_sha1[20]; | |
bf0f910d | 1424 | unsigned char discard[4096]; |
8237b185 DB |
1425 | int ret; |
1426 | SHA_CTX c; | |
1427 | ||
1428 | local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666); | |
1429 | ||
1430 | if (local < 0) | |
1431 | return error("Couldn't open %s\n", filename); | |
1432 | ||
1433 | memset(&stream, 0, sizeof(stream)); | |
1434 | ||
1435 | inflateInit(&stream); | |
1436 | ||
1437 | SHA1_Init(&c); | |
1438 | ||
1439 | do { | |
1440 | ssize_t size; | |
70b9829e DB |
1441 | if (*bufposn) { |
1442 | stream.avail_in = *bufposn; | |
96ad15ae | 1443 | stream.next_in = (unsigned char *) buffer; |
70b9829e DB |
1444 | do { |
1445 | stream.next_out = discard; | |
1446 | stream.avail_out = sizeof(discard); | |
1447 | ret = inflate(&stream, Z_SYNC_FLUSH); | |
1448 | SHA1_Update(&c, discard, sizeof(discard) - | |
1449 | stream.avail_out); | |
1450 | } while (stream.avail_in && ret == Z_OK); | |
1451 | write(local, buffer, *bufposn - stream.avail_in); | |
1452 | memmove(buffer, buffer + *bufposn - stream.avail_in, | |
1453 | stream.avail_in); | |
1454 | *bufposn = stream.avail_in; | |
1455 | if (ret != Z_OK) | |
1456 | break; | |
1457 | } | |
1458 | size = read(fd, buffer + *bufposn, bufsize - *bufposn); | |
8237b185 DB |
1459 | if (size <= 0) { |
1460 | close(local); | |
1461 | unlink(filename); | |
1462 | if (!size) | |
1463 | return error("Connection closed?"); | |
1464 | perror("Reading from connection"); | |
1465 | return -1; | |
1466 | } | |
70b9829e DB |
1467 | *bufposn += size; |
1468 | } while (1); | |
8237b185 DB |
1469 | inflateEnd(&stream); |
1470 | ||
1471 | close(local); | |
1472 | SHA1_Final(real_sha1, &c); | |
1473 | if (ret != Z_STREAM_END) { | |
1474 | unlink(filename); | |
1475 | return error("File %s corrupted", sha1_to_hex(sha1)); | |
1476 | } | |
1477 | if (memcmp(sha1, real_sha1, 20)) { | |
1478 | unlink(filename); | |
1479 | return error("File %s has bad hash\n", sha1_to_hex(sha1)); | |
1480 | } | |
1481 | ||
1482 | return 0; | |
1483 | } | |
1484 | ||
bf592c50 DB |
1485 | int has_pack_index(const unsigned char *sha1) |
1486 | { | |
1487 | struct stat st; | |
1488 | if (stat(sha1_pack_index_name(sha1), &st)) | |
1489 | return 0; | |
1490 | return 1; | |
1491 | } | |
1492 | ||
1493 | int has_pack_file(const unsigned char *sha1) | |
1494 | { | |
1495 | struct stat st; | |
1496 | if (stat(sha1_pack_name(sha1), &st)) | |
1497 | return 0; | |
1498 | return 1; | |
1499 | } | |
1500 | ||
dade09c2 LT |
1501 | int has_sha1_pack(const unsigned char *sha1) |
1502 | { | |
1503 | struct pack_entry e; | |
1504 | return find_pack_entry(sha1, &e); | |
1505 | } | |
1506 | ||
8237b185 DB |
1507 | int has_sha1_file(const unsigned char *sha1) |
1508 | { | |
8237b185 | 1509 | struct stat st; |
1f688557 JH |
1510 | struct pack_entry e; |
1511 | ||
ab90ea5d | 1512 | if (find_pack_entry(sha1, &e)) |
1f688557 | 1513 | return 1; |
ab90ea5d | 1514 | return find_sha1_file(sha1, &st) ? 1 : 0; |
8237b185 | 1515 | } |
74400e71 | 1516 | |
7672db20 | 1517 | int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type) |
74400e71 | 1518 | { |
74400e71 | 1519 | unsigned long size = st->st_size; |
aac17941 LT |
1520 | void *buf; |
1521 | int ret; | |
7672db20 BL |
1522 | unsigned char hdr[50]; |
1523 | int hdrlen; | |
74400e71 | 1524 | |
aac17941 | 1525 | buf = ""; |
74400e71 | 1526 | if (size) |
aac17941 | 1527 | buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
74400e71 | 1528 | close(fd); |
e35f9824 | 1529 | if (buf == MAP_FAILED) |
74400e71 JH |
1530 | return -1; |
1531 | ||
7672db20 BL |
1532 | if (!type) |
1533 | type = "blob"; | |
1534 | if (write_object) | |
1535 | ret = write_sha1_file(buf, size, type, sha1); | |
1536 | else { | |
1537 | write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen); | |
1538 | ret = 0; | |
1539 | } | |
aac17941 LT |
1540 | if (size) |
1541 | munmap(buf, size); | |
1542 | return ret; | |
74400e71 | 1543 | } |