397
return NULL;
398
}
399
400
-static int pack_used_ctr;
401
-static unsigned long pack_mapped;
400
+static unsigned int pack_used_ctr;
401
+static size_t pack_mapped;
402
+static size_t page_size;
403
struct packed_git *packed_git;
404
405
static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
537
die("packfile %s does not match index", p->pack_name);
538
}
539
540
+static int in_window(struct pack_window *win, unsigned long offset)
541
+{
542
+ /* We must promise at least 20 bytes (one hash) after the
543
+ * offset is available from this window, otherwise the offset
544
+ * is not actually in this window and a different window (which
545
+ * has that one hash excess) must be used. This is to support
546
+ * the object header and delta base parsing routines below.
547
+ */
548
+ off_t win_off = win->offset;
549
+ return win_off <= offset
550
+ && (offset + 20) <= (win_off + win->len);
551
+}
552
+
553
unsigned char* use_pack(struct packed_git *p,
554
struct pack_window **w_cursor,
555
unsigned long offset,
556
unsigned int *left)
557
{
544
- struct pack_window *win = p->windows;
558
+ struct pack_window *win = *w_cursor;
559
560
if (p->pack_fd == -1)
561
open_packed_git(p);
548
- if (!win) {
549
- pack_mapped += p->pack_size;
550
- while (packed_git_limit < pack_mapped && unuse_one_window())
551
- ; /* nothing */
552
- win = xcalloc(1, sizeof(*win));
553
- win->len = p->pack_size;
554
- win->base = mmap(NULL, p->pack_size, PROT_READ, MAP_PRIVATE, p->pack_fd, 0);
555
- if (win->base == MAP_FAILED)
556
- die("packfile %s cannot be mapped.", p->pack_name);
557
- p->windows = win;
562
+
563
+ /* Since packfiles end in a hash of their content and its
564
+ * pointless to ask for an offset into the middle of that
565
+ * hash, and the in_window function above wouldn't match
566
+ * don't allow an offset too close to the end of the file.
567
+ */
568
+ if (offset > (p->pack_size - 20))
569
+ die("offset beyond end of packfile (truncated pack?)");
570
+
571
+ if (!win || !in_window(win, offset)) {
572
+ if (win)
573
+ win->inuse_cnt--;
574
+ for (win = p->windows; win; win = win->next) {
575
+ if (in_window(win, offset))
576
+ break;
577
+ }
578
+ if (!win) {
579
+ if (!page_size)
580
+ page_size = getpagesize();
581
+ win = xcalloc(1, sizeof(*win));
582
+ win->offset = (offset / page_size) * page_size;
583
+ win->len = p->pack_size - win->offset;
584
+ if (win->len > packed_git_window_size)
585
+ win->len = packed_git_window_size;
586
+ pack_mapped += win->len;
587
+ while (packed_git_limit < pack_mapped && unuse_one_window())
588
+ ; /* nothing */
589
+ win->base = mmap(NULL, win->len,
590
+ PROT_READ, MAP_PRIVATE,
591
+ p->pack_fd, win->offset);
592
+ if (win->base == MAP_FAILED)
593
+ die("packfile %s cannot be mapped.", p->pack_name);
594
+ win->next = p->windows;
595
+ p->windows = win;
596
+ }
597
}
598
if (win != *w_cursor) {
599
win->last_used = pack_used_ctr++;
600
win->inuse_cnt++;
601
*w_cursor = win;
602
}
603
+ offset -= win->offset;
604
if (left)
605
*left = win->len - offset;
606
return win->base + offset;