make got_packfile_open_object() accept a pack instead of a repo
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index ba5b7f5..1202094 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -161,7 +161,7 @@ const struct got_error* got_packidx_close(struct got_packidx *);
int got_packidx_get_object_idx(struct got_packidx *, struct got_object_id *);
const struct got_error *got_packfile_open_object(struct got_object **,
- struct got_object_id *, struct got_repository *);
+ struct got_pack *, struct got_packidx *, int, struct got_object_id *);
const struct got_error *got_packfile_extract_object(FILE **,
struct got_object *, struct got_repository *);
const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
diff --git a/lib/object.c b/lib/object.c
index 11b768f..cf703fb 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -164,6 +164,66 @@ done:
return err;
}
+static const struct got_error *
+get_packfile_path(char **path_packfile, struct got_packidx *packidx)
+{
+ size_t size;
+
+ /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
+ size = strlen(packidx->path_packidx) + 2;
+ if (size < GOT_PACKFILE_NAMELEN + 1)
+ return got_error(GOT_ERR_BAD_PATH);
+
+ *path_packfile = calloc(size, sizeof(**path_packfile));
+ if (*path_packfile == NULL)
+ return got_error_from_errno();
+
+ /* Copy up to and excluding ".idx". */
+ if (strlcpy(*path_packfile, packidx->path_packidx,
+ size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
+ return got_error(GOT_ERR_NO_SPACE);
+
+ if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
+ return got_error(GOT_ERR_NO_SPACE);
+
+ return NULL;
+}
+
+static const struct got_error *
+open_packed_object(struct got_object **obj, struct got_object_id *id,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_pack *pack = NULL;
+ struct got_packidx *packidx = NULL;
+ int idx;
+ char *path_packfile;
+
+ err = got_repo_search_packidx(&packidx, &idx, repo, id);
+ if (err)
+ return err;
+
+ err = get_packfile_path(&path_packfile, packidx);
+ if (err)
+ return err;
+
+ pack = got_repo_get_cached_pack(repo, path_packfile);
+ if (pack == NULL) {
+ err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
+ if (err)
+ goto done;
+ }
+
+ err = got_packfile_open_object(obj, pack, packidx, idx, id);
+ if (err)
+ goto done;
+
+ err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
+done:
+ free(path_packfile);
+ return err;
+}
+
const struct got_error *
got_object_open(struct got_object **obj, struct got_repository *repo,
struct got_object_id *id)
@@ -188,7 +248,7 @@ got_object_open(struct got_object **obj, struct got_repository *repo,
err = got_error_from_errno();
goto done;
}
- err = got_packfile_open_object(obj, id, repo);
+ err = open_packed_object(obj, id, repo);
if (err)
goto done;
if (*obj == NULL)
diff --git a/lib/pack.c b/lib/pack.c
index 4ea89f7..7e57651 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -461,31 +461,6 @@ got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id
return -1;
}
-static const struct got_error *
-get_packfile_path(char **path_packfile, struct got_packidx *packidx)
-{
- size_t size;
-
- /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
- size = strlen(packidx->path_packidx) + 2;
- if (size < GOT_PACKFILE_NAMELEN + 1)
- return got_error(GOT_ERR_BAD_PATH);
-
- *path_packfile = calloc(size, sizeof(**path_packfile));
- if (*path_packfile == NULL)
- return got_error_from_errno();
-
- /* Copy up to and excluding ".idx". */
- if (strlcpy(*path_packfile, packidx->path_packidx,
- size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
- return got_error(GOT_ERR_NO_SPACE);
-
- if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
- return got_error(GOT_ERR_NO_SPACE);
-
- return NULL;
-}
-
const struct got_error *
got_pack_close(struct got_pack *pack)
{
@@ -905,8 +880,8 @@ done:
return err;
}
-static const struct got_error *
-open_packed_object(struct got_object **obj, struct got_pack *pack,
+const struct got_error *
+got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
struct got_packidx *packidx, int idx, struct got_object_id *id)
{
const struct got_error *err = NULL;
@@ -946,41 +921,6 @@ open_packed_object(struct got_object **obj, struct got_pack *pack,
return err;
}
-const struct got_error *
-got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
- struct got_repository *repo)
-{
- const struct got_error *err = NULL;
- struct got_packidx *packidx = NULL;
- struct got_pack *pack;
- int idx;
- char *path_packfile;
-
- err = got_repo_search_packidx(&packidx, &idx, repo, id);
- if (err)
- return err;
-
- err = get_packfile_path(&path_packfile, packidx);
- if (err)
- return err;
-
- pack = got_repo_get_cached_pack(repo, path_packfile);
- if (pack == NULL) {
- err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
- if (err)
- goto done;
- }
-
- err = open_packed_object(obj, pack, packidx, idx, id);
- if (err)
- goto done;
-
- err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
-done:
- free(path_packfile);
- return err;
-}
-
static const struct got_error *
get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
{