]> git.ipfire.org Git - thirdparty/git.git/blame - sha1_file.c
Fix broken sha1 locking
[thirdparty/git.git] / sha1_file.c
CommitLineData
0fcfd160
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This handles basic git sha1 object files - packing, unpacking,
7 * creation etc.
8 */
0fcfd160 9#include "cache.h"
1f688557 10#include "delta.h"
a733cb60 11#include "pack.h"
8e440259
PE
12#include "blob.h"
13#include "commit.h"
14#include "tag.h"
15#include "tree.h"
0fcfd160 16
144bde78
LT
17#ifndef O_NOATIME
18#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
19#define O_NOATIME 01000000
20#else
21#define O_NOATIME 0
22#endif
23#endif
24
96f1e58f 25const unsigned char null_sha1[20];
88cd621d 26
144bde78
LT
27static unsigned int sha1_file_open_flag = O_NOATIME;
28
a41fae9c 29static inline unsigned int hexval(unsigned int c)
0fcfd160 30{
a41fae9c
JH
31 static signed char val[256] = {
32 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
33 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
34 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
35 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
36 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
37 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
38 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
39 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
40 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
41 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
42 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
43 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
44 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
45 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
46 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
47 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
48 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
49 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
50 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
51 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
52 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
53 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
54 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
55 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
56 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
57 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
58 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
59 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
60 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
61 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
62 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
63 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
64 };
65 return val[c];
0fcfd160
LT
66}
67
68int get_sha1_hex(const char *hex, unsigned char *sha1)
69{
70 int i;
71 for (i = 0; i < 20; i++) {
72 unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
73 if (val & ~0xff)
74 return -1;
75 *sha1++ = val;
76 hex += 2;
77 }
78 return 0;
79}
80
b2cb9425
JH
81int safe_create_leading_directories(char *path)
82{
83 char *pos = path;
67d42212
JR
84 struct stat st;
85
67ffdf4c
JS
86 if (*pos == '/')
87 pos++;
b2cb9425
JH
88
89 while (pos) {
90 pos = strchr(pos, '/');
91 if (!pos)
92 break;
93 *pos = 0;
67d42212
JR
94 if (!stat(path, &st)) {
95 /* path exists */
96 if (!S_ISDIR(st.st_mode)) {
b2cb9425 97 *pos = '/';
67d42212 98 return -3;
b2cb9425 99 }
457f06d6 100 }
67d42212
JR
101 else if (mkdir(path, 0777)) {
102 *pos = '/';
103 return -1;
104 }
457f06d6
JS
105 else if (adjust_shared_perm(path)) {
106 *pos = '/';
107 return -2;
108 }
b2cb9425
JH
109 *pos++ = '/';
110 }
111 return 0;
112}
723c31fe 113
0fcfd160
LT
114char * sha1_to_hex(const unsigned char *sha1)
115{
dcb3450f
LT
116 static int bufno;
117 static char hexbuffer[4][50];
0fcfd160 118 static const char hex[] = "0123456789abcdef";
dcb3450f 119 char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
0fcfd160
LT
120 int i;
121
122 for (i = 0; i < 20; i++) {
123 unsigned int val = *sha1++;
124 *buf++ = hex[val >> 4];
125 *buf++ = hex[val & 0xf];
126 }
1e80e044
JS
127 *buf = '\0';
128
0fcfd160
LT
129 return buffer;
130}
131
ace1534d
JH
132static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
133{
134 int i;
135 for (i = 0; i < 20; i++) {
136 static char hex[] = "0123456789abcdef";
137 unsigned int val = sha1[i];
138 char *pos = pathbuf + i*2 + (i > 0);
139 *pos++ = hex[val >> 4];
140 *pos = hex[val & 0xf];
141 }
142}
143
0fcfd160
LT
144/*
145 * NOTE! This returns a statically allocated buffer, so you have to be
9befac47 146 * careful about using it. Do a "xstrdup()" if you need to save the
0fcfd160 147 * filename.
ace1534d
JH
148 *
149 * Also note that this returns the location for creating. Reading
150 * SHA1 file can happen from any alternate directory listed in the
d19938ab 151 * DB_ENVIRONMENT environment variable if it is not found in
ace1534d 152 * the primary object database.
0fcfd160
LT
153 */
154char *sha1_file_name(const unsigned char *sha1)
155{
0fcfd160
LT
156 static char *name, *base;
157
158 if (!base) {
d19938ab 159 const char *sha1_file_directory = get_object_directory();
0fcfd160 160 int len = strlen(sha1_file_directory);
812666c8 161 base = xmalloc(len + 60);
0fcfd160
LT
162 memcpy(base, sha1_file_directory, len);
163 memset(base+len, 0, 60);
164 base[len] = '/';
165 base[len+3] = '/';
166 name = base + len + 1;
167 }
ace1534d 168 fill_sha1_path(name, sha1);
0fcfd160
LT
169 return base;
170}
171
bf592c50
DB
172char *sha1_pack_name(const unsigned char *sha1)
173{
174 static const char hex[] = "0123456789abcdef";
175 static char *name, *base, *buf;
176 int i;
177
178 if (!base) {
179 const char *sha1_file_directory = get_object_directory();
180 int len = strlen(sha1_file_directory);
181 base = xmalloc(len + 60);
182 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
183 name = base + len + 11;
184 }
185
186 buf = name;
187
188 for (i = 0; i < 20; i++) {
189 unsigned int val = *sha1++;
190 *buf++ = hex[val >> 4];
191 *buf++ = hex[val & 0xf];
192 }
193
194 return base;
195}
196
197char *sha1_pack_index_name(const unsigned char *sha1)
198{
199 static const char hex[] = "0123456789abcdef";
200 static char *name, *base, *buf;
201 int i;
202
203 if (!base) {
204 const char *sha1_file_directory = get_object_directory();
205 int len = strlen(sha1_file_directory);
206 base = xmalloc(len + 60);
207 sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
208 name = base + len + 11;
209 }
210
211 buf = name;
212
213 for (i = 0; i < 20; i++) {
214 unsigned int val = *sha1++;
215 *buf++ = hex[val >> 4];
216 *buf++ = hex[val & 0xf];
217 }
218
219 return base;
220}
221
d5a63b99
JH
222struct alternate_object_database *alt_odb_list;
223static struct alternate_object_database **alt_odb_tail;
ace1534d 224
c2f493a4
MW
225static void read_info_alternates(const char * alternates, int depth);
226
ddd5d056
JH
227/*
228 * Prepare alternate object database registry.
d5a63b99
JH
229 *
230 * The variable alt_odb_list points at the list of struct
231 * alternate_object_database. The elements on this list come from
232 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
233 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
1494e038
JH
234 * whose contents is similar to that environment variable but can be
235 * LF separated. Its base points at a statically allocated buffer that
d5a63b99
JH
236 * contains "/the/directory/corresponding/to/.git/objects/...", while
237 * its name points just after the slash at the end of ".git/objects/"
238 * in the example above, and has enough space to hold 40-byte hex
239 * SHA1, an extra slash for the first level indirection, and the
240 * terminating NUL.
ddd5d056 241 */
c2f493a4 242static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
ace1534d 243{
c2f493a4 244 struct stat st;
1494e038 245 const char *objdir = get_object_directory();
c2f493a4
MW
246 struct alternate_object_database *ent;
247 struct alternate_object_database *alt;
248 /* 43 = 40-byte + 2 '/' + terminating NUL */
249 int pfxlen = len;
250 int entlen = pfxlen + 43;
ccfd3e99 251 int base_len = -1;
d5a63b99 252
c2f493a4
MW
253 if (*entry != '/' && relative_base) {
254 /* Relative alt-odb */
255 if (base_len < 0)
256 base_len = strlen(relative_base) + 1;
257 entlen += base_len;
258 pfxlen += base_len;
259 }
260 ent = xmalloc(sizeof(*ent) + entlen);
261
262 if (*entry != '/' && relative_base) {
263 memcpy(ent->base, relative_base, base_len - 1);
264 ent->base[base_len - 1] = '/';
265 memcpy(ent->base + base_len, entry, len);
266 }
267 else
268 memcpy(ent->base, entry, pfxlen);
269
270 ent->name = ent->base + pfxlen + 1;
271 ent->base[pfxlen + 3] = '/';
272 ent->base[pfxlen] = ent->base[entlen-1] = 0;
273
274 /* Detect cases where alternate disappeared */
275 if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
276 error("object directory %s does not exist; "
277 "check .git/objects/info/alternates.",
278 ent->base);
279 free(ent);
280 return -1;
281 }
282
283 /* Prevent the common mistake of listing the same
284 * thing twice, or object directory itself.
285 */
286 for (alt = alt_odb_list; alt; alt = alt->next) {
287 if (!memcmp(ent->base, alt->base, pfxlen)) {
288 free(ent);
289 return -1;
290 }
291 }
292 if (!memcmp(ent->base, objdir, pfxlen)) {
293 free(ent);
294 return -1;
295 }
296
297 /* add the alternate entry */
298 *alt_odb_tail = ent;
299 alt_odb_tail = &(ent->next);
300 ent->next = NULL;
301
302 /* recursively add alternates */
303 read_info_alternates(ent->base, depth + 1);
304
305 ent->base[pfxlen] = '/';
306
307 return 0;
308}
309
310static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
311 const char *relative_base, int depth)
312{
313 const char *cp, *last;
314
315 if (depth > 5) {
316 error("%s: ignoring alternate object stores, nesting too deep.",
317 relative_base);
318 return;
319 }
320
d5a63b99 321 last = alt;
9577e7e3
JH
322 while (last < ep) {
323 cp = last;
324 if (cp < ep && *cp == '#') {
325 while (cp < ep && *cp != sep)
326 cp++;
327 last = cp + 1;
328 continue;
329 }
c2f493a4
MW
330 while (cp < ep && *cp != sep)
331 cp++;
d5a63b99 332 if (last != cp) {
c2f493a4
MW
333 if ((*last != '/') && depth) {
334 error("%s: ignoring relative alternate object store %s",
335 relative_base, last);
336 } else {
337 link_alt_odb_entry(last, cp - last,
338 relative_base, depth);
1494e038 339 }
d5a63b99 340 }
9577e7e3 341 while (cp < ep && *cp == sep)
d5a63b99
JH
342 cp++;
343 last = cp;
9577e7e3 344 }
d5a63b99
JH
345}
346
c2f493a4 347static void read_info_alternates(const char * relative_base, int depth)
d5a63b99 348{
9577e7e3 349 char *map;
d5a63b99 350 struct stat st;
c2f493a4
MW
351 char path[PATH_MAX];
352 int fd;
d5a63b99 353
c2f493a4 354 sprintf(path, "%s/info/alternates", relative_base);
d5a63b99
JH
355 fd = open(path, O_RDONLY);
356 if (fd < 0)
357 return;
358 if (fstat(fd, &st) || (st.st_size == 0)) {
359 close(fd);
9a217f2a 360 return;
ace1534d 361 }
d5a63b99
JH
362 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
363 close(fd);
364 if (map == MAP_FAILED)
365 return;
366
c2f493a4
MW
367 link_alt_odb_entries(map, map + st.st_size, '\n', relative_base, depth);
368
d5a63b99 369 munmap(map, st.st_size);
ace1534d
JH
370}
371
c2f493a4
MW
372void prepare_alt_odb(void)
373{
554fe20d 374 const char *alt;
c2f493a4
MW
375
376 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
377 if (!alt) alt = "";
378
379 if (alt_odb_tail)
380 return;
381 alt_odb_tail = &alt_odb_list;
382 link_alt_odb_entries(alt, alt + strlen(alt), ':', NULL, 0);
383
384 read_info_alternates(get_object_directory(), 0);
385}
386
ace1534d
JH
387static char *find_sha1_file(const unsigned char *sha1, struct stat *st)
388{
ace1534d 389 char *name = sha1_file_name(sha1);
d5a63b99 390 struct alternate_object_database *alt;
ace1534d
JH
391
392 if (!stat(name, st))
393 return name;
9a217f2a 394 prepare_alt_odb();
d5a63b99
JH
395 for (alt = alt_odb_list; alt; alt = alt->next) {
396 name = alt->name;
ace1534d 397 fill_sha1_path(name, sha1);
d5a63b99
JH
398 if (!stat(alt->base, st))
399 return alt->base;
ace1534d
JH
400 }
401 return NULL;
402}
403
1f688557
JH
404#define PACK_MAX_SZ (1<<26)
405static int pack_used_ctr;
406static unsigned long pack_mapped;
9a217f2a 407struct packed_git *packed_git;
1f688557 408
1f688557
JH
409static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
410 void **idx_map_)
411{
412 void *idx_map;
413 unsigned int *index;
414 unsigned long idx_size;
415 int nr, i;
e93ec6f9 416 int fd = open(path, O_RDONLY);
1f688557
JH
417 struct stat st;
418 if (fd < 0)
419 return -1;
420 if (fstat(fd, &st)) {
421 close(fd);
422 return -1;
423 }
424 idx_size = st.st_size;
425 idx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
426 close(fd);
427 if (idx_map == MAP_FAILED)
428 return -1;
429
430 index = idx_map;
4d235c80
LT
431 *idx_map_ = idx_map;
432 *idx_size_ = idx_size;
1f688557
JH
433
434 /* check index map */
f9253394 435 if (idx_size < 4*256 + 20 + 20)
1f688557
JH
436 return error("index file too small");
437 nr = 0;
438 for (i = 0; i < 256; i++) {
439 unsigned int n = ntohl(index[i]);
440 if (n < nr)
441 return error("non-monotonic index");
442 nr = n;
443 }
444
445 /*
446 * Total size:
447 * - 256 index entries 4 bytes each
448 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
449 * - 20-byte SHA1 of the packfile
450 * - 20-byte SHA1 file checksum
451 */
452 if (idx_size != 4*256 + nr * 24 + 20 + 20)
453 return error("wrong index file size");
454
1f688557
JH
455 return 0;
456}
457
f9253394 458static int unuse_one_packed_git(void)
1f688557 459{
f9253394
JH
460 struct packed_git *p, *lru = NULL;
461
462 for (p = packed_git; p; p = p->next) {
463 if (p->pack_use_cnt || !p->pack_base)
464 continue;
465 if (!lru || p->pack_last_used < lru->pack_last_used)
466 lru = p;
467 }
468 if (!lru)
469 return 0;
470 munmap(lru->pack_base, lru->pack_size);
471 lru->pack_base = NULL;
472 return 1;
473}
474
475void unuse_packed_git(struct packed_git *p)
476{
477 p->pack_use_cnt--;
1f688557
JH
478}
479
f9253394 480int use_packed_git(struct packed_git *p)
1f688557 481{
bf592c50
DB
482 if (!p->pack_size) {
483 struct stat st;
a9486b02 484 /* We created the struct before we had the pack */
bf592c50
DB
485 stat(p->pack_name, &st);
486 if (!S_ISREG(st.st_mode))
487 die("packfile %s not a regular file", p->pack_name);
488 p->pack_size = st.st_size;
489 }
1f688557
JH
490 if (!p->pack_base) {
491 int fd;
492 struct stat st;
493 void *map;
da756011 494 struct pack_header *hdr;
1f688557
JH
495
496 pack_mapped += p->pack_size;
f9253394
JH
497 while (PACK_MAX_SZ < pack_mapped && unuse_one_packed_git())
498 ; /* nothing */
1f688557
JH
499 fd = open(p->pack_name, O_RDONLY);
500 if (fd < 0)
f9253394 501 die("packfile %s cannot be opened", p->pack_name);
1f688557
JH
502 if (fstat(fd, &st)) {
503 close(fd);
f9253394 504 die("packfile %s cannot be opened", p->pack_name);
1f688557
JH
505 }
506 if (st.st_size != p->pack_size)
f9253394 507 die("packfile %s size mismatch.", p->pack_name);
1f688557
JH
508 map = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, fd, 0);
509 close(fd);
510 if (map == MAP_FAILED)
f9253394 511 die("packfile %s cannot be mapped.", p->pack_name);
1f688557 512 p->pack_base = map;
f9253394 513
da756011
SP
514 /* Check if we understand this pack file. If we don't we're
515 * likely too old to handle it.
516 */
517 hdr = map;
518 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
519 die("packfile %s isn't actually a pack.", p->pack_name);
520 if (!pack_version_ok(hdr->hdr_version))
521 die("packfile %s is version %i and not supported"
522 " (try upgrading GIT to a newer version)",
523 p->pack_name, ntohl(hdr->hdr_version));
524
f9253394
JH
525 /* Check if the pack file matches with the index file.
526 * this is cheap.
527 */
a89fccd2
DR
528 if (hashcmp((unsigned char *)(p->index_base) +
529 p->index_size - 40,
530 (unsigned char *)p->pack_base +
531 p->pack_size - 20)) {
f9253394 532 die("packfile %s does not match index.", p->pack_name);
bf592c50 533 }
1f688557
JH
534 }
535 p->pack_last_used = pack_used_ctr++;
f9253394 536 p->pack_use_cnt++;
1f688557
JH
537 return 0;
538}
539
9d835df2 540struct packed_git *add_packed_git(char *path, int path_len, int local)
1f688557
JH
541{
542 struct stat st;
543 struct packed_git *p;
544 unsigned long idx_size;
545 void *idx_map;
c0bbbb1b 546 unsigned char sha1[20];
1f688557
JH
547
548 if (check_packed_git_idx(path, &idx_size, &idx_map))
549 return NULL;
550
551 /* do we have a corresponding .pack file? */
552 strcpy(path + path_len - 4, ".pack");
553 if (stat(path, &st) || !S_ISREG(st.st_mode)) {
554 munmap(idx_map, idx_size);
555 return NULL;
556 }
557 /* ok, it looks sane as far as we can check without
558 * actually mapping the pack file.
559 */
560 p = xmalloc(sizeof(*p) + path_len + 2);
561 strcpy(p->pack_name, path);
562 p->index_size = idx_size;
563 p->pack_size = st.st_size;
564 p->index_base = idx_map;
565 p->next = NULL;
d85a4fee 566 p->pack_base = NULL;
1f688557 567 p->pack_last_used = 0;
f9253394 568 p->pack_use_cnt = 0;
9d835df2 569 p->pack_local = local;
f4a11066 570 if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1))
e702496e 571 hashcpy(p->sha1, sha1);
1f688557
JH
572 return p;
573}
574
bf592c50 575struct packed_git *parse_pack_index(unsigned char *sha1)
c508df5e
DB
576{
577 char *path = sha1_pack_index_name(sha1);
578 return parse_pack_index_file(sha1, path);
579}
580
2ab141a2 581struct packed_git *parse_pack_index_file(const unsigned char *sha1, char *idx_path)
bf592c50
DB
582{
583 struct packed_git *p;
584 unsigned long idx_size;
585 void *idx_map;
c508df5e 586 char *path;
bf592c50 587
c508df5e 588 if (check_packed_git_idx(idx_path, &idx_size, &idx_map))
bf592c50
DB
589 return NULL;
590
591 path = sha1_pack_name(sha1);
592
593 p = xmalloc(sizeof(*p) + strlen(path) + 2);
594 strcpy(p->pack_name, path);
595 p->index_size = idx_size;
596 p->pack_size = 0;
597 p->index_base = idx_map;
598 p->next = NULL;
599 p->pack_base = NULL;
600 p->pack_last_used = 0;
601 p->pack_use_cnt = 0;
e702496e 602 hashcpy(p->sha1, sha1);
bf592c50
DB
603 return p;
604}
605
606void install_packed_git(struct packed_git *pack)
607{
608 pack->next = packed_git;
609 packed_git = pack;
610}
611
9d835df2 612static void prepare_packed_git_one(char *objdir, int local)
1f688557
JH
613{
614 char path[PATH_MAX];
615 int len;
616 DIR *dir;
617 struct dirent *de;
618
619 sprintf(path, "%s/pack", objdir);
620 len = strlen(path);
621 dir = opendir(path);
b5b16990 622 if (!dir) {
26125f6b 623 if (errno != ENOENT)
bd2afde8 624 error("unable to open object pack directory: %s: %s",
26125f6b 625 path, strerror(errno));
1f688557 626 return;
b5b16990 627 }
1f688557
JH
628 path[len++] = '/';
629 while ((de = readdir(dir)) != NULL) {
630 int namelen = strlen(de->d_name);
631 struct packed_git *p;
632
5bb1cda5 633 if (!has_extension(de->d_name, ".idx"))
1f688557
JH
634 continue;
635
636 /* we have .idx. Is it a file we can map? */
637 strcpy(path + len, de->d_name);
86f7780c
JK
638 for (p = packed_git; p; p = p->next) {
639 if (!memcmp(path, p->pack_name, len + namelen - 4))
640 break;
641 }
642 if (p)
643 continue;
9d835df2 644 p = add_packed_git(path, len + namelen, local);
1f688557
JH
645 if (!p)
646 continue;
647 p->next = packed_git;
648 packed_git = p;
649 }
5b35bcd5 650 closedir(dir);
1f688557
JH
651}
652
637cdd9d 653static int prepare_packed_git_run_once = 0;
9a217f2a 654void prepare_packed_git(void)
1f688557 655{
d5a63b99 656 struct alternate_object_database *alt;
1f688557 657
637cdd9d 658 if (prepare_packed_git_run_once)
1f688557 659 return;
9d835df2 660 prepare_packed_git_one(get_object_directory(), 1);
9a217f2a 661 prepare_alt_odb();
d5a63b99 662 for (alt = alt_odb_list; alt; alt = alt->next) {
1494e038 663 alt->name[-1] = 0;
9d835df2 664 prepare_packed_git_one(alt->base, 0);
1494e038 665 alt->name[-1] = '/';
1f688557 666 }
637cdd9d
JK
667 prepare_packed_git_run_once = 1;
668}
669
670static void reprepare_packed_git(void)
671{
672 prepare_packed_git_run_once = 0;
673 prepare_packed_git();
1f688557
JH
674}
675
5d6ccf5c 676int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)
0fcfd160 677{
d98b46f8 678 char header[100];
0fcfd160
LT
679 unsigned char real_sha1[20];
680 SHA_CTX c;
681
682 SHA1_Init(&c);
d98b46f8 683 SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
0fcfd160
LT
684 SHA1_Update(&c, map, size);
685 SHA1_Final(real_sha1, &c);
a89fccd2 686 return hashcmp(sha1, real_sha1) ? -1 : 0;
0fcfd160
LT
687}
688
bb6b8e4f 689void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
0fcfd160 690{
0fcfd160
LT
691 struct stat st;
692 void *map;
144bde78 693 int fd;
ace1534d
JH
694 char *filename = find_sha1_file(sha1, &st);
695
696 if (!filename) {
ace1534d
JH
697 return NULL;
698 }
0fcfd160 699
144bde78 700 fd = open(filename, O_RDONLY | sha1_file_open_flag);
0fcfd160 701 if (fd < 0) {
144bde78
LT
702 /* See if it works without O_NOATIME */
703 switch (sha1_file_open_flag) {
704 default:
705 fd = open(filename, O_RDONLY);
706 if (fd >= 0)
707 break;
708 /* Fallthrough */
709 case 0:
144bde78
LT
710 return NULL;
711 }
712
1f688557
JH
713 /* If it failed once, it will probably fail again.
714 * Stop using O_NOATIME
715 */
144bde78 716 sha1_file_open_flag = 0;
0fcfd160 717 }
0fcfd160
LT
718 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
719 close(fd);
e35f9824 720 if (map == MAP_FAILED)
0fcfd160
LT
721 return NULL;
722 *size = st.st_size;
723 return map;
724}
725
bb6b8e4f
JH
726int legacy_loose_object(unsigned char *map)
727{
728 unsigned int word;
729
730 /*
731 * Is it a zlib-compressed buffer? If so, the first byte
732 * must be 0x78 (15-bit window size, deflated), and the
733 * first 16-bit word is evenly divisible by 31
734 */
735 word = (map[0] << 8) + map[1];
736 if (map[0] == 0x78 && !(word % 31))
737 return 1;
738 else
739 return 0;
740}
741
72518e9c 742unsigned long unpack_object_header_gently(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep)
c4483576 743{
ad1ed5ee 744 unsigned shift;
93821bd9 745 unsigned char c;
93821bd9 746 unsigned long size;
ad1ed5ee
JH
747 unsigned long used = 0;
748
749 c = buf[used++];
750 *type = (c >> 4) & 7;
751 size = c & 15;
752 shift = 4;
753 while (c & 0x80) {
754 if (len <= used)
755 return 0;
756 if (sizeof(long) * 8 <= shift)
757 return 0;
758 c = buf[used++];
759 size += (c & 0x7f) << shift;
760 shift += 7;
761 }
762 *sizep = size;
763 return used;
764}
765
766static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
767{
768 unsigned long size, used;
769 static const char valid_loose_object_type[8] = {
770 0, /* OBJ_EXT */
771 1, 1, 1, 1, /* "commit", "tree", "blob", "tag" */
772 0, /* "delta" and others are invalid in a loose object */
93821bd9 773 };
ad1ed5ee 774 enum object_type type;
93821bd9 775
c4483576
LT
776 /* Get the data stream */
777 memset(stream, 0, sizeof(*stream));
778 stream->next_in = map;
779 stream->avail_in = mapsize;
780 stream->next_out = buffer;
93821bd9
LT
781 stream->avail_out = bufsiz;
782
bb6b8e4f 783 if (legacy_loose_object(map)) {
93821bd9
LT
784 inflateInit(stream);
785 return inflate(stream, 0);
786 }
787
ad1ed5ee
JH
788 used = unpack_object_header_gently(map, mapsize, &type, &size);
789 if (!used || !valid_loose_object_type[type])
93821bd9 790 return -1;
ad1ed5ee
JH
791 map += used;
792 mapsize -= used;
93821bd9
LT
793
794 /* Set up the stream for the rest.. */
795 stream->next_in = map;
796 stream->avail_in = mapsize;
c4483576 797 inflateInit(stream);
93821bd9
LT
798
799 /* And generate the fake traditional header */
ad1ed5ee
JH
800 stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu",
801 type_names[type], size);
93821bd9 802 return 0;
c4483576
LT
803}
804
6da4016a 805static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
5180cacc
LT
806{
807 int bytes = strlen(buffer) + 1;
d565b341 808 unsigned char *buf = xmalloc(1+size);
93821bd9 809 unsigned long n;
5180cacc 810
93821bd9
LT
811 n = stream->total_out - bytes;
812 if (n > size)
813 n = size;
814 memcpy(buf, (char *) buffer + bytes, n);
815 bytes = n;
5180cacc
LT
816 if (bytes < size) {
817 stream->next_out = buf + bytes;
818 stream->avail_out = size - bytes;
819 while (inflate(stream, Z_FINISH) == Z_OK)
820 /* nothing */;
821 }
822 buf[size] = 0;
823 inflateEnd(stream);
824 return buf;
825}
826
827/*
828 * We used to just use "sscanf()", but that's actually way
829 * too permissive for what we want to check. So do an anal
830 * object header parse by hand.
831 */
38d3874d 832static int parse_sha1_header(char *hdr, char *type, unsigned long *sizep)
5180cacc
LT
833{
834 int i;
835 unsigned long size;
836
837 /*
838 * The type can be at most ten bytes (including the
839 * terminating '\0' that we add), and is followed by
840 * a space.
841 */
842 i = 10;
843 for (;;) {
844 char c = *hdr++;
845 if (c == ' ')
846 break;
847 if (!--i)
848 return -1;
849 *type++ = c;
850 }
851 *type = 0;
852
853 /*
854 * The length must follow immediately, and be in canonical
855 * decimal format (ie "010" is not valid).
856 */
857 size = *hdr++ - '0';
858 if (size > 9)
859 return -1;
860 if (size) {
861 for (;;) {
862 unsigned long c = *hdr - '0';
863 if (c > 9)
864 break;
865 hdr++;
866 size = size * 10 + c;
867 }
868 }
869 *sizep = size;
870
871 /*
872 * The length must be followed by a zero byte
873 */
874 return *hdr ? -1 : 0;
875}
876
0fcfd160
LT
877void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
878{
5180cacc 879 int ret;
0fcfd160 880 z_stream stream;
5180cacc 881 char hdr[8192];
0fcfd160 882
5180cacc
LT
883 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
884 if (ret < Z_OK || parse_sha1_header(hdr, type, size) < 0)
0fcfd160
LT
885 return NULL;
886
5180cacc 887 return unpack_sha1_rest(&stream, hdr, *size);
0fcfd160
LT
888}
889
f3bf9224
JH
890/* forward declaration for a mutually recursive function */
891static int packed_object_info(struct pack_entry *entry,
892 char *type, unsigned long *sizep);
893
5db47c2b
JH
894static int packed_delta_info(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)
5db47c2b 900{
f3bf9224
JH
901 struct pack_entry base_ent;
902
5db47c2b
JH
903 if (left < 20)
904 die("truncated pack file");
5db47c2b 905
f3bf9224
JH
906 /* The base entry _must_ be in the same pack */
907 if (!find_pack_entry_one(base_sha1, &base_ent, p))
908 die("failed to find delta-pack base object %s",
909 sha1_to_hex(base_sha1));
910
c62266f3
JH
911 /* We choose to only get the type of the base object and
912 * ignore potentially corrupt pack file that expects the delta
913 * based on a base with a wrong size. This saves tons of
914 * inflate() calls.
915 */
5db47c2b 916
f3bf9224 917 if (packed_object_info(&base_ent, type, NULL))
c62266f3 918 die("cannot get info for delta-pack base");
5db47c2b 919
c62266f3
JH
920 if (sizep) {
921 const unsigned char *data;
922 unsigned char delta_head[64];
923 unsigned long result_size;
924 z_stream stream;
925 int st;
926
927 memset(&stream, 0, sizeof(stream));
928
929 data = stream.next_in = base_sha1 + 20;
930 stream.avail_in = left - 20;
931 stream.next_out = delta_head;
932 stream.avail_out = sizeof(delta_head);
933
934 inflateInit(&stream);
935 st = inflate(&stream, Z_FINISH);
936 inflateEnd(&stream);
937 if ((st != Z_STREAM_END) &&
938 stream.total_out != sizeof(delta_head))
939 die("delta data unpack-initial failed");
940
941 /* Examine the initial part of the delta to figure out
942 * the result size.
943 */
944 data = delta_head;
8960844a
NP
945
946 /* ignore base size */
947 get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
5db47c2b 948
c62266f3 949 /* Read the result size */
8960844a 950 result_size = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
c62266f3
JH
951 *sizep = result_size;
952 }
5db47c2b
JH
953 return 0;
954}
955
a733cb60
LT
956static unsigned long unpack_object_header(struct packed_git *p, unsigned long offset,
957 enum object_type *type, unsigned long *sizep)
958{
ad1ed5ee 959 unsigned long used;
a733cb60 960
ad1ed5ee 961 if (p->pack_size <= offset)
a733cb60 962 die("object offset outside of pack file");
ad1ed5ee
JH
963
964 used = unpack_object_header_gently((unsigned char *)p->pack_base +
965 offset,
966 p->pack_size - offset, type, sizep);
967 if (!used)
968 die("object offset outside of pack file");
969
970 return offset + used;
a733cb60
LT
971}
972
a49dd05f
JH
973int check_reuse_pack_delta(struct packed_git *p, unsigned long offset,
974 unsigned char *base, unsigned long *sizep,
975 enum object_type *kindp)
976{
977 unsigned long ptr;
978 int status = -1;
979
980 use_packed_git(p);
981 ptr = offset;
982 ptr = unpack_object_header(p, ptr, kindp, sizep);
983 if (*kindp != OBJ_DELTA)
984 goto done;
e702496e 985 hashcpy(base, (unsigned char *) p->pack_base + ptr);
a49dd05f
JH
986 status = 0;
987 done:
988 unuse_packed_git(p);
989 return status;
990}
991
ad8c80a5
JH
992void packed_object_info_detail(struct pack_entry *e,
993 char *type,
994 unsigned long *size,
995 unsigned long *store_size,
f8f135c9 996 unsigned int *delta_chain_length,
ad8c80a5
JH
997 unsigned char *base_sha1)
998{
999 struct packed_git *p = e->p;
2855d580 1000 unsigned long offset;
ad8c80a5
JH
1001 unsigned char *pack;
1002 enum object_type kind;
1003
1004 offset = unpack_object_header(p, e->offset, &kind, size);
1d7f171c 1005 pack = (unsigned char *) p->pack_base + offset;
ad8c80a5
JH
1006 if (kind != OBJ_DELTA)
1007 *delta_chain_length = 0;
1008 else {
f8f135c9 1009 unsigned int chain_length = 0;
2855d580
JH
1010 if (p->pack_size <= offset + 20)
1011 die("pack file %s records an incomplete delta base",
1012 p->pack_name);
e702496e 1013 hashcpy(base_sha1, pack);
ad8c80a5
JH
1014 do {
1015 struct pack_entry base_ent;
1016 unsigned long junk;
1017
1018 find_pack_entry_one(pack, &base_ent, p);
1019 offset = unpack_object_header(p, base_ent.offset,
1020 &kind, &junk);
1d7f171c 1021 pack = (unsigned char *) p->pack_base + offset;
ad8c80a5
JH
1022 chain_length++;
1023 } while (kind == OBJ_DELTA);
1024 *delta_chain_length = chain_length;
1025 }
1026 switch (kind) {
1027 case OBJ_COMMIT:
ad8c80a5 1028 case OBJ_TREE:
ad8c80a5 1029 case OBJ_BLOB:
ad8c80a5 1030 case OBJ_TAG:
5a18f540 1031 strcpy(type, type_names[kind]);
ad8c80a5
JH
1032 break;
1033 default:
264b16b6
JH
1034 die("corrupted pack file %s containing object of kind %d",
1035 p->pack_name, kind);
ad8c80a5
JH
1036 }
1037 *store_size = 0; /* notyet */
1038}
1039
1f688557
JH
1040static int packed_object_info(struct pack_entry *entry,
1041 char *type, unsigned long *sizep)
1042{
1043 struct packed_git *p = entry->p;
1044 unsigned long offset, size, left;
1045 unsigned char *pack;
a733cb60 1046 enum object_type kind;
f9253394 1047 int retval;
5db47c2b
JH
1048
1049 if (use_packed_git(p))
1050 die("cannot map packed file");
1051
a733cb60 1052 offset = unpack_object_header(p, entry->offset, &kind, &size);
1d7f171c 1053 pack = (unsigned char *) p->pack_base + offset;
a733cb60
LT
1054 left = p->pack_size - offset;
1055
1056 switch (kind) {
1057 case OBJ_DELTA:
f3bf9224 1058 retval = packed_delta_info(pack, size, left, type, sizep, p);
f9253394
JH
1059 unuse_packed_git(p);
1060 return retval;
a733cb60 1061 case OBJ_COMMIT:
a733cb60 1062 case OBJ_TREE:
a733cb60 1063 case OBJ_BLOB:
a733cb60 1064 case OBJ_TAG:
5a18f540 1065 strcpy(type, type_names[kind]);
a69d0943 1066 break;
1f688557 1067 default:
264b16b6
JH
1068 die("corrupted pack file %s containing object of kind %d",
1069 p->pack_name, kind);
1f688557 1070 }
c62266f3
JH
1071 if (sizep)
1072 *sizep = size;
f9253394 1073 unuse_packed_git(p);
1f688557
JH
1074 return 0;
1075}
1076
eb950c19
SP
1077static void *unpack_compressed_entry(struct packed_git *p,
1078 unsigned long offset,
1079 unsigned long size)
de530aaa
SP
1080{
1081 int st;
1082 z_stream stream;
1083 unsigned char *buffer;
1084
1085 buffer = xmalloc(size + 1);
1086 buffer[size] = 0;
1087 memset(&stream, 0, sizeof(stream));
eb950c19
SP
1088 stream.next_in = (unsigned char*)p->pack_base + offset;
1089 stream.avail_in = p->pack_size - offset;
de530aaa
SP
1090 stream.next_out = buffer;
1091 stream.avail_out = size;
1092
1093 inflateInit(&stream);
1094 st = inflate(&stream, Z_FINISH);
1095 inflateEnd(&stream);
1096 if ((st != Z_STREAM_END) || stream.total_out != size) {
1097 free(buffer);
1098 return NULL;
1099 }
1100
1101 return buffer;
1102}
1103
eb950c19
SP
1104static void *unpack_delta_entry(struct packed_git *p,
1105 unsigned long offset,
1f688557 1106 unsigned long delta_size,
1f688557 1107 char *type,
eb950c19 1108 unsigned long *sizep)
1f688557 1109{
f3bf9224 1110 struct pack_entry base_ent;
7c3e8be3
SP
1111 void *delta_data, *result, *base;
1112 unsigned long result_size, base_size;
eb950c19 1113 unsigned char* base_sha1;
1f688557 1114
eb950c19 1115 if ((offset + 20) >= p->pack_size)
1f688557 1116 die("truncated pack file");
67686d95
JH
1117
1118 /* The base entry _must_ be in the same pack */
eb950c19 1119 base_sha1 = (unsigned char*)p->pack_base + offset;
67686d95
JH
1120 if (!find_pack_entry_one(base_sha1, &base_ent, p))
1121 die("failed to find delta-pack base object %s",
1122 sha1_to_hex(base_sha1));
1123 base = unpack_entry_gently(&base_ent, type, &base_size);
1124 if (!base)
1125 die("failed to read delta-pack base object %s",
1126 sha1_to_hex(base_sha1));
1127
eb950c19 1128 delta_data = unpack_compressed_entry(p, offset + 20, delta_size);
1f688557
JH
1129 result = patch_delta(base, base_size,
1130 delta_data, delta_size,
1131 &result_size);
1132 if (!result)
1133 die("failed to apply delta");
1134 free(delta_data);
1135 free(base);
1136 *sizep = result_size;
1137 return result;
1138}
1139
1f688557
JH
1140static void *unpack_entry(struct pack_entry *entry,
1141 char *type, unsigned long *sizep)
1142{
1143 struct packed_git *p = entry->p;
f9253394 1144 void *retval;
1f688557
JH
1145
1146 if (use_packed_git(p))
1147 die("cannot map packed file");
f3bf9224
JH
1148 retval = unpack_entry_gently(entry, type, sizep);
1149 unuse_packed_git(p);
1150 if (!retval)
264b16b6 1151 die("corrupted pack file %s", p->pack_name);
f3bf9224
JH
1152 return retval;
1153}
1154
1155/* The caller is responsible for use_packed_git()/unuse_packed_git() pair */
1156void *unpack_entry_gently(struct pack_entry *entry,
1157 char *type, unsigned long *sizep)
1158{
1159 struct packed_git *p = entry->p;
eb950c19 1160 unsigned long offset, size;
f3bf9224 1161 enum object_type kind;
1f688557 1162
a733cb60 1163 offset = unpack_object_header(p, entry->offset, &kind, &size);
a733cb60
LT
1164 switch (kind) {
1165 case OBJ_DELTA:
eb950c19 1166 return unpack_delta_entry(p, offset, size, type, sizep);
a733cb60 1167 case OBJ_COMMIT:
a733cb60 1168 case OBJ_TREE:
a733cb60 1169 case OBJ_BLOB:
a733cb60 1170 case OBJ_TAG:
5a18f540
SP
1171 strcpy(type, type_names[kind]);
1172 *sizep = size;
eb950c19 1173 return unpack_compressed_entry(p, offset, size);
1f688557 1174 default:
f3bf9224 1175 return NULL;
1f688557 1176 }
1f688557
JH
1177}
1178
9a217f2a
JH
1179int num_packed_objects(const struct packed_git *p)
1180{
f9253394 1181 /* See check_packed_git_idx() */
9a217f2a
JH
1182 return (p->index_size - 20 - 20 - 4*256) / 24;
1183}
1184
1185int nth_packed_object_sha1(const struct packed_git *p, int n,
1186 unsigned char* sha1)
1187{
1188 void *index = p->index_base + 256;
1189 if (n < 0 || num_packed_objects(p) <= n)
1190 return -1;
e702496e 1191 hashcpy(sha1, (unsigned char *) index + (24 * n) + 4);
9a217f2a
JH
1192 return 0;
1193}
1194
f3bf9224
JH
1195int find_pack_entry_one(const unsigned char *sha1,
1196 struct pack_entry *e, struct packed_git *p)
1f688557 1197{
4d235c80 1198 unsigned int *level1_ofs = p->index_base;
1f688557
JH
1199 int hi = ntohl(level1_ofs[*sha1]);
1200 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1201 void *index = p->index_base + 256;
1202
1203 do {
1204 int mi = (lo + hi) / 2;
a89fccd2 1205 int cmp = hashcmp((unsigned char *)index + (24 * mi) + 4, sha1);
1f688557 1206 if (!cmp) {
1d7f171c 1207 e->offset = ntohl(*((unsigned int *) ((char *) index + (24 * mi))));
e702496e 1208 hashcpy(e->sha1, sha1);
1f688557
JH
1209 e->p = p;
1210 return 1;
1211 }
1212 if (cmp > 0)
1213 hi = mi;
1214 else
1215 lo = mi+1;
1216 } while (lo < hi);
1217 return 0;
1218}
1219
106d710b 1220static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, const char **ignore_packed)
1f688557
JH
1221{
1222 struct packed_git *p;
1223 prepare_packed_git();
1224
1225 for (p = packed_git; p; p = p->next) {
106d710b
JH
1226 if (ignore_packed) {
1227 const char **ig;
1228 for (ig = ignore_packed; *ig; ig++)
1229 if (!strcmp(p->pack_name, *ig))
1230 break;
1231 if (*ig)
1232 continue;
1233 }
f3bf9224 1234 if (find_pack_entry_one(sha1, e, p))
1f688557
JH
1235 return 1;
1236 }
1237 return 0;
1238}
1239
bf592c50
DB
1240struct packed_git *find_sha1_pack(const unsigned char *sha1,
1241 struct packed_git *packs)
1242{
1243 struct packed_git *p;
1244 struct pack_entry e;
1245
1246 for (p = packs; p; p = p->next) {
1247 if (find_pack_entry_one(sha1, &e, p))
1248 return p;
1249 }
1250 return NULL;
1251
1252}
1253
36e4d74a 1254int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep)
65c2e0c3 1255{
36e4d74a 1256 int status;
65c2e0c3
JH
1257 unsigned long mapsize, size;
1258 void *map;
1259 z_stream stream;
36e4d74a 1260 char hdr[128];
65c2e0c3 1261
bb6b8e4f 1262 map = map_sha1_file(sha1, &mapsize);
1f688557
JH
1263 if (!map) {
1264 struct pack_entry e;
1265
106d710b 1266 if (find_pack_entry(sha1, &e, NULL))
637cdd9d
JK
1267 return packed_object_info(&e, type, sizep);
1268 reprepare_packed_git();
106d710b 1269 if (find_pack_entry(sha1, &e, NULL))
637cdd9d
JK
1270 return packed_object_info(&e, type, sizep);
1271 return error("unable to find %s", sha1_to_hex(sha1));
1f688557 1272 }
36e4d74a
JH
1273 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
1274 status = error("unable to unpack %s header",
1275 sha1_to_hex(sha1));
1276 if (parse_sha1_header(hdr, type, &size) < 0)
1277 status = error("unable to parse %s header", sha1_to_hex(sha1));
c4584ae3 1278 else {
65c2e0c3 1279 status = 0;
c62266f3
JH
1280 if (sizep)
1281 *sizep = size;
65c2e0c3 1282 }
65c2e0c3
JH
1283 inflateEnd(&stream);
1284 munmap(map, mapsize);
1285 return status;
1286}
1287
1f688557
JH
1288static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned long *size)
1289{
1290 struct pack_entry e;
1291
106d710b 1292 if (!find_pack_entry(sha1, &e, NULL)) {
1f688557
JH
1293 error("cannot read sha1_file for %s", sha1_to_hex(sha1));
1294 return NULL;
1295 }
1296 return unpack_entry(&e, type, size);
1297}
1298
0fcfd160
LT
1299void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
1300{
1301 unsigned long mapsize;
1302 void *map, *buf;
ab90ea5d 1303 struct pack_entry e;
0fcfd160 1304
106d710b 1305 if (find_pack_entry(sha1, &e, NULL))
ab90ea5d 1306 return read_packed_sha1(sha1, type, size);
bb6b8e4f 1307 map = map_sha1_file(sha1, &mapsize);
0fcfd160
LT
1308 if (map) {
1309 buf = unpack_sha1_file(map, mapsize, type, size);
1310 munmap(map, mapsize);
1311 return buf;
1312 }
637cdd9d 1313 reprepare_packed_git();
106d710b 1314 if (find_pack_entry(sha1, &e, NULL))
637cdd9d 1315 return read_packed_sha1(sha1, type, size);
ab90ea5d 1316 return NULL;
0fcfd160
LT
1317}
1318
40469ee9 1319void *read_object_with_reference(const unsigned char *sha1,
bf0f910d 1320 const char *required_type,
40469ee9
JH
1321 unsigned long *size,
1322 unsigned char *actual_sha1_return)
f4913f91
JH
1323{
1324 char type[20];
1325 void *buffer;
1326 unsigned long isize;
40469ee9 1327 unsigned char actual_sha1[20];
f4913f91 1328
e702496e 1329 hashcpy(actual_sha1, sha1);
40469ee9
JH
1330 while (1) {
1331 int ref_length = -1;
1332 const char *ref_type = NULL;
f4913f91 1333
40469ee9
JH
1334 buffer = read_sha1_file(actual_sha1, type, &isize);
1335 if (!buffer)
1336 return NULL;
1337 if (!strcmp(type, required_type)) {
1338 *size = isize;
1339 if (actual_sha1_return)
e702496e 1340 hashcpy(actual_sha1_return, actual_sha1);
40469ee9
JH
1341 return buffer;
1342 }
1343 /* Handle references */
8e440259 1344 else if (!strcmp(type, commit_type))
40469ee9 1345 ref_type = "tree ";
8e440259 1346 else if (!strcmp(type, tag_type))
40469ee9
JH
1347 ref_type = "object ";
1348 else {
1349 free(buffer);
1350 return NULL;
1351 }
1352 ref_length = strlen(ref_type);
f4913f91 1353
40469ee9 1354 if (memcmp(buffer, ref_type, ref_length) ||
1d7f171c 1355 get_sha1_hex((char *) buffer + ref_length, actual_sha1)) {
40469ee9
JH
1356 free(buffer);
1357 return NULL;
1358 }
1cf58e72 1359 free(buffer);
40469ee9
JH
1360 /* Now we have the ID of the referred-to object in
1361 * actual_sha1. Check again. */
f4913f91 1362 }
f4913f91
JH
1363}
1364
7672db20
BL
1365char *write_sha1_file_prepare(void *buf,
1366 unsigned long len,
1367 const char *type,
1368 unsigned char *sha1,
1369 unsigned char *hdr,
1370 int *hdrlen)
d410c0f5
JH
1371{
1372 SHA_CTX c;
1373
1374 /* Generate the header */
1375 *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1376
1377 /* Sha1.. */
1378 SHA1_Init(&c);
1379 SHA1_Update(&c, hdr, *hdrlen);
1380 SHA1_Update(&c, buf, len);
1381 SHA1_Final(sha1, &c);
1382
1383 return sha1_file_name(sha1);
1384}
1385
230f1322
LT
1386/*
1387 * Link the tempfile to the final place, possibly creating the
1388 * last directory level as you do so.
1389 *
1390 * Returns the errno on failure, 0 on success.
1391 */
839837b9 1392static int link_temp_to_file(const char *tmpfile, const char *filename)
230f1322
LT
1393{
1394 int ret;
756aaf4a 1395 char *dir;
230f1322
LT
1396
1397 if (!link(tmpfile, filename))
1398 return 0;
1399
1400 /*
756aaf4a 1401 * Try to mkdir the last path component if that failed.
230f1322
LT
1402 *
1403 * Re-try the "link()" regardless of whether the mkdir
1404 * succeeds, since a race might mean that somebody
1405 * else succeeded.
1406 */
1407 ret = errno;
756aaf4a
SP
1408 dir = strrchr(filename, '/');
1409 if (dir) {
1410 *dir = 0;
1411 mkdir(filename, 0777);
1412 if (adjust_shared_perm(filename))
1413 return -2;
1414 *dir = '/';
1415 if (!link(tmpfile, filename))
1416 return 0;
1417 ret = errno;
230f1322
LT
1418 }
1419 return ret;
1420}
1421
1422/*
1423 * Move the just written object into its final resting place
1424 */
839837b9 1425int move_temp_to_file(const char *tmpfile, const char *filename)
230f1322
LT
1426{
1427 int ret = link_temp_to_file(tmpfile, filename);
7ebb6fca
LT
1428
1429 /*
1430 * Coda hack - coda doesn't like cross-directory links,
1431 * so we fall back to a rename, which will mean that it
1432 * won't be able to check collisions, but that's not a
1433 * big deal.
1434 *
1435 * The same holds for FAT formatted media.
1436 *
1437 * When this succeeds, we just return 0. We have nothing
1438 * left to unlink.
1439 */
1440 if (ret && ret != EEXIST) {
1441 if (!rename(tmpfile, filename))
230f1322 1442 return 0;
9e48b389 1443 ret = errno;
230f1322
LT
1444 }
1445 unlink(tmpfile);
1446 if (ret) {
1447 if (ret != EEXIST) {
7246ed43 1448 fprintf(stderr, "unable to write sha1 filename %s: %s\n", filename, strerror(ret));
230f1322
LT
1449 return -1;
1450 }
1451 /* FIXME!!! Collision check here ? */
1452 }
1453
1454 return 0;
1455}
1456
4d548150
LT
1457static int write_buffer(int fd, const void *buf, size_t len)
1458{
1459 while (len) {
1460 ssize_t size;
1461
1462 size = write(fd, buf, len);
1463 if (!size)
1464 return error("file write: disk full");
1465 if (size < 0) {
1466 if (errno == EINTR || errno == EAGAIN)
1467 continue;
1468 return error("file write error (%s)", strerror(errno));
1469 }
1470 len -= size;
1d7f171c 1471 buf = (char *) buf + size;
4d548150
LT
1472 }
1473 return 0;
1474}
1475
93821bd9
LT
1476static int write_binary_header(unsigned char *hdr, enum object_type type, unsigned long len)
1477{
1478 int hdr_len;
1479 unsigned char c;
1480
1481 c = (type << 4) | (len & 15);
1482 len >>= 4;
1483 hdr_len = 1;
1484 while (len) {
1485 *hdr++ = c | 0x80;
1486 hdr_len++;
1487 c = (len & 0x7f);
1488 len >>= 7;
1489 }
1490 *hdr = c;
1491 return hdr_len;
1492}
1493
1494static void setup_object_header(z_stream *stream, const char *type, unsigned long len)
1495{
1496 int obj_type, hdr;
1497
1498 if (use_legacy_headers) {
1499 while (deflate(stream, 0) == Z_OK)
1500 /* nothing */;
1501 return;
1502 }
1503 if (!strcmp(type, blob_type))
1504 obj_type = OBJ_BLOB;
1505 else if (!strcmp(type, tree_type))
1506 obj_type = OBJ_TREE;
1507 else if (!strcmp(type, commit_type))
1508 obj_type = OBJ_COMMIT;
1509 else if (!strcmp(type, tag_type))
1510 obj_type = OBJ_TAG;
1511 else
1512 die("trying to generate bogus object of type '%s'", type);
1513 hdr = write_binary_header(stream->next_out, obj_type, len);
1514 stream->total_out = hdr;
1515 stream->next_out += hdr;
1516 stream->avail_out -= hdr;
1517}
1518
bf0f910d 1519int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
0fcfd160
LT
1520{
1521 int size;
bf0f910d 1522 unsigned char *compressed;
0fcfd160
LT
1523 z_stream stream;
1524 unsigned char sha1[20];
706bc531 1525 char *filename;
aac17941 1526 static char tmpfile[PATH_MAX];
bf0f910d 1527 unsigned char hdr[50];
230f1322 1528 int fd, hdrlen;
a44c9a5e 1529
d410c0f5
JH
1530 /* Normally if we have it in the pack then we do not bother writing
1531 * it out into .git/objects/??/?{38} file.
1532 */
1533 filename = write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
706bc531 1534 if (returnsha1)
e702496e 1535 hashcpy(returnsha1, sha1);
d410c0f5
JH
1536 if (has_sha1_file(sha1))
1537 return 0;
aac17941
LT
1538 fd = open(filename, O_RDONLY);
1539 if (fd >= 0) {
706bc531 1540 /*
aac17941
LT
1541 * FIXME!!! We might do collision checking here, but we'd
1542 * need to uncompress the old file and check it. Later.
706bc531 1543 */
aac17941 1544 close(fd);
706bc531
LT
1545 return 0;
1546 }
1547
aac17941 1548 if (errno != ENOENT) {
7246ed43 1549 fprintf(stderr, "sha1 file %s: %s\n", filename, strerror(errno));
aac17941
LT
1550 return -1;
1551 }
1552
1553 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
ace1534d 1554
aac17941
LT
1555 fd = mkstemp(tmpfile);
1556 if (fd < 0) {
7246ed43 1557 fprintf(stderr, "unable to create temporary sha1 filename %s: %s\n", tmpfile, strerror(errno));
aac17941
LT
1558 return -1;
1559 }
1560
0fcfd160
LT
1561 /* Set it up */
1562 memset(&stream, 0, sizeof(stream));
12f6c308 1563 deflateInit(&stream, zlib_compression_level);
93821bd9 1564 size = 8 + deflateBound(&stream, len+hdrlen);
812666c8 1565 compressed = xmalloc(size);
0fcfd160
LT
1566
1567 /* Compress it */
0fcfd160
LT
1568 stream.next_out = compressed;
1569 stream.avail_out = size;
a44c9a5e
LT
1570
1571 /* First header.. */
1572 stream.next_in = hdr;
1573 stream.avail_in = hdrlen;
93821bd9 1574 setup_object_header(&stream, type, len);
a44c9a5e
LT
1575
1576 /* Then the data itself.. */
1577 stream.next_in = buf;
1578 stream.avail_in = len;
0fcfd160
LT
1579 while (deflate(&stream, Z_FINISH) == Z_OK)
1580 /* nothing */;
1581 deflateEnd(&stream);
1582 size = stream.total_out;
1583
4d548150
LT
1584 if (write_buffer(fd, compressed, size) < 0)
1585 die("unable to write sha1 file");
aac17941 1586 fchmod(fd, 0444);
706bc531 1587 close(fd);
383f85b7 1588 free(compressed);
0fcfd160 1589
230f1322 1590 return move_temp_to_file(tmpfile, filename);
0fcfd160 1591}
8237b185 1592
4d548150
LT
1593/*
1594 * We need to unpack and recompress the object for writing
1595 * it out to a different file.
1596 */
1597static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
a5eda52b 1598{
4d548150 1599 size_t size;
a5eda52b 1600 z_stream stream;
4d548150
LT
1601 unsigned char *unpacked;
1602 unsigned long len;
1603 char type[20];
1604 char hdr[50];
1605 int hdrlen;
1606 void *buf;
bfc66daf 1607
a9486b02 1608 /* need to unpack and recompress it by itself */
4d548150 1609 unpacked = read_packed_sha1(sha1, type, &len);
a5eda52b 1610
4d548150 1611 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
a5eda52b 1612
4d548150
LT
1613 /* Set it up */
1614 memset(&stream, 0, sizeof(stream));
12f6c308 1615 deflateInit(&stream, zlib_compression_level);
4d548150
LT
1616 size = deflateBound(&stream, len + hdrlen);
1617 buf = xmalloc(size);
a5eda52b 1618
4d548150
LT
1619 /* Compress it */
1620 stream.next_out = buf;
1621 stream.avail_out = size;
a5eda52b 1622
4d548150
LT
1623 /* First header.. */
1624 stream.next_in = (void *)hdr;
1625 stream.avail_in = hdrlen;
1626 while (deflate(&stream, 0) == Z_OK)
1627 /* nothing */;
bfc66daf 1628
4d548150
LT
1629 /* Then the data itself.. */
1630 stream.next_in = unpacked;
1631 stream.avail_in = len;
1632 while (deflate(&stream, Z_FINISH) == Z_OK)
1633 /* nothing */;
1634 deflateEnd(&stream);
1635 free(unpacked);
bfc66daf 1636
4d548150
LT
1637 *objsize = stream.total_out;
1638 return buf;
1639}
1640
1641int write_sha1_to_fd(int fd, const unsigned char *sha1)
1642{
1643 int retval;
1644 unsigned long objsize;
bb6b8e4f 1645 void *buf = map_sha1_file(sha1, &objsize);
4d548150
LT
1646
1647 if (buf) {
1648 retval = write_buffer(fd, buf, objsize);
1649 munmap(buf, objsize);
1650 return retval;
1651 }
1652
1653 buf = repack_object(sha1, &objsize);
1654 retval = write_buffer(fd, buf, objsize);
1655 free(buf);
1656 return retval;
a5eda52b
DB
1657}
1658
70b9829e
DB
1659int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1660 size_t bufsize, size_t *bufposn)
8237b185 1661{
230f1322 1662 char tmpfile[PATH_MAX];
8237b185
DB
1663 int local;
1664 z_stream stream;
1665 unsigned char real_sha1[20];
bf0f910d 1666 unsigned char discard[4096];
8237b185
DB
1667 int ret;
1668 SHA_CTX c;
1669
230f1322 1670 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
8237b185 1671
230f1322 1672 local = mkstemp(tmpfile);
8237b185 1673 if (local < 0)
bd2afde8
JH
1674 return error("Couldn't open %s for %s",
1675 tmpfile, sha1_to_hex(sha1));
8237b185
DB
1676
1677 memset(&stream, 0, sizeof(stream));
1678
1679 inflateInit(&stream);
1680
1681 SHA1_Init(&c);
1682
1683 do {
1684 ssize_t size;
70b9829e
DB
1685 if (*bufposn) {
1686 stream.avail_in = *bufposn;
96ad15ae 1687 stream.next_in = (unsigned char *) buffer;
70b9829e
DB
1688 do {
1689 stream.next_out = discard;
1690 stream.avail_out = sizeof(discard);
1691 ret = inflate(&stream, Z_SYNC_FLUSH);
1692 SHA1_Update(&c, discard, sizeof(discard) -
1693 stream.avail_out);
1694 } while (stream.avail_in && ret == Z_OK);
4d548150
LT
1695 if (write_buffer(local, buffer, *bufposn - stream.avail_in) < 0)
1696 die("unable to write sha1 file");
70b9829e
DB
1697 memmove(buffer, buffer + *bufposn - stream.avail_in,
1698 stream.avail_in);
1699 *bufposn = stream.avail_in;
1700 if (ret != Z_OK)
1701 break;
1702 }
1703 size = read(fd, buffer + *bufposn, bufsize - *bufposn);
8237b185
DB
1704 if (size <= 0) {
1705 close(local);
230f1322 1706 unlink(tmpfile);
8237b185
DB
1707 if (!size)
1708 return error("Connection closed?");
1709 perror("Reading from connection");
1710 return -1;
1711 }
70b9829e
DB
1712 *bufposn += size;
1713 } while (1);
8237b185
DB
1714 inflateEnd(&stream);
1715
1716 close(local);
1717 SHA1_Final(real_sha1, &c);
1718 if (ret != Z_STREAM_END) {
230f1322 1719 unlink(tmpfile);
8237b185
DB
1720 return error("File %s corrupted", sha1_to_hex(sha1));
1721 }
a89fccd2 1722 if (hashcmp(sha1, real_sha1)) {
230f1322 1723 unlink(tmpfile);
bd2afde8 1724 return error("File %s has bad hash", sha1_to_hex(sha1));
8237b185 1725 }
230f1322
LT
1726
1727 return move_temp_to_file(tmpfile, sha1_file_name(sha1));
8237b185
DB
1728}
1729
bf592c50
DB
1730int has_pack_index(const unsigned char *sha1)
1731{
1732 struct stat st;
1733 if (stat(sha1_pack_index_name(sha1), &st))
1734 return 0;
1735 return 1;
1736}
1737
1738int has_pack_file(const unsigned char *sha1)
1739{
1740 struct stat st;
1741 if (stat(sha1_pack_name(sha1), &st))
1742 return 0;
1743 return 1;
1744}
1745
106d710b 1746int has_sha1_pack(const unsigned char *sha1, const char **ignore_packed)
dade09c2
LT
1747{
1748 struct pack_entry e;
106d710b 1749 return find_pack_entry(sha1, &e, ignore_packed);
dade09c2
LT
1750}
1751
8237b185
DB
1752int has_sha1_file(const unsigned char *sha1)
1753{
8237b185 1754 struct stat st;
1f688557
JH
1755 struct pack_entry e;
1756
106d710b 1757 if (find_pack_entry(sha1, &e, NULL))
1f688557 1758 return 1;
ab90ea5d 1759 return find_sha1_file(sha1, &st) ? 1 : 0;
8237b185 1760}
74400e71 1761
e7332f96
BE
1762/*
1763 * reads from fd as long as possible into a supplied buffer of size bytes.
82e5a82f 1764 * If necessary the buffer's size is increased using realloc()
e7332f96
BE
1765 *
1766 * returns 0 if anything went fine and -1 otherwise
1767 *
1768 * NOTE: both buf and size may change, but even when -1 is returned
1769 * you still have to free() it yourself.
1770 */
1771int read_pipe(int fd, char** return_buf, unsigned long* return_size)
024510c8 1772{
e7332f96
BE
1773 char* buf = *return_buf;
1774 unsigned long size = *return_size;
1775 int iret;
024510c8 1776 unsigned long off = 0;
e7332f96 1777
024510c8 1778 do {
e7332f96 1779 iret = xread(fd, buf + off, size - off);
024510c8
DB
1780 if (iret > 0) {
1781 off += iret;
1782 if (off == size) {
1783 size *= 2;
83572c1a 1784 buf = xrealloc(buf, size);
024510c8
DB
1785 }
1786 }
1787 } while (iret > 0);
e7332f96
BE
1788
1789 *return_buf = buf;
1790 *return_size = off;
1791
1792 if (iret < 0)
1793 return -1;
1794 return 0;
1795}
1796
1797int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
1798{
1799 unsigned long size = 4096;
2d7320d0 1800 char *buf = xmalloc(size);
e7332f96
BE
1801 int ret;
1802 unsigned char hdr[50];
1803 int hdrlen;
1804
1805 if (read_pipe(fd, &buf, &size)) {
024510c8
DB
1806 free(buf);
1807 return -1;
1808 }
e7332f96 1809
024510c8 1810 if (!type)
8e440259 1811 type = blob_type;
024510c8 1812 if (write_object)
e7332f96 1813 ret = write_sha1_file(buf, size, type, sha1);
024510c8 1814 else {
e7332f96 1815 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
024510c8
DB
1816 ret = 0;
1817 }
1818 free(buf);
1819 return ret;
1820}
1821
7672db20 1822int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type)
74400e71 1823{
74400e71 1824 unsigned long size = st->st_size;
aac17941
LT
1825 void *buf;
1826 int ret;
7672db20
BL
1827 unsigned char hdr[50];
1828 int hdrlen;
74400e71 1829
aac17941 1830 buf = "";
74400e71 1831 if (size)
aac17941 1832 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
74400e71 1833 close(fd);
e35f9824 1834 if (buf == MAP_FAILED)
74400e71
JH
1835 return -1;
1836
7672db20 1837 if (!type)
8e440259 1838 type = blob_type;
7672db20
BL
1839 if (write_object)
1840 ret = write_sha1_file(buf, size, type, sha1);
1841 else {
1842 write_sha1_file_prepare(buf, size, type, sha1, hdr, &hdrlen);
1843 ret = 0;
1844 }
aac17941
LT
1845 if (size)
1846 munmap(buf, size);
1847 return ret;
74400e71 1848}
ec1fcc16
JH
1849
1850int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object)
1851{
1852 int fd;
1853 char *target;
1854
1855 switch (st->st_mode & S_IFMT) {
1856 case S_IFREG:
1857 fd = open(path, O_RDONLY);
1858 if (fd < 0)
1859 return error("open(\"%s\"): %s", path,
1860 strerror(errno));
1861 if (index_fd(sha1, fd, st, write_object, NULL) < 0)
1862 return error("%s: failed to insert into database",
1863 path);
1864 break;
1865 case S_IFLNK:
1866 target = xmalloc(st->st_size+1);
1867 if (readlink(path, target, st->st_size+1) != st->st_size) {
1868 char *errstr = strerror(errno);
1869 free(target);
1870 return error("readlink(\"%s\"): %s", path,
1871 errstr);
1872 }
1873 if (!write_object) {
1874 unsigned char hdr[50];
1875 int hdrlen;
8e440259 1876 write_sha1_file_prepare(target, st->st_size, blob_type,
ec1fcc16 1877 sha1, hdr, &hdrlen);
8e440259 1878 } else if (write_sha1_file(target, st->st_size, blob_type, sha1))
ec1fcc16
JH
1879 return error("%s: failed to insert into database",
1880 path);
1881 free(target);
1882 break;
1883 default:
1884 return error("%s: unsupported file type", path);
1885 }
1886 return 0;
1887}