]> git.proxmox.com Git - libgit2.git/blob - src/odb_pack.c
Merge pull request #302 from carlosmn/development
[libgit2.git] / src / odb_pack.c
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "common.h"
27 #include "git2/zlib.h"
28 #include "git2/repository.h"
29 #include "git2/oid.h"
30 #include "fileops.h"
31 #include "hash.h"
32 #include "odb.h"
33 #include "delta-apply.h"
34 #include "sha1_lookup.h"
35
36 #include "git2/odb_backend.h"
37
38 #define DEFAULT_WINDOW_SIZE \
39 (sizeof(void*) >= 8 \
40 ? 1 * 1024 * 1024 * 1024 \
41 : 32 * 1024 * 1024)
42
43 #define DEFAULT_MAPPED_LIMIT \
44 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
45
46 #define PACK_SIGNATURE 0x5041434b /* "PACK" */
47 #define PACK_VERSION 2
48 #define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3))
49 struct pack_header {
50 uint32_t hdr_signature;
51 uint32_t hdr_version;
52 uint32_t hdr_entries;
53 };
54
55 /*
56 * The first four bytes of index formats later than version 1 should
57 * start with this signature, as all older git binaries would find this
58 * value illegal and abort reading the file.
59 *
60 * This is the case because the number of objects in a packfile
61 * cannot exceed 1,431,660,000 as every object would need at least
62 * 3 bytes of data and the overall packfile cannot exceed 4 GiB with
63 * version 1 of the index file due to the offsets limited to 32 bits.
64 * Clearly the signature exceeds this maximum.
65 *
66 * Very old git binaries will also compare the first 4 bytes to the
67 * next 4 bytes in the index and abort with a "non-monotonic index"
68 * error if the second 4 byte word is smaller than the first 4
69 * byte word. This would be true in the proposed future index
70 * format as idx_signature would be greater than idx_version.
71 */
72 #define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */
73
74 struct pack_idx_header {
75 uint32_t idx_signature;
76 uint32_t idx_version;
77 };
78
79 struct pack_window {
80 struct pack_window *next;
81 git_map window_map;
82 off_t offset;
83 unsigned int last_used;
84 unsigned int inuse_cnt;
85 };
86
87 struct pack_file {
88 struct pack_window *windows;
89 off_t pack_size;
90
91 git_map index_map;
92
93 uint32_t num_objects;
94 uint32_t num_bad_objects;
95 git_oid *bad_object_sha1; /* array of git_oid */
96
97 int index_version;
98 git_time_t mtime;
99 int pack_fd;
100 unsigned pack_local:1, pack_keep:1;
101 git_oid sha1;
102
103 /* something like ".git/objects/pack/xxxxx.pack" */
104 char pack_name[GIT_FLEX_ARRAY]; /* more */
105 };
106
107 struct pack_entry {
108 off_t offset;
109 git_oid sha1;
110 struct pack_file *p;
111 };
112
113 struct pack_backend {
114 git_odb_backend parent;
115 git_vector packs;
116 struct pack_file *last_found;
117 char *pack_folder;
118 time_t pack_folder_mtime;
119
120 size_t window_size; /* needs default value */
121
122 size_t mapped_limit; /* needs default value */
123 size_t peak_mapped;
124 size_t mapped;
125
126 size_t used_ctr;
127
128 unsigned int peak_open_windows;
129 unsigned int open_windows;
130
131 unsigned int mmap_calls;
132 };
133
134 /**
135 * The wonderful tale of a Packed Object lookup query
136 * ===================================================
137 * A riveting and epic story of epicness and ASCII
138 * art, presented by yours truly,
139 * Sir Vicent of Marti
140 *
141 *
142 * Chapter 1: Once upon a time...
143 * Initialization of the Pack Backend
144 * --------------------------------------------------
145 *
146 * # git_odb_backend_pack
147 * | Creates the pack backend structure, initializes the
148 * | callback pointers to our default read() and exist() methods,
149 * | and tries to preload all the known packfiles in the ODB.
150 * |
151 * |-# packfile_load_all
152 * | Tries to find the `pack` folder, if it exists. ODBs without
153 * | a pack folder are ignored altogether. If there's a `pack` folder
154 * | we run a `dirent` callback through every file in the pack folder
155 * | to find our packfiles. The packfiles are then sorted according
156 * | to a sorting callback.
157 * |
158 * |-# packfile_load__cb
159 * | | This callback is called from `dirent` with every single file
160 * | | inside the pack folder. We find the packs by actually locating
161 * | | their index (ends in ".idx"). From that index, we verify that
162 * | | the corresponding packfile exists and is valid, and if so, we
163 * | | add it to the pack list.
164 * | |
165 * | |-# packfile_check
166 * | Make sure that there's a packfile to back this index, and store
167 * | some very basic information regarding the packfile itself,
168 * | such as the full path, the size, and the modification time.
169 * | We don't actually open the packfile to check for internal consistency.
170 * |
171 * |-# packfile_sort__cb
172 * Sort all the preloaded packs according to some specific criteria:
173 * we prioritize the "newer" packs because it's more likely they
174 * contain the objects we are looking for, and we prioritize local
175 * packs over remote ones.
176 *
177 *
178 *
179 * Chapter 2: To be, or not to be...
180 * A standard packed `exist` query for an OID
181 * --------------------------------------------------
182 *
183 * # pack_backend__exists
184 * | Check if the given SHA1 oid exists in any of the packs
185 * | that have been loaded for our ODB.
186 * |
187 * |-# pack_entry_find
188 * | Iterate through all the packs that have been preloaded
189 * | (starting by the pack where the latest object was found)
190 * | to try to find the OID in one of them.
191 * |
192 * |-# pack_entry_find1
193 * | Check the index of an individual pack to see if the SHA1
194 * | OID can be found. If we can find the offset to that SHA1
195 * | inside of the index, that means the object is contained
196 * | inside of the packfile and we can stop searching.
197 * | Before returning, we verify that the packfile behing the
198 * | index we are searching still exists on disk.
199 * |
200 * |-# pack_entry_find_offset
201 * | | Mmap the actual index file to disk if it hasn't been opened
202 * | | yet, and run a binary search through it to find the OID.
203 * | | See <http://book.git-scm.com/7_the_packfile.html> for specifics
204 * | | on the Packfile Index format and how do we find entries in it.
205 * | |
206 * | |-# pack_index_open
207 * | | Guess the name of the index based on the full path to the
208 * | | packfile, open it and verify its contents. Only if the index
209 * | | has not been opened already.
210 * | |
211 * | |-# pack_index_check
212 * | Mmap the index file and do a quick run through the header
213 * | to guess the index version (right now we support v1 and v2),
214 * | and to verify that the size of the index makes sense.
215 * |
216 * |-# packfile_open
217 * See `packfile_open` in Chapter 3
218 *
219 *
220 *
221 * Chapter 3: The neverending story...
222 * A standard packed `lookup` query for an OID
223 * --------------------------------------------------
224 * TODO
225 *
226 */
227
228
229
230
231 /***********************************************************
232 *
233 * FORWARD DECLARATIONS
234 *
235 ***********************************************************/
236
237 static void pack_window_free_all(struct pack_backend *backend, struct pack_file *p);
238 static int pack_window_contains(struct pack_window *win, off_t offset);
239
240 static void pack_window_scan_lru(struct pack_file *p, struct pack_file **lru_p,
241 struct pack_window **lru_w, struct pack_window **lru_l);
242
243 static int pack_window_close_lru( struct pack_backend *backend,
244 struct pack_file *current, git_file keep_fd);
245
246 static void pack_window_close(struct pack_window **w_cursor);
247
248 static unsigned char *pack_window_open( struct pack_backend *backend,
249 struct pack_file *p, struct pack_window **w_cursor, off_t offset,
250 unsigned int *left);
251
252 static int packfile_sort__cb(const void *a_, const void *b_);
253
254 static void pack_index_free(struct pack_file *p);
255
256 static int pack_index_check(const char *path, struct pack_file *p);
257 static int pack_index_open(struct pack_file *p);
258
259 static struct pack_file *packfile_alloc(int extra);
260 static int packfile_open(struct pack_file *p);
261 static int packfile_check(struct pack_file **pack_out, const char *path);
262 static int packfile_load__cb(void *_data, char *path);
263 static int packfile_refresh_all(struct pack_backend *backend);
264
265 static off_t nth_packed_object_offset(const struct pack_file *p, uint32_t n);
266
267 /* Can find the offset of an object given
268 * a prefix of an identifier.
269 * Throws GIT_EAMBIGUOUSOIDPREFIX if short oid
270 * is ambiguous within the pack.
271 * This method assumes that len is between
272 * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
273 */
274 static int pack_entry_find_offset(
275 off_t *offset_out,
276 git_oid *found_oid,
277 struct pack_file *p,
278 const git_oid *short_oid,
279 unsigned int len);
280
281 static int pack_entry_find1(
282 struct pack_entry *e,
283 struct pack_file *p,
284 const git_oid *short_oid,
285 unsigned int len);
286
287 static int pack_entry_find(struct pack_entry *e,
288 struct pack_backend *backend, const git_oid *oid);
289
290 /* Can find the offset of an object given
291 * a prefix of an identifier.
292 * Throws GIT_EAMBIGUOUSOIDPREFIX if short oid
293 * is ambiguous.
294 * This method assumes that len is between
295 * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
296 */
297 static int pack_entry_find_prefix(struct pack_entry *e,
298 struct pack_backend *backend,
299 const git_oid *short_oid,
300 unsigned int len);
301
302 static off_t get_delta_base(struct pack_backend *backend,
303 struct pack_file *p, struct pack_window **w_curs,
304 off_t *curpos, git_otype type,
305 off_t delta_obj_offset);
306
307 static unsigned long packfile_unpack_header1(
308 size_t *sizep,
309 git_otype *type,
310 const unsigned char *buf,
311 unsigned long len);
312
313 static int packfile_unpack_header(
314 size_t *size_p,
315 git_otype *type_p,
316 struct pack_backend *backend,
317 struct pack_file *p,
318 struct pack_window **w_curs,
319 off_t *curpos);
320
321 static int packfile_unpack_compressed(
322 git_rawobj *obj,
323 struct pack_backend *backend,
324 struct pack_file *p,
325 struct pack_window **w_curs,
326 off_t curpos,
327 size_t size,
328 git_otype type);
329
330 static int packfile_unpack_delta(
331 git_rawobj *obj,
332 struct pack_backend *backend,
333 struct pack_file *p,
334 struct pack_window **w_curs,
335 off_t curpos,
336 size_t delta_size,
337 git_otype delta_type,
338 off_t obj_offset);
339
340 static int packfile_unpack(git_rawobj *obj, struct pack_backend *backend,
341 struct pack_file *p, off_t obj_offset);
342
343
344
345
346
347 /***********************************************************
348 *
349 * PACK WINDOW MANAGEMENT
350 *
351 ***********************************************************/
352
353 void pack_window_free_all(struct pack_backend *backend, struct pack_file *p)
354 {
355 while (p->windows) {
356 struct pack_window *w = p->windows;
357 assert(w->inuse_cnt == 0);
358
359 backend->mapped -= w->window_map.len;
360 backend->open_windows--;
361
362 git_futils_mmap_free(&w->window_map);
363
364 p->windows = w->next;
365 free(w);
366 }
367 }
368
369 GIT_INLINE(int) pack_window_contains(struct pack_window *win, off_t offset)
370 {
371 /* We must promise at least 20 bytes (one hash) after the
372 * offset is available from this window, otherwise the offset
373 * is not actually in this window and a different window (which
374 * has that one hash excess) must be used. This is to support
375 * the object header and delta base parsing routines below.
376 */
377 off_t win_off = win->offset;
378 return win_off <= offset
379 && (offset + 20) <= (off_t)(win_off + win->window_map.len);
380 }
381
382 static void pack_window_scan_lru(
383 struct pack_file *p,
384 struct pack_file **lru_p,
385 struct pack_window **lru_w,
386 struct pack_window **lru_l)
387 {
388 struct pack_window *w, *w_l;
389
390 for (w_l = NULL, w = p->windows; w; w = w->next) {
391 if (!w->inuse_cnt) {
392 if (!*lru_w || w->last_used < (*lru_w)->last_used) {
393 *lru_p = p;
394 *lru_w = w;
395 *lru_l = w_l;
396 }
397 }
398 w_l = w;
399 }
400 }
401
402 static int pack_window_close_lru(
403 struct pack_backend *backend,
404 struct pack_file *current,
405 git_file keep_fd)
406 {
407 struct pack_file *lru_p = NULL;
408 struct pack_window *lru_w = NULL, *lru_l = NULL;
409 size_t i;
410
411 if (current)
412 pack_window_scan_lru(current, &lru_p, &lru_w, &lru_l);
413
414 for (i = 0; i < backend->packs.length; ++i)
415 pack_window_scan_lru(git_vector_get(&backend->packs, i), &lru_p, &lru_w, &lru_l);
416
417 if (lru_p) {
418 backend->mapped -= lru_w->window_map.len;
419 git_futils_mmap_free(&lru_w->window_map);
420
421 if (lru_l)
422 lru_l->next = lru_w->next;
423 else {
424 lru_p->windows = lru_w->next;
425 if (!lru_p->windows && lru_p->pack_fd != keep_fd) {
426 p_close(lru_p->pack_fd);
427 lru_p->pack_fd = -1;
428 }
429 }
430
431 free(lru_w);
432 backend->open_windows--;
433 return GIT_SUCCESS;
434 }
435
436 return git__throw(GIT_ERROR, "Failed to close pack window");
437 }
438
439 static void pack_window_close(struct pack_window **w_cursor)
440 {
441 struct pack_window *w = *w_cursor;
442 if (w) {
443 w->inuse_cnt--;
444 *w_cursor = NULL;
445 }
446 }
447
448 static unsigned char *pack_window_open(
449 struct pack_backend *backend,
450 struct pack_file *p,
451 struct pack_window **w_cursor,
452 off_t offset,
453 unsigned int *left)
454 {
455 struct pack_window *win = *w_cursor;
456
457 if (p->pack_fd == -1 && packfile_open(p) < GIT_SUCCESS)
458 return NULL;
459
460 /* Since packfiles end in a hash of their content and it's
461 * pointless to ask for an offset into the middle of that
462 * hash, and the pack_window_contains function above wouldn't match
463 * don't allow an offset too close to the end of the file.
464 */
465 if (offset > (p->pack_size - 20))
466 return NULL;
467
468 if (!win || !pack_window_contains(win, offset)) {
469
470 if (win)
471 win->inuse_cnt--;
472
473 for (win = p->windows; win; win = win->next) {
474 if (pack_window_contains(win, offset))
475 break;
476 }
477
478 if (!win) {
479 size_t window_align = backend->window_size / 2;
480 size_t len;
481
482 win = git__calloc(1, sizeof(*win));
483 if (win == NULL)
484 return NULL;
485
486 win->offset = (offset / window_align) * window_align;
487
488 len = (size_t)(p->pack_size - win->offset);
489 if (len > backend->window_size)
490 len = backend->window_size;
491
492 backend->mapped += len;
493
494 while (backend->mapped_limit < backend->mapped &&
495 pack_window_close_lru(backend, p, p->pack_fd) == GIT_SUCCESS) {}
496
497 if (git_futils_mmap_ro(&win->window_map, p->pack_fd,
498 win->offset, len) < GIT_SUCCESS) {
499 free(win);
500 return NULL;
501 }
502
503 backend->mmap_calls++;
504 backend->open_windows++;
505
506 if (backend->mapped > backend->peak_mapped)
507 backend->peak_mapped = backend->mapped;
508
509 if (backend->open_windows > backend->peak_open_windows)
510 backend->peak_open_windows = backend->open_windows;
511
512 win->next = p->windows;
513 p->windows = win;
514 }
515 }
516
517 if (win != *w_cursor) {
518 win->last_used = backend->used_ctr++;
519 win->inuse_cnt++;
520 *w_cursor = win;
521 }
522
523 offset -= win->offset;
524 assert(git__is_sizet(offset));
525
526 if (left)
527 *left = win->window_map.len - (size_t)offset;
528
529 return (unsigned char *)win->window_map.data + offset;
530 }
531
532
533
534
535
536
537
538 /***********************************************************
539 *
540 * PACK INDEX METHODS
541 *
542 ***********************************************************/
543
544 static void pack_index_free(struct pack_file *p)
545 {
546 if (p->index_map.data) {
547 git_futils_mmap_free(&p->index_map);
548 p->index_map.data = NULL;
549 }
550 }
551
552 static int pack_index_check(const char *path, struct pack_file *p)
553 {
554 struct pack_idx_header *hdr;
555 uint32_t version, nr, i, *index;
556
557 void *idx_map;
558 size_t idx_size;
559
560 struct stat st;
561
562 /* TODO: properly open the file without access time */
563 git_file fd = p_open(path, O_RDONLY /*| O_NOATIME */);
564
565 int error;
566
567 if (fd < 0)
568 return git__throw(GIT_EOSERR, "Failed to check index. File missing or corrupted");
569
570 if (p_fstat(fd, &st) < GIT_SUCCESS) {
571 p_close(fd);
572 return git__throw(GIT_EOSERR, "Failed to check index. File appears to be corrupted");
573 }
574
575 if (!git__is_sizet(st.st_size))
576 return GIT_ENOMEM;
577
578 idx_size = (size_t)st.st_size;
579
580 if (idx_size < 4 * 256 + 20 + 20) {
581 p_close(fd);
582 return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Object is corrupted");
583 }
584
585 error = git_futils_mmap_ro(&p->index_map, fd, 0, idx_size);
586 p_close(fd);
587
588 if (error < GIT_SUCCESS)
589 return git__rethrow(error, "Failed to check index");
590
591 hdr = idx_map = p->index_map.data;
592
593 if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
594 version = ntohl(hdr->idx_version);
595
596 if (version < 2 || version > 2) {
597 git_futils_mmap_free(&p->index_map);
598 return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Unsupported index version");
599 }
600
601 } else
602 version = 1;
603
604 nr = 0;
605 index = idx_map;
606
607 if (version > 1)
608 index += 2; /* skip index header */
609
610 for (i = 0; i < 256; i++) {
611 uint32_t n = ntohl(index[i]);
612 if (n < nr) {
613 git_futils_mmap_free(&p->index_map);
614 return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Index is non-monotonic");
615 }
616 nr = n;
617 }
618
619 if (version == 1) {
620 /*
621 * Total size:
622 * - 256 index entries 4 bytes each
623 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
624 * - 20-byte SHA1 of the packfile
625 * - 20-byte SHA1 file checksum
626 */
627 if (idx_size != 4*256 + nr * 24 + 20 + 20) {
628 git_futils_mmap_free(&p->index_map);
629 return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Object is corrupted");
630 }
631 } else if (version == 2) {
632 /*
633 * Minimum size:
634 * - 8 bytes of header
635 * - 256 index entries 4 bytes each
636 * - 20-byte sha1 entry * nr
637 * - 4-byte crc entry * nr
638 * - 4-byte offset entry * nr
639 * - 20-byte SHA1 of the packfile
640 * - 20-byte SHA1 file checksum
641 * And after the 4-byte offset table might be a
642 * variable sized table containing 8-byte entries
643 * for offsets larger than 2^31.
644 */
645 unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
646 unsigned long max_size = min_size;
647
648 if (nr)
649 max_size += (nr - 1)*8;
650
651 if (idx_size < min_size || idx_size > max_size) {
652 git_futils_mmap_free(&p->index_map);
653 return git__throw(GIT_EOBJCORRUPTED, "Failed to check index. Wrong index size");
654 }
655
656 /* Make sure that off_t is big enough to access the whole pack...
657 * Is this an issue in libgit2? It shouldn't. */
658 if (idx_size != min_size && (sizeof(off_t) <= 4)) {
659 git_futils_mmap_free(&p->index_map);
660 return git__throw(GIT_EOSERR, "Failed to check index. off_t not big enough to access the whole pack");
661 }
662 }
663
664 p->index_version = version;
665 p->num_objects = nr;
666 return GIT_SUCCESS;
667 }
668
669 static int pack_index_open(struct pack_file *p)
670 {
671 char *idx_name;
672 int error;
673
674 if (p->index_map.data)
675 return GIT_SUCCESS;
676
677 idx_name = git__strdup(p->pack_name);
678 strcpy(idx_name + strlen(idx_name) - STRLEN(".pack"), ".idx");
679
680 error = pack_index_check(idx_name, p);
681 free(idx_name);
682
683 return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to open index");
684 }
685
686
687
688
689
690
691
692
693
694 /***********************************************************
695 *
696 * PACKFILE METHODS
697 *
698 ***********************************************************/
699
700 static int packfile_sort__cb(const void *a_, const void *b_)
701 {
702 struct pack_file *a = *((struct pack_file **)a_);
703 struct pack_file *b = *((struct pack_file **)b_);
704 int st;
705
706 /*
707 * Local packs tend to contain objects specific to our
708 * variant of the project than remote ones. In addition,
709 * remote ones could be on a network mounted filesystem.
710 * Favor local ones for these reasons.
711 */
712 st = a->pack_local - b->pack_local;
713 if (st)
714 return -st;
715
716 /*
717 * Younger packs tend to contain more recent objects,
718 * and more recent objects tend to get accessed more
719 * often.
720 */
721 if (a->mtime < b->mtime)
722 return 1;
723 else if (a->mtime == b->mtime)
724 return 0;
725
726 return -1;
727 }
728
729 static struct pack_file *packfile_alloc(int extra)
730 {
731 struct pack_file *p = git__malloc(sizeof(*p) + extra);
732 memset(p, 0, sizeof(*p));
733 p->pack_fd = -1;
734 return p;
735 }
736
737
738 static void packfile_free(struct pack_backend *backend, struct pack_file *p)
739 {
740 assert(p);
741
742 /* clear_delta_base_cache(); */
743 pack_window_free_all(backend, p);
744
745 if (p->pack_fd != -1)
746 p_close(p->pack_fd);
747
748 pack_index_free(p);
749
750 free(p->bad_object_sha1);
751 free(p);
752 }
753
754 static int packfile_open(struct pack_file *p)
755 {
756 struct stat st;
757 struct pack_header hdr;
758 git_oid sha1;
759 unsigned char *idx_sha1;
760
761 if (!p->index_map.data && pack_index_open(p) < GIT_SUCCESS)
762 return git__throw(GIT_ENOTFOUND, "Failed to open packfile. File not found");
763
764 /* TODO: open with noatime */
765 p->pack_fd = p_open(p->pack_name, O_RDONLY);
766 if (p->pack_fd < 0 || p_fstat(p->pack_fd, &st) < GIT_SUCCESS)
767 return git__throw(GIT_EOSERR, "Failed to open packfile. File appears to be corrupted");
768
769 /* If we created the struct before we had the pack we lack size. */
770 if (!p->pack_size) {
771 if (!S_ISREG(st.st_mode))
772 goto cleanup;
773 p->pack_size = (off_t)st.st_size;
774 } else if (p->pack_size != st.st_size)
775 goto cleanup;
776
777 #if 0
778 /* We leave these file descriptors open with sliding mmap;
779 * there is no point keeping them open across exec(), though.
780 */
781 fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
782 if (fd_flag < 0)
783 return error("cannot determine file descriptor flags");
784
785 fd_flag |= FD_CLOEXEC;
786 if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
787 return GIT_EOSERR;
788 #endif
789
790 /* Verify we recognize this pack file format. */
791 if (p_read(p->pack_fd, &hdr, sizeof(hdr)) < GIT_SUCCESS)
792 goto cleanup;
793
794 if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
795 goto cleanup;
796
797 if (!pack_version_ok(hdr.hdr_version))
798 goto cleanup;
799
800 /* Verify the pack matches its index. */
801 if (p->num_objects != ntohl(hdr.hdr_entries))
802 goto cleanup;
803
804 if (p_lseek(p->pack_fd, p->pack_size - GIT_OID_RAWSZ, SEEK_SET) == -1)
805 goto cleanup;
806
807 if (p_read(p->pack_fd, sha1.id, GIT_OID_RAWSZ) < GIT_SUCCESS)
808 goto cleanup;
809
810 idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;
811
812 if (git_oid_cmp(&sha1, (git_oid *)idx_sha1) != 0)
813 goto cleanup;
814
815 return GIT_SUCCESS;
816
817 cleanup:
818 p_close(p->pack_fd);
819 p->pack_fd = -1;
820 return git__throw(GIT_EPACKCORRUPTED, "Failed to packfile. Pack is corrupted");
821 }
822
823 static int packfile_check(struct pack_file **pack_out, const char *path)
824 {
825 struct stat st;
826 struct pack_file *p;
827 size_t path_len;
828
829 *pack_out = NULL;
830 path_len = strlen(path);
831 p = packfile_alloc(path_len + 2);
832
833 /*
834 * Make sure a corresponding .pack file exists and that
835 * the index looks sane.
836 */
837 path_len -= STRLEN(".idx");
838 if (path_len < 1) {
839 free(p);
840 return git__throw(GIT_ENOTFOUND, "Failed to check packfile. Wrong path name");
841 }
842
843 memcpy(p->pack_name, path, path_len);
844
845 strcpy(p->pack_name + path_len, ".keep");
846 if (git_futils_exists(p->pack_name) == GIT_SUCCESS)
847 p->pack_keep = 1;
848
849 strcpy(p->pack_name + path_len, ".pack");
850 if (p_stat(p->pack_name, &st) < GIT_SUCCESS || !S_ISREG(st.st_mode)) {
851 free(p);
852 return git__throw(GIT_ENOTFOUND, "Failed to check packfile. File not found");
853 }
854
855 /* ok, it looks sane as far as we can check without
856 * actually mapping the pack file.
857 */
858 p->pack_size = (off_t)st.st_size;
859 p->pack_local = 1;
860 p->mtime = (git_time_t)st.st_mtime;
861
862 /* see if we can parse the sha1 oid in the packfile name */
863 if (path_len < 40 ||
864 git_oid_fromstr(&p->sha1, path + path_len - GIT_OID_HEXSZ) < GIT_SUCCESS)
865 memset(&p->sha1, 0x0, GIT_OID_RAWSZ);
866
867 *pack_out = p;
868 return GIT_SUCCESS;
869 }
870
871 static int packfile_load__cb(void *_data, char *path)
872 {
873 struct pack_backend *backend = (struct pack_backend *)_data;
874 struct pack_file *pack;
875 int error;
876 size_t i;
877
878 if (git__suffixcmp(path, ".idx") != 0)
879 return GIT_SUCCESS; /* not an index */
880
881 for (i = 0; i < backend->packs.length; ++i) {
882 struct pack_file *p = git_vector_get(&backend->packs, i);
883 if (memcmp(p->pack_name, path, strlen(path) - STRLEN(".idx")) == 0)
884 return GIT_SUCCESS;
885 }
886
887 error = packfile_check(&pack, path);
888 if (error < GIT_SUCCESS)
889 return git__rethrow(error, "Failed to load packfile");
890
891 if (git_vector_insert(&backend->packs, pack) < GIT_SUCCESS) {
892 free(pack);
893 return GIT_ENOMEM;
894 }
895
896 return GIT_SUCCESS;
897 }
898
899 static int packfile_refresh_all(struct pack_backend *backend)
900 {
901 int error;
902 struct stat st;
903
904 if (backend->pack_folder == NULL)
905 return GIT_SUCCESS;
906
907 if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
908 return git__throw(GIT_ENOTFOUND, "Failed to refresh packfiles. Backend not found");
909
910 if (st.st_mtime != backend->pack_folder_mtime) {
911 char path[GIT_PATH_MAX];
912 strcpy(path, backend->pack_folder);
913
914 /* reload all packs */
915 error = git_futils_direach(path, GIT_PATH_MAX, packfile_load__cb, (void *)backend);
916 if (error < GIT_SUCCESS)
917 return git__rethrow(error, "Failed to refresh packfiles");
918
919 git_vector_sort(&backend->packs);
920 backend->pack_folder_mtime = st.st_mtime;
921 }
922
923 return GIT_SUCCESS;
924 }
925
926
927
928
929
930
931
932
933 /***********************************************************
934 *
935 * PACKFILE ENTRY SEARCH INTERNALS
936 *
937 ***********************************************************/
938
939 static off_t nth_packed_object_offset(const struct pack_file *p, uint32_t n)
940 {
941 const unsigned char *index = p->index_map.data;
942 index += 4 * 256;
943 if (p->index_version == 1) {
944 return ntohl(*((uint32_t *)(index + 24 * n)));
945 } else {
946 uint32_t off;
947 index += 8 + p->num_objects * (20 + 4);
948 off = ntohl(*((uint32_t *)(index + 4 * n)));
949 if (!(off & 0x80000000))
950 return off;
951 index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
952 return (((uint64_t)ntohl(*((uint32_t *)(index + 0)))) << 32) |
953 ntohl(*((uint32_t *)(index + 4)));
954 }
955 }
956
957 static int pack_entry_find_offset(
958 off_t *offset_out,
959 git_oid *found_oid,
960 struct pack_file *p,
961 const git_oid *short_oid,
962 unsigned int len)
963 {
964 const uint32_t *level1_ofs = p->index_map.data;
965 const unsigned char *index = p->index_map.data;
966 unsigned hi, lo, stride;
967 int pos, found = 0;
968 const unsigned char *current = 0;
969
970 *offset_out = 0;
971
972 if (index == NULL) {
973 int error;
974
975 if ((error = pack_index_open(p)) < GIT_SUCCESS)
976 return git__rethrow(error, "Failed to find offset for pack entry");
977
978 assert(p->index_map.data);
979
980 index = p->index_map.data;
981 level1_ofs = p->index_map.data;
982 }
983
984 if (p->index_version > 1) {
985 level1_ofs += 2;
986 index += 8;
987 }
988
989 index += 4 * 256;
990 hi = ntohl(level1_ofs[(int)short_oid->id[0]]);
991 lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(level1_ofs[(int)short_oid->id[0] - 1]));
992
993 if (p->index_version > 1) {
994 stride = 20;
995 } else {
996 stride = 24;
997 index += 4;
998 }
999
1000 #ifdef INDEX_DEBUG_LOOKUP
1001 printf("%02x%02x%02x... lo %u hi %u nr %d\n",
1002 short_oid->id[0], short_oid->id[1], short_oid->id[2], lo, hi, p->num_objects);
1003 #endif
1004
1005 /* Use git.git lookup code */
1006 pos = sha1_entry_pos(index, stride, 0, lo, hi, p->num_objects, short_oid->id);
1007
1008 if (pos >= 0) {
1009 /* An object matching exactly the oid was found */
1010 found = 1;
1011 current = index + pos * stride;
1012 } else {
1013 /* No object was found */
1014 /* pos refers to the object with the "closest" oid to short_oid */
1015 pos = - 1 - pos;
1016 if (pos < (int)p->num_objects) {
1017 current = index + pos * stride;
1018
1019 if (!git_oid_ncmp(short_oid, (const git_oid *)current, len)) {
1020 found = 1;
1021 }
1022 }
1023 }
1024
1025 if (found && pos + 1 < (int)p->num_objects) {
1026 /* Check for ambiguousity */
1027 const unsigned char *next = current + stride;
1028
1029 if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) {
1030 found = 2;
1031 }
1032 }
1033
1034 if (!found) {
1035 return git__throw(GIT_ENOTFOUND, "Failed to find offset for pack entry. Entry not found");
1036 } else if (found > 1) {
1037 return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Failed to find offset for pack entry. Ambiguous sha1 prefix within pack");
1038 } else {
1039 *offset_out = nth_packed_object_offset(p, pos);
1040 git_oid_fromraw(found_oid, current);
1041
1042 #ifdef INDEX_DEBUG_LOOKUP
1043 unsigned char hex_sha1[GIT_OID_HEXSZ + 1];
1044 git_oid_fmt(hex_sha1, found_oid);
1045 hex_sha1[GIT_OID_HEXSZ] = '\0';
1046 printf("found lo=%d %s\n", lo, hex_sha1);
1047 #endif
1048 return GIT_SUCCESS;
1049 }
1050 }
1051
1052 static int pack_entry_find1(
1053 struct pack_entry *e,
1054 struct pack_file *p,
1055 const git_oid *short_oid,
1056 unsigned int len)
1057 {
1058 off_t offset;
1059 git_oid found_oid;
1060 int error;
1061
1062 assert(p);
1063
1064 if (len == GIT_OID_HEXSZ && p->num_bad_objects) {
1065 unsigned i;
1066 for (i = 0; i < p->num_bad_objects; i++)
1067 if (git_oid_cmp(short_oid, &p->bad_object_sha1[i]) == 0)
1068 return git__throw(GIT_ERROR, "Failed to find pack entry. Bad object found");
1069 }
1070
1071 error = pack_entry_find_offset(&offset, &found_oid, p, short_oid, len);
1072 if (error < GIT_SUCCESS)
1073 return git__rethrow(error, "Failed to find pack entry. Couldn't find offset");
1074
1075 /* we found a unique entry in the index;
1076 * make sure the packfile backing the index
1077 * still exists on disk */
1078 if (p->pack_fd == -1 && packfile_open(p) < GIT_SUCCESS)
1079 return git__throw(GIT_EOSERR, "Failed to find pack entry. Packfile doesn't exist on disk");
1080
1081 e->offset = offset;
1082 e->p = p;
1083
1084 git_oid_cpy(&e->sha1, &found_oid);
1085 return GIT_SUCCESS;
1086 }
1087
1088 static int pack_entry_find(struct pack_entry *e, struct pack_backend *backend, const git_oid *oid)
1089 {
1090 int error;
1091 size_t i;
1092
1093 if ((error = packfile_refresh_all(backend)) < GIT_SUCCESS)
1094 return git__rethrow(error, "Failed to find pack entry");
1095
1096 if (backend->last_found &&
1097 pack_entry_find1(e, backend->last_found, oid, GIT_OID_HEXSZ) == GIT_SUCCESS)
1098 return GIT_SUCCESS;
1099
1100 for (i = 0; i < backend->packs.length; ++i) {
1101 struct pack_file *p;
1102
1103 p = git_vector_get(&backend->packs, i);
1104 if (p == backend->last_found)
1105 continue;
1106
1107 if (pack_entry_find1(e, p, oid, GIT_OID_HEXSZ) == GIT_SUCCESS) {
1108 backend->last_found = p;
1109 return GIT_SUCCESS;
1110 }
1111 }
1112
1113 return git__throw(GIT_ENOTFOUND, "Failed to find pack entry");
1114 }
1115
1116 static int pack_entry_find_prefix(
1117 struct pack_entry *e,
1118 struct pack_backend *backend,
1119 const git_oid *short_oid,
1120 unsigned int len)
1121 {
1122 int error;
1123 size_t i;
1124 unsigned found = 0;
1125
1126 if ((error = packfile_refresh_all(backend)) < GIT_SUCCESS)
1127 return git__rethrow(error, "Failed to find pack entry");
1128
1129 if (backend->last_found) {
1130 error = pack_entry_find1(e, backend->last_found, short_oid, len);
1131 if (error == GIT_EAMBIGUOUSOIDPREFIX) {
1132 return git__rethrow(error, "Failed to find pack entry. Ambiguous sha1 prefix");
1133 } else if (error == GIT_SUCCESS) {
1134 found = 1;
1135 }
1136 }
1137
1138 for (i = 0; i < backend->packs.length; ++i) {
1139 struct pack_file *p;
1140
1141 p = git_vector_get(&backend->packs, i);
1142 if (p == backend->last_found)
1143 continue;
1144
1145 error = pack_entry_find1(e, p, short_oid, len);
1146 if (error == GIT_EAMBIGUOUSOIDPREFIX) {
1147 return git__rethrow(error, "Failed to find pack entry. Ambiguous sha1 prefix");
1148 } else if (error == GIT_SUCCESS) {
1149 found++;
1150 if (found > 1)
1151 break;
1152 backend->last_found = p;
1153 }
1154 }
1155
1156 if (!found) {
1157 return git__rethrow(GIT_ENOTFOUND, "Failed to find pack entry");
1158 } else if (found > 1) {
1159 return git__rethrow(GIT_EAMBIGUOUSOIDPREFIX, "Failed to find pack entry. Ambiguous sha1 prefix");
1160 } else {
1161 return GIT_SUCCESS;
1162 }
1163
1164 }
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177 /***********************************************************
1178 *
1179 * PACKFILE ENTRY UNPACK INTERNALS
1180 *
1181 ***********************************************************/
1182
1183 static unsigned long packfile_unpack_header1(
1184 size_t *sizep,
1185 git_otype *type,
1186 const unsigned char *buf,
1187 unsigned long len)
1188 {
1189 unsigned shift;
1190 unsigned long size, c;
1191 unsigned long used = 0;
1192
1193 c = buf[used++];
1194 *type = (c >> 4) & 7;
1195 size = c & 15;
1196 shift = 4;
1197 while (c & 0x80) {
1198 if (len <= used || bitsizeof(long) <= shift)
1199 return 0;
1200
1201 c = buf[used++];
1202 size += (c & 0x7f) << shift;
1203 shift += 7;
1204 }
1205
1206 *sizep = (size_t)size;
1207 return used;
1208 }
1209
1210 static int packfile_unpack_header(
1211 size_t *size_p,
1212 git_otype *type_p,
1213 struct pack_backend *backend,
1214 struct pack_file *p,
1215 struct pack_window **w_curs,
1216 off_t *curpos)
1217 {
1218 unsigned char *base;
1219 unsigned int left;
1220 unsigned long used;
1221
1222 /* pack_window_open() assures us we have [base, base + 20) available
1223 * as a range that we can look at at. (Its actually the hash
1224 * size that is assured.) With our object header encoding
1225 * the maximum deflated object size is 2^137, which is just
1226 * insane, so we know won't exceed what we have been given.
1227 */
1228 base = pack_window_open(backend, p, w_curs, *curpos, &left);
1229 if (base == NULL)
1230 return GIT_ENOMEM;
1231
1232 used = packfile_unpack_header1(size_p, type_p, base, left);
1233
1234 if (used == 0)
1235 return git__throw(GIT_EOBJCORRUPTED, "Header length is zero");
1236
1237 *curpos += used;
1238 return GIT_SUCCESS;
1239 }
1240
1241 static int packfile_unpack_compressed(
1242 git_rawobj *obj,
1243 struct pack_backend *backend,
1244 struct pack_file *p,
1245 struct pack_window **w_curs,
1246 off_t curpos,
1247 size_t size,
1248 git_otype type)
1249 {
1250 int st;
1251 z_stream stream;
1252 unsigned char *buffer, *in;
1253
1254 buffer = git__malloc(size + 1);
1255 memset(buffer, 0x0, size + 1);
1256
1257 memset(&stream, 0, sizeof(stream));
1258 stream.next_out = buffer;
1259 stream.avail_out = size + 1;
1260
1261 st = inflateInit(&stream);
1262 if (st != Z_OK) {
1263 free(buffer);
1264 return git__throw(GIT_EZLIB, "Error in zlib");
1265 }
1266
1267 do {
1268 in = pack_window_open(backend, p, w_curs, curpos, &stream.avail_in);
1269 stream.next_in = in;
1270 st = inflate(&stream, Z_FINISH);
1271
1272 if (!stream.avail_out)
1273 break; /* the payload is larger than it should be */
1274
1275 curpos += stream.next_in - in;
1276 } while (st == Z_OK || st == Z_BUF_ERROR);
1277
1278 inflateEnd(&stream);
1279
1280 if ((st != Z_STREAM_END) || stream.total_out != size) {
1281 free(buffer);
1282 return git__throw(GIT_EZLIB, "Error in zlib");
1283 }
1284
1285 obj->type = type;
1286 obj->len = size;
1287 obj->data = buffer;
1288 return GIT_SUCCESS;
1289 }
1290
1291 static off_t get_delta_base(
1292 struct pack_backend *backend,
1293 struct pack_file *p,
1294 struct pack_window **w_curs,
1295 off_t *curpos,
1296 git_otype type,
1297 off_t delta_obj_offset)
1298 {
1299 unsigned char *base_info = pack_window_open(backend, p, w_curs, *curpos, NULL);
1300 off_t base_offset;
1301 git_oid unused;
1302
1303 /* pack_window_open() assured us we have [base_info, base_info + 20)
1304 * as a range that we can look at without walking off the
1305 * end of the mapped window. Its actually the hash size
1306 * that is assured. An OFS_DELTA longer than the hash size
1307 * is stupid, as then a REF_DELTA would be smaller to store.
1308 */
1309 if (type == GIT_OBJ_OFS_DELTA) {
1310 unsigned used = 0;
1311 unsigned char c = base_info[used++];
1312 base_offset = c & 127;
1313 while (c & 128) {
1314 base_offset += 1;
1315 if (!base_offset || MSB(base_offset, 7))
1316 return 0; /* overflow */
1317 c = base_info[used++];
1318 base_offset = (base_offset << 7) + (c & 127);
1319 }
1320 base_offset = delta_obj_offset - base_offset;
1321 if (base_offset <= 0 || base_offset >= delta_obj_offset)
1322 return 0; /* out of bound */
1323 *curpos += used;
1324 } else if (type == GIT_OBJ_REF_DELTA) {
1325 /* The base entry _must_ be in the same pack */
1326 if (pack_entry_find_offset(&base_offset, &unused, p, (git_oid *)base_info, GIT_OID_HEXSZ) < GIT_SUCCESS)
1327 return git__throw(GIT_EPACKCORRUPTED, "Base entry delta is not in the same pack");
1328 *curpos += 20;
1329 } else
1330 return 0;
1331
1332 return base_offset;
1333 }
1334
1335 static int packfile_unpack_delta(
1336 git_rawobj *obj,
1337 struct pack_backend *backend,
1338 struct pack_file *p,
1339 struct pack_window **w_curs,
1340 off_t curpos,
1341 size_t delta_size,
1342 git_otype delta_type,
1343 off_t obj_offset)
1344 {
1345 off_t base_offset;
1346 git_rawobj base, delta;
1347 int error;
1348
1349 base_offset = get_delta_base(backend, p, w_curs, &curpos, delta_type, obj_offset);
1350 if (base_offset == 0)
1351 return git__throw(GIT_EOBJCORRUPTED, "Delta offset is zero");
1352
1353 pack_window_close(w_curs);
1354 error = packfile_unpack(&base, backend, p, base_offset);
1355
1356 /* TODO: git.git tries to load the base from other packfiles
1357 * or loose objects */
1358 if (error < GIT_SUCCESS)
1359 return git__rethrow(error, "Corrupted delta");
1360
1361 error = packfile_unpack_compressed(&delta, backend, p, w_curs, curpos, delta_size, delta_type);
1362 if (error < GIT_SUCCESS) {
1363 free(base.data);
1364 return git__rethrow(error, "Corrupted delta");
1365 }
1366
1367 obj->type = base.type;
1368 error = git__delta_apply(obj,
1369 base.data, base.len,
1370 delta.data, delta.len);
1371
1372 free(base.data);
1373 free(delta.data);
1374
1375 /* TODO: we might want to cache this shit. eventually */
1376 //add_delta_base_cache(p, base_offset, base, base_size, *type);
1377 return error; /* error set by git__delta_apply */
1378 }
1379
1380 static int packfile_unpack(
1381 git_rawobj *obj,
1382 struct pack_backend *backend,
1383 struct pack_file *p,
1384 off_t obj_offset)
1385 {
1386 struct pack_window *w_curs = NULL;
1387 off_t curpos = obj_offset;
1388 int error;
1389
1390 size_t size = 0;
1391 git_otype type;
1392
1393 /*
1394 * TODO: optionally check the CRC on the packfile
1395 */
1396
1397 obj->data = NULL;
1398 obj->len = 0;
1399 obj->type = GIT_OBJ_BAD;
1400
1401 error = packfile_unpack_header(&size, &type, backend, p, &w_curs, &curpos);
1402 if (error < GIT_SUCCESS)
1403 return git__rethrow(error, "Failed to unpack packfile");
1404
1405 switch (type) {
1406 case GIT_OBJ_OFS_DELTA:
1407 case GIT_OBJ_REF_DELTA:
1408 error = packfile_unpack_delta(
1409 obj, backend, p, &w_curs, curpos,
1410 size, type, obj_offset);
1411 break;
1412
1413 case GIT_OBJ_COMMIT:
1414 case GIT_OBJ_TREE:
1415 case GIT_OBJ_BLOB:
1416 case GIT_OBJ_TAG:
1417 error = packfile_unpack_compressed(
1418 obj, backend, p, &w_curs, curpos,
1419 size, type);
1420 break;
1421
1422 default:
1423 error = GIT_EOBJCORRUPTED;
1424 break;
1425 }
1426
1427 pack_window_close(&w_curs);
1428 return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to unpack packfile");
1429 }
1430
1431
1432
1433
1434
1435 /***********************************************************
1436 *
1437 * PACKED BACKEND PUBLIC API
1438 *
1439 * Implement the git_odb_backend API calls
1440 *
1441 ***********************************************************/
1442
1443 /*
1444 int pack_backend__read_header(git_rawobj *obj, git_odb_backend *backend, const git_oid *oid)
1445 {
1446 pack_location location;
1447
1448 assert(obj && backend && oid);
1449
1450 if (locate_packfile(&location, (struct pack_backend *)backend, oid) < 0)
1451 return GIT_ENOTFOUND;
1452
1453 return read_header_packed(obj, &location);
1454 }
1455 */
1456
1457 int pack_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
1458 {
1459 struct pack_entry e;
1460 git_rawobj raw;
1461 int error;
1462
1463 if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < GIT_SUCCESS)
1464 return git__rethrow(error, "Failed to read pack backend");
1465
1466 if ((error = packfile_unpack(&raw, (struct pack_backend *)backend, e.p, e.offset)) < GIT_SUCCESS)
1467 return git__rethrow(error, "Failed to read pack backend");
1468
1469 *buffer_p = raw.data;
1470 *len_p = raw.len;
1471 *type_p = raw.type;
1472
1473 return GIT_SUCCESS;
1474 }
1475
1476 int pack_backend__read_prefix(
1477 git_oid *out_oid,
1478 void **buffer_p,
1479 size_t *len_p,
1480 git_otype *type_p,
1481 git_odb_backend *backend,
1482 const git_oid *short_oid,
1483 unsigned int len)
1484 {
1485 if (len < GIT_OID_MINPREFIXLEN)
1486 return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Failed to read pack backend. Prefix length is lower than %d.", GIT_OID_MINPREFIXLEN);
1487
1488 if (len >= GIT_OID_HEXSZ) {
1489 /* We can fall back to regular read method */
1490 int error = pack_backend__read(buffer_p, len_p, type_p, backend, short_oid);
1491 if (error == GIT_SUCCESS)
1492 git_oid_cpy(out_oid, short_oid);
1493
1494 return error;
1495 } else {
1496 struct pack_entry e;
1497 git_rawobj raw;
1498 int error;
1499
1500 if ((error = pack_entry_find_prefix(&e, (struct pack_backend *)backend, short_oid, len)) < GIT_SUCCESS)
1501 return git__rethrow(error, "Failed to read pack backend");
1502
1503 if ((error = packfile_unpack(&raw, (struct pack_backend *)backend, e.p, e.offset)) < GIT_SUCCESS)
1504 return git__rethrow(error, "Failed to read pack backend");
1505
1506 *buffer_p = raw.data;
1507 *len_p = raw.len;
1508 *type_p = raw.type;
1509 git_oid_cpy(out_oid, &e.sha1);
1510 }
1511
1512 return GIT_SUCCESS;
1513 }
1514
1515 int pack_backend__exists(git_odb_backend *backend, const git_oid *oid)
1516 {
1517 struct pack_entry e;
1518 return pack_entry_find(&e, (struct pack_backend *)backend, oid) == GIT_SUCCESS;
1519 }
1520
1521 void pack_backend__free(git_odb_backend *_backend)
1522 {
1523 struct pack_backend *backend;
1524 size_t i;
1525
1526 assert(_backend);
1527
1528 backend = (struct pack_backend *)_backend;
1529
1530 for (i = 0; i < backend->packs.length; ++i) {
1531 struct pack_file *p = git_vector_get(&backend->packs, i);
1532 packfile_free(backend, p);
1533 }
1534
1535 git_vector_free(&backend->packs);
1536 free(backend->pack_folder);
1537 free(backend);
1538 }
1539
1540 int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
1541 {
1542 struct pack_backend *backend;
1543 char path[GIT_PATH_MAX];
1544
1545 backend = git__calloc(1, sizeof(struct pack_backend));
1546 if (backend == NULL)
1547 return GIT_ENOMEM;
1548
1549 if (git_vector_init(&backend->packs, 8, packfile_sort__cb) < GIT_SUCCESS) {
1550 free(backend);
1551 return GIT_ENOMEM;
1552 }
1553
1554 backend->window_size = DEFAULT_WINDOW_SIZE;
1555 backend->mapped_limit = DEFAULT_MAPPED_LIMIT;
1556
1557 git_path_join(path, objects_dir, "pack");
1558 if (git_futils_isdir(path) == GIT_SUCCESS) {
1559 backend->pack_folder = git__strdup(path);
1560 backend->pack_folder_mtime = 0;
1561
1562 if (backend->pack_folder == NULL) {
1563 free(backend);
1564 return GIT_ENOMEM;
1565 }
1566 }
1567
1568 backend->parent.read = &pack_backend__read;
1569 backend->parent.read_prefix = &pack_backend__read_prefix;
1570 backend->parent.read_header = NULL;
1571 backend->parent.exists = &pack_backend__exists;
1572 backend->parent.free = &pack_backend__free;
1573
1574 *backend_out = (git_odb_backend *)backend;
1575 return GIT_SUCCESS;
1576 }