add a helper function for opening a pack file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
diff --git a/lib/pack.c b/lib/pack.c
index 5f0f002..e3805da 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -361,7 +361,7 @@ done:
return err;
}
-const struct got_error *
+static const struct got_error *
get_packfile_path(char **path_packfile, struct got_repository *repo,
struct got_packidx_v2_hdr *packidx)
{
@@ -411,6 +411,33 @@ read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
}
static const struct got_error *
+open_packfile(FILE **packfile, char **path_packfile,
+ struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
+{
+ const struct got_error *err;
+
+ *packfile = NULL;
+
+ err = get_packfile_path(path_packfile, repo, packidx);
+ if (err)
+ return err;
+
+ *packfile = fopen(*path_packfile, "rb");
+ if (*packfile == NULL) {
+ err = got_error_from_errno();
+ free(*path_packfile);
+ return err;
+ }
+
+ err = read_packfile_hdr(*packfile, packidx);
+ if (err) {
+ fclose(*packfile);
+ *packfile = NULL;
+ }
+ return err;
+}
+
+static const struct got_error *
parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
FILE *packfile)
{
@@ -583,17 +610,11 @@ resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
return got_error(GOT_ERR_BAD_PACKIDX);
}
- err = get_packfile_path(&path_base_packfile, repo, packidx);
+ err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
got_packidx_close(packidx);
if (err)
return err;
- base_packfile = fopen(path_base_packfile, "rb");
- if (base_packfile == NULL) {
- err = got_error_from_errno();
- goto done;
- }
-
if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;
@@ -720,20 +741,10 @@ open_packed_object(struct got_object **obj, struct got_repository *repo,
if (offset == (uint64_t)-1)
return got_error(GOT_ERR_BAD_PACKIDX);
- err = get_packfile_path(&path_packfile, repo, packidx);
+ err = open_packfile(&packfile, &path_packfile, repo, packidx);
if (err)
return err;
- packfile = fopen(path_packfile, "rb");
- if (packfile == NULL) {
- err = got_error_from_errno();
- goto done;
- }
-
- err = read_packfile_hdr(packfile, packidx);
- if (err)
- goto done;
-
if (fseeko(packfile, offset, SEEK_SET) != 0) {
err = got_error_from_errno();
goto done;