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