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