Commit 2090a03dadad63d6ea945cbe4abb19949b5aff44

Stefan Sperling 2018-09-09T17:00:33

make got_packfile_open_object() accept a pack instead of a repo

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)
 {