Commit 7e212e3dbc3a87450c1a245dd3a735e05c8d3eb0

Stefan Sperling 2018-09-09T17:24:46

make got_packfile_extract_object_to_mem() accept pack instead of repo

diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index 549406d..4de7dfb 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -165,6 +165,6 @@ const struct got_error *got_packfile_open_object(struct got_object **,
 const struct got_error *got_packfile_extract_object(struct got_pack *,
     struct got_object *, FILE *);
 const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
-    struct got_object *, struct got_repository *);
+    struct got_object *, struct got_pack *);
 const struct got_error *got_pack_get_packfile_size(size_t *, const char *);
 struct got_pack *got_repo_get_cached_pack(struct got_repository *, const char *);
diff --git a/lib/object.c b/lib/object.c
index a19cdc7..876b0f7 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -327,6 +327,27 @@ got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
 	return NULL;
 }
 
+static const struct got_error *
+extract_packed_object_to_mem(uint8_t **buf, size_t *len,
+    struct got_object *obj, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_pack *pack;
+
+	if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
+		return got_error(GOT_ERR_OBJ_NOT_PACKED);
+
+	pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+	if (pack == NULL) {
+		err = got_repo_cache_pack(&pack, repo,
+		    obj->path_packfile, NULL);
+		if (err)
+			return err;
+	}
+
+	return got_packfile_extract_object_to_mem(buf, len, obj, pack);
+}
+
 const struct got_error *
 got_object_commit_open(struct got_commit_object **commit,
     struct got_repository *repo, struct got_object *obj)
@@ -345,7 +366,7 @@ got_object_commit_open(struct got_commit_object **commit,
 	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
 		uint8_t *buf;
 		size_t len;
-		err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
+		err = extract_packed_object_to_mem(&buf, &len, obj, repo);
 		if (err)
 			return err;
 		obj->size = len;
@@ -386,7 +407,7 @@ got_object_tree_open(struct got_tree_object **tree,
 	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
 		uint8_t *buf;
 		size_t len;
-		err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
+		err = extract_packed_object_to_mem(&buf, &len, obj, repo);
 		if (err)
 			return err;
 		obj->size = len;
diff --git a/lib/pack.c b/lib/pack.c
index 329d024..d1d6698 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -1231,39 +1231,27 @@ got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
 
 const struct got_error *
 got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
-    struct got_object *obj, struct got_repository *repo)
+    struct got_object *obj, struct got_pack *pack)
 {
 	const struct got_error *err = NULL;
-	struct got_pack *pack;
 
 	if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
 		return got_error(GOT_ERR_OBJ_NOT_PACKED);
 
-	pack = got_repo_get_cached_pack(repo, obj->path_packfile);
-	if (pack == NULL) {
-		err = got_repo_cache_pack(&pack, repo, obj->path_packfile, NULL);
-		if (err)
-			goto done;
-	}
-
 	if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
-		if (obj->pack_offset >= pack->filesize) {
-			err = got_error(GOT_ERR_PACK_OFFSET);
-			goto done;
-		}
+		if (obj->pack_offset >= pack->filesize)
+			return got_error(GOT_ERR_PACK_OFFSET);
 		if (pack->map) {
 			size_t mapoff = (size_t)obj->pack_offset;
 			err = got_inflate_to_mem_mmap(buf, len, pack->map,
 			    mapoff, pack->filesize - mapoff);
 		} else {
-			if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
-				err = got_error_from_errno();
-				goto done;
-			}
+			if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
+				return got_error_from_errno();
 			err = got_inflate_to_mem_fd(buf, len, pack->fd);
 		}
 	} else
 		err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
-done:
+
 	return err;
 }