link_alt_odb_entry: refactor string handling
[git/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"
6eac50d8 10#include "string-list.h"
697cc8ef 11#include "lockfile.h"
1f688557 12#include "delta.h"
a733cb60 13#include "pack.h"
8e440259
PE
14#include "blob.h"
15#include "commit.h"
4dd1fbc7 16#include "run-command.h"
8e440259
PE
17#include "tag.h"
18#include "tree.h"
c879daa2 19#include "tree-walk.h"
f35a6d3b 20#include "refs.h"
70f5d5d3 21#include "pack-revindex.h"
628522ec 22#include "sha1-lookup.h"
568508e7 23#include "bulk-checkin.h"
090ea126 24#include "streaming.h"
543c5caa 25#include "dir.h"
a73cdd21 26#include "mru.h"
12d95ef6 27#include "list.h"
c4c6effa 28#include "mergesort.h"
0fcfd160 29
144bde78
LT
30#ifndef O_NOATIME
31#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
32#define O_NOATIME 01000000
33#else
34#define O_NOATIME 0
35#endif
36#endif
37
28bd70d8
JN
38#define SZ_FMT PRIuMAX
39static inline uintmax_t sz_fmt(size_t s) { return s; }
e05db0fd 40
96f1e58f 41const unsigned char null_sha1[20];
3e56e724 42const struct object_id null_oid;
8576fde6
JK
43const struct object_id empty_tree_oid = {
44 EMPTY_TREE_SHA1_BIN_LITERAL
45};
46const struct object_id empty_blob_oid = {
47 EMPTY_BLOB_SHA1_BIN_LITERAL
48};
88cd621d 49
c597ba80
NTND
50/*
51 * This is meant to hold a *small* number of objects that you would
52 * want read_sha1_file() to be able to return, but yet you do not want
53 * to write them into the object store (e.g. a browse-only
54 * application).
55 */
56static struct cached_object {
57 unsigned char sha1[20];
58 enum object_type type;
59 void *buf;
60 unsigned long size;
61} *cached_objects;
62static int cached_object_nr, cached_object_alloc;
63
64static struct cached_object empty_tree = {
dab0d410 65 EMPTY_TREE_SHA1_BIN_LITERAL,
c597ba80
NTND
66 OBJ_TREE,
67 "",
68 0
69};
70
71static struct cached_object *find_cached_object(const unsigned char *sha1)
72{
73 int i;
74 struct cached_object *co = cached_objects;
75
76 for (i = 0; i < cached_object_nr; i++, co++) {
77 if (!hashcmp(co->sha1, sha1))
78 return co;
79 }
80 if (!hashcmp(sha1, empty_tree.sha1))
81 return &empty_tree;
82 return NULL;
83}
84
90a6464b
JH
85int mkdir_in_gitdir(const char *path)
86{
87 if (mkdir(path, 0777)) {
88 int saved_errno = errno;
89 struct stat st;
90 struct strbuf sb = STRBUF_INIT;
91
92 if (errno != EEXIST)
93 return -1;
94 /*
95 * Are we looking at a path in a symlinked worktree
96 * whose original repository does not yet have it?
97 * e.g. .git/rr-cache pointing at its original
98 * repository in which the user hasn't performed any
99 * conflict resolution yet?
100 */
101 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
102 strbuf_readlink(&sb, path, st.st_size) ||
103 !is_absolute_path(sb.buf) ||
104 mkdir(sb.buf, 0777)) {
105 strbuf_release(&sb);
106 errno = saved_errno;
107 return -1;
108 }
109 strbuf_release(&sb);
110 }
111 return adjust_shared_perm(path);
112}
113
0be0521b 114enum scld_error safe_create_leading_directories(char *path)
b2cb9425 115{
26c8ae2a 116 char *next_component = path + offset_1st_component(path);
0be0521b 117 enum scld_error ret = SCLD_OK;
67d42212 118
0be0521b 119 while (ret == SCLD_OK && next_component) {
f0502332 120 struct stat st;
0f527403 121 char *slash = next_component, slash_character;
f0502332 122
0f527403
MH
123 while (*slash && !is_dir_sep(*slash))
124 slash++;
125
126 if (!*slash)
b2cb9425 127 break;
bf10cf70 128
26c8ae2a 129 next_component = slash + 1;
0f527403 130 while (is_dir_sep(*next_component))
bf10cf70 131 next_component++;
26c8ae2a 132 if (!*next_component)
5f0bdf50 133 break;
831651fd 134
0f527403 135 slash_character = *slash;
831651fd 136 *slash = '\0';
67d42212
JR
137 if (!stat(path, &st)) {
138 /* path exists */
9e6f885d 139 if (!S_ISDIR(st.st_mode))
0be0521b 140 ret = SCLD_EXISTS;
53a39721 141 } else if (mkdir(path, 0777)) {
928734d9 142 if (errno == EEXIST &&
9e6f885d 143 !stat(path, &st) && S_ISDIR(st.st_mode))
928734d9 144 ; /* somebody created it since we checked */
18d37e86
MH
145 else if (errno == ENOENT)
146 /*
147 * Either mkdir() failed because
148 * somebody just pruned the containing
149 * directory, or stat() failed because
150 * the file that was in our way was
151 * just removed. Either way, inform
152 * the caller that it might be worth
153 * trying again:
154 */
155 ret = SCLD_VANISHED;
9e6f885d 156 else
0be0521b 157 ret = SCLD_FAILED;
53a39721 158 } else if (adjust_shared_perm(path)) {
0be0521b 159 ret = SCLD_PERMS;
457f06d6 160 }
0f527403 161 *slash = slash_character;
b2cb9425 162 }
9e6f885d 163 return ret;
b2cb9425 164}
723c31fe 165
0be0521b 166enum scld_error safe_create_leading_directories_const(const char *path)
8e21d63b
JK
167{
168 /* path points to cache entries, so xstrdup before messing with it */
169 char *buf = xstrdup(path);
0be0521b 170 enum scld_error result = safe_create_leading_directories(buf);
8e21d63b
JK
171 free(buf);
172 return result;
173}
174
ace1534d
JH
175static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
176{
177 int i;
178 for (i = 0; i < 20; i++) {
179 static char hex[] = "0123456789abcdef";
180 unsigned int val = sha1[i];
181 char *pos = pathbuf + i*2 + (i > 0);
182 *pos++ = hex[val >> 4];
183 *pos = hex[val & 0xf];
184 }
185}
186
30d6c6ea 187const char *sha1_file_name(const unsigned char *sha1)
0fcfd160 188{
560fb6a1
JK
189 static char buf[PATH_MAX];
190 const char *objdir;
191 int len;
0fcfd160 192
560fb6a1
JK
193 objdir = get_object_directory();
194 len = strlen(objdir);
195
196 /* '/' + sha1(2) + '/' + sha1(38) + '\0' */
197 if (len + 43 > PATH_MAX)
198 die("insanely long object directory %s", objdir);
199 memcpy(buf, objdir, len);
200 buf[len] = '/';
201 buf[len+3] = '/';
202 buf[len+42] = '\0';
203 fill_sha1_path(buf + len + 1, sha1);
204 return buf;
0fcfd160
LT
205}
206
d40d535b
MH
207/*
208 * Return the name of the pack or index file with the specified sha1
209 * in its filename. *base and *name are scratch space that must be
210 * provided by the caller. which should be "pack" or "idx".
211 */
633f43e1 212static char *sha1_get_pack_name(const unsigned char *sha1,
ac5190cc
JK
213 struct strbuf *buf,
214 const char *which)
bf592c50 215{
ac5190cc
JK
216 strbuf_reset(buf);
217 strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
218 sha1_to_hex(sha1), which);
219 return buf->buf;
bf592c50
DB
220}
221
633f43e1 222char *sha1_pack_name(const unsigned char *sha1)
bf592c50 223{
ac5190cc
JK
224 static struct strbuf buf = STRBUF_INIT;
225 return sha1_get_pack_name(sha1, &buf, "pack");
633f43e1 226}
bf592c50 227
633f43e1
HO
228char *sha1_pack_index_name(const unsigned char *sha1)
229{
ac5190cc
JK
230 static struct strbuf buf = STRBUF_INIT;
231 return sha1_get_pack_name(sha1, &buf, "idx");
bf592c50
DB
232}
233
d5a63b99
JH
234struct alternate_object_database *alt_odb_list;
235static struct alternate_object_database **alt_odb_tail;
ace1534d 236
4ea82473
JK
237/*
238 * Return non-zero iff the path is usable as an alternate object database.
239 */
240static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
241{
242 struct alternate_object_database *alt;
243
244 /* Detect cases where alternate disappeared */
245 if (!is_directory(path->buf)) {
246 error("object directory %s does not exist; "
247 "check .git/objects/info/alternates.",
248 path->buf);
249 return 0;
250 }
251
252 /*
253 * Prevent the common mistake of listing the same
254 * thing twice, or object directory itself.
255 */
256 for (alt = alt_odb_list; alt; alt = alt->next) {
257 if (path->len == alt->name - alt->base - 1 &&
258 !memcmp(path->buf, alt->base, path->len))
259 return 0;
260 }
261 if (!fspathcmp(path->buf, normalized_objdir))
262 return 0;
263
264 return 1;
265}
266
ddd5d056
JH
267/*
268 * Prepare alternate object database registry.
d5a63b99
JH
269 *
270 * The variable alt_odb_list points at the list of struct
271 * alternate_object_database. The elements on this list come from
272 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
273 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
1494e038
JH
274 * whose contents is similar to that environment variable but can be
275 * LF separated. Its base points at a statically allocated buffer that
d5a63b99
JH
276 * contains "/the/directory/corresponding/to/.git/objects/...", while
277 * its name points just after the slash at the end of ".git/objects/"
278 * in the example above, and has enough space to hold 40-byte hex
279 * SHA1, an extra slash for the first level indirection, and the
280 * terminating NUL.
ddd5d056 281 */
539e7506
EK
282static int link_alt_odb_entry(const char *entry, const char *relative_base,
283 int depth, const char *normalized_objdir)
ace1534d 284{
c2f493a4 285 struct alternate_object_database *ent;
4ea82473 286 size_t entlen;
5bdf0a84 287 struct strbuf pathbuf = STRBUF_INIT;
d5a63b99 288
85dadc38 289 if (!is_absolute_path(entry) && relative_base) {
5bdf0a84
HW
290 strbuf_addstr(&pathbuf, real_path(relative_base));
291 strbuf_addch(&pathbuf, '/');
c2f493a4 292 }
6eac50d8 293 strbuf_addstr(&pathbuf, entry);
c2f493a4 294
670c359d
JK
295 if (strbuf_normalize_path(&pathbuf) < 0) {
296 error("unable to normalize alternate object path: %s",
297 pathbuf.buf);
298 strbuf_release(&pathbuf);
299 return -1;
300 }
5bdf0a84 301
5bdf0a84
HW
302 /*
303 * The trailing slash after the directory name is given by
304 * this function at the end. Remove duplicates.
305 */
4ea82473
JK
306 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
307 strbuf_setlen(&pathbuf, pathbuf.len - 1);
5bdf0a84 308
4ea82473
JK
309 if (!alt_odb_usable(&pathbuf, normalized_objdir)) {
310 strbuf_release(&pathbuf);
c2f493a4
MW
311 return -1;
312 }
313
4ea82473
JK
314 entlen = st_add(pathbuf.len, 43); /* '/' + 2 hex + '/' + 38 hex + NUL */
315 ent = xmalloc(st_add(sizeof(*ent), entlen));
316 memcpy(ent->base, pathbuf.buf, pathbuf.len);
317
318 ent->name = ent->base + pathbuf.len + 1;
319 ent->base[pathbuf.len] = '/';
320 ent->base[pathbuf.len + 3] = '/';
321 ent->base[entlen-1] = 0;
c2f493a4
MW
322
323 /* add the alternate entry */
324 *alt_odb_tail = ent;
325 alt_odb_tail = &(ent->next);
326 ent->next = NULL;
327
328 /* recursively add alternates */
4ea82473 329 read_info_alternates(pathbuf.buf, depth + 1);
c2f493a4 330
4ea82473 331 strbuf_release(&pathbuf);
c2f493a4
MW
332 return 0;
333}
334
c5950164 335static void link_alt_odb_entries(const char *alt, int len, int sep,
c2f493a4
MW
336 const char *relative_base, int depth)
337{
6eac50d8
MH
338 struct string_list entries = STRING_LIST_INIT_NODUP;
339 char *alt_copy;
340 int i;
539e7506 341 struct strbuf objdirbuf = STRBUF_INIT;
c2f493a4
MW
342
343 if (depth > 5) {
344 error("%s: ignoring alternate object stores, nesting too deep.",
345 relative_base);
346 return;
347 }
348
f655651e 349 strbuf_add_absolute_path(&objdirbuf, get_object_directory());
670c359d
JK
350 if (strbuf_normalize_path(&objdirbuf) < 0)
351 die("unable to normalize object directory: %s",
352 objdirbuf.buf);
539e7506 353
c5950164 354 alt_copy = xmemdupz(alt, len);
6eac50d8
MH
355 string_list_split_in_place(&entries, alt_copy, sep, -1);
356 for (i = 0; i < entries.nr; i++) {
357 const char *entry = entries.items[i].string;
358 if (entry[0] == '\0' || entry[0] == '#')
9577e7e3 359 continue;
6eac50d8
MH
360 if (!is_absolute_path(entry) && depth) {
361 error("%s: ignoring relative alternate object store %s",
362 relative_base, entry);
363 } else {
539e7506 364 link_alt_odb_entry(entry, relative_base, depth, objdirbuf.buf);
9577e7e3 365 }
9577e7e3 366 }
6eac50d8
MH
367 string_list_clear(&entries, 0);
368 free(alt_copy);
539e7506 369 strbuf_release(&objdirbuf);
d5a63b99
JH
370}
371
5e73633d 372void read_info_alternates(const char * relative_base, int depth)
d5a63b99 373{
9577e7e3 374 char *map;
dc49cd76 375 size_t mapsz;
d5a63b99 376 struct stat st;
5015f01c 377 char *path;
c2f493a4 378 int fd;
d5a63b99 379
5015f01c 380 path = xstrfmt("%s/info/alternates", relative_base);
f4e51683 381 fd = git_open_noatime(path);
5015f01c 382 free(path);
d5a63b99
JH
383 if (fd < 0)
384 return;
385 if (fstat(fd, &st) || (st.st_size == 0)) {
386 close(fd);
9a217f2a 387 return;
ace1534d 388 }
dc49cd76
SP
389 mapsz = xsize_t(st.st_size);
390 map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
d5a63b99 391 close(fd);
d5a63b99 392
c5950164 393 link_alt_odb_entries(map, mapsz, '\n', relative_base, depth);
c2f493a4 394
dc49cd76 395 munmap(map, mapsz);
ace1534d
JH
396}
397
bef70b22
DB
398void add_to_alternates_file(const char *reference)
399{
400 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
77b9b1d1
JK
401 char *alts = git_pathdup("objects/info/alternates");
402 FILE *in, *out;
403
404 hold_lock_file_for_update(lock, alts, LOCK_DIE_ON_ERROR);
405 out = fdopen_lock_file(lock, "w");
406 if (!out)
407 die_errno("unable to fdopen alternates lockfile");
408
409 in = fopen(alts, "r");
410 if (in) {
411 struct strbuf line = STRBUF_INIT;
412 int found = 0;
413
3f163962 414 while (strbuf_getline(&line, in) != EOF) {
77b9b1d1
JK
415 if (!strcmp(reference, line.buf)) {
416 found = 1;
417 break;
418 }
419 fprintf_or_die(out, "%s\n", line.buf);
420 }
421
422 strbuf_release(&line);
423 fclose(in);
424
425 if (found) {
426 rollback_lock_file(lock);
427 lock = NULL;
428 }
429 }
430 else if (errno != ENOENT)
431 die_errno("unable to read alternates file");
432
433 if (lock) {
434 fprintf_or_die(out, "%s\n", reference);
435 if (commit_lock_file(lock))
436 die_errno("unable to move new alternates file into place");
437 if (alt_odb_tail)
438 link_alt_odb_entries(reference, strlen(reference), '\n', NULL, 0);
439 }
440 free(alts);
bef70b22
DB
441}
442
9eeea7d2
SB
443/*
444 * Compute the exact path an alternate is at and returns it. In case of
445 * error NULL is returned and the human readable error is added to `err`
446 * `path` may be relative and should point to $GITDIR.
447 * `err` must not be null.
448 */
449char *compute_alternate_path(const char *path, struct strbuf *err)
450{
451 char *ref_git = NULL;
452 const char *repo, *ref_git_s;
453 int seen_error = 0;
454
455 ref_git_s = real_path_if_valid(path);
456 if (!ref_git_s) {
457 seen_error = 1;
458 strbuf_addf(err, _("path '%s' does not exist"), path);
459 goto out;
460 } else
461 /*
462 * Beware: read_gitfile(), real_path() and mkpath()
463 * return static buffer
464 */
465 ref_git = xstrdup(ref_git_s);
466
467 repo = read_gitfile(ref_git);
468 if (!repo)
469 repo = read_gitfile(mkpath("%s/.git", ref_git));
470 if (repo) {
471 free(ref_git);
472 ref_git = xstrdup(repo);
473 }
474
475 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
476 char *ref_git_git = mkpathdup("%s/.git", ref_git);
477 free(ref_git);
478 ref_git = ref_git_git;
479 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
480 struct strbuf sb = STRBUF_INIT;
481 seen_error = 1;
482 if (get_common_dir(&sb, ref_git)) {
483 strbuf_addf(err,
484 _("reference repository '%s' as a linked "
485 "checkout is not supported yet."),
486 path);
487 goto out;
488 }
489
490 strbuf_addf(err, _("reference repository '%s' is not a "
491 "local repository."), path);
492 goto out;
493 }
494
495 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
496 strbuf_addf(err, _("reference repository '%s' is shallow"),
497 path);
498 seen_error = 1;
499 goto out;
500 }
501
502 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
503 strbuf_addf(err,
504 _("reference repository '%s' is grafted"),
505 path);
506 seen_error = 1;
507 goto out;
508 }
509
510out:
511 if (seen_error) {
512 free(ref_git);
513 ref_git = NULL;
514 }
515
516 return ref_git;
517}
518
fe1b2268 519int foreach_alt_odb(alt_odb_fn fn, void *cb)
d79796bc
JH
520{
521 struct alternate_object_database *ent;
fe1b2268 522 int r = 0;
d79796bc
JH
523
524 prepare_alt_odb();
fe1b2268
JK
525 for (ent = alt_odb_list; ent; ent = ent->next) {
526 r = fn(ent, cb);
527 if (r)
528 break;
529 }
530 return r;
d79796bc
JH
531}
532
c2f493a4
MW
533void prepare_alt_odb(void)
534{
554fe20d 535 const char *alt;
c2f493a4 536
7dc24aa5
SP
537 if (alt_odb_tail)
538 return;
539
c2f493a4
MW
540 alt = getenv(ALTERNATE_DB_ENVIRONMENT);
541 if (!alt) alt = "";
542
c2f493a4 543 alt_odb_tail = &alt_odb_list;
c5950164 544 link_alt_odb_entries(alt, strlen(alt), PATH_SEP, NULL, 0);
c2f493a4
MW
545
546 read_info_alternates(get_object_directory(), 0);
547}
548
3096b2ec 549/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
33d4221c 550static int freshen_file(const char *fn)
ace1534d 551{
33d4221c
JK
552 struct utimbuf t;
553 t.actime = t.modtime = time(NULL);
554 return !utime(fn, &t);
0f4dc14a 555}
ace1534d 556
3096b2ec
JK
557/*
558 * All of the check_and_freshen functions return 1 if the file exists and was
559 * freshened (if freshening was requested), 0 otherwise. If they return
560 * 0, you should not assume that it is safe to skip a write of the object (it
561 * either does not exist on disk, or has a stale mtime and may be subject to
562 * pruning).
563 */
33d4221c
JK
564static int check_and_freshen_file(const char *fn, int freshen)
565{
566 if (access(fn, F_OK))
567 return 0;
3096b2ec 568 if (freshen && !freshen_file(fn))
33d4221c
JK
569 return 0;
570 return 1;
571}
572
573static int check_and_freshen_local(const unsigned char *sha1, int freshen)
574{
575 return check_and_freshen_file(sha1_file_name(sha1), freshen);
576}
577
578static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
0f4dc14a
BC
579{
580 struct alternate_object_database *alt;
9a217f2a 581 prepare_alt_odb();
d5a63b99 582 for (alt = alt_odb_list; alt; alt = alt->next) {
0f4dc14a 583 fill_sha1_path(alt->name, sha1);
33d4221c 584 if (check_and_freshen_file(alt->base, freshen))
c529d75a 585 return 1;
ace1534d 586 }
c529d75a 587 return 0;
ace1534d
JH
588}
589
33d4221c
JK
590static int check_and_freshen(const unsigned char *sha1, int freshen)
591{
592 return check_and_freshen_local(sha1, freshen) ||
593 check_and_freshen_nonlocal(sha1, freshen);
594}
595
596int has_loose_object_nonlocal(const unsigned char *sha1)
597{
598 return check_and_freshen_nonlocal(sha1, 0);
599}
600
0f4dc14a
BC
601static int has_loose_object(const unsigned char *sha1)
602{
33d4221c 603 return check_and_freshen(sha1, 0);
0f4dc14a
BC
604}
605
60bb8b14 606static unsigned int pack_used_ctr;
a53128b6
SP
607static unsigned int pack_mmap_calls;
608static unsigned int peak_pack_open_windows;
609static unsigned int pack_open_windows;
c7934306
SP
610static unsigned int pack_open_fds;
611static unsigned int pack_max_fds;
a53128b6 612static size_t peak_pack_mapped;
60bb8b14 613static size_t pack_mapped;
9a217f2a 614struct packed_git *packed_git;
1f688557 615
a73cdd21
JK
616static struct mru packed_git_mru_storage;
617struct mru *packed_git_mru = &packed_git_mru_storage;
618
b79d18c9 619void pack_report(void)
a53128b6
SP
620{
621 fprintf(stderr,
e05db0fd
PR
622 "pack_report: getpagesize() = %10" SZ_FMT "\n"
623 "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
624 "pack_report: core.packedGitLimit = %10" SZ_FMT "\n",
9e42d6a1
SP
625 sz_fmt(getpagesize()),
626 sz_fmt(packed_git_window_size),
627 sz_fmt(packed_git_limit));
a53128b6
SP
628 fprintf(stderr,
629 "pack_report: pack_used_ctr = %10u\n"
630 "pack_report: pack_mmap_calls = %10u\n"
631 "pack_report: pack_open_windows = %10u / %10u\n"
e05db0fd
PR
632 "pack_report: pack_mapped = "
633 "%10" SZ_FMT " / %10" SZ_FMT "\n",
a53128b6
SP
634 pack_used_ctr,
635 pack_mmap_calls,
636 pack_open_windows, peak_pack_open_windows,
9e42d6a1 637 sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
a53128b6
SP
638}
639
d40d535b
MH
640/*
641 * Open and mmap the index file at path, perform a couple of
642 * consistency checks, then record its information to p. Return 0 on
643 * success.
644 */
645static int check_packed_git_idx(const char *path, struct packed_git *p)
1f688557
JH
646{
647 void *idx_map;
42873078 648 struct pack_idx_header *hdr;
dc49cd76 649 size_t idx_size;
74e34e1f 650 uint32_t version, nr, i, *index;
f4e51683 651 int fd = git_open_noatime(path);
1f688557 652 struct stat st;
42873078 653
1f688557
JH
654 if (fd < 0)
655 return -1;
656 if (fstat(fd, &st)) {
657 close(fd);
658 return -1;
659 }
dc49cd76 660 idx_size = xsize_t(st.st_size);
2d88451b
SP
661 if (idx_size < 4 * 256 + 20 + 20) {
662 close(fd);
663 return error("index file %s is too small", path);
664 }
c4712e45 665 idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
1f688557 666 close(fd);
1f688557 667
42873078
NP
668 hdr = idx_map;
669 if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
74e34e1f
NP
670 version = ntohl(hdr->idx_version);
671 if (version < 2 || version > 2) {
672 munmap(idx_map, idx_size);
6e1c2344 673 return error("index file %s is version %"PRIu32
74e34e1f
NP
674 " and is not supported by this binary"
675 " (try upgrading GIT to a newer version)",
676 path, version);
677 }
678 } else
679 version = 1;
df1b059d 680
1f688557 681 nr = 0;
42873078 682 index = idx_map;
74e34e1f
NP
683 if (version > 1)
684 index += 2; /* skip index header */
1f688557 685 for (i = 0; i < 256; i++) {
326bf396 686 uint32_t n = ntohl(index[i]);
2d88451b
SP
687 if (n < nr) {
688 munmap(idx_map, idx_size);
df1b059d 689 return error("non-monotonic index %s", path);
2d88451b 690 }
1f688557
JH
691 nr = n;
692 }
693
74e34e1f
NP
694 if (version == 1) {
695 /*
696 * Total size:
697 * - 256 index entries 4 bytes each
698 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
699 * - 20-byte SHA1 of the packfile
700 * - 20-byte SHA1 file checksum
701 */
702 if (idx_size != 4*256 + nr * 24 + 20 + 20) {
703 munmap(idx_map, idx_size);
eef427a0 704 return error("wrong index v1 file size in %s", path);
74e34e1f
NP
705 }
706 } else if (version == 2) {
707 /*
708 * Minimum size:
709 * - 8 bytes of header
710 * - 256 index entries 4 bytes each
711 * - 20-byte sha1 entry * nr
712 * - 4-byte crc entry * nr
713 * - 4-byte offset entry * nr
714 * - 20-byte SHA1 of the packfile
715 * - 20-byte SHA1 file checksum
716 * And after the 4-byte offset table might be a
717 * variable sized table containing 8-byte entries
718 * for offsets larger than 2^31.
719 */
720 unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
1164f1e4
LT
721 unsigned long max_size = min_size;
722 if (nr)
723 max_size += (nr - 1)*8;
724 if (idx_size < min_size || idx_size > max_size) {
74e34e1f 725 munmap(idx_map, idx_size);
eef427a0 726 return error("wrong index v2 file size in %s", path);
74e34e1f 727 }
7109c889
JH
728 if (idx_size != min_size &&
729 /*
730 * make sure we can deal with large pack offsets.
731 * 31-bit signed offset won't be enough, neither
732 * 32-bit unsigned one will be.
733 */
734 (sizeof(off_t) <= 4)) {
735 munmap(idx_map, idx_size);
736 return error("pack too large for current definition of off_t in %s", path);
74e34e1f 737 }
2d88451b 738 }
1f688557 739
74e34e1f 740 p->index_version = version;
42873078
NP
741 p->index_data = idx_map;
742 p->index_size = idx_size;
57059091 743 p->num_objects = nr;
1f688557
JH
744 return 0;
745}
746
bc8e478a 747int open_pack_index(struct packed_git *p)
d079837e
SP
748{
749 char *idx_name;
9ae97018 750 size_t len;
d079837e
SP
751 int ret;
752
753 if (p->index_data)
754 return 0;
755
9ae97018
JK
756 if (!strip_suffix(p->pack_name, ".pack", &len))
757 die("BUG: pack_name does not end in .pack");
758 idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
d079837e
SP
759 ret = check_packed_git_idx(idx_name, p);
760 free(idx_name);
761 return ret;
762}
763
11daf39b
SP
764static void scan_windows(struct packed_git *p,
765 struct packed_git **lru_p,
766 struct pack_window **lru_w,
767 struct pack_window **lru_l)
1f688557 768{
11daf39b
SP
769 struct pack_window *w, *w_l;
770
771 for (w_l = NULL, w = p->windows; w; w = w->next) {
772 if (!w->inuse_cnt) {
773 if (!*lru_w || w->last_used < (*lru_w)->last_used) {
774 *lru_p = p;
775 *lru_w = w;
776 *lru_l = w_l;
54044bf8 777 }
54044bf8 778 }
11daf39b 779 w_l = w;
f9253394 780 }
11daf39b
SP
781}
782
7c3ecb32 783static int unuse_one_window(struct packed_git *current)
11daf39b
SP
784{
785 struct packed_git *p, *lru_p = NULL;
786 struct pack_window *lru_w = NULL, *lru_l = NULL;
787
788 if (current)
789 scan_windows(current, &lru_p, &lru_w, &lru_l);
790 for (p = packed_git; p; p = p->next)
791 scan_windows(p, &lru_p, &lru_w, &lru_l);
54044bf8
SP
792 if (lru_p) {
793 munmap(lru_w->base, lru_w->len);
794 pack_mapped -= lru_w->len;
795 if (lru_l)
796 lru_l->next = lru_w->next;
7c3ecb32 797 else
54044bf8 798 lru_p->windows = lru_w->next;
54044bf8 799 free(lru_w);
a53128b6 800 pack_open_windows--;
54044bf8
SP
801 return 1;
802 }
803 return 0;
f9253394
JH
804}
805
7c3ecb32 806void release_pack_memory(size_t need)
97bfeb34
SP
807{
808 size_t cur = pack_mapped;
7c3ecb32 809 while (need >= (cur - pack_mapped) && unuse_one_window(NULL))
97bfeb34
SP
810 ; /* nothing */
811}
812
02710228
SP
813static void mmap_limit_check(size_t length)
814{
815 static size_t limit = 0;
816 if (!limit) {
817 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
818 if (!limit)
819 limit = SIZE_MAX;
820 }
821 if (length > limit)
822 die("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX,
823 (uintmax_t)length, (uintmax_t)limit);
824}
825
1570856b
JK
826void *xmmap_gently(void *start, size_t length,
827 int prot, int flags, int fd, off_t offset)
58ecbd5e 828{
02710228
SP
829 void *ret;
830
831 mmap_limit_check(length);
832 ret = mmap(start, length, prot, flags, fd, offset);
58ecbd5e
JN
833 if (ret == MAP_FAILED) {
834 if (!length)
835 return NULL;
7c3ecb32 836 release_pack_memory(length);
58ecbd5e 837 ret = mmap(start, length, prot, flags, fd, offset);
58ecbd5e
JN
838 }
839 return ret;
840}
841
1570856b
JK
842void *xmmap(void *start, size_t length,
843 int prot, int flags, int fd, off_t offset)
844{
845 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
846 if (ret == MAP_FAILED)
9ca0aaf6 847 die_errno("mmap failed");
1570856b
JK
848 return ret;
849}
850
c9ced051
SP
851void close_pack_windows(struct packed_git *p)
852{
853 while (p->windows) {
854 struct pack_window *w = p->windows;
855
856 if (w->inuse_cnt)
857 die("pack '%s' still has open windows to it",
858 p->pack_name);
859 munmap(w->base, w->len);
860 pack_mapped -= w->len;
861 pack_open_windows--;
862 p->windows = w->next;
863 free(w);
864 }
865}
866
71fe5d7f
JS
867static int close_pack_fd(struct packed_git *p)
868{
869 if (p->pack_fd < 0)
870 return 0;
871
872 close(p->pack_fd);
873 pack_open_fds--;
874 p->pack_fd = -1;
875
876 return 1;
877}
878
38849a81
JS
879static void close_pack(struct packed_git *p)
880{
881 close_pack_windows(p);
882 close_pack_fd(p);
883 close_pack_index(p);
884}
885
886void close_all_packs(void)
887{
888 struct packed_git *p;
889
890 for (p = packed_git; p; p = p->next)
891 if (p->do_not_close)
ef1177d1 892 die("BUG: want to close pack marked 'do-not-close'");
38849a81
JS
893 else
894 close_pack(p);
895}
896
897
88d0db55
BC
898/*
899 * The LRU pack is the one with the oldest MRU window, preferring packs
900 * with no used windows, or the oldest mtime if it has no windows allocated.
901 */
902static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
903{
904 struct pack_window *w, *this_mru_w;
905 int has_windows_inuse = 0;
906
907 /*
908 * Reject this pack if it has windows and the previously selected
909 * one does not. If this pack does not have windows, reject
910 * it if the pack file is newer than the previously selected one.
911 */
912 if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
913 return;
914
915 for (w = this_mru_w = p->windows; w; w = w->next) {
916 /*
917 * Reject this pack if any of its windows are in use,
918 * but the previously selected pack did not have any
919 * inuse windows. Otherwise, record that this pack
920 * has windows in use.
921 */
922 if (w->inuse_cnt) {
923 if (*accept_windows_inuse)
924 has_windows_inuse = 1;
925 else
926 return;
927 }
928
929 if (w->last_used > this_mru_w->last_used)
930 this_mru_w = w;
931
932 /*
933 * Reject this pack if it has windows that have been
934 * used more recently than the previously selected pack.
935 * If the previously selected pack had windows inuse and
936 * we have not encountered a window in this pack that is
937 * inuse, skip this check since we prefer a pack with no
938 * inuse windows to one that has inuse windows.
939 */
940 if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
941 this_mru_w->last_used > (*mru_w)->last_used)
942 return;
943 }
944
945 /*
946 * Select this pack.
947 */
948 *mru_w = this_mru_w;
949 *lru_p = p;
950 *accept_windows_inuse = has_windows_inuse;
951}
952
953static int close_one_pack(void)
954{
955 struct packed_git *p, *lru_p = NULL;
956 struct pack_window *mru_w = NULL;
957 int accept_windows_inuse = 1;
958
959 for (p = packed_git; p; p = p->next) {
960 if (p->pack_fd == -1)
961 continue;
962 find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
963 }
964
71fe5d7f
JS
965 if (lru_p)
966 return close_pack_fd(lru_p);
88d0db55
BC
967
968 return 0;
969}
970
03e79c88 971void unuse_pack(struct pack_window **w_cursor)
f9253394 972{
03e79c88
SP
973 struct pack_window *w = *w_cursor;
974 if (w) {
975 w->inuse_cnt--;
976 *w_cursor = NULL;
977 }
1f688557
JH
978}
979
fa5fc15d
SP
980void close_pack_index(struct packed_git *p)
981{
982 if (p->index_data) {
983 munmap((void *)p->index_data, p->index_size);
984 p->index_data = NULL;
985 }
986}
987
a0788266
JS
988static unsigned int get_max_fd_limit(void)
989{
990#ifdef RLIMIT_NOFILE
491a8dec
JH
991 {
992 struct rlimit lim;
a0788266 993
491a8dec
JH
994 if (!getrlimit(RLIMIT_NOFILE, &lim))
995 return lim.rlim_cur;
996 }
997#endif
998
999#ifdef _SC_OPEN_MAX
1000 {
1001 long open_max = sysconf(_SC_OPEN_MAX);
1002 if (0 < open_max)
1003 return open_max;
1004 /*
1005 * Otherwise, we got -1 for one of the two
1006 * reasons:
1007 *
1008 * (1) sysconf() did not understand _SC_OPEN_MAX
1009 * and signaled an error with -1; or
1010 * (2) sysconf() said there is no limit.
1011 *
1012 * We _could_ clear errno before calling sysconf() to
1013 * tell these two cases apart and return a huge number
1014 * in the latter case to let the caller cap it to a
1015 * value that is not so selfish, but letting the
1016 * fallback OPEN_MAX codepath take care of these cases
1017 * is a lot simpler.
1018 */
1019 }
1020#endif
a0788266 1021
491a8dec 1022#ifdef OPEN_MAX
a0788266
JS
1023 return OPEN_MAX;
1024#else
1025 return 1; /* see the caller ;-) */
1026#endif
1027}
1028
3cf8b462
SP
1029/*
1030 * Do not call this directly as this leaks p->pack_fd on error return;
1031 * call open_packed_git() instead.
1032 */
1033static int open_packed_git_1(struct packed_git *p)
1f688557 1034{
9bc879c1
SP
1035 struct stat st;
1036 struct pack_header hdr;
1037 unsigned char sha1[20];
1038 unsigned char *idx_sha1;
2c039da8 1039 long fd_flag;
9bc879c1 1040
d079837e
SP
1041 if (!p->index_data && open_pack_index(p))
1042 return error("packfile %s index unavailable", p->pack_name);
1043
c7934306 1044 if (!pack_max_fds) {
a0788266 1045 unsigned int max_fds = get_max_fd_limit();
c7934306
SP
1046
1047 /* Save 3 for stdin/stdout/stderr, 22 for work */
1048 if (25 < max_fds)
1049 pack_max_fds = max_fds - 25;
1050 else
1051 pack_max_fds = 1;
1052 }
1053
88d0db55 1054 while (pack_max_fds <= pack_open_fds && close_one_pack())
c7934306
SP
1055 ; /* nothing */
1056
f4e51683 1057 p->pack_fd = git_open_noatime(p->pack_name);
9bc879c1 1058 if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
072db278 1059 return -1;
c7934306 1060 pack_open_fds++;
9bc879c1
SP
1061
1062 /* If we created the struct before we had the pack we lack size. */
bf592c50 1063 if (!p->pack_size) {
bf592c50 1064 if (!S_ISREG(st.st_mode))
072db278 1065 return error("packfile %s not a regular file", p->pack_name);
bf592c50 1066 p->pack_size = st.st_size;
9bc879c1 1067 } else if (p->pack_size != st.st_size)
072db278 1068 return error("packfile %s size changed", p->pack_name);
9bc879c1 1069
2c039da8
JH
1070 /* We leave these file descriptors open with sliding mmap;
1071 * there is no point keeping them open across exec(), though.
1072 */
1073 fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
1074 if (fd_flag < 0)
072db278 1075 return error("cannot determine file descriptor flags");
2c039da8
JH
1076 fd_flag |= FD_CLOEXEC;
1077 if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
072db278 1078 return error("cannot set FD_CLOEXEC");
2c039da8 1079
9bc879c1 1080 /* Verify we recognize this pack file format. */
e6e2bd62 1081 if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
072db278 1082 return error("file %s is far too short to be a packfile", p->pack_name);
9bc879c1 1083 if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
072db278 1084 return error("file %s is not a GIT packfile", p->pack_name);
9bc879c1 1085 if (!pack_version_ok(hdr.hdr_version))
6e1c2344
RJ
1086 return error("packfile %s is version %"PRIu32" and not"
1087 " supported (try upgrading GIT to a newer version)",
9bc879c1
SP
1088 p->pack_name, ntohl(hdr.hdr_version));
1089
1090 /* Verify the pack matches its index. */
57059091 1091 if (p->num_objects != ntohl(hdr.hdr_entries))
6e1c2344
RJ
1092 return error("packfile %s claims to have %"PRIu32" objects"
1093 " while index indicates %"PRIu32" objects",
57059091
NP
1094 p->pack_name, ntohl(hdr.hdr_entries),
1095 p->num_objects);
9bc879c1 1096 if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
072db278 1097 return error("end of packfile %s is unavailable", p->pack_name);
e6e2bd62 1098 if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
072db278 1099 return error("packfile %s signature is unavailable", p->pack_name);
42873078 1100 idx_sha1 = ((unsigned char *)p->index_data) + p->index_size - 40;
9bc879c1 1101 if (hashcmp(sha1, idx_sha1))
072db278
SP
1102 return error("packfile %s does not match index", p->pack_name);
1103 return 0;
9bc879c1
SP
1104}
1105
3cf8b462
SP
1106static int open_packed_git(struct packed_git *p)
1107{
1108 if (!open_packed_git_1(p))
1109 return 0;
71fe5d7f 1110 close_pack_fd(p);
3cf8b462
SP
1111 return -1;
1112}
1113
c4001d92 1114static int in_window(struct pack_window *win, off_t offset)
60bb8b14
SP
1115{
1116 /* We must promise at least 20 bytes (one hash) after the
1117 * offset is available from this window, otherwise the offset
1118 * is not actually in this window and a different window (which
1119 * has that one hash excess) must be used. This is to support
1120 * the object header and delta base parsing routines below.
1121 */
1122 off_t win_off = win->offset;
1123 return win_off <= offset
1124 && (offset + 20) <= (win_off + win->len);
1125}
1126
4b25d091 1127unsigned char *use_pack(struct packed_git *p,
03e79c88 1128 struct pack_window **w_cursor,
c4001d92 1129 off_t offset,
ef49a7a0 1130 unsigned long *left)
9bc879c1 1131{
60bb8b14 1132 struct pack_window *win = *w_cursor;
03e79c88 1133
a9d98a14 1134 /* Since packfiles end in a hash of their content and it's
60bb8b14
SP
1135 * pointless to ask for an offset into the middle of that
1136 * hash, and the in_window function above wouldn't match
1137 * don't allow an offset too close to the end of the file.
1138 */
d131b7af
SP
1139 if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
1140 die("packfile %s cannot be accessed", p->pack_name);
60bb8b14
SP
1141 if (offset > (p->pack_size - 20))
1142 die("offset beyond end of packfile (truncated pack?)");
13e0b0d3 1143 if (offset < 0)
7465feba 1144 die(_("offset before end of packfile (broken .idx?)"));
60bb8b14
SP
1145
1146 if (!win || !in_window(win, offset)) {
1147 if (win)
1148 win->inuse_cnt--;
1149 for (win = p->windows; win; win = win->next) {
1150 if (in_window(win, offset))
1151 break;
1152 }
1153 if (!win) {
78a28df9 1154 size_t window_align = packed_git_window_size / 2;
dc49cd76 1155 off_t len;
d131b7af
SP
1156
1157 if (p->pack_fd == -1 && open_packed_git(p))
1158 die("packfile %s cannot be accessed", p->pack_name);
1159
60bb8b14 1160 win = xcalloc(1, sizeof(*win));
78a28df9 1161 win->offset = (offset / window_align) * window_align;
dc49cd76
SP
1162 len = p->pack_size - win->offset;
1163 if (len > packed_git_window_size)
1164 len = packed_git_window_size;
1165 win->len = (size_t)len;
60bb8b14 1166 pack_mapped += win->len;
11daf39b 1167 while (packed_git_limit < pack_mapped
7c3ecb32 1168 && unuse_one_window(p))
60bb8b14 1169 ; /* nothing */
c4712e45 1170 win->base = xmmap(NULL, win->len,
60bb8b14
SP
1171 PROT_READ, MAP_PRIVATE,
1172 p->pack_fd, win->offset);
1173 if (win->base == MAP_FAILED)
7616c6ca
NTND
1174 die_errno("packfile %s cannot be mapped",
1175 p->pack_name);
d131b7af 1176 if (!win->offset && win->len == p->pack_size
71fe5d7f
JS
1177 && !p->do_not_close)
1178 close_pack_fd(p);
a53128b6
SP
1179 pack_mmap_calls++;
1180 pack_open_windows++;
1181 if (pack_mapped > peak_pack_mapped)
1182 peak_pack_mapped = pack_mapped;
1183 if (pack_open_windows > peak_pack_open_windows)
1184 peak_pack_open_windows = pack_open_windows;
60bb8b14
SP
1185 win->next = p->windows;
1186 p->windows = win;
1187 }
1f688557 1188 }
03e79c88
SP
1189 if (win != *w_cursor) {
1190 win->last_used = pack_used_ctr++;
1191 win->inuse_cnt++;
1192 *w_cursor = win;
1193 }
60bb8b14 1194 offset -= win->offset;
03e79c88 1195 if (left)
dc49cd76 1196 *left = win->len - xsize_t(offset);
03e79c88 1197 return win->base + offset;
1f688557
JH
1198}
1199
27d69a46
NP
1200static struct packed_git *alloc_packed_git(int extra)
1201{
50a6c8ef 1202 struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
27d69a46
NP
1203 memset(p, 0, sizeof(*p));
1204 p->pack_fd = -1;
1205 return p;
1206}
1207
e0500293
JN
1208static void try_to_free_pack_memory(size_t size)
1209{
7c3ecb32 1210 release_pack_memory(size);
e0500293
JN
1211}
1212
48bcc1c3 1213struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
1f688557 1214{
e0500293 1215 static int have_set_try_to_free_routine;
1f688557 1216 struct stat st;
48bcc1c3
JK
1217 size_t alloc;
1218 struct packed_git *p;
1f688557 1219
e0500293
JN
1220 if (!have_set_try_to_free_routine) {
1221 have_set_try_to_free_routine = 1;
1222 set_try_to_free_routine(try_to_free_pack_memory);
1223 }
1224
42873078
NP
1225 /*
1226 * Make sure a corresponding .pack file exists and that
1227 * the index looks sane.
1228 */
48bcc1c3 1229 if (!strip_suffix_mem(path, &path_len, ".idx"))
1f688557 1230 return NULL;
48bcc1c3
JK
1231
1232 /*
1233 * ".pack" is long enough to hold any suffix we're adding (and
1234 * the use xsnprintf double-checks that)
1235 */
50a6c8ef 1236 alloc = st_add3(path_len, strlen(".pack"), 1);
48bcc1c3 1237 p = alloc_packed_git(alloc);
42873078 1238 memcpy(p->pack_name, path, path_len);
8d25931d 1239
48bcc1c3 1240 xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
8d25931d
BC
1241 if (!access(p->pack_name, F_OK))
1242 p->pack_keep = 1;
1243
48bcc1c3 1244 xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
d079837e 1245 if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
42873078 1246 free(p);
1f688557
JH
1247 return NULL;
1248 }
42873078 1249
1f688557
JH
1250 /* ok, it looks sane as far as we can check without
1251 * actually mapping the pack file.
1252 */
1f688557 1253 p->pack_size = st.st_size;
9d835df2 1254 p->pack_local = local;
b867092f 1255 p->mtime = st.st_mtime;
42873078
NP
1256 if (path_len < 40 || get_sha1_hex(path + path_len - 40, p->sha1))
1257 hashclr(p->sha1);
1f688557
JH
1258 return p;
1259}
1260
7b64469a 1261struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
c508df5e 1262{
42873078 1263 const char *path = sha1_pack_name(sha1);
50a6c8ef 1264 size_t alloc = st_add(strlen(path), 1);
c7ab0ba3 1265 struct packed_git *p = alloc_packed_git(alloc);
bf592c50 1266
c7ab0ba3 1267 memcpy(p->pack_name, path, alloc); /* includes NUL */
27d69a46 1268 hashcpy(p->sha1, sha1);
42873078
NP
1269 if (check_packed_git_idx(idx_path, p)) {
1270 free(p);
bf592c50 1271 return NULL;
42873078 1272 }
bf592c50 1273
bf592c50
DB
1274 return p;
1275}
1276
1277void install_packed_git(struct packed_git *pack)
1278{
c7934306
SP
1279 if (pack->pack_fd != -1)
1280 pack_open_fds++;
1281
bf592c50
DB
1282 pack->next = packed_git;
1283 packed_git = pack;
1284}
1285
0a489b06 1286void (*report_garbage)(unsigned seen_bits, const char *path);
543c5caa
NTND
1287
1288static void report_helper(const struct string_list *list,
1289 int seen_bits, int first, int last)
1290{
0a489b06 1291 if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
543c5caa 1292 return;
0a489b06 1293
543c5caa 1294 for (; first < last; first++)
0a489b06 1295 report_garbage(seen_bits, list->items[first].string);
543c5caa
NTND
1296}
1297
1298static void report_pack_garbage(struct string_list *list)
1299{
1300 int i, baselen = -1, first = 0, seen_bits = 0;
1301
1302 if (!report_garbage)
1303 return;
1304
3383e199 1305 string_list_sort(list);
543c5caa
NTND
1306
1307 for (i = 0; i < list->nr; i++) {
1308 const char *path = list->items[i].string;
1309 if (baselen != -1 &&
1310 strncmp(path, list->items[first].string, baselen)) {
1311 report_helper(list, seen_bits, first, i);
1312 baselen = -1;
1313 seen_bits = 0;
1314 }
1315 if (baselen == -1) {
1316 const char *dot = strrchr(path, '.');
1317 if (!dot) {
0a489b06 1318 report_garbage(PACKDIR_FILE_GARBAGE, path);
543c5caa
NTND
1319 continue;
1320 }
1321 baselen = dot - path + 1;
1322 first = i;
1323 }
1324 if (!strcmp(path + baselen, "pack"))
1325 seen_bits |= 1;
1326 else if (!strcmp(path + baselen, "idx"))
1327 seen_bits |= 2;
1328 }
1329 report_helper(list, seen_bits, first, list->nr);
1330}
1331
9d835df2 1332static void prepare_packed_git_one(char *objdir, int local)
1f688557 1333{
880fb8de
RS
1334 struct strbuf path = STRBUF_INIT;
1335 size_t dirnamelen;
1f688557
JH
1336 DIR *dir;
1337 struct dirent *de;
543c5caa 1338 struct string_list garbage = STRING_LIST_INIT_DUP;
1f688557 1339
880fb8de
RS
1340 strbuf_addstr(&path, objdir);
1341 strbuf_addstr(&path, "/pack");
1342 dir = opendir(path.buf);
b5b16990 1343 if (!dir) {
26125f6b 1344 if (errno != ENOENT)
7616c6ca
NTND
1345 error_errno("unable to open object pack directory: %s",
1346 path.buf);
880fb8de 1347 strbuf_release(&path);
1f688557 1348 return;
b5b16990 1349 }
880fb8de
RS
1350 strbuf_addch(&path, '/');
1351 dirnamelen = path.len;
1f688557 1352 while ((de = readdir(dir)) != NULL) {
1f688557 1353 struct packed_git *p;
47bf4b0f 1354 size_t base_len;
543c5caa
NTND
1355
1356 if (is_dot_or_dotdot(de->d_name))
9cb18f56
JM
1357 continue;
1358
880fb8de
RS
1359 strbuf_setlen(&path, dirnamelen);
1360 strbuf_addstr(&path, de->d_name);
d90906a9 1361
47bf4b0f
JK
1362 base_len = path.len;
1363 if (strip_suffix_mem(path.buf, &base_len, ".idx")) {
d90906a9
NTND
1364 /* Don't reopen a pack we already have. */
1365 for (p = packed_git; p; p = p->next) {
47bf4b0f
JK
1366 size_t len;
1367 if (strip_suffix(p->pack_name, ".pack", &len) &&
1368 len == base_len &&
1369 !memcmp(p->pack_name, path.buf, len))
d90906a9
NTND
1370 break;
1371 }
1372 if (p == NULL &&
1373 /*
1374 * See if it really is a valid .idx file with
1375 * corresponding .pack file that we can map.
1376 */
880fb8de 1377 (p = add_packed_git(path.buf, path.len, local)) != NULL)
d90906a9 1378 install_packed_git(p);
86f7780c 1379 }
543c5caa
NTND
1380
1381 if (!report_garbage)
1382 continue;
1383
2975c770
JK
1384 if (ends_with(de->d_name, ".idx") ||
1385 ends_with(de->d_name, ".pack") ||
1386 ends_with(de->d_name, ".bitmap") ||
1387 ends_with(de->d_name, ".keep"))
880fb8de 1388 string_list_append(&garbage, path.buf);
543c5caa 1389 else
0a489b06 1390 report_garbage(PACKDIR_FILE_GARBAGE, path.buf);
1f688557 1391 }
5b35bcd5 1392 closedir(dir);
543c5caa
NTND
1393 report_pack_garbage(&garbage);
1394 string_list_clear(&garbage, 0);
880fb8de 1395 strbuf_release(&path);
1f688557
JH
1396}
1397
c4c6effa
RS
1398static void *get_next_packed_git(const void *p)
1399{
1400 return ((const struct packed_git *)p)->next;
1401}
1402
1403static void set_next_packed_git(void *p, void *next)
1404{
1405 ((struct packed_git *)p)->next = next;
1406}
1407
b867092f
JH
1408static int sort_pack(const void *a_, const void *b_)
1409{
c4c6effa
RS
1410 const struct packed_git *a = a_;
1411 const struct packed_git *b = b_;
b867092f
JH
1412 int st;
1413
1414 /*
1415 * Local packs tend to contain objects specific to our
1416 * variant of the project than remote ones. In addition,
1417 * remote ones could be on a network mounted filesystem.
1418 * Favor local ones for these reasons.
1419 */
1420 st = a->pack_local - b->pack_local;
1421 if (st)
1422 return -st;
1423
1424 /*
1425 * Younger packs tend to contain more recent objects,
1426 * and more recent objects tend to get accessed more
1427 * often.
1428 */
1429 if (a->mtime < b->mtime)
1430 return 1;
1431 else if (a->mtime == b->mtime)
1432 return 0;
1433 return -1;
1434}
1435
1436static void rearrange_packed_git(void)
1437{
c4c6effa
RS
1438 packed_git = llist_mergesort(packed_git, get_next_packed_git,
1439 set_next_packed_git, sort_pack);
b867092f
JH
1440}
1441
a73cdd21
JK
1442static void prepare_packed_git_mru(void)
1443{
1444 struct packed_git *p;
1445
1446 mru_clear(packed_git_mru);
1447 for (p = packed_git; p; p = p->next)
1448 mru_append(packed_git_mru, p);
1449}
1450
637cdd9d 1451static int prepare_packed_git_run_once = 0;
9a217f2a 1452void prepare_packed_git(void)
1f688557 1453{
d5a63b99 1454 struct alternate_object_database *alt;
1f688557 1455
637cdd9d 1456 if (prepare_packed_git_run_once)
1f688557 1457 return;
9d835df2 1458 prepare_packed_git_one(get_object_directory(), 1);
9a217f2a 1459 prepare_alt_odb();
d5a63b99 1460 for (alt = alt_odb_list; alt; alt = alt->next) {
1494e038 1461 alt->name[-1] = 0;
9d835df2 1462 prepare_packed_git_one(alt->base, 0);
1494e038 1463 alt->name[-1] = '/';
1f688557 1464 }
b867092f 1465 rearrange_packed_git();
a73cdd21 1466 prepare_packed_git_mru();
637cdd9d
JK
1467 prepare_packed_git_run_once = 1;
1468}
1469
fc04c412 1470void reprepare_packed_git(void)
637cdd9d
JK
1471{
1472 prepare_packed_git_run_once = 0;
1473 prepare_packed_git();
1f688557
JH
1474}
1475
8eca0b47
NP
1476static void mark_bad_packed_object(struct packed_git *p,
1477 const unsigned char *sha1)
1478{
1479 unsigned i;
1480 for (i = 0; i < p->num_bad_objects; i++)
50a6c8ef 1481 if (!hashcmp(sha1, p->bad_object_sha1 + GIT_SHA1_RAWSZ * i))
8eca0b47 1482 return;
50a6c8ef
JK
1483 p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
1484 st_mult(GIT_SHA1_RAWSZ,
1485 st_add(p->num_bad_objects, 1)));
1486 hashcpy(p->bad_object_sha1 + GIT_SHA1_RAWSZ * p->num_bad_objects, sha1);
8eca0b47
NP
1487 p->num_bad_objects++;
1488}
1489
b6c4cecc 1490static const struct packed_git *has_packed_and_bad(const unsigned char *sha1)
ac939109
NP
1491{
1492 struct packed_git *p;
1493 unsigned i;
1494
1495 for (p = packed_git; p; p = p->next)
1496 for (i = 0; i < p->num_bad_objects; i++)
1497 if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
b6c4cecc
JH
1498 return p;
1499 return NULL;
ac939109
NP
1500}
1501
090ea126
NTND
1502/*
1503 * With an in-core object data in "map", rehash it to make sure the
1504 * object name actually matches "sha1" to detect object corruption.
1505 * With "map" == NULL, try reading the object named with "sha1" using
1506 * the streaming interface and rehash it to do the same.
1507 */
1508int check_sha1_signature(const unsigned char *sha1, void *map,
1509 unsigned long size, const char *type)
0fcfd160
LT
1510{
1511 unsigned char real_sha1[20];
090ea126
NTND
1512 enum object_type obj_type;
1513 struct git_istream *st;
1514 git_SHA_CTX c;
1515 char hdr[32];
1516 int hdrlen;
1517
1518 if (map) {
1519 hash_sha1_file(map, size, type, real_sha1);
1520 return hashcmp(sha1, real_sha1) ? -1 : 0;
1521 }
1522
1523 st = open_istream(sha1, &obj_type, &size, NULL);
1524 if (!st)
1525 return -1;
1526
1527 /* Generate the header */
ef1286d3 1528 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(obj_type), size) + 1;
090ea126
NTND
1529
1530 /* Sha1.. */
1531 git_SHA1_Init(&c);
1532 git_SHA1_Update(&c, hdr, hdrlen);
1533 for (;;) {
1534 char buf[1024 * 16];
1535 ssize_t readlen = read_istream(st, buf, sizeof(buf));
1536
f54fac53
JK
1537 if (readlen < 0) {
1538 close_istream(st);
1539 return -1;
1540 }
090ea126
NTND
1541 if (!readlen)
1542 break;
1543 git_SHA1_Update(&c, buf, readlen);
1544 }
1545 git_SHA1_Final(real_sha1, &c);
1546 close_istream(st);
a89fccd2 1547 return hashcmp(sha1, real_sha1) ? -1 : 0;
0fcfd160
LT
1548}
1549
ec73f580 1550int git_open_noatime(const char *name)
44d1c19e
LT
1551{
1552 static int sha1_file_open_flag = O_NOATIME;
44d1c19e 1553
f2e872aa 1554 for (;;) {
dff6f280
CB
1555 int fd;
1556
1557 errno = 0;
1558 fd = open(name, O_RDONLY | sha1_file_open_flag);
44d1c19e 1559 if (fd >= 0)
f2e872aa
SP
1560 return fd;
1561
f2e872aa
SP
1562 /* Might the failure be due to O_NOATIME? */
1563 if (errno != ENOENT && sha1_file_open_flag) {
44d1c19e 1564 sha1_file_open_flag = 0;
f2e872aa
SP
1565 continue;
1566 }
1567
1568 return -1;
44d1c19e 1569 }
44d1c19e
LT
1570}
1571
052fe5ea
JK
1572static int stat_sha1_file(const unsigned char *sha1, struct stat *st)
1573{
052fe5ea
JK
1574 struct alternate_object_database *alt;
1575
30d6c6ea 1576 if (!lstat(sha1_file_name(sha1), st))
052fe5ea
JK
1577 return 0;
1578
1579 prepare_alt_odb();
1580 errno = ENOENT;
1581 for (alt = alt_odb_list; alt; alt = alt->next) {
30d6c6ea 1582 fill_sha1_path(alt->name, sha1);
052fe5ea
JK
1583 if (!lstat(alt->base, st))
1584 return 0;
1585 }
1586
1587 return -1;
1588}
1589
44d1c19e
LT
1590static int open_sha1_file(const unsigned char *sha1)
1591{
1592 int fd;
44d1c19e 1593 struct alternate_object_database *alt;
d6c8a05b 1594 int most_interesting_errno;
44d1c19e 1595
30d6c6ea 1596 fd = git_open_noatime(sha1_file_name(sha1));
44d1c19e
LT
1597 if (fd >= 0)
1598 return fd;
d6c8a05b 1599 most_interesting_errno = errno;
44d1c19e
LT
1600
1601 prepare_alt_odb();
44d1c19e 1602 for (alt = alt_odb_list; alt; alt = alt->next) {
30d6c6ea 1603 fill_sha1_path(alt->name, sha1);
f4e51683 1604 fd = git_open_noatime(alt->base);
44d1c19e
LT
1605 if (fd >= 0)
1606 return fd;
d6c8a05b
JK
1607 if (most_interesting_errno == ENOENT)
1608 most_interesting_errno = errno;
44d1c19e 1609 }
d6c8a05b 1610 errno = most_interesting_errno;
44d1c19e
LT
1611 return -1;
1612}
1613
f0270efd 1614void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
0fcfd160 1615{
0fcfd160 1616 void *map;
144bde78 1617 int fd;
ace1534d 1618
44d1c19e
LT
1619 fd = open_sha1_file(sha1);
1620 map = NULL;
1621 if (fd >= 0) {
1622 struct stat st;
0fcfd160 1623
44d1c19e
LT
1624 if (!fstat(fd, &st)) {
1625 *size = xsize_t(st.st_size);
33e42de0
MM
1626 if (!*size) {
1627 /* mmap() is forbidden on empty files */
1628 error("object file %s is empty", sha1_file_name(sha1));
1629 return NULL;
1630 }
44d1c19e 1631 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
144bde78 1632 }
44d1c19e 1633 close(fd);
0fcfd160 1634 }
0fcfd160
LT
1635 return map;
1636}
1637
09ded04b
NP
1638unsigned long unpack_object_header_buffer(const unsigned char *buf,
1639 unsigned long len, enum object_type *type, unsigned long *sizep)
c4483576 1640{
ad1ed5ee 1641 unsigned shift;
48fb7deb 1642 unsigned long size, c;
ad1ed5ee
JH
1643 unsigned long used = 0;
1644
1645 c = buf[used++];
1646 *type = (c >> 4) & 7;
1647 size = c & 15;
1648 shift = 4;
1649 while (c & 0x80) {
f630cfda 1650 if (len <= used || bitsizeof(long) <= shift) {
09ded04b 1651 error("bad object header");
ea4f9685
JH
1652 size = used = 0;
1653 break;
09ded04b 1654 }
ad1ed5ee
JH
1655 c = buf[used++];
1656 size += (c & 0x7f) << shift;
1657 shift += 7;
1658 }
1659 *sizep = size;
1660 return used;
1661}
1662
d21f8426
JH
1663static int unpack_sha1_short_header(git_zstream *stream,
1664 unsigned char *map, unsigned long mapsize,
1665 void *buffer, unsigned long bufsiz)
ad1ed5ee 1666{
c4483576
LT
1667 /* Get the data stream */
1668 memset(stream, 0, sizeof(*stream));
1669 stream->next_in = map;
1670 stream->avail_in = mapsize;
1671 stream->next_out = buffer;
93821bd9
LT
1672 stream->avail_out = bufsiz;
1673
39c68542 1674 git_inflate_init(stream);
cc5c54e7 1675 return git_inflate(stream, 0);
c4483576
LT
1676}
1677
d21f8426
JH
1678int unpack_sha1_header(git_zstream *stream,
1679 unsigned char *map, unsigned long mapsize,
1680 void *buffer, unsigned long bufsiz)
1681{
1682 int status = unpack_sha1_short_header(stream, map, mapsize,
1683 buffer, bufsiz);
1684
1685 if (status < Z_OK)
1686 return status;
1687
1688 /* Make sure we have the terminating NUL */
1689 if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1690 return -1;
1691 return 0;
1692}
1693
46f03448
KN
1694static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
1695 unsigned long mapsize, void *buffer,
1696 unsigned long bufsiz, struct strbuf *header)
1697{
1698 int status;
1699
d21f8426
JH
1700 status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
1701 if (status < Z_OK)
1702 return -1;
46f03448
KN
1703
1704 /*
1705 * Check if entire header is unpacked in the first iteration.
1706 */
1707 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1708 return 0;
1709
1710 /*
1711 * buffer[0..bufsiz] was not large enough. Copy the partial
1712 * result out to header, and then append the result of further
1713 * reading the stream.
1714 */
1715 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1716 stream->next_out = buffer;
1717 stream->avail_out = bufsiz;
1718
1719 do {
1720 status = git_inflate(stream, 0);
1721 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1722 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1723 return 0;
1724 stream->next_out = buffer;
1725 stream->avail_out = bufsiz;
1726 } while (status != Z_STREAM_END);
1727 return -1;
1728}
1729
ef49a7a0 1730static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
5180cacc
LT
1731{
1732 int bytes = strlen(buffer) + 1;
3aee68aa 1733 unsigned char *buf = xmallocz(size);
93821bd9 1734 unsigned long n;
7efbff75 1735 int status = Z_OK;
5180cacc 1736
93821bd9
LT
1737 n = stream->total_out - bytes;
1738 if (n > size)
1739 n = size;
1740 memcpy(buf, (char *) buffer + bytes, n);
1741 bytes = n;
456cdf6e
LT
1742 if (bytes <= size) {
1743 /*
1744 * The above condition must be (bytes <= size), not
1745 * (bytes < size). In other words, even though we
ccf5ace0 1746 * expect no more output and set avail_out to zero,
456cdf6e
LT
1747 * the input zlib stream may have bytes that express
1748 * "this concludes the stream", and we *do* want to
1749 * eat that input.
1750 *
1751 * Otherwise we would not be able to test that we
1752 * consumed all the input to reach the expected size;
1753 * we also want to check that zlib tells us that all
1754 * went well with status == Z_STREAM_END at the end.
1755 */
5180cacc
LT
1756 stream->next_out = buf + bytes;
1757 stream->avail_out = size - bytes;
7efbff75 1758 while (status == Z_OK)
39c68542 1759 status = git_inflate(stream, Z_FINISH);
5180cacc 1760 }
456cdf6e 1761 if (status == Z_STREAM_END && !stream->avail_in) {
39c68542 1762 git_inflate_end(stream);
7efbff75
JH
1763 return buf;
1764 }
1765
1766 if (status < 0)
1767 error("corrupt loose object '%s'", sha1_to_hex(sha1));
1768 else if (stream->avail_in)
1769 error("garbage at end of loose object '%s'",
1770 sha1_to_hex(sha1));
1771 free(buf);
1772 return NULL;
5180cacc
LT
1773}
1774
1775/*
1776 * We used to just use "sscanf()", but that's actually way
1777 * too permissive for what we want to check. So do an anal
1778 * object header parse by hand.
1779 */
46f03448
KN
1780static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1781 unsigned int flags)
5180cacc 1782{
46f03448 1783 const char *type_buf = hdr;
5180cacc 1784 unsigned long size;
46f03448 1785 int type, type_len = 0;
5180cacc
LT
1786
1787 /*
46f03448 1788 * The type can be of any size but is followed by
21666f1a 1789 * a space.
5180cacc 1790 */
5180cacc
LT
1791 for (;;) {
1792 char c = *hdr++;
d21f8426
JH
1793 if (!c)
1794 return -1;
5180cacc
LT
1795 if (c == ' ')
1796 break;
46f03448 1797 type_len++;
5180cacc 1798 }
46f03448
KN
1799
1800 type = type_from_string_gently(type_buf, type_len, 1);
1801 if (oi->typename)
1802 strbuf_add(oi->typename, type_buf, type_len);
1803 /*
1804 * Set type to 0 if its an unknown object and
2e3a16b2 1805 * we're obtaining the type using '--allow-unknown-type'
46f03448
KN
1806 * option.
1807 */
1808 if ((flags & LOOKUP_UNKNOWN_OBJECT) && (type < 0))
1809 type = 0;
1810 else if (type < 0)
1811 die("invalid object type");
1812 if (oi->typep)
1813 *oi->typep = type;
5180cacc
LT
1814
1815 /*
1816 * The length must follow immediately, and be in canonical
1817 * decimal format (ie "010" is not valid).
1818 */
1819 size = *hdr++ - '0';
1820 if (size > 9)
1821 return -1;
1822 if (size) {
1823 for (;;) {
1824 unsigned long c = *hdr - '0';
1825 if (c > 9)
1826 break;
1827 hdr++;
1828 size = size * 10 + c;
1829 }
1830 }
46f03448
KN
1831
1832 if (oi->sizep)
1833 *oi->sizep = size;
5180cacc
LT
1834
1835 /*
1836 * The length must be followed by a zero byte
1837 */
46f03448
KN
1838 return *hdr ? -1 : type;
1839}
1840
1841int parse_sha1_header(const char *hdr, unsigned long *sizep)
1842{
1843 struct object_info oi;
1844
1845 oi.sizep = sizep;
1846 oi.typename = NULL;
1847 oi.typep = NULL;
1848 return parse_sha1_header_extended(hdr, &oi, LOOKUP_REPLACE_OBJECT);
5180cacc
LT
1849}
1850
7efbff75 1851static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
0fcfd160 1852{
5180cacc 1853 int ret;
ef49a7a0 1854 git_zstream stream;
5180cacc 1855 char hdr[8192];
0fcfd160 1856
5180cacc 1857 ret = unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr));
21666f1a 1858 if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0)
0fcfd160
LT
1859 return NULL;
1860
7efbff75 1861 return unpack_sha1_rest(&stream, hdr, *size, sha1);
0fcfd160
LT
1862}
1863
54dab52a
NP
1864unsigned long get_size_from_delta(struct packed_git *p,
1865 struct pack_window **w_curs,
1866 off_t curpos)
1867{
1868 const unsigned char *data;
1869 unsigned char delta_head[20], *in;
ef49a7a0 1870 git_zstream stream;
54dab52a
NP
1871 int st;
1872
1873 memset(&stream, 0, sizeof(stream));
1874 stream.next_out = delta_head;
1875 stream.avail_out = sizeof(delta_head);
1876
39c68542 1877 git_inflate_init(&stream);
54dab52a
NP
1878 do {
1879 in = use_pack(p, w_curs, curpos, &stream.avail_in);
1880 stream.next_in = in;
39c68542 1881 st = git_inflate(&stream, Z_FINISH);
54dab52a
NP
1882 curpos += stream.next_in - in;
1883 } while ((st == Z_OK || st == Z_BUF_ERROR) &&
1884 stream.total_out < sizeof(delta_head));
39c68542 1885 git_inflate_end(&stream);
3d77d877
NP
1886 if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
1887 error("delta data unpack-initial failed");
1888 return 0;
1889 }
54dab52a
NP
1890
1891 /* Examine the initial part of the delta to figure out
1892 * the result size.
1893 */
1894 data = delta_head;
1895
1896 /* ignore base size */
1897 get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1898
1899 /* Read the result size */
1900 return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1901}
1902
c4001d92 1903static off_t get_delta_base(struct packed_git *p,
03e79c88 1904 struct pack_window **w_curs,
c4001d92 1905 off_t *curpos,
21666f1a 1906 enum object_type type,
c4001d92 1907 off_t delta_obj_offset)
eb32d236 1908{
2b87c45b 1909 unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
c4001d92 1910 off_t base_offset;
eb32d236 1911
8d8a4ea5
SP
1912 /* use_pack() assured us we have [base_info, base_info + 20)
1913 * as a range that we can look at without walking off the
1914 * end of the mapped window. Its actually the hash size
1915 * that is assured. An OFS_DELTA longer than the hash size
1916 * is stupid, as then a REF_DELTA would be smaller to store.
1917 */
21666f1a 1918 if (type == OBJ_OFS_DELTA) {
eb32d236
NP
1919 unsigned used = 0;
1920 unsigned char c = base_info[used++];
1921 base_offset = c & 127;
1922 while (c & 128) {
1923 base_offset += 1;
8723f216 1924 if (!base_offset || MSB(base_offset, 7))
8eca0b47 1925 return 0; /* overflow */
eb32d236
NP
1926 c = base_info[used++];
1927 base_offset = (base_offset << 7) + (c & 127);
1928 }
1929 base_offset = delta_obj_offset - base_offset;
d8f32556 1930 if (base_offset <= 0 || base_offset >= delta_obj_offset)
8eca0b47 1931 return 0; /* out of bound */
2b87c45b 1932 *curpos += used;
21666f1a 1933 } else if (type == OBJ_REF_DELTA) {
eb32d236
NP
1934 /* The base entry _must_ be in the same pack */
1935 base_offset = find_pack_entry_one(base_info, p);
2b87c45b 1936 *curpos += 20;
eb32d236
NP
1937 } else
1938 die("I am totally screwed");
2b87c45b 1939 return base_offset;
eb32d236
NP
1940}
1941
5d642e75
JK
1942/*
1943 * Like get_delta_base above, but we return the sha1 instead of the pack
1944 * offset. This means it is cheaper for REF deltas (we do not have to do
1945 * the final object lookup), but more expensive for OFS deltas (we
1946 * have to load the revidx to convert the offset back into a sha1).
1947 */
1948static const unsigned char *get_delta_base_sha1(struct packed_git *p,
1949 struct pack_window **w_curs,
1950 off_t curpos,
1951 enum object_type type,
1952 off_t delta_obj_offset)
1953{
1954 if (type == OBJ_REF_DELTA) {
1955 unsigned char *base = use_pack(p, w_curs, curpos, NULL);
1956 return base;
1957 } else if (type == OBJ_OFS_DELTA) {
1958 struct revindex_entry *revidx;
1959 off_t base_offset = get_delta_base(p, w_curs, &curpos,
1960 type, delta_obj_offset);
1961
1962 if (!base_offset)
1963 return NULL;
1964
1965 revidx = find_pack_revindex(p, base_offset);
1966 if (!revidx)
1967 return NULL;
1968
1969 return nth_packed_object_sha1(p, revidx->nr);
1970 } else
1971 return NULL;
1972}
1973
f8c8abc5
JH
1974int unpack_object_header(struct packed_git *p,
1975 struct pack_window **w_curs,
1976 off_t *curpos,
1977 unsigned long *sizep)
a733cb60 1978{
03e79c88 1979 unsigned char *base;
ef49a7a0 1980 unsigned long left;
ad1ed5ee 1981 unsigned long used;
2b87c45b 1982 enum object_type type;
a733cb60 1983
8d8a4ea5 1984 /* use_pack() assures us we have [base, base + 20) available
0353a0c4 1985 * as a range that we can look at. (Its actually the hash
3dff5379 1986 * size that is assured.) With our object header encoding
8d8a4ea5
SP
1987 * the maximum deflated object size is 2^137, which is just
1988 * insane, so we know won't exceed what we have been given.
1989 */
2b87c45b 1990 base = use_pack(p, w_curs, *curpos, &left);
09ded04b
NP
1991 used = unpack_object_header_buffer(base, left, &type, sizep);
1992 if (!used) {
1993 type = OBJ_BAD;
1994 } else
1995 *curpos += used;
ad1ed5ee 1996
2b87c45b 1997 return type;
a733cb60
LT
1998}
1999
790d96c0
TR
2000static int retry_bad_packed_offset(struct packed_git *p, off_t obj_offset)
2001{
2002 int type;
2003 struct revindex_entry *revidx;
2004 const unsigned char *sha1;
2005 revidx = find_pack_revindex(p, obj_offset);
2006 if (!revidx)
2007 return OBJ_BAD;
2008 sha1 = nth_packed_object_sha1(p, revidx->nr);
2009 mark_bad_packed_object(p, sha1);
2010 type = sha1_object_info(sha1, NULL);
2011 if (type <= OBJ_NONE)
2012 return OBJ_BAD;
2013 return type;
2014}
2015
790d96c0
TR
2016#define POI_STACK_PREALLOC 64
2017
90191d37
JK
2018static enum object_type packed_to_object_type(struct packed_git *p,
2019 off_t obj_offset,
2020 enum object_type type,
2021 struct pack_window **w_curs,
2022 off_t curpos)
1f688557 2023{
790d96c0
TR
2024 off_t small_poi_stack[POI_STACK_PREALLOC];
2025 off_t *poi_stack = small_poi_stack;
2026 int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
5db47c2b 2027
790d96c0
TR
2028 while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
2029 off_t base_offset;
90191d37 2030 unsigned long size;
790d96c0
TR
2031 /* Push the object we're going to leave behind */
2032 if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
2033 poi_stack_alloc = alloc_nr(poi_stack_nr);
b32fa95f 2034 ALLOC_ARRAY(poi_stack, poi_stack_alloc);
790d96c0
TR
2035 memcpy(poi_stack, small_poi_stack, sizeof(off_t)*poi_stack_nr);
2036 } else {
2037 ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
2038 }
2039 poi_stack[poi_stack_nr++] = obj_offset;
2040 /* If parsing the base offset fails, just unwind */
90191d37 2041 base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
790d96c0
TR
2042 if (!base_offset)
2043 goto unwind;
2044 curpos = obj_offset = base_offset;
90191d37 2045 type = unpack_object_header(p, w_curs, &curpos, &size);
790d96c0
TR
2046 if (type <= OBJ_NONE) {
2047 /* If getting the base itself fails, we first
2048 * retry the base, otherwise unwind */
2049 type = retry_bad_packed_offset(p, base_offset);
2050 if (type > OBJ_NONE)
2051 goto out;
2052 goto unwind;
2053 }
2054 }
2055
21666f1a 2056 switch (type) {
790d96c0 2057 case OBJ_BAD:
a733cb60 2058 case OBJ_COMMIT:
a733cb60 2059 case OBJ_TREE:
a733cb60 2060 case OBJ_BLOB:
a733cb60 2061 case OBJ_TAG:
a69d0943 2062 break;
1f688557 2063 default:
3d77d877
NP
2064 error("unknown object type %i at offset %"PRIuMAX" in %s",
2065 type, (uintmax_t)obj_offset, p->pack_name);
2066 type = OBJ_BAD;
1f688557 2067 }
790d96c0
TR
2068
2069out:
2070 if (poi_stack != small_poi_stack)
2071 free(poi_stack);
21666f1a 2072 return type;
790d96c0
TR
2073
2074unwind:
2075 while (poi_stack_nr) {
2076 obj_offset = poi_stack[--poi_stack_nr];
2077 type = retry_bad_packed_offset(p, obj_offset);
2078 if (type > OBJ_NONE)
2079 goto out;
2080 }
2081 type = OBJ_BAD;
2082 goto out;
1f688557
JH
2083}
2084
90191d37 2085static int packed_object_info(struct packed_git *p, off_t obj_offset,
23c339c0 2086 struct object_info *oi)
90191d37
JK
2087{
2088 struct pack_window *w_curs = NULL;
2089 unsigned long size;
2090 off_t curpos = obj_offset;
2091 enum object_type type;
2092
412916ee
JK
2093 /*
2094 * We always get the representation type, but only convert it to
2095 * a "real" type later if the caller is interested.
2096 */
90191d37
JK
2097 type = unpack_object_header(p, &w_curs, &curpos, &size);
2098
23c339c0 2099 if (oi->sizep) {
90191d37
JK
2100 if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
2101 off_t tmp_pos = curpos;
2102 off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
2103 type, obj_offset);
2104 if (!base_offset) {
2105 type = OBJ_BAD;
2106 goto out;
2107 }
23c339c0
JK
2108 *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos);
2109 if (*oi->sizep == 0) {
90191d37
JK
2110 type = OBJ_BAD;
2111 goto out;
2112 }
2113 } else {
23c339c0 2114 *oi->sizep = size;
90191d37
JK
2115 }
2116 }
2117
23c339c0 2118 if (oi->disk_sizep) {
90191d37 2119 struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
23c339c0 2120 *oi->disk_sizep = revidx[1].offset - obj_offset;
90191d37
JK
2121 }
2122
23c339c0
JK
2123 if (oi->typep) {
2124 *oi->typep = packed_to_object_type(p, obj_offset, type, &w_curs, curpos);
2125 if (*oi->typep < 0) {
412916ee
JK
2126 type = OBJ_BAD;
2127 goto out;
2128 }
2129 }
90191d37 2130
5d642e75
JK
2131 if (oi->delta_base_sha1) {
2132 if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
2133 const unsigned char *base;
2134
2135 base = get_delta_base_sha1(p, &w_curs, curpos,
2136 type, obj_offset);
2137 if (!base) {
2138 type = OBJ_BAD;
2139 goto out;
2140 }
2141
2142 hashcpy(oi->delta_base_sha1, base);
2143 } else
2144 hashclr(oi->delta_base_sha1);
2145 }
2146
90191d37
JK
2147out:
2148 unuse_pack(&w_curs);
2149 return type;
2150}
2151
eb950c19 2152static void *unpack_compressed_entry(struct packed_git *p,
03e79c88 2153 struct pack_window **w_curs,
c4001d92 2154 off_t curpos,
eb950c19 2155 unsigned long size)
de530aaa
SP
2156{
2157 int st;
ef49a7a0 2158 git_zstream stream;
079afb18 2159 unsigned char *buffer, *in;
de530aaa 2160
735efde8
NTND
2161 buffer = xmallocz_gently(size);
2162 if (!buffer)
2163 return NULL;
de530aaa 2164 memset(&stream, 0, sizeof(stream));
de530aaa 2165 stream.next_out = buffer;
39eea7bd 2166 stream.avail_out = size + 1;
de530aaa 2167
39c68542 2168 git_inflate_init(&stream);
079afb18 2169 do {
2b87c45b 2170 in = use_pack(p, w_curs, curpos, &stream.avail_in);
079afb18 2171 stream.next_in = in;
39c68542 2172 st = git_inflate(&stream, Z_FINISH);
39eea7bd
JH
2173 if (!stream.avail_out)
2174 break; /* the payload is larger than it should be */
2b87c45b 2175 curpos += stream.next_in - in;
079afb18 2176 } while (st == Z_OK || st == Z_BUF_ERROR);
39c68542 2177 git_inflate_end(&stream);
de530aaa
SP
2178 if ((st != Z_STREAM_END) || stream.total_out != size) {
2179 free(buffer);
2180 return NULL;
2181 }
2182
2183 return buffer;
2184}
2185
8261e1f1 2186static struct hashmap delta_base_cache;
18bdec11 2187static size_t delta_base_cached;
5e08ecbf 2188
12d95ef6 2189static LIST_HEAD(delta_base_cache_lru);
5e08ecbf 2190
8261e1f1 2191struct delta_base_cache_key {
e5e01619
LT
2192 struct packed_git *p;
2193 off_t base_offset;
8261e1f1
JK
2194};
2195
2196struct delta_base_cache_entry {
2197 struct hashmap hash;
2198 struct delta_base_cache_key key;
2199 struct list_head lru;
2200 void *data;
e5e01619 2201 unsigned long size;
e5e01619 2202 enum object_type type;
8261e1f1 2203};
e5e01619 2204
8261e1f1 2205static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
e5e01619 2206{
8261e1f1 2207 unsigned int hash;
e5e01619 2208
8261e1f1 2209 hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
e5e01619 2210 hash += (hash >> 8) + (hash >> 16);
8261e1f1 2211 return hash;
e5e01619
LT
2212}
2213
84dd81c1
TR
2214static struct delta_base_cache_entry *
2215get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
5266d369 2216{
8261e1f1
JK
2217 struct hashmap_entry entry;
2218 struct delta_base_cache_key key;
2219
2220 if (!delta_base_cache.cmpfn)
2221 return NULL;
2222
2223 hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
2224 key.p = p;
2225 key.base_offset = base_offset;
2226 return hashmap_get(&delta_base_cache, &entry, &key);
2227}
2228
2229static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
2230 const struct delta_base_cache_key *b)
2231{
2232 return a->p == b->p && a->base_offset == b->base_offset;
84dd81c1
TR
2233}
2234
8261e1f1
JK
2235static int delta_base_cache_hash_cmp(const void *va, const void *vb,
2236 const void *vkey)
84dd81c1 2237{
8261e1f1
JK
2238 const struct delta_base_cache_entry *a = va, *b = vb;
2239 const struct delta_base_cache_key *key = vkey;
2240 if (key)
2241 return !delta_base_cache_key_eq(&a->key, key);
2242 else
2243 return !delta_base_cache_key_eq(&a->key, &b->key);
5266d369
JH
2244}
2245
84dd81c1
TR
2246static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
2247{
8261e1f1 2248 return !!get_delta_base_cache_entry(p, base_offset);
84dd81c1
TR
2249}
2250
4a5397ca
JK
2251/*
2252 * Remove the entry from the cache, but do _not_ free the associated
2253 * entry data. The caller takes ownership of the "data" buffer, and
2254 * should copy out any fields it wants before detaching.
2255 */
2256static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
84dd81c1 2257{
8261e1f1 2258 hashmap_remove(&delta_base_cache, ent, &ent->key);
12d95ef6 2259 list_del(&ent->lru);
84dd81c1 2260 delta_base_cached -= ent->size;
8261e1f1 2261 free(ent);
84dd81c1
TR
2262}
2263
62f255ad 2264static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
85fe35ab 2265 unsigned long *base_size, enum object_type *type)
62f255ad 2266{
84dd81c1 2267 struct delta_base_cache_entry *ent;
e5e01619 2268
84dd81c1 2269 ent = get_delta_base_cache_entry(p, base_offset);
8261e1f1 2270 if (!ent)
749bc58c 2271 return unpack_entry(p, base_offset, type, base_size);
e5e01619 2272
e5e01619
LT
2273 *type = ent->type;
2274 *base_size = ent->size;
85fe35ab 2275 return xmemdupz(ent->data, ent->size);
62f255ad
LT
2276}
2277
18bdec11
SP
2278static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
2279{
8261e1f1
JK
2280 free(ent->data);
2281 detach_delta_base_cache_entry(ent);
18bdec11
SP
2282}
2283
3d20c636
SP
2284void clear_delta_base_cache(void)
2285{
8261e1f1
JK
2286 struct hashmap_iter iter;
2287 struct delta_base_cache_entry *entry;
2288 for (entry = hashmap_iter_first(&delta_base_cache, &iter);
2289 entry;
2290 entry = hashmap_iter_next(&iter)) {
2291 release_delta_base_cache(entry);
2292 }
3d20c636
SP
2293}
2294
62f255ad
LT
2295static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
2296 void *base, unsigned long base_size, enum object_type type)
2297{
8261e1f1 2298 struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
4b92bae7 2299 struct list_head *lru, *tmp;
e5e01619 2300
18bdec11 2301 delta_base_cached += base_size;
5e08ecbf 2302
4b92bae7 2303 list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
12d95ef6
JK
2304 struct delta_base_cache_entry *f =
2305 list_entry(lru, struct delta_base_cache_entry, lru);
2306 if (delta_base_cached <= delta_base_cache_limit)
2307 break;
5e08ecbf
NP
2308 release_delta_base_cache(f);
2309 }
18bdec11 2310
8261e1f1
JK
2311 ent->key.p = p;
2312 ent->key.base_offset = base_offset;
e5e01619
LT
2313 ent->type = type;
2314 ent->data = base;
2315 ent->size = base_size;
12d95ef6 2316 list_add_tail(&ent->lru, &delta_base_cache_lru);
8261e1f1
JK
2317
2318 if (!delta_base_cache.cmpfn)
2319 hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, 0);
2320 hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
2321 hashmap_add(&delta_base_cache, ent);
62f255ad
LT
2322}
2323
c2c5b270
CC
2324static void *read_object(const unsigned char *sha1, enum object_type *type,
2325 unsigned long *size);
2326
5f44324d
JH
2327static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
2328{
67dc598e
KB
2329 static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
2330 trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
2331 p->pack_name, (uintmax_t)obj_offset);
5f44324d
JH
2332}
2333
0e8189e2
NP
2334int do_check_packed_object_crc;
2335
abe601bb
TR
2336#define UNPACK_ENTRY_STACK_PREALLOC 64
2337struct unpack_entry_stack_ent {
2338 off_t obj_offset;
2339 off_t curpos;
2340 unsigned long size;
2341};
2342
c4001d92 2343void *unpack_entry(struct packed_git *p, off_t obj_offset,
abe601bb 2344 enum object_type *final_type, unsigned long *final_size)
1f688557 2345{
03e79c88 2346 struct pack_window *w_curs = NULL;
c4001d92 2347 off_t curpos = obj_offset;
abe601bb
TR
2348 void *data = NULL;
2349 unsigned long size;
2350 enum object_type type;
2351 struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
2352 struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
2353 int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
2354 int base_from_cache = 0;
1f688557 2355
67dc598e 2356 write_pack_access_log(p, obj_offset);
5f44324d 2357
abe601bb
TR
2358 /* PHASE 1: drill down to the innermost base object */
2359 for (;;) {
2360 off_t base_offset;
2361 int i;
2362 struct delta_base_cache_entry *ent;
2363
77965f8b 2364 ent = get_delta_base_cache_entry(p, curpos);
8261e1f1 2365 if (ent) {
77965f8b
NTND
2366 type = ent->type;
2367 data = ent->data;
2368 size = ent->size;
4a5397ca 2369 detach_delta_base_cache_entry(ent);
77965f8b
NTND
2370 base_from_cache = 1;
2371 break;
2372 }
2373
abe601bb
TR
2374 if (do_check_packed_object_crc && p->index_version > 1) {
2375 struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
211c61c6 2376 off_t len = revidx[1].offset - obj_offset;
abe601bb
TR
2377 if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
2378 const unsigned char *sha1 =
2379 nth_packed_object_sha1(p, revidx->nr);
2380 error("bad packed object CRC for %s",
2381 sha1_to_hex(sha1));
2382 mark_bad_packed_object(p, sha1);
2383 unuse_pack(&w_curs);
2384 return NULL;
2385 }
2386 }
2387
abe601bb
TR
2388 type = unpack_object_header(p, &w_curs, &curpos, &size);
2389 if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
2390 break;
2391
2392 base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
2393 if (!base_offset) {
2394 error("failed to validate delta base reference "
2395 "at offset %"PRIuMAX" from %s",
2396 (uintmax_t)curpos, p->pack_name);
2397 /* bail to phase 2, in hopes of recovery */
2398 data = NULL;
2399 break;
2400 }
2401
2402 /* push object, proceed to base */
2403 if (delta_stack_nr >= delta_stack_alloc
2404 && delta_stack == small_delta_stack) {
2405 delta_stack_alloc = alloc_nr(delta_stack_nr);
b32fa95f 2406 ALLOC_ARRAY(delta_stack, delta_stack_alloc);
abe601bb
TR
2407 memcpy(delta_stack, small_delta_stack,
2408 sizeof(*delta_stack)*delta_stack_nr);
2409 } else {
2410 ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
2411 }
2412 i = delta_stack_nr++;
2413 delta_stack[i].obj_offset = obj_offset;
2414 delta_stack[i].curpos = curpos;
2415 delta_stack[i].size = size;
2416
2417 curpos = obj_offset = base_offset;
0e8189e2
NP
2418 }
2419
abe601bb
TR
2420 /* PHASE 2: handle the base */
2421 switch (type) {
eb32d236
NP
2422 case OBJ_OFS_DELTA:
2423 case OBJ_REF_DELTA:
abe601bb 2424 if (data)
ef1177d1 2425 die("BUG: unpack_entry: left loop at a valid delta");
4d703a1a 2426 break;
a733cb60 2427 case OBJ_COMMIT:
a733cb60 2428 case OBJ_TREE:
a733cb60 2429 case OBJ_BLOB:
a733cb60 2430 case OBJ_TAG:
abe601bb
TR
2431 if (!base_from_cache)
2432 data = unpack_compressed_entry(p, &w_curs, curpos, size);
4d703a1a 2433 break;
1f688557 2434 default:
8eca0b47
NP
2435 data = NULL;
2436 error("unknown object type %i at offset %"PRIuMAX" in %s",
abe601bb
TR
2437 type, (uintmax_t)obj_offset, p->pack_name);
2438 }
2439
2440 /* PHASE 3: apply deltas in order */
2441
2442 /* invariants:
2443 * 'data' holds the base data, or NULL if there was corruption
2444 */
2445 while (delta_stack_nr) {
2446 void *delta_data;
2447 void *base = data;
2448 unsigned long delta_size, base_size = size;
2449 int i;
2450
2451 data = NULL;
2452
2453 if (base)
2454 add_delta_base_cache(p, obj_offset, base, base_size, type);
2455
2456 if (!base) {
2457 /*
2458 * We're probably in deep shit, but let's try to fetch
2459 * the required base anyway from another pack or loose.
2460 * This is costly but should happen only in the presence
2461 * of a corrupted pack, and is better than failing outright.
2462 */
2463 struct revindex_entry *revidx;
2464 const unsigned char *base_sha1;
2465 revidx = find_pack_revindex(p, obj_offset);
2466 if (revidx) {
2467 base_sha1 = nth_packed_object_sha1(p, revidx->nr);
2468 error("failed to read delta base object %s"
2469 " at offset %"PRIuMAX" from %s",
2470 sha1_to_hex(base_sha1), (uintmax_t)obj_offset,
2471 p->pack_name);
2472 mark_bad_packed_object(p, base_sha1);
2473 base = read_object(base_sha1, &type, &base_size);
2474 }
2475 }
2476
2477 i = --delta_stack_nr;
2478 obj_offset = delta_stack[i].obj_offset;
2479 curpos = delta_stack[i].curpos;
2480 delta_size = delta_stack[i].size;
2481
2482 if (!base)
2483 continue;
2484
2485 delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
2486
2487 if (!delta_data) {
2488 error("failed to unpack compressed delta "
2489 "at offset %"PRIuMAX" from %s",
2490 (uintmax_t)curpos, p->pack_name);
abe601bb
TR
2491 data = NULL;
2492 continue;
2493 }
2494
2495 data = patch_delta(base, base_size,
2496 delta_data, delta_size,
2497 &size);
1ee886c1
JK
2498
2499 /*
2500 * We could not apply the delta; warn the user, but keep going.
2501 * Our failure will be noticed either in the next iteration of
2502 * the loop, or if this is the final delta, in the caller when
2503 * we return NULL. Those code paths will take care of making
2504 * a more explicit warning and retrying with another copy of
2505 * the object.
2506 */
abe601bb 2507 if (!data)
1ee886c1 2508 error("failed to apply delta");
abe601bb 2509
4b8f772c 2510 free(delta_data);
1f688557 2511 }
abe601bb
TR
2512
2513 *final_type = type;
2514 *final_size = size;
2515
03e79c88 2516 unuse_pack(&w_curs);
019d1e65
NTND
2517
2518 if (delta_stack != small_delta_stack)
2519 free(delta_stack);
2520
21666f1a 2521 return data;
1f688557
JH
2522}
2523
d079837e 2524const unsigned char *nth_packed_object_sha1(struct packed_git *p,
d72308e0 2525 uint32_t n)
9a217f2a 2526{
42873078 2527 const unsigned char *index = p->index_data;
d079837e
SP
2528 if (!index) {
2529 if (open_pack_index(p))
2530 return NULL;
2531 index = p->index_data;
2532 }
57059091 2533 if (n >= p->num_objects)
d72308e0 2534 return NULL;
74e34e1f
NP
2535 index += 4 * 256;
2536 if (p->index_version == 1) {
2537 return index + 24 * n + 4;
2538 } else {
2539 index += 8;
2540 return index + 20 * n;
2541 }
2542}
2543
47fe3f6e
JK
2544void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
2545{
2546 const unsigned char *ptr = vptr;
2547 const unsigned char *start = p->index_data;
2548 const unsigned char *end = start + p->index_size;
2549 if (ptr < start)
7465feba 2550 die(_("offset before start of pack index for %s (corrupt index?)"),
47fe3f6e
JK
2551 p->pack_name);
2552 /* No need to check for underflow; .idx files must be at least 8 bytes */
2553 if (ptr >= end - 8)
7465feba 2554 die(_("offset beyond end of pack index for %s (truncated index?)"),
47fe3f6e
JK
2555 p->pack_name);
2556}
2557
99093238 2558off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
74e34e1f
NP
2559{
2560 const unsigned char *index = p->index_data;
2561 index += 4 * 256;
2562 if (p->index_version == 1) {
2563 return ntohl(*((uint32_t *)(index + 24 * n)));
2564 } else {
2565 uint32_t off;
2566 index += 8 + p->num_objects * (20 + 4);
2567 off = ntohl(*((uint32_t *)(index + 4 * n)));
2568 if (!(off & 0x80000000))
2569 return off;
2570 index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
47fe3f6e 2571 check_pack_index_ptr(p, index);
74e34e1f
NP
2572 return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
2573 ntohl(*((uint32_t *)(index + 4)));
2574 }
9a217f2a
JH
2575}
2576
c4001d92 2577off_t find_pack_entry_one(const unsigned char *sha1,
43057304 2578 struct packed_git *p)
1f688557 2579{
42873078 2580 const uint32_t *level1_ofs = p->index_data;
42873078 2581 const unsigned char *index = p->index_data;
628522ec
JH
2582 unsigned hi, lo, stride;
2583 static int use_lookup = -1;
2584 static int debug_lookup = -1;
2585
2586 if (debug_lookup < 0)
2587 debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
42873078 2588
d079837e
SP
2589 if (!index) {
2590 if (open_pack_index(p))
2591 return 0;
2592 level1_ofs = p->index_data;
2593 index = p->index_data;
2594 }
74e34e1f
NP
2595 if (p->index_version > 1) {
2596 level1_ofs += 2;
2597 index += 8;
2598 }
42873078 2599 index += 4 * 256;
74e34e1f
NP
2600 hi = ntohl(level1_ofs[*sha1]);
2601 lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
628522ec
JH
2602 if (p->index_version > 1) {
2603 stride = 20;
2604 } else {
2605 stride = 24;
2606 index += 4;
2607 }
2608
2609 if (debug_lookup)
6e1c2344 2610 printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
628522ec
JH
2611 sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
2612
2613 if (use_lookup < 0)
2614 use_lookup = !!getenv("GIT_USE_LOOKUP");
2615 if (use_lookup) {
2616 int pos = sha1_entry_pos(index, stride, 0,
2617 lo, hi, p->num_objects, sha1);
2618 if (pos < 0)
2619 return 0;
2620 return nth_packed_object_offset(p, pos);
2621 }
1f688557
JH
2622
2623 do {
74e34e1f 2624 unsigned mi = (lo + hi) / 2;
628522ec
JH
2625 int cmp = hashcmp(index + mi * stride, sha1);
2626
2627 if (debug_lookup)
2628 printf("lo %u hi %u rg %u mi %u\n",
2629 lo, hi, hi - lo, mi);
43057304 2630 if (!cmp)
74e34e1f 2631 return nth_packed_object_offset(p, mi);
1f688557
JH
2632 if (cmp > 0)
2633 hi = mi;
2634 else
2635 lo = mi+1;
2636 } while (lo < hi);
2637 return 0;
2638}
2639
4c080182 2640int is_pack_valid(struct packed_git *p)
d131b7af
SP
2641{
2642 /* An already open pack is known to be valid. */
2643 if (p->pack_fd != -1)
2644 return 1;
2645
2646 /* If the pack has one window completely covering the
2647 * file size, the pack is known to be valid even if
2648 * the descriptor is not currently open.
2649 */
2650 if (p->windows) {
2651 struct pack_window *w = p->windows;
2652
2653 if (!w->offset && w->len == p->pack_size)
2654 return 1;
2655 }
2656
2657 /* Force the pack to open to prove its valid. */
2658 return !open_packed_git(p);
2659}
2660
95099731
NTND
2661static int fill_pack_entry(const unsigned char *sha1,
2662 struct pack_entry *e,
2663 struct packed_git *p)
2664{
2665 off_t offset;
2666
2667 if (p->num_bad_objects) {
2668 unsigned i;
2669 for (i = 0; i < p->num_bad_objects; i++)
2670 if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
2671 return 0;
2672 }
2673
2674 offset = find_pack_entry_one(sha1, p);
2675 if (!offset)
2676 return 0;
2677
2678 /*
2679 * We are about to tell the caller where they can locate the
2680 * requested object. We better make sure the packfile is
2681 * still here and can be accessed before supplying that
2682 * answer, as it may have been deleted since the index was
2683 * loaded!
2684 */
319b678a 2685 if (!is_pack_valid(p))
95099731 2686 return 0;
95099731
NTND
2687 e->offset = offset;
2688 e->p = p;
2689 hashcpy(e->sha1, sha1);
2690 return 1;
2691}
2692
d40d535b
MH
2693/*
2694 * Iff a pack file contains the object named by sha1, return true and
2695 * store its location to e.
2696 */
4d6acb70 2697static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
1f688557 2698{
a73cdd21 2699 struct mru_entry *p;
43057304 2700
1f688557 2701 prepare_packed_git();
f7c22cc6
NP
2702 if (!packed_git)
2703 return 0;
1f688557 2704
a73cdd21
JK
2705 for (p = packed_git_mru->head; p; p = p->next) {
2706 if (fill_pack_entry(sha1, e, p->item)) {
2707 mru_mark(packed_git_mru, p);
1b1005d1
MH
2708 return 1;
2709 }
c01f51cc 2710 }
1f688557
JH
2711 return 0;
2712}
2713
a6080a0a 2714struct packed_git *find_sha1_pack(const unsigned char *sha1,
bf592c50
DB
2715 struct packed_git *packs)
2716{
2717 struct packed_git *p;
bf592c50
DB
2718
2719 for (p = packs; p; p = p->next) {
43057304 2720 if (find_pack_entry_one(sha1, p))
bf592c50
DB
2721 return p;
2722 }
2723 return NULL;
21666f1a 2724
bf592c50
DB
2725}
2726
052fe5ea 2727static int sha1_loose_object_info(const unsigned char *sha1,
46f03448
KN
2728 struct object_info *oi,
2729 int flags)
65c2e0c3 2730{
46f03448
KN
2731 int status = 0;
2732 unsigned long mapsize;
65c2e0c3 2733 void *map;
ef49a7a0 2734 git_zstream stream;
d65a16f6 2735 char hdr[32];
46f03448 2736 struct strbuf hdrbuf = STRBUF_INIT;
65c2e0c3 2737
5d642e75
JK
2738 if (oi->delta_base_sha1)
2739 hashclr(oi->delta_base_sha1);
2740
052fe5ea
JK
2741 /*
2742 * If we don't care about type or size, then we don't
4ef8d1dd
JH
2743 * need to look inside the object at all. Note that we
2744 * do not optimize out the stat call, even if the
2745 * caller doesn't care about the disk-size, since our
2746 * return value implicitly indicates whether the
2747 * object even exists.
052fe5ea 2748 */
46f03448 2749 if (!oi->typep && !oi->typename && !oi->sizep) {
4ef8d1dd
JH
2750 struct stat st;
2751 if (stat_sha1_file(sha1, &st) < 0)
2752 return -1;
2753 if (oi->disk_sizep)
23c339c0 2754 *oi->disk_sizep = st.st_size;
052fe5ea
JK
2755 return 0;
2756 }
2757
bb6b8e4f 2758 map = map_sha1_file(sha1, &mapsize);
f0df4ed5 2759 if (!map)
dbea72a8 2760 return -1;
23c339c0
JK
2761 if (oi->disk_sizep)
2762 *oi->disk_sizep = mapsize;
46f03448
KN
2763 if ((flags & LOOKUP_UNKNOWN_OBJECT)) {
2764 if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
2765 status = error("unable to unpack %s header with --allow-unknown-type",
2766 sha1_to_hex(sha1));
2767 } else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
36e4d74a
JH
2768 status = error("unable to unpack %s header",
2769 sha1_to_hex(sha1));
46f03448
KN
2770 if (status < 0)
2771 ; /* Do nothing */
2772 else if (hdrbuf.len) {
2773 if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0)
2774 status = error("unable to parse %s header with --allow-unknown-type",
2775 sha1_to_hex(sha1));
2776 } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
36e4d74a 2777 status = error("unable to parse %s header", sha1_to_hex(sha1));
39c68542 2778 git_inflate_end(&stream);
65c2e0c3 2779 munmap(map, mapsize);
46f03448 2780 if (status && oi->typep)
23c339c0 2781 *oi->typep = status;
46f03448 2782 strbuf_release(&hdrbuf);
052fe5ea 2783 return 0;
65c2e0c3
JH
2784}
2785
de7b5d62 2786int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, unsigned flags)
f0df4ed5 2787{
c4d9986f 2788 struct cached_object *co;
f0df4ed5 2789 struct pack_entry e;
5b086407 2790 int rtype;
46f03448 2791 enum object_type real_type;
1f7117ef 2792 const unsigned char *real = lookup_replace_object_extended(sha1, flags);
f0df4ed5 2793
1f7117ef 2794 co = find_cached_object(real);
c4d9986f 2795 if (co) {
5b086407
JK
2796 if (oi->typep)
2797 *(oi->typep) = co->type;
9a490590
JH
2798 if (oi->sizep)
2799 *(oi->sizep) = co->size;
161f00e7
JK
2800 if (oi->disk_sizep)
2801 *(oi->disk_sizep) = 0;
5d642e75
JK
2802 if (oi->delta_base_sha1)
2803 hashclr(oi->delta_base_sha1);
46f03448
KN
2804 if (oi->typename)
2805 strbuf_addstr(oi->typename, typename(co->type));
9a490590 2806 oi->whence = OI_CACHED;
5b086407 2807 return 0;
c4d9986f
NTND
2808 }
2809
1f7117ef 2810 if (!find_pack_entry(real, &e)) {
ddd63e64 2811 /* Most likely it's a loose object. */
46f03448 2812 if (!sha1_loose_object_info(real, oi, flags)) {
9a490590 2813 oi->whence = OI_LOOSE;
5b086407 2814 return 0;
9a490590 2815 }
ddd63e64
SG
2816
2817 /* Not a loose object; someone else may have just packed it. */
f0df4ed5 2818 reprepare_packed_git();
1f7117ef 2819 if (!find_pack_entry(real, &e))
052fe5ea 2820 return -1;
f0df4ed5 2821 }
3d77d877 2822
46f03448
KN
2823 /*
2824 * packed_object_info() does not follow the delta chain to
2825 * find out the real type, unless it is given oi->typep.
2826 */
2827 if (oi->typename && !oi->typep)
2828 oi->typep = &real_type;
2829
23c339c0 2830 rtype = packed_object_info(e.p, e.offset, oi);
412916ee 2831 if (rtype < 0) {
1f7117ef 2832 mark_bad_packed_object(e.p, real);
46f03448
KN
2833 if (oi->typep == &real_type)
2834 oi->typep = NULL;
1f7117ef 2835 return sha1_object_info_extended(real, oi, 0);
5266d369
JH
2836 } else if (in_delta_base_cache(e.p, e.offset)) {
2837 oi->whence = OI_DBCACHED;
9a490590
JH
2838 } else {
2839 oi->whence = OI_PACKED;
2840 oi->u.packed.offset = e.offset;
2841 oi->u.packed.pack = e.p;
2842 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
2843 rtype == OBJ_OFS_DELTA);
3d77d877 2844 }
46f03448
KN
2845 if (oi->typename)
2846 strbuf_addstr(oi->typename, typename(*oi->typep));
2847 if (oi->typep == &real_type)
2848 oi->typep = NULL;
3d77d877 2849
5b086407 2850 return 0;
f0df4ed5
JS
2851}
2852
3fc0dca9 2853/* returns enum object_type or negative */
9a490590
JH
2854int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
2855{
5b086407 2856 enum object_type type;
d099b717 2857 struct object_info oi = {NULL};
9a490590 2858
5b086407 2859 oi.typep = &type;
9a490590 2860 oi.sizep = sizep;
de7b5d62 2861 if (sha1_object_info_extended(sha1, &oi, LOOKUP_REPLACE_OBJECT) < 0)
5b086407
JK
2862 return -1;
2863 return type;
9a490590
JH
2864}
2865
21666f1a
NP
2866static void *read_packed_sha1(const unsigned char *sha1,
2867 enum object_type *type, unsigned long *size)
1f688557
JH
2868{
2869 struct pack_entry e;
8eca0b47 2870 void *data;
1f688557 2871
cd673c1f 2872 if (!find_pack_entry(sha1, &e))
1f688557 2873 return NULL;
85fe35ab 2874 data = cache_or_unpack_entry(e.p, e.offset, size, type);
8eca0b47
NP
2875 if (!data) {
2876 /*
2877 * We're probably in deep shit, but let's try to fetch
2878 * the required object anyway from another pack or loose.
2879 * This should happen only in the presence of a corrupted
2880 * pack, and is better than failing outright.
2881 */
2882 error("failed to read object %s at offset %"PRIuMAX" from %s",
2883 sha1_to_hex(sha1), (uintmax_t)e.offset, e.p->pack_name);
2884 mark_bad_packed_object(e.p, sha1);
ac939109 2885 data = read_object(sha1, type, size);
8eca0b47
NP
2886 }
2887 return data;
1f688557
JH
2888}
2889
21666f1a
NP
2890int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,
2891 unsigned char *sha1)
d66b37bb
JH
2892{
2893 struct cached_object *co;
2894
21666f1a 2895 hash_sha1_file(buf, len, typename(type), sha1);
d66b37bb
JH
2896 if (has_sha1_file(sha1) || find_cached_object(sha1))
2897 return 0;
c7353967 2898 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
d66b37bb
JH
2899 co = &cached_objects[cached_object_nr++];
2900 co->size = len;
21666f1a 2901 co->type = type;
efa13f7b
JH
2902 co->buf = xmalloc(len);
2903 memcpy(co->buf, buf, len);
d66b37bb
JH
2904 hashcpy(co->sha1, sha1);
2905 return 0;
2906}
2907
c2c5b270
CC
2908static void *read_object(const unsigned char *sha1, enum object_type *type,
2909 unsigned long *size)
0fcfd160
LT
2910{
2911 unsigned long mapsize;
2912 void *map, *buf;
d66b37bb
JH
2913 struct cached_object *co;
2914
2915 co = find_cached_object(sha1);
2916 if (co) {
21666f1a 2917 *type = co->type;
d66b37bb 2918 *size = co->size;
182af834 2919 return xmemdupz(co->buf, co->size);
d66b37bb 2920 }
0fcfd160 2921
8276c007
PE
2922 buf = read_packed_sha1(sha1, type, size);
2923 if (buf)
2924 return buf;
bb6b8e4f 2925 map = map_sha1_file(sha1, &mapsize);
0fcfd160 2926 if (map) {
7efbff75 2927 buf = unpack_sha1_file(map, mapsize, type, size, sha1);
0fcfd160
LT
2928 munmap(map, mapsize);
2929 return buf;
2930 }
637cdd9d 2931 reprepare_packed_git();
8276c007 2932 return read_packed_sha1(sha1, type, size);
0fcfd160
LT
2933}
2934
b6c4cecc
JH
2935/*
2936 * This function dies on corrupt objects; the callers who want to
2937 * deal with them should arrange to call read_object() and give error
2938 * messages themselves.
2939 */
5bf29b95
JH
2940void *read_sha1_file_extended(const unsigned char *sha1,
2941 enum object_type *type,
2942 unsigned long *size,
2943 unsigned flag)
ac939109 2944{
3ba7a065 2945 void *data;
b6c4cecc 2946 const struct packed_git *p;
bf93eea0 2947 const unsigned char *repl = lookup_replace_object_extended(sha1, flag);
b6c4cecc 2948
3ba7a065
JH
2949 errno = 0;
2950 data = read_object(repl, type, size);
4bbf5a26 2951 if (data)
b6c4cecc 2952 return data;
68095570 2953
25f3af3f 2954 if (errno && errno != ENOENT)
3ba7a065
JH
2955 die_errno("failed to read object %s", sha1_to_hex(sha1));
2956
68095570 2957 /* die if we replaced an object with one that does not exist */
b6c4cecc 2958 if (repl != sha1)
68095570
CC
2959 die("replacement %s not found for %s",
2960 sha1_to_hex(repl), sha1_to_hex(sha1));
2961
b6c4cecc 2962 if (has_loose_object(repl)) {
30d6c6ea
MH
2963 const char *path = sha1_file_name(sha1);
2964
b6c4cecc
JH
2965 die("loose object %s (stored in %s) is corrupt",
2966 sha1_to_hex(repl), path);
e8b15e61 2967 }
68095570 2968
b6c4cecc
JH
2969 if ((p = has_packed_and_bad(repl)) != NULL)
2970 die("packed object %s (stored in %s) is corrupt",
2971 sha1_to_hex(repl), p->pack_name);
f5552aee 2972
b6c4cecc 2973 return NULL;
ac939109
NP
2974}
2975
40469ee9 2976void *read_object_with_reference(const unsigned char *sha1,
21666f1a 2977 const char *required_type_name,
40469ee9
JH
2978 unsigned long *size,
2979 unsigned char *actual_sha1_return)
f4913f91 2980{
21666f1a 2981 enum object_type type, required_type;
f4913f91
JH
2982 void *buffer;
2983 unsigned long isize;
40469ee9 2984 unsigned char actual_sha1[20];
f4913f91 2985
21666f1a 2986 required_type = type_from_string(required_type_name);
e702496e 2987 hashcpy(actual_sha1, sha1);
40469ee9
JH
2988 while (1) {
2989 int ref_length = -1;
2990 const char *ref_type = NULL;
f4913f91 2991
21666f1a 2992 buffer = read_sha1_file(actual_sha1, &type, &isize);
40469ee9
JH
2993 if (!buffer)
2994 return NULL;
21666f1a 2995 if (type == required_type) {
40469ee9
JH
2996 *size = isize;
2997 if (actual_sha1_return)
e702496e 2998 hashcpy(actual_sha1_return, actual_sha1);
40469ee9
JH
2999 return buffer;
3000 }
3001 /* Handle references */
21666f1a 3002 else if (type == OBJ_COMMIT)
40469ee9 3003 ref_type = "tree ";
21666f1a 3004 else if (type == OBJ_TAG)
40469ee9
JH
3005 ref_type = "object ";
3006 else {
3007 free(buffer);
3008 return NULL;
3009 }
3010 ref_length = strlen(ref_type);
f4913f91 3011
50974ec9
MK
3012 if (ref_length + 40 > isize ||
3013 memcmp(buffer, ref_type, ref_length) ||
1d7f171c 3014 get_sha1_hex((char *) buffer + ref_length, actual_sha1)) {
40469ee9
JH
3015 free(buffer);
3016 return NULL;
3017 }
1cf58e72 3018 free(buffer);
40469ee9
JH
3019 /* Now we have the ID of the referred-to object in
3020 * actual_sha1. Check again. */
f4913f91 3021 }
f4913f91
JH
3022}
3023
ce9fbf16 3024static void write_sha1_file_prepare(const void *buf, unsigned long len,
972a9155 3025 const char *type, unsigned char *sha1,
d65a16f6 3026 char *hdr, int *hdrlen)
d410c0f5 3027{
9126f009 3028 git_SHA_CTX c;
d410c0f5
JH
3029
3030 /* Generate the header */
ef1286d3 3031 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
d410c0f5
JH
3032
3033 /* Sha1.. */
9126f009
NP
3034 git_SHA1_Init(&c);
3035 git_SHA1_Update(&c, hdr, *hdrlen);
3036 git_SHA1_Update(&c, buf, len);
3037 git_SHA1_Final(sha1, &c);
d410c0f5
JH
3038}
3039
230f1322 3040/*
5a688fe4 3041 * Move the just written object into its final resting place.
230f1322 3042 */
cb5add58 3043int finalize_object_file(const char *tmpfile, const char *filename)
230f1322 3044{
e32c0a9c 3045 int ret = 0;
5a688fe4 3046
348df166 3047 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
be66a6c4
JS
3048 goto try_rename;
3049 else if (link(tmpfile, filename))
e32c0a9c 3050 ret = errno;
7ebb6fca
LT
3051
3052 /*
3053 * Coda hack - coda doesn't like cross-directory links,
3054 * so we fall back to a rename, which will mean that it
3055 * won't be able to check collisions, but that's not a
3056 * big deal.
3057 *
3058 * The same holds for FAT formatted media.
3059 *
3be1f18e 3060 * When this succeeds, we just return. We have nothing
7ebb6fca
LT
3061 * left to unlink.
3062 */
3063 if (ret && ret != EEXIST) {
be66a6c4 3064 try_rename:
7ebb6fca 3065 if (!rename(tmpfile, filename))
3be1f18e 3066 goto out;
9e48b389 3067 ret = errno;
230f1322 3068 }
691f1a28 3069 unlink_or_warn(tmpfile);
230f1322
LT
3070 if (ret) {
3071 if (ret != EEXIST) {
7616c6ca 3072 return error_errno("unable to write sha1 filename %s", filename);
230f1322
LT
3073 }
3074 /* FIXME!!! Collision check here ? */
3075 }
3076
3be1f18e 3077out:
5256b006 3078 if (adjust_shared_perm(filename))
5a688fe4 3079 return error("unable to set permission to '%s'", filename);
230f1322
LT
3080 return 0;
3081}
3082
4d548150
LT
3083static int write_buffer(int fd, const void *buf, size_t len)
3084{
d34cf19b 3085 if (write_in_full(fd, buf, len) < 0)
7616c6ca 3086 return error_errno("file write error");
4d548150
LT
3087 return 0;
3088}
3089
ce9fbf16 3090int hash_sha1_file(const void *buf, unsigned long len, const char *type,
abdc3fc8
RS
3091 unsigned char *sha1)
3092{
d65a16f6 3093 char hdr[32];
ef1286d3 3094 int hdrlen = sizeof(hdr);
abdc3fc8
RS
3095 write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
3096 return 0;
3097}
3098
e9039dd3
LT
3099/* Finalize a file on disk, and close it. */
3100static void close_sha1_file(int fd)
3101{
aafe9fba
LT
3102 if (fsync_object_files)
3103 fsync_or_die(fd, "sha1 file");
e9039dd3 3104 if (close(fd) != 0)
d824cbba 3105 die_errno("error when closing sha1 file");
e9039dd3
LT
3106}
3107
5723fe7e
LT
3108/* Size of directory component, including the ending '/' */
3109static inline int directory_size(const char *filename)
3110{
3111 const char *s = strrchr(filename, '/');
3112 if (!s)
3113 return 0;
3114 return s - filename + 1;
3115}
3116
3117/*
3118 * This creates a temporary file in the same directory as the final
3119 * 'filename'
3120 *
3121 * We want to avoid cross-directory filename renames, because those
3122 * can have problems on various filesystems (FAT, NFS, Coda).
3123 */
d4b3d11a 3124static int create_tmpfile(struct strbuf *tmp, const char *filename)
5723fe7e
LT
3125{
3126 int fd, dirlen = directory_size(filename);
3127
d4b3d11a
JK
3128 strbuf_reset(tmp);
3129 strbuf_add(tmp, filename, dirlen);
3130 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
3131 fd = git_mkstemp_mode(tmp->buf, 0444);
cbacbf4e 3132 if (fd < 0 && dirlen && errno == ENOENT) {
d4b3d11a
JK
3133 /*
3134 * Make sure the directory exists; note that the contents
3135 * of the buffer are undefined after mkstemp returns an
3136 * error, so we have to rewrite the whole buffer from
3137 * scratch.
3138 */
3139 strbuf_reset(tmp);
3140 strbuf_add(tmp, filename, dirlen - 1);
3141 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
b2476a60 3142 return -1;
d4b3d11a 3143 if (adjust_shared_perm(tmp->buf))
5723fe7e
LT
3144 return -1;
3145
3146 /* Try again */
d4b3d11a
JK
3147 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
3148 fd = git_mkstemp_mode(tmp->buf, 0444);
5723fe7e
LT
3149 }
3150 return fd;
3151}
3152
bbac7311 3153static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
c00e657d 3154 const void *buf, unsigned long len, time_t mtime)
0fcfd160 3155{
915308b1 3156 int fd, ret;
9892beba 3157 unsigned char compressed[4096];
ef49a7a0 3158 git_zstream stream;
748af44c
NP
3159 git_SHA_CTX c;
3160 unsigned char parano_sha1[20];
d4b3d11a 3161 static struct strbuf tmp_file = STRBUF_INIT;
30d6c6ea 3162 const char *filename = sha1_file_name(sha1);
a44c9a5e 3163
d4b3d11a 3164 fd = create_tmpfile(&tmp_file, filename);
aac17941 3165 if (fd < 0) {
35243577 3166 if (errno == EACCES)
82247e9b 3167 return error("insufficient permission for adding an object to repository database %s", get_object_directory());
916d081b 3168 else
7616c6ca 3169 return error_errno("unable to create temporary file");
aac17941
LT
3170 }
3171
0fcfd160 3172 /* Set it up */
55bb5c91 3173 git_deflate_init(&stream, zlib_compression_level);
0fcfd160 3174 stream.next_out = compressed;
9892beba 3175 stream.avail_out = sizeof(compressed);
748af44c 3176 git_SHA1_Init(&c);
a44c9a5e
LT
3177
3178 /* First header.. */
d65a16f6 3179 stream.next_in = (unsigned char *)hdr;
a44c9a5e 3180 stream.avail_in = hdrlen;
55bb5c91
JH
3181 while (git_deflate(&stream, 0) == Z_OK)
3182 ; /* nothing */
748af44c 3183 git_SHA1_Update(&c, hdr, hdrlen);
a44c9a5e
LT
3184
3185 /* Then the data itself.. */
c00e657d 3186 stream.next_in = (void *)buf;
a44c9a5e 3187 stream.avail_in = len;
9892beba 3188 do {
748af44c 3189 unsigned char *in0 = stream.next_in;
55bb5c91 3190 ret = git_deflate(&stream, Z_FINISH);
748af44c 3191 git_SHA1_Update(&c, in0, stream.next_in - in0);
9892beba
NP
3192 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
3193 die("unable to write sha1 file");
3194 stream.next_out = compressed;
3195 stream.avail_out = sizeof(compressed);
3196 } while (ret == Z_OK);
3197
ac54c277
LT
3198 if (ret != Z_STREAM_END)
3199 die("unable to deflate new object %s (%d)", sha1_to_hex(sha1), ret);
55bb5c91 3200 ret = git_deflate_end_gently(&stream);
ac54c277
LT
3201 if (ret != Z_OK)
3202 die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1), ret);
748af44c
NP
3203 git_SHA1_Final(parano_sha1, &c);
3204 if (hashcmp(sha1, parano_sha1) != 0)
3205 die("confused by unstable object source data for %s", sha1_to_hex(sha1));
ac54c277 3206
e9039dd3 3207 close_sha1_file(fd);
0fcfd160 3208
bbac7311
NP
3209 if (mtime) {
3210 struct utimbuf utb;
3211 utb.actime = mtime;
3212 utb.modtime = mtime;
d4b3d11a 3213 if (utime(tmp_file.buf, &utb) < 0)
7616c6ca 3214 warning_errno("failed utime() on %s", tmp_file.buf);
bbac7311
NP
3215 }
3216
d4b3d11a 3217 return finalize_object_file(tmp_file.buf, filename);
0fcfd160 3218}
8237b185 3219
33d4221c
JK
3220static int freshen_loose_object(const unsigned char *sha1)
3221{
3222 return check_and_freshen(sha1, 1);
3223}
3224
3225static int freshen_packed_object(const unsigned char *sha1)
3226{
3227 struct pack_entry e;
ee1c6c34
JK
3228 if (!find_pack_entry(sha1, &e))
3229 return 0;
3230 if (e.p->freshened)
3231 return 1;
3232 if (!freshen_file(e.p->pack_name))
3233 return 0;
3234 e.p->freshened = 1;
3235 return 1;
33d4221c
JK
3236}
3237
1427a7ff 3238int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
bbac7311 3239{
bbac7311 3240 char hdr[32];
ef1286d3 3241 int hdrlen = sizeof(hdr);
bbac7311
NP
3242
3243 /* Normally if we have it in the pack then we do not bother writing
3244 * it out into .git/objects/??/?{38} file.
3245 */
3246 write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
b5f52f37 3247 if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
bbac7311
NP
3248 return 0;
3249 return write_loose_object(sha1, hdr, hdrlen, buf, len, 0);
3250}
3251
0c3db67c
ES
3252int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type,
3253 unsigned char *sha1, unsigned flags)
3254{
3255 char *header;
3256 int hdrlen, status = 0;
3257
3258 /* type string, SP, %lu of the length plus NUL must fit this */
ef1286d3
JK
3259 hdrlen = strlen(type) + 32;
3260 header = xmalloc(hdrlen);
0c3db67c
ES
3261 write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
3262
3263 if (!(flags & HASH_WRITE_OBJECT))
3264 goto cleanup;
051086b9 3265 if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
0c3db67c
ES
3266 goto cleanup;
3267 status = write_loose_object(sha1, header, hdrlen, buf, len, 0);
3268
3269cleanup:
3270 free(header);
3271 return status;
3272}
3273
bbac7311
NP
3274int force_object_loose(const unsigned char *sha1, time_t mtime)
3275{
bbac7311
NP
3276 void *buf;
3277 unsigned long len;
3278 enum object_type type;
3279 char hdr[32];
3280 int hdrlen;
1fb23e65 3281 int ret;
bbac7311 3282
c529d75a 3283 if (has_loose_object(sha1))
bbac7311
NP
3284 return 0;
3285 buf = read_packed_sha1(sha1, &type, &len);
3286 if (!buf)
3287 return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
ef1286d3 3288 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
1fb23e65
BS
3289 ret = write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);
3290 free(buf);
3291
3292 return ret;
bbac7311
NP
3293}
3294
bf592c50
DB
3295int has_pack_index(const unsigned char *sha1)
3296{
3297 struct stat st;
3298 if (stat(sha1_pack_index_name(sha1), &st))
3299 return 0;
3300 return 1;
bf592c50
DB
3301}
3302
cd673c1f
JH
3303int has_sha1_pack(const unsigned char *sha1)
3304{
3305 struct pack_entry e;
3306 return find_pack_entry(sha1, &e);
3307}
3308
0eeb077b 3309int has_sha1_file_with_flags(const unsigned char *sha1, int flags)
8237b185 3310{
1f688557
JH
3311 struct pack_entry e;
3312
cd673c1f 3313 if (find_pack_entry(sha1, &e))
1f688557 3314 return 1;
45e8a748
JK
3315 if (has_loose_object(sha1))
3316 return 1;
0eeb077b
JK
3317 if (flags & HAS_SHA1_QUICK)
3318 return 0;
45e8a748
JK
3319 reprepare_packed_git();
3320 return find_pack_entry(sha1, &e);
b419aa25 3321}
3322
3323int has_object_file(const struct object_id *oid)
3324{
3325 return has_sha1_file(oid->hash);
8237b185 3326}
74400e71 3327
c879daa2
NTND
3328static void check_tree(const void *buf, size_t size)
3329{
3330 struct tree_desc desc;
3331 struct name_entry entry;
3332
3333 init_tree_desc(&desc, buf, size);
3334 while (tree_entry(&desc, &entry))
3335 /* do nothing
3336 * tree_entry() will die() on malformed entries */
3337 ;
3338}
3339
3340static void check_commit(const void *buf, size_t size)
3341{
3342 struct commit c;
3343 memset(&c, 0, sizeof(c));
3344 if (parse_commit_buffer(&c, buf, size))
3345 die("corrupt commit");
3346}
3347
3348static void check_tag(const void *buf, size_t size)
3349{
3350 struct tag t;
3351 memset(&t, 0, sizeof(t));
3352 if (parse_tag_buffer(&t, buf, size))
3353 die("corrupt tag");
3354}
3355
43df4f86 3356static int index_mem(unsigned char *sha1, void *buf, size_t size,
c4ce46fc
JH
3357 enum object_type type,
3358 const char *path, unsigned flags)
e7332f96 3359{
6c510bee 3360 int ret, re_allocated = 0;
c4ce46fc 3361 int write_object = flags & HASH_WRITE_OBJECT;
74400e71 3362
7672db20 3363 if (!type)
edaec3fb 3364 type = OBJ_BLOB;
6c510bee
LT
3365
3366 /*
3367 * Convert blobs to git internal format
3368 */
43df4f86 3369 if ((type == OBJ_BLOB) && path) {
f285a2d7 3370 struct strbuf nbuf = STRBUF_INIT;
21e5ad50 3371 if (convert_to_git(path, buf, size, &nbuf,
5e12e78e 3372 write_object ? safe_crlf : SAFE_CRLF_FALSE)) {
b315c5c0 3373 buf = strbuf_detach(&nbuf, &size);
6c510bee
LT
3374 re_allocated = 1;
3375 }
3376 }
c4ce46fc 3377 if (flags & HASH_FORMAT_CHECK) {
c879daa2
NTND
3378 if (type == OBJ_TREE)
3379 check_tree(buf, size);
3380 if (type == OBJ_COMMIT)
3381 check_commit(buf, size);
3382 if (type == OBJ_TAG)
3383 check_tag(buf, size);
3384 }
6c510bee 3385
7672db20 3386 if (write_object)
edaec3fb 3387 ret = write_sha1_file(buf, size, typename(type), sha1);
abdc3fc8 3388 else
edaec3fb 3389 ret = hash_sha1_file(buf, size, typename(type), sha1);
43df4f86 3390 if (re_allocated)
6c510bee 3391 free(buf);
43df4f86
DP
3392 return ret;
3393}
3394
9035d75a
SP
3395static int index_stream_convert_blob(unsigned char *sha1, int fd,
3396 const char *path, unsigned flags)
3397{
3398 int ret;
3399 const int write_object = flags & HASH_WRITE_OBJECT;
3400 struct strbuf sbuf = STRBUF_INIT;
3401
3402 assert(path);
3403 assert(would_convert_to_git_filter_fd(path));
3404
3405 convert_to_git_filter_fd(path, fd, &sbuf,
3406 write_object ? safe_crlf : SAFE_CRLF_FALSE);
3407
3408 if (write_object)
3409 ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
3410 sha1);
3411 else
3412 ret = hash_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
3413 sha1);
3414 strbuf_release(&sbuf);
3415 return ret;
3416}
3417
7b41e1e1
JH
3418static int index_pipe(unsigned char *sha1, int fd, enum object_type type,
3419 const char *path, unsigned flags)
3420{
3421 struct strbuf sbuf = STRBUF_INIT;
3422 int ret;
3423
3424 if (strbuf_read(&sbuf, fd, 4096) >= 0)
3425 ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags);
3426 else
3427 ret = -1;
3428 strbuf_release(&sbuf);
3429 return ret;
3430}
3431
ea68b0ce
DP
3432#define SMALL_FILE_SIZE (32*1024)
3433
7b41e1e1
JH
3434static int index_core(unsigned char *sha1, int fd, size_t size,
3435 enum object_type type, const char *path,
3436 unsigned flags)
43df4f86
DP
3437{
3438 int ret;
43df4f86 3439
7b41e1e1 3440 if (!size) {
f6a1e1e2 3441 ret = index_mem(sha1, "", size, type, path, flags);
ea68b0ce
DP
3442 } else if (size <= SMALL_FILE_SIZE) {
3443 char *buf = xmalloc(size);
3444 if (size == read_in_full(fd, buf, size))
c4ce46fc 3445 ret = index_mem(sha1, buf, size, type, path, flags);
ea68b0ce 3446 else
7616c6ca 3447 ret = error_errno("short read");
ea68b0ce 3448 free(buf);
08bda208 3449 } else {
43df4f86 3450 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
c4ce46fc 3451 ret = index_mem(sha1, buf, size, type, path, flags);
aac17941 3452 munmap(buf, size);
08bda208 3453 }
7b41e1e1
JH
3454 return ret;
3455}
3456
4dd1fbc7 3457/*
568508e7
JH
3458 * This creates one packfile per large blob unless bulk-checkin
3459 * machinery is "plugged".
4dd1fbc7
JH
3460 *
3461 * This also bypasses the usual "convert-to-git" dance, and that is on
3462 * purpose. We could write a streaming version of the converting
3463 * functions and insert that before feeding the data to fast-import
4f22b101
JK
3464 * (or equivalent in-core API described above). However, that is
3465 * somewhat complicated, as we do not know the size of the filter
3466 * result, which we need to know beforehand when writing a git object.
3467 * Since the primary motivation for trying to stream from the working
3468 * tree file and to avoid mmaping it in core is to deal with large
3469 * binary blobs, they generally do not want to get any conversion, and
3470 * callers should avoid this code path when filters are requested.
4dd1fbc7
JH
3471 */
3472static int index_stream(unsigned char *sha1, int fd, size_t size,
3473 enum object_type type, const char *path,
3474 unsigned flags)
3475{
568508e7 3476 return index_bulk_checkin(sha1, fd, size, type, path, flags);
4dd1fbc7
JH
3477}
3478
7b41e1e1
JH
3479int index_fd(unsigned char *sha1, int fd, struct stat *st,
3480 enum object_type type, const char *path, unsigned flags)
3481{
3482 int ret;
7b41e1e1 3483
9079ab7c
SP
3484 /*
3485 * Call xsize_t() only when needed to avoid potentially unnecessary
3486 * die() for large files.
3487 */
9035d75a
SP
3488 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
3489 ret = index_stream_convert_blob(sha1, fd, path, flags);
3490 else if (!S_ISREG(st->st_mode))
7b41e1e1 3491 ret = index_pipe(sha1, fd, type, path, flags);
9079ab7c 3492 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
7ce7c760 3493 (path && would_convert_to_git(path)))
9079ab7c
SP
3494 ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
3495 flags);
4dd1fbc7 3496 else
9079ab7c
SP
3497 ret = index_stream(sha1, fd, xsize_t(st->st_size), type, path,
3498 flags);
43df4f86 3499 close(fd);
aac17941 3500 return ret;
74400e71 3501}
ec1fcc16 3502
c4ce46fc 3503int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned flags)
ec1fcc16
JH
3504{
3505 int fd;
b760d3aa 3506 struct strbuf sb = STRBUF_INIT;
ec1fcc16
JH
3507
3508 switch (st->st_mode & S_IFMT) {
3509 case S_IFREG:
3510 fd = open(path, O_RDONLY);
3511 if (fd < 0)
7616c6ca 3512 return error_errno("open(\"%s\")", path);
c4ce46fc 3513 if (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)
ec1fcc16
JH
3514 return error("%s: failed to insert into database",
3515 path);