Skip to content

Commit 072db27

Browse files
spearceJunio C Hamano
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54a15a8 commit 072db27

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

sha1_file.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ void unuse_pack(struct pack_window **w_cursor)
552552
}
553553
}
554554

555-
static void open_packed_git(struct packed_git *p)
555+
static int open_packed_git(struct packed_git *p)
556556
{
557557
struct stat st;
558558
struct pack_header hdr;
@@ -562,49 +562,50 @@ static void open_packed_git(struct packed_git *p)
562562

563563
p->pack_fd = open(p->pack_name, O_RDONLY);
564564
if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
565-
die("packfile %s cannot be opened", p->pack_name);
565+
return -1;
566566

567567
/* If we created the struct before we had the pack we lack size. */
568568
if (!p->pack_size) {
569569
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);
571571
p->pack_size = st.st_size;
572572
} 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);
574574

575575
/* We leave these file descriptors open with sliding mmap;
576576
* there is no point keeping them open across exec(), though.
577577
*/
578578
fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
579579
if (fd_flag < 0)
580-
die("cannot determine file descriptor flags");
580+
return error("cannot determine file descriptor flags");
581581
fd_flag |= FD_CLOEXEC;
582582
if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
583-
die("cannot set FD_CLOEXEC");
583+
return error("cannot set FD_CLOEXEC");
584584

585585
/* Verify we recognize this pack file format. */
586586
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);
588588
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);
590590
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"
592592
" (try upgrading GIT to a newer version)",
593593
p->pack_name, ntohl(hdr.hdr_version));
594594

595595
/* Verify the pack matches its index. */
596596
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"
598598
" while index size indicates %u objects",
599599
p->pack_name, ntohl(hdr.hdr_entries),
600600
num_packed_objects(p));
601601
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);
603603
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);
605605
idx_sha1 = ((unsigned char *)p->index_base) + p->index_size - 40;
606606
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;
608609
}
609610

610611
static int in_window(struct pack_window *win, unsigned long offset)
@@ -627,8 +628,8 @@ unsigned char* use_pack(struct packed_git *p,
627628
{
628629
struct pack_window *win = *w_cursor;
629630

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);
632633

633634
/* Since packfiles end in a hash of their content and its
634635
* pointless to ask for an offset into the middle of that

0 commit comments

Comments
 (0)