make open_packed_object() expect 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
diff --git a/lib/pack.c b/lib/pack.c
index 9ca9a73..20a09ec 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -1146,13 +1146,11 @@ done:
}
static const struct got_error *
-open_packed_object(struct got_object **obj, struct got_repository *repo,
+open_packed_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;
off_t offset;
- char *path_packfile;
- struct got_pack *pack;
uint8_t type;
uint64_t size;
size_t tslen;
@@ -1163,42 +1161,28 @@ 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, packidx);
- if (err)
- return err;
-
- pack = get_cached_pack(path_packfile, repo);
- if (pack == NULL) {
- err = cache_pack(&pack, path_packfile, packidx, repo);
- if (err)
- goto done;
- }
-
err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
if (err)
- goto done;
+ return err;
switch (type) {
case GOT_OBJ_TYPE_COMMIT:
case GOT_OBJ_TYPE_TREE:
case GOT_OBJ_TYPE_BLOB:
case GOT_OBJ_TYPE_TAG:
- err = open_plain_object(obj, path_packfile, id, type,
+ err = open_plain_object(obj, pack->path_packfile, id, type,
offset + tslen, size);
break;
-
case GOT_OBJ_TYPE_OFFSET_DELTA:
case GOT_OBJ_TYPE_REF_DELTA:
err = open_delta_object(obj, packidx, pack, id, offset,
tslen, type, size);
break;
-
default:
err = got_error(GOT_ERR_OBJ_TYPE);
- goto done;
+ break;
}
-done:
- free(path_packfile);
+
return err;
}
@@ -1208,17 +1192,32 @@ got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
{
const struct got_error *err = NULL;
struct got_packidx *packidx = NULL;
+ struct got_pack *pack;
int idx;
+ char *path_packfile;
err = search_packidx(&packidx, &idx, repo, id);
if (err)
return err;
- err = open_packed_object(obj, repo, packidx, idx, id);
+ err = get_packfile_path(&path_packfile, packidx);
if (err)
return err;
+ pack = get_cached_pack(path_packfile, repo);
+ if (pack == NULL) {
+ err = cache_pack(&pack, path_packfile, packidx, repo);
+ if (err)
+ goto done;
+ }
+
+ err = open_packed_object(obj, pack, packidx, idx, id);
+ if (err)
+ goto done;
+
err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
+done:
+ free(path_packfile);
return err;
}