[PATCH 3/6] packfile: inline `open_packed_git_1()`
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Nov 20, 2026, 12:02 UTC
- Message-ID
- <20261120-pks-packfile-global-statistics-v1-3-19beb83b145f@pks.im>
- In-Reply-To
- <20261120-pks-packfile-global-statistics-v1-0-19beb83b145f@pks.im>
The function `open_packed_git()` is responsible for opening a packfile. The majority of work is not performed by it though, but instead by its `_1()` sibling. But this sibling really only exists because we thought it too hard to handle errors properly and close the packfile's file descriptor on an error path. So `_1()` doesn't handle those, and instead defers to its only caller to close the file descriptor in the error path again.
Fix this design smell by introducing a common exit path that knows to close the file descriptor on error. This allows us to inline the call to `open_packed_git_1()`.
While at it, rename the function to `packfile_open_pack()` to match modern best practices. Besides that, it also gives the caller a better idea of _what_ is being opened, as we also have functions that open other files that belong to a packfile.
Signed-off-by: Patrick Steinhardt <ps@pks.im> --- packfile.c | 115 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 70 insertions(+), 45 deletions(-)
diff --git a/packfile.c b/packfile.c index 95a97c302c7..9b03258311f 100644 --- a/packfile.c +++ b/packfile.c @@ -480,11 +480,7 @@ const char *pack_basename(struct packed_git *p) return ret; } -/* - * Do not call this directly as this leaks p->pack_fd on error return; - * call open_packed_git() instead. - */ -static int open_packed_git_1(struct packed_git *p) +static int packfile_open_pack(struct packed_git *p) { struct stat st; struct pack_header hdr; @@ -492,9 +488,12 @@ static int open_packed_git_1(struct packed_git *p) unsigned char *idx_hash; ssize_t read_result; const unsigned hashsz = p->repo->hash_algo->rawsz; + int ret; - if (packfile_open_index(p)) - return error("packfile %s index unavailable", p->pack_name); + if (packfile_open_index(p)) { + ret = error("packfile %s index unavailable", p->pack_name); + goto out; + } if (!pack_max_fds) { unsigned int max_fds = get_max_fd_limit(); @@ -510,55 +509,81 @@ static int open_packed_git_1(struct packed_git *p) ; /* nothing */ p->pack_fd = git_open(p->pack_name); - if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) - return -1; + if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) { + ret = -1; + goto out; + } pack_open_fds++; /* If we created the struct before we had the pack we lack size. */ if (!p->pack_size) { - if (!S_ISREG(st.st_mode)) - return error("packfile %s not a regular file", p->pack_name); + if (!S_ISREG(st.st_mode)) { + ret = error("packfile %s not a regular file", p->pack_name); + goto out; + } + p->pack_size = st.st_size; - } else if (p->pack_size != st.st_size) - return error("packfile %s size changed", p->pack_name); + } else if (p->pack_size != st.st_size) { + ret = error("packfile %s size changed", p->pack_name); + goto out; + } /* Verify we recognize this pack file format. */ read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr)); - if (read_result < 0) - return error_errno("error reading from %s", p->pack_name); - if (read_result != sizeof(hdr)) - return error("file %s is far too short to be a packfile", p->pack_name); - if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) - return error("file %s is not a GIT packfile", p->pack_name); - if (!pack_version_ok(hdr.hdr_version)) - return error("packfile %s is version %"PRIu32" and not" - " supported (try upgrading GIT to a newer version)", - p->pack_name, ntohl(hdr.hdr_version)); + if (read_result < 0) { + ret = error_errno("error reading from %s", p->pack_name); + goto out; + } + + if (read_result != sizeof(hdr)) { + ret = error("file %s is far too short to be a packfile", p->pack_name); + goto out; + } + + if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) { + ret = error("file %s is not a GIT packfile", p->pack_name); + goto out; + } + + if (!pack_version_ok(hdr.hdr_version)) { + ret = error("packfile %s is version %"PRIu32" and not" + " supported (try upgrading GIT to a newer version)", + p->pack_name, ntohl(hdr.hdr_version)); + goto out; + } /* Verify the pack matches its index. */ - if (p->num_objects != ntohl(hdr.hdr_entries)) - return error("packfile %s claims to have %"PRIu32" objects" - " while index indicates %"PRIu32" objects", - p->pack_name, ntohl(hdr.hdr_entries), - p->num_objects); + if (p->num_objects != ntohl(hdr.hdr_entries)) { + ret = error("packfile %s claims to have %"PRIu32" objects" + " while index indicates %"PRIu32" objects", + p->pack_name, ntohl(hdr.hdr_entries), + p->num_objects); + goto out; + } + read_result = pread_in_full(p->pack_fd, hash, hashsz, p->pack_size - hashsz); - if (read_result < 0) - return error_errno("error reading from %s", p->pack_name); - if (read_result != hashsz) - return error("packfile %s signature is unavailable", p->pack_name); + if (read_result < 0) { + ret = error_errno("error reading from %s", p->pack_name); + goto out; + } + if (read_result != hashsz) { + ret = error("packfile %s signature is unavailable", p->pack_name); + goto out; + } + idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2; - if (!hasheq(hash, idx_hash, p->repo->hash_algo)) - return error("packfile %s does not match index", p->pack_name); - return 0; -} + if (!hasheq(hash, idx_hash, p->repo->hash_algo)) { + ret = error("packfile %s does not match index", p->pack_name); + goto out; + } -static int open_packed_git(struct packed_git *p) -{ - if (!open_packed_git_1(p)) - return 0; - packfile_close_fd(p); - return -1; + ret = 0; + +out: + if (ret < 0) + packfile_close_fd(p); + return ret; } static int in_window(struct repository *r, struct pack_window *win, @@ -587,7 +612,7 @@ unsigned char *use_pack(struct packed_git *p, * hash, and the in_window function above wouldn't match * don't allow an offset too close to the end of the file. */ - if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p)) + if (!p->pack_size && p->pack_fd == -1 && packfile_open_pack(p)) die("packfile %s cannot be accessed", p->pack_name); if (offset > (p->pack_size - p->repo->hash_algo->rawsz)) die("offset beyond end of packfile (truncated pack?)"); @@ -612,7 +637,7 @@ unsigned char *use_pack(struct packed_git *p, window_align = settings->packed_git_window_size / 2; - if (p->pack_fd == -1 && open_packed_git(p)) + if (p->pack_fd == -1 && packfile_open_pack(p)) die("packfile %s cannot be accessed", p->pack_name); CALLOC_ARRAY(win, 1); @@ -1810,7 +1835,7 @@ int is_pack_valid(struct packed_git *p) } /* Force the pack to open to prove its valid. */ - return !open_packed_git(p); + return !packfile_open_pack(p); } int packfile_fill_entry(struct packed_git *p,
-- 2.56.1.831.g720f3f5937.dirty