make got-read-pack actually use its object cache
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/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 3952d83..7147a97 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -52,6 +52,26 @@ catch_sigint(int signo)
}
static const struct got_error *
+open_object(struct got_object **obj, struct got_pack *pack,
+ struct got_packidx *packidx, int idx, struct got_object_id *id,
+ struct got_object_cache *objcache)
+{
+ const struct got_error *err;
+
+ err = got_packfile_open_object(obj, pack, packidx, idx, id);
+ if (err)
+ return err;
+ (*obj)->refcnt++;
+
+ err = got_object_cache_add(objcache, id, *obj);
+ if (err)
+ return err;
+ (*obj)->refcnt++;
+
+ return NULL;
+}
+
+static const struct got_error *
object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
struct got_packidx *packidx, struct got_object_cache *objcache)
{
@@ -67,15 +87,15 @@ object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
memcpy(&iobj, imsg->data, sizeof(iobj));
memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
- err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
- if (err)
- return err;
- obj->refcnt++;
-
- err = got_object_cache_add(objcache, &obj->id, obj);
- if (err)
- goto done;
- obj->refcnt++;
+ obj = got_object_cache_get(objcache, &id);
+ if (obj) {
+ obj->refcnt++;
+ } else {
+ err = open_object(&obj, pack, packidx, iobj.idx, &id,
+ objcache);
+ if (err)
+ goto done;
+ }
err = got_privsep_send_obj(ibuf, obj);
done:
@@ -102,9 +122,15 @@ commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
memcpy(&iobj, imsg->data, sizeof(iobj));
memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
- err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
- if (err)
- return err;
+ obj = got_object_cache_get(objcache, &id);
+ if (obj) {
+ obj->refcnt++;
+ } else {
+ err = open_object(&obj, pack, packidx, iobj.idx, &id,
+ objcache);
+ if (err)
+ return err;
+ }
err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
if (err)
@@ -149,9 +175,15 @@ tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
memcpy(&iobj, imsg->data, sizeof(iobj));
memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
- err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
- if (err)
- return err;
+ obj = got_object_cache_get(objcache, &id);
+ if (obj) {
+ obj->refcnt++;
+ } else {
+ err = open_object(&obj, pack, packidx, iobj.idx, &id,
+ objcache);
+ if (err)
+ return err;
+ }
err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
if (err)
@@ -231,9 +263,15 @@ blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
memcpy(&iobj, imsg->data, sizeof(iobj));
memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
- err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
- if (err)
- return err;
+ obj = got_object_cache_get(objcache, &id);
+ if (obj) {
+ obj->refcnt++;
+ } else {
+ err = open_object(&obj, pack, packidx, iobj.idx, &id,
+ objcache);
+ if (err)
+ return err;
+ }
err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
if (err)
@@ -297,9 +335,15 @@ tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
memcpy(&iobj, imsg->data, sizeof(iobj));
memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
- err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
- if (err)
- return err;
+ obj = got_object_cache_get(objcache, &id);
+ if (obj) {
+ obj->refcnt++;
+ } else {
+ err = open_object(&obj, pack, packidx, iobj.idx, &id,
+ objcache);
+ if (err)
+ return err;
+ }
err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
if (err)