Commit c59b334653ceb49c2009ed64fe3a16fa89e0e7f4

Stefan Sperling 2018-09-11T12:29:46

stop sending deltas in imsg; cache deltas in got-read-pack

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index fab4287..fc8b2ee 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -29,6 +29,7 @@ struct got_object {
 	struct got_object_id id;
 
 	char *path_packfile;	/* if packed */
+	int pack_idx;		/* if packed */
 	off_t pack_offset;	/* if packed */
 	struct got_delta_chain deltas; /* if deltified */
 	int refcnt;		/* > 0 if open and/or cached */
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index c36311d..38a8347 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -92,10 +92,6 @@ enum got_imsg_type {
 	GOT_IMSG_PACKIDX,
 	GOT_IMSG_PACK,
 	GOT_IMSG_PACKED_OBJECT_REQUEST,
-
-	/* Messages for transmitting deltas and associated delta streams: */
-	GOT_IMSG_DELTA,
-	GOT_IMSG_DELTA_STREAM,
 };
 
 /* Structure for GOT_IMSG_ERROR. */
@@ -104,43 +100,20 @@ struct got_imsg_error {
 	int errno_code; /* in case code equals GOT_ERR_ERRNO */
 };
 
-/* Structure for GOT_IMSG_DELTA data. */
-struct got_imsg_delta {
-	/* These fields are the same as in struct got_delta. */
-	off_t offset;
-	size_t tslen;
-	int type;
-	size_t size;
-	off_t data_offset;
-	size_t delta_len;
-
-	/*
-	 * Followed by one or more DELTA_STREAM messages until delta_len
-	 * bytes of delta stream have been transmitted.
-	 */
-};
-
-/* Structure for GOT_IMSG_DELTA_STREAM data. */
-struct got_imsg_delta_stream {
-	/*
-	 * Empty since the following is implied:
-	 * Read additional delta stream data from imsg buffer.
-	 */
-};
-
 /*
  * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
  * and GOT_IMSG_OBJECT data.
  */
 struct got_imsg_object {
+	uint8_t id[SHA1_DIGEST_LENGTH];
+
 	/* These fields are the same as in struct got_object. */
 	int type;
 	int flags;
 	size_t hdrlen;
 	size_t size;
-
 	off_t pack_offset;
-	int ndeltas; /* this many GOT_IMSG_DELTA messages follow */
+	int pack_idx;
 };
 
 /* Structure for GOT_IMSG_COMMIT data. */
diff --git a/lib/object_idcache.c b/lib/object_idcache.c
index adef4e5..c10be9b 100644
--- a/lib/object_idcache.c
+++ b/lib/object_idcache.c
@@ -105,7 +105,7 @@ got_object_idcache_get(struct got_object_idcache *cache, struct got_object_id *i
 	struct got_object_idcache_element *entry;
 
 	TAILQ_FOREACH(entry, &cache->entries, entry) {
-		if (got_object_id_cmp(&entry->id, id) != 0)
+		if (memcmp(&entry->id.sha1, id->sha1, SHA1_DIGEST_LENGTH) != 0)
 			continue;
 		if (entry != TAILQ_FIRST(&cache->entries)) {
 			TAILQ_REMOVE(&cache->entries, entry, entry);
@@ -144,7 +144,7 @@ got_object_idcache_contains(struct got_object_idcache *cache,
 	struct got_object_idcache_element *entry;
 
 	TAILQ_FOREACH(entry, &cache->entries, entry) {
-		if (got_object_id_cmp(&entry->id, id) == 0)
+		if (memcmp(&entry->id.sha1, id->sha1, SHA1_DIGEST_LENGTH) == 0)
 			return 1;
 	}
 
diff --git a/lib/pack.c b/lib/pack.c
index 00d5a15..3560e5c 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -554,7 +554,7 @@ parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
 
 static const struct got_error *
 open_plain_object(struct got_object **obj, const char *path_packfile,
-    struct got_object_id *id, uint8_t type, off_t offset, size_t size)
+    struct got_object_id *id, uint8_t type, off_t offset, size_t size, int idx)
 {
 	*obj = calloc(1, sizeof(**obj));
 	if (*obj == NULL)
@@ -570,6 +570,7 @@ open_plain_object(struct got_object **obj, const char *path_packfile,
 
 	(*obj)->type = type;
 	(*obj)->flags = GOT_OBJ_FLAG_PACKED;
+	(*obj)->pack_idx = idx;
 	(*obj)->hdrlen = 0;
 	(*obj)->size = size;
 	if (id)
@@ -858,7 +859,7 @@ resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
 static const struct got_error *
 open_delta_object(struct got_object **obj, struct got_packidx *packidx,
     struct got_pack *pack, struct got_object_id *id, off_t offset,
-    size_t tslen, int delta_type, size_t delta_size)
+    size_t tslen, int delta_type, size_t delta_size, int idx)
 {
 	const struct got_error *err = NULL;
 	int resolved_type;
@@ -880,6 +881,7 @@ open_delta_object(struct got_object **obj, struct got_packidx *packidx,
 		goto done;
 	}
 	(*obj)->flags |= GOT_OBJ_FLAG_PACKED;
+	(*obj)->pack_idx = idx;
 
 	SIMPLEQ_INIT(&(*obj)->deltas.entries);
 	(*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
@@ -893,7 +895,6 @@ open_delta_object(struct got_object **obj, struct got_packidx *packidx,
 	if (err)
 		goto done;
 	(*obj)->type = resolved_type;
-
 done:
 	if (err) {
 		got_object_close(*obj);
@@ -928,12 +929,12 @@ got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
 	case GOT_OBJ_TYPE_BLOB:
 	case GOT_OBJ_TYPE_TAG:
 		err = open_plain_object(obj, pack->path_packfile, id, type,
-		    offset + tslen, size);
+		    offset + tslen, size, idx);
 		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);
+		    tslen, type, size, idx);
 		break;
 	default:
 		err = got_error(GOT_ERR_OBJ_TYPE);
diff --git a/lib/privsep.c b/lib/privsep.c
index b69b1fe..b101ac4 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -214,49 +214,9 @@ got_privsep_send_stop(int fd)
 	return err;
 }
 
-static const struct got_error *
-send_delta(struct got_delta *delta, struct imsgbuf *ibuf)
-{
-	struct got_imsg_delta idelta;
-	size_t offset, remain;
-
-	idelta.offset = delta->offset;
-	idelta.tslen = delta->tslen;
-	idelta.type = delta->type;
-	idelta.size = delta->size;
-	idelta.data_offset = delta->data_offset;
-	idelta.delta_len = delta->delta_len;
-
-	if (imsg_compose(ibuf, GOT_IMSG_DELTA, 0, 0, -1,
-	    &idelta, sizeof(idelta)) == -1)
-		return got_error_from_errno();
-
-	if (imsg_flush(ibuf) == -1)
-		return got_error_from_errno();
-
-	offset = 0;
-	remain = delta->delta_len;
-	while (remain > 0) {
-		size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
-
-		if (imsg_compose(ibuf, GOT_IMSG_DELTA_STREAM, 0, 0, -1,
-		    delta->delta_buf + offset, n) == -1)
-			return got_error_from_errno();
-
-		if (imsg_flush(ibuf) == -1)
-			return got_error_from_errno();
-
-		offset += n;
-		remain -= n;
-	}
-
-	return NULL;
-}
-
 const struct got_error *
 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
 {
-	const struct got_error *err = NULL;
 	struct got_imsg_object iobj, *iobjp = NULL;
 	size_t iobj_size = 0;
 	int imsg_code = GOT_IMSG_OBJECT_REQUEST;
@@ -276,13 +236,15 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
 			return got_error(GOT_ERR_OBJ_TYPE);
 		}
 
+		memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
 		iobj.type = obj->type;
 		iobj.flags = obj->flags;
 		iobj.hdrlen = obj->hdrlen;
 		iobj.size = obj->size;
-		iobj.ndeltas = obj->deltas.nentries;
-		if (iobj.flags & GOT_OBJ_FLAG_PACKED)
+		if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
 			iobj.pack_offset = obj->pack_offset;
+			iobj.pack_idx = obj->pack_idx;
+		}
 
 		iobjp = &iobj;
 		iobj_size = sizeof(iobj);
@@ -291,20 +253,7 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
 	if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
 		return got_error_from_errno();
 
-	err = flush_imsg(ibuf);
-	if (err)
-		return err;
-
-	if (obj && obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
-		struct got_delta *delta;
-		SIMPLEQ_FOREACH(delta, &obj->deltas.entries, entry) {
-			err = send_delta(delta, ibuf);
-			if (err)
-				break;
-		}
-	}
-
-	return err;
+	return flush_imsg(ibuf);
 }
 
 const struct got_error *
@@ -330,117 +279,23 @@ got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
 const struct got_error *
 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
 {
-	const struct got_error *err = NULL;
 	struct got_imsg_object iobj;
-	struct got_delta *delta;
 
+	memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
 	iobj.type = obj->type;
 	iobj.flags = obj->flags;
 	iobj.hdrlen = obj->hdrlen;
 	iobj.size = obj->size;
-	iobj.ndeltas = obj->deltas.nentries;
-	if (iobj.flags & GOT_OBJ_FLAG_PACKED)
+	if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
 		iobj.pack_offset = obj->pack_offset;
+		iobj.pack_idx = obj->pack_idx;
+	}
 
 	if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
 	    == -1)
 		return got_error_from_errno();
 
-	err = flush_imsg(ibuf);
-	if (err)
-		return err;
-
-	SIMPLEQ_FOREACH(delta, &obj->deltas.entries, entry) {
-		err = send_delta(delta, ibuf);
-		if (err)
-			break;
-	}
-
-	return err;
-}
-
-static const struct got_error *
-receive_delta(struct got_delta **delta, struct imsgbuf *ibuf)
-{
-	const struct got_error *err = NULL;
-	struct imsg imsg;
-	struct got_imsg_delta idelta;
-	uint8_t *delta_buf = NULL;
-	const size_t min_datalen =
-	    MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_delta));
-	size_t datalen, offset, remain;
-
-	err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
-	if (err)
-		return err;
-
-	datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
-	if (imsg.hdr.type == GOT_IMSG_ERROR)
-		return recv_imsg_error(&imsg, datalen);
-
-	if (imsg.hdr.type != GOT_IMSG_DELTA)
-		return got_error(GOT_ERR_PRIVSEP_MSG);
-	if (datalen != sizeof(idelta))
-		return got_error(GOT_ERR_PRIVSEP_LEN);
-
-	memcpy(&idelta, imsg.data, sizeof(idelta));
-	imsg_free(&imsg);
-
-	switch (idelta.type) {
-	case GOT_OBJ_TYPE_OFFSET_DELTA:
-	case GOT_OBJ_TYPE_REF_DELTA:
-		if (idelta.delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
-			return got_error(GOT_ERR_BAD_DELTA);
-		break;
-	default:
-		if (idelta.delta_len != 0)
-			return got_error(GOT_ERR_BAD_DELTA);
-		break;
-	}
-
-	if (idelta.delta_len > 0) {
-		delta_buf = calloc(1, idelta.delta_len);
-		if (delta_buf == NULL)
-			return got_error_from_errno();
-
-		offset = 0;
-		remain = idelta.delta_len;
-		while (remain > 0) {
-			size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
-
-			err = got_privsep_recv_imsg(&imsg, ibuf, n);
-			if (err)
-				return err;
-
-			if (imsg.hdr.type == GOT_IMSG_ERROR)
-				return recv_imsg_error(&imsg, datalen);
-
-			if (imsg.hdr.type == GOT_IMSG_STOP)
-				break;
-
-			if (imsg.hdr.type != GOT_IMSG_DELTA_STREAM)
-				return got_error(GOT_ERR_PRIVSEP_MSG);
-
-			datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
-			if (datalen != n)
-				return got_error(GOT_ERR_PRIVSEP_LEN);
-
-			memcpy(delta_buf + offset, imsg.data, n);
-			imsg_free(&imsg);
-
-			offset += n;
-			remain -= n;
-		}
-	}
-
-	*delta = got_delta_open(idelta.offset, idelta.tslen, idelta.type,
-	    idelta.size, idelta.data_offset, delta_buf, idelta.delta_len);
-	if (*delta == NULL) {
-		err = got_error_from_errno();
-		free(delta_buf);
-	}
-
-	return err;
+	return flush_imsg(ibuf);
 }
 
 const struct got_error *
