Refactor open_packed_git to return an error code.
Because I want to reuse open_packed_git in a context where I don't want the process to die if the packfile in question is bogus, I'm changing its behavior to return error("...") rather than die("...") when it detects something is wrong with the packfile it was given. Right now we still must die out of use_pack should open_packed_git fail, as none of use_pack's callers are prepared to handle a failure from that function. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Feb 1, 2007 at 15:52 UTC
072db2789c21314cfbf43215186341d0b2b47523
1 file changed
+16
-15
sha1_file.c
+16
-15
@@ -552,7 +552,7 @@ void unuse_pack(struct pack_window **w_cursor)
552
}
553
}
554
555
-static void open_packed_git(struct packed_git *p)
555
+static int open_packed_git(struct packed_git *p)
556
{
557
struct stat st;
558
struct pack_header hdr;
@@ -562,49 +562,50 @@ static void open_packed_git(struct packed_git *p)
562
563
p->pack_fd = open(p->pack_name, O_RDONLY);
564
if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
565
- die("packfile %s cannot be opened", p->pack_name);
565
+ return -1;
566
567
/* If we created the struct before we had the pack we lack size. */
568
if (!p->pack_size) {
569
if (!S_ISREG(st.st_mode))
570
- die("packfile %s not a regular file", p->pack_name);
570
+ return error("packfile %s not a regular file", p->pack_name);
571
p->pack_size = st.st_size;
572
} else if (p->pack_size != st.st_size)
573
- die("packfile %s size changed", p->pack_name);
573
+ return error("packfile %s size changed", p->pack_name);
574
575
/* We leave these file descriptors open with sliding mmap;
576
* there is no point keeping them open across exec(), though.
577
*/
578
fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
579
if (fd_flag < 0)
580
- die("cannot determine file descriptor flags");
580
+ return error("cannot determine file descriptor flags");
581
fd_flag |= FD_CLOEXEC;
582
if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
583
- die("cannot set FD_CLOEXEC");
583
+ return error("cannot set FD_CLOEXEC");
584
585
/* Verify we recognize this pack file format. */
586
if (read_in_full(p->pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
587
- die("file %s is far too short to be a packfile", p->pack_name);
587
+ return error("file %s is far too short to be a packfile", p->pack_name);
588
if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
589
- die("file %s is not a GIT packfile", p->pack_name);
589
+ return error("file %s is not a GIT packfile", p->pack_name);
590
if (!pack_version_ok(hdr.hdr_version))
591
- die("packfile %s is version %u and not supported"
591
+ return error("packfile %s is version %u and not supported"
592
" (try upgrading GIT to a newer version)",
593
p->pack_name, ntohl(hdr.hdr_version));
594
595
/* Verify the pack matches its index. */
596
if (num_packed_objects(p) != ntohl(hdr.hdr_entries))
597
- die("packfile %s claims to have %u objects"
597
+ return error("packfile %s claims to have %u objects"
598
" while index size indicates %u objects",
599
p->pack_name, ntohl(hdr.hdr_entries),
600
num_packed_objects(p));
601
if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
602
- die("end of packfile %s is unavailable", p->pack_name);
602
+ return error("end of packfile %s is unavailable", p->pack_name);
603
if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
604
- die("packfile %s signature is unavailable", p->pack_name);
604
+ return error("packfile %s signature is unavailable", p->pack_name);
605
idx_sha1 = ((unsigned char *)p->index_base) + p->index_size - 40;
606
if (hashcmp(sha1, idx_sha1))
607
- die("packfile %s does not match index", p->pack_name);
607
+ return error("packfile %s does not match index", p->pack_name);
608
+ return 0;
609
}
610
611
static int in_window(struct pack_window *win, unsigned long offset)
@@ -627,8 +628,8 @@ unsigned char* use_pack(struct packed_git *p,
628
{
629
struct pack_window *win = *w_cursor;
630
630
- if (p->pack_fd == -1)
631
- open_packed_git(p);
631
+ if (p->pack_fd == -1 && open_packed_git(p))
632
+ die("packfile %s cannot be accessed", p->pack_name);
633
634
/* Since packfiles end in a hash of their content and its
635
* pointless to ask for an offset into the middle of that