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