@@ -450,39 +305,25 @@ got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
 	const struct got_error *err = NULL;
 	struct got_imsg_object iobj;
 	size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	int i;
 
 	if (datalen != sizeof(iobj))
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	memcpy(&iobj, imsg->data, sizeof(iobj));
-	if (iobj.ndeltas < 0 ||
-	    iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX)
-		return got_error(GOT_ERR_PRIVSEP_LEN);
-
-	if (iobj.ndeltas > 0 &&
-	    (iobj.flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
-		return got_error(GOT_ERR_BAD_OBJ_DATA);
 
 	*obj = calloc(1, sizeof(**obj));
 	if (*obj == NULL)
 		return got_error_from_errno();
 
+	memcpy((*obj)->id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
 	(*obj)->type = iobj.type;
 	(*obj)->flags = iobj.flags;
 	(*obj)->hdrlen = iobj.hdrlen;
 	(*obj)->size = iobj.size;
-	/* id and path_packfile might be copied in by caller */
-	(*obj)->pack_offset = iobj.pack_offset;
-	SIMPLEQ_INIT(&(*obj)->deltas.entries);
-	for (i = 0; i < iobj.ndeltas; i++) {
-		struct got_delta *delta;
-		err = receive_delta(&delta, ibuf);
-		if (err)
-			break;
-		(*obj)->deltas.nentries++;
-		SIMPLEQ_INSERT_TAIL(&(*obj)->deltas.entries, delta,
-		    entry);
+	/* path_packfile is handled by caller */
+	if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
+		(*obj)->pack_offset = iobj.pack_offset;
+		(*obj)->pack_idx = iobj.pack_idx;
 	}
 
 	return err;
diff --git a/lib/repository.c b/lib/repository.c
index 682e940..ee42ef1 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -218,7 +218,6 @@ got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
 	err = got_object_cache_add(&repo->commitcache, id, commit);
 	if (err)
 		return err;
-
 	commit->refcnt++;
 #endif
 	return NULL;
diff --git a/libexec/got-read-pack/Makefile b/libexec/got-read-pack/Makefile
index 688107f..1d11b5c 100644
--- a/libexec/got-read-pack/Makefile
+++ b/libexec/got-read-pack/Makefile
@@ -1,8 +1,9 @@
 .PATH:${.CURDIR}/../../lib
 
 PROG=		got-read-pack
-SRCS=		got-read-pack.c delta.c error.c inflate.c object_parse.c \
-		opentemp.c pack.c path.c privsep.c sha1.c
+SRCS=		got-read-pack.c delta.c error.c inflate.c object_cache.c \
+		object_idcache.c object_parse.c opentemp.c pack.c path.c \
+		privsep.c sha1.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
 LDADD = -lutil -lz
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index c6634c6..001b16f 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -37,13 +37,14 @@
 #include "got_lib_delta.h"
 #include "got_lib_inflate.h"
 #include "got_lib_object.h"
+#include "got_lib_object_cache.h"
 #include "got_lib_object_parse.h"
 #include "got_lib_privsep.h"
 #include "got_lib_pack.h"
 
 static const struct got_error *
 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
-    struct got_packidx *packidx)
+    struct got_packidx *packidx, struct got_object_cache *objcache)
 {
 	const struct got_error *err = NULL;
 	struct got_imsg_packed_object iobj;
@@ -58,15 +59,55 @@ object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 	err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
 	if (err)
 		return err;
+	obj->refcnt++;
+
+	err = got_object_cache_add(objcache, &obj->id, obj);
+	if (err)
+		goto done;
+	obj->refcnt++;
 
 	err = got_privsep_send_obj(ibuf, obj);
+done:
 	got_object_close(obj);
 	return err;
 }
 
 static const struct got_error *
+get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
+    struct got_pack *pack, struct got_packidx *packidx,
+    struct got_object_cache *objcache, int type)
+{
+	const struct got_error *err = NULL;
+	struct got_object *iobj;
+
+	err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
+	if (err)
+		return err;
+
+	if (iobj->type != type) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+
+	if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
+		return got_error(GOT_ERR_OBJ_NOT_PACKED);
+
+	*obj = got_object_cache_get(objcache, &iobj->id);
+	if (*obj == NULL) {
+		err = got_packfile_open_object(obj, pack, packidx,
+		    iobj->pack_idx, &iobj->id);
+		if (err)
+			goto done;
+	}
+	(*obj)->refcnt++;
+done:
+	got_object_close(iobj);
+	return err;
+}
+
+static const struct got_error *
 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
