move packidx header initialization to a separate function
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index ac420c8..ffb585a 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -144,6 +144,7 @@ struct got_packfile_obj_data {
} __attribute__((__packed__));
} __attribute__((__packed__));
+const struct got_error *got_packidx_init_hdr(struct got_packidx *, int);
const struct got_error *got_packidx_open(struct got_packidx **,
const char *, int);
const struct got_error* got_packidx_close(struct got_packidx *);
diff --git a/lib/pack.c b/lib/pack.c
index 440b49e..e50ad5c 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -119,53 +119,17 @@ get_packfile_size(size_t *size, const char *path)
}
const struct got_error *
-got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
+got_packidx_init_hdr(struct got_packidx *p, int verify)
{
- struct got_packidx *p;
- struct got_packidx_v2_hdr *h;
const struct got_error *err = NULL;
- size_t nobj, len_fanout, len_ids, offset, remain;
- ssize_t n;
+ struct got_packidx_v2_hdr *h;
SHA1_CTX ctx;
uint8_t sha1[SHA1_DIGEST_LENGTH];
-
- *packidx = NULL;
+ size_t nobj, len_fanout, len_ids, offset, remain;
+ ssize_t n;
SHA1Init(&ctx);
- p = calloc(1, sizeof(*p));
- if (p == NULL)
- return got_error_from_errno();
-
- p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
- if (p->fd == -1)
- return got_error_from_errno();
-
- err = get_packfile_size(&p->len, path);
- if (err) {
- close(p->fd);
- free(p);
- return err;
- }
- if (p->len < sizeof(p->hdr)) {
- err = got_error(GOT_ERR_BAD_PACKIDX);
- close(p->fd);
- free(p);
- return err;
- }
-
- p->path_packidx = strdup(path);
- if (p->path_packidx == NULL) {
- err = got_error_from_errno();
- goto done;
- }
-
-#ifndef GOT_PACK_NO_MMAP
- p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
- if (p->map == MAP_FAILED)
- p->map = NULL; /* fall back to read(2) */
-#endif
-
h = &p->hdr;
offset = 0;
remain = p->len;
@@ -384,10 +348,57 @@ checksum:
err = got_error(GOT_ERR_PACKIDX_CSUM);
}
done:
+ return err;
+}
+
+const struct got_error *
+got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
+{
+ const struct got_error *err = NULL;
+ struct got_packidx *p;
+
+ *packidx = NULL;
+
+ p = calloc(1, sizeof(*p));
+ if (p == NULL)
+ return got_error_from_errno();
+
+ p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
+ if (p->fd == -1)
+ return got_error_from_errno();
+
+ err = get_packfile_size(&p->len, path);
+ if (err) {
+ close(p->fd);
+ free(p);
+ return err;
+ }
+ if (p->len < sizeof(p->hdr)) {
+ err = got_error(GOT_ERR_BAD_PACKIDX);
+ close(p->fd);
+ free(p);
+ return err;
+ }
+
+ p->path_packidx = strdup(path);
+ if (p->path_packidx == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+#ifndef GOT_PACK_NO_MMAP
+ p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
+ if (p->map == MAP_FAILED)
+ p->map = NULL; /* fall back to read(2) */
+#endif
+
+ err = got_packidx_init_hdr(p, verify);
+done:
if (err)
got_packidx_close(p);
else
*packidx = p;
+
return err;
}