-    struct got_packidx *packidx)
+    struct got_packidx *packidx, struct got_object_cache *objcache)
 {
 	const struct got_error *err = NULL;
 	struct got_object *obj = NULL;
@@ -74,13 +115,11 @@ commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 	uint8_t *buf;
 	size_t len;
 
-	err = got_privsep_get_imsg_obj(&obj, imsg, ibuf);
+	err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
+	    GOT_OBJ_TYPE_COMMIT);
 	if (err)
 		return err;
 
-	if (obj->type != GOT_OBJ_TYPE_COMMIT)
-		return got_error(GOT_ERR_OBJ_TYPE);
-
 	err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
 	if (err)
 		return err;
@@ -105,7 +144,7 @@ commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 
 static const struct got_error *
 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
-    struct got_packidx *packidx)
+    struct got_packidx *packidx, struct got_object_cache *objcache)
 {
 	const struct got_error *err = NULL;
 	struct got_object *obj = NULL;
@@ -113,13 +152,11 @@ tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 	uint8_t *buf;
 	size_t len;
 
-	err = got_privsep_get_imsg_obj(&obj, imsg, ibuf);
+	err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
+	    GOT_OBJ_TYPE_TREE);
 	if (err)
 		return err;
 
-	if (obj->type != GOT_OBJ_TYPE_TREE)
-		return got_error(GOT_ERR_OBJ_TYPE);
-
 	err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
 	if (err)
 		return err;
@@ -144,7 +181,7 @@ tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 
 static const struct got_error *
 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
-    struct got_packidx *packidx)
+    struct got_packidx *packidx, struct got_object_cache *objcache)
 {
 	const struct got_error *err = NULL;
 	struct got_object *obj = NULL;
@@ -155,16 +192,11 @@ blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
 	memset(&imsg_outfd, 0, sizeof(imsg_outfd));
 	imsg_outfd.fd = -1;
 
-	err = got_privsep_get_imsg_obj(&obj, imsg, ibuf);
+	err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
+	    GOT_OBJ_TYPE_BLOB);
 	if (err)
 		return err;
 
-	if (obj->type != GOT_OBJ_TYPE_BLOB)
-		return got_error(GOT_ERR_OBJ_TYPE);
-
-	if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
-		return got_error(GOT_ERR_OBJ_NOT_PACKED);
-
 	err = got_privsep_recv_imsg(&imsg_outfd, ibuf, 0);
 	if (err)
 		return err;
@@ -201,7 +233,8 @@ done:
 	else if (imsg_outfd.fd != -1)
 		close(imsg_outfd.fd);
 	imsg_free(&imsg_outfd);
-
+	if (obj)
+		got_object_close(obj);
 	if (err) {
 		if (err->code == GOT_ERR_PRIVSEP_PIPE)
 			err = NULL;
@@ -347,14 +380,22 @@ main(int argc, char *argv[])
 	const struct got_error *err = NULL;
 	struct imsgbuf ibuf;
 	struct imsg imsg;
-	struct got_packidx *packidx;
-	struct got_pack *pack;
+	struct got_packidx *packidx = NULL;
+	struct got_pack *pack = NULL;
+	struct got_object_cache objcache;
 
 	//static int attached;
 	//while (!attached) sleep(1);
 
 	imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
 
+	err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
+	if (err) {
+		err = got_error_from_errno();
+		got_privsep_send_error(&ibuf, err);
+		return 1;
+	}
+
 	/* revoke access to most system calls */
 	if (pledge("stdio recvfd", NULL) == -1) {
 		err = got_error_from_errno();
@@ -389,16 +430,20 @@ main(int argc, char *argv[])
 
 		switch (imsg.hdr.type) {
 		case GOT_IMSG_PACKED_OBJECT_REQUEST:
-			err = object_request(&imsg, &ibuf, pack, packidx);
+			err = object_request(&imsg, &ibuf, pack, packidx,
+			    &objcache);
 			break;
 		case GOT_IMSG_COMMIT_REQUEST:
-			err = commit_request(&imsg, &ibuf, pack, packidx);
+			err = commit_request(&imsg, &ibuf, pack, packidx,
+			    &objcache);
 			break;
 		case GOT_IMSG_TREE_REQUEST:
-			err = tree_request(&imsg, &ibuf, pack, packidx);
+			err = tree_request(&imsg, &ibuf, pack, packidx,
+			   &objcache);
 			break;
 		case GOT_IMSG_BLOB_REQUEST:
-			err = blob_request(&imsg, &ibuf, pack, packidx);
+			err = blob_request(&imsg, &ibuf, pack, packidx,
+			   &objcache);
 			break;
 		default:
 			err = got_error(GOT_ERR_PRIVSEP_MSG);
@@ -417,7 +462,10 @@ main(int argc, char *argv[])
 		}
 	}
 
-	got_pack_close(pack);
+	if (packidx)
+		got_packidx_close(packidx);
+	if (pack)
+		got_pack_close(pack);
 	imsg_clear(&ibuf);
 	if (err)
 		fprintf(stderr, "%s: %s\n", getprogname(), err->msg);