Commit 268f7291e63638be363b7bccd95f97a73298d039

Stefan Sperling 2018-12-24T11:18:58

eliminate got_object_open() round-trip when opening tags

diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index 6e02969..115b8ff 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -26,7 +26,7 @@ const struct got_error *got_object_read_commit_privsep(
 const struct got_error *got_object_read_tree_privsep(struct got_tree_object **,
     int, struct got_repository *);
 const struct got_error *got_object_read_tag_privsep(struct got_tag_object **,
-    struct got_object *, int, struct got_repository *);
+    int, struct got_repository *);
 
 const struct got_error *got_object_parse_commit(struct got_commit_object **,
     char *, size_t);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 2aeba35..74f300e 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -222,6 +222,8 @@ const struct got_error *got_privsep_send_commit_req(struct imsgbuf *, int,
     struct got_object_id *, int);
 const struct got_error *got_privsep_send_tree_req(struct imsgbuf *, int,
     struct got_object_id *, int);
+const struct got_error *got_privsep_send_tag_req(struct imsgbuf *, int,
+    struct got_object_id *, int);
 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int);
 const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
diff --git a/lib/object.c b/lib/object.c
index 7c1b78c..11a2f74 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -906,27 +906,48 @@ got_object_blob_dump_to_file(size_t *total_len, int *nlines,
 }
 
 static const struct got_error *
-read_packed_tag_privsep(struct got_tag_object **tag,
-    struct got_object *obj, struct got_pack *pack)
+request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
+    int pack_idx, struct got_object_id *id)
 {
 	const struct got_error *err = NULL;
 
-	err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
+	err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
+	    pack_idx);
 	if (err)
 		return err;
 
 	return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
 }
 
+static const struct got_error *
+read_packed_tag_privsep(struct got_tag_object **tag,
+    struct got_pack *pack, struct got_packidx *packidx, int idx,
+    struct got_object_id *id)
+{
+	const struct got_error *err = NULL;
+
+	if (pack->privsep_child)
+		return request_packed_tag(tag, pack, idx, id);
+
+	err = start_pack_privsep_child(pack, packidx);
+	if (err)
+		return err;
+
+	return request_packed_tag(tag, pack, idx, id);
+}
+
 
 static const struct got_error *
-open_tag(struct got_tag_object **tag,
-    struct got_repository *repo, struct got_object *obj, int check_cache)
+open_tag(struct got_tag_object **tag, struct got_repository *repo,
+    struct got_object_id *id, int check_cache)
 {
 	const struct got_error *err = NULL;
+	struct got_packidx *packidx = NULL;
+	int idx;
+	char *path_packfile;
 
 	if (check_cache) {
-		*tag = got_repo_get_cached_tag(repo, &obj->id);
+		*tag = got_repo_get_cached_tag(repo, id);
 		if (*tag != NULL) {
 			(*tag)->refcnt++;
 			return NULL;
@@ -934,31 +955,36 @@ open_tag(struct got_tag_object **tag,
 	} else
 		*tag = NULL;
 
-	if (obj->type != GOT_OBJ_TYPE_TAG)
-		return got_error(GOT_ERR_OBJ_TYPE);
+	err = got_repo_search_packidx(&packidx, &idx, repo, id);
+	if (err == NULL) {
+		struct got_pack *pack = NULL;
 
-	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
-		struct got_pack *pack;
-		pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+		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,
-			    obj->path_packfile, NULL);
+			err = got_repo_cache_pack(&pack, repo, path_packfile,
+			    packidx);
 			if (err)
 				return err;
 		}
-		err = read_packed_tag_privsep(tag, obj, pack);
-	} else {
+		err = read_packed_tag_privsep(tag, pack,
+		    packidx, idx, id);
+	} else if (err->code == GOT_ERR_NO_OBJ) {
 		int fd;
-		err = open_loose_object(&fd, got_object_get_id(obj), repo);
+
+		err = open_loose_object(&fd, id, repo);
 		if (err)
 			return err;
-		err = got_object_read_tag_privsep(tag, obj, fd, repo);
+		err = got_object_read_tag_privsep(tag, fd, repo);
 		close(fd);
 	}
 
 	if (err == NULL) {
 		(*tag)->refcnt++;
-		err = got_repo_cache_tag(repo, &obj->id, *tag);
+		err = got_repo_cache_tag(repo, id, *tag);
 	}
 
 	return err;
@@ -968,34 +994,20 @@ const struct got_error *
 got_object_open_as_tag(struct got_tag_object **tag,
     struct got_repository *repo, struct got_object_id *id)
 {
-	const struct got_error *err;
-	struct got_object *obj;
-
 	*tag = got_repo_get_cached_tag(repo, id);
 	if (*tag != NULL) {
 		(*tag)->refcnt++;
 		return NULL;
 	}
 
-	err = got_object_open(&obj, repo, id);
-	if (err)
-		return err;
-	if (obj->type != GOT_OBJ_TYPE_COMMIT) {
-		err = got_error(GOT_ERR_OBJ_TYPE);
-		goto done;
-	}
-
-	err = open_tag(tag, repo, obj, 0);
-done:
-	got_object_close(obj);
-	return err;
+	return open_tag(tag, repo, id, 0);
 }
 
 const struct got_error *
 got_object_tag_open(struct got_tag_object **tag,
     struct got_repository *repo, struct got_object *obj)
 {
-	return open_tag(tag, repo, obj, 1);
+	return open_tag(tag, repo, got_object_get_id(obj), 1);
 }
 
 static struct got_tree_entry *
@@ -1426,14 +1438,14 @@ got_object_read_blob_privsep(size_t *size, int outfd, int infd,
 
 static const struct got_error *
 request_tag(struct got_tag_object **tag, struct got_repository *repo,
-    struct got_object *obj, int fd)
+    int fd)
 {
 	const struct got_error *err = NULL;
 	struct imsgbuf *ibuf;
 
 	ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
 
-	err = got_privsep_send_obj_req(ibuf, fd, obj);
+	err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
 	if (err)
 		return err;
 
@@ -1442,14 +1454,14 @@ request_tag(struct got_tag_object **tag, struct got_repository *repo,
 
 const struct got_error *
 got_object_read_tag_privsep(struct got_tag_object **tag,
-    struct got_object *obj, int obj_fd, struct got_repository *repo)
+    int obj_fd, struct got_repository *repo)
 {
 	int imsg_fds[2];
 	pid_t pid;
 	struct imsgbuf *ibuf;
 
 	if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
-		return request_tag(tag, repo, obj, obj_fd);
+		return request_tag(tag, repo, obj_fd);
 
 	ibuf = calloc(1, sizeof(*ibuf));
 	if (ibuf == NULL)
@@ -1474,5 +1486,5 @@ got_object_read_tag_privsep(struct got_tag_object **tag,
 	imsg_init(ibuf, imsg_fds[0]);
 	repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
 
-	return request_tag(tag, repo, obj, obj_fd);
+	return request_tag(tag, repo, obj_fd);
 }
diff --git a/lib/privsep.c b/lib/privsep.c
index 17fe8da..3c8114c 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -314,6 +314,30 @@ got_privsep_send_tree_req(struct imsgbuf *ibuf, int fd,
 }
 
 const struct got_error *
+got_privsep_send_tag_req(struct imsgbuf *ibuf, int fd,
+    struct got_object_id *id, int pack_idx)
+{
+	struct got_imsg_packed_object iobj, *iobjp;
+	size_t len;
+
+	if (id) { /* tag is packed */
+		iobj.idx = pack_idx;
+		memcpy(iobj.id, id->sha1, sizeof(iobj.id));
+		iobjp = &iobj;
+		len = sizeof(iobj);
+	} else {
+		iobjp = NULL;
+		len = 0;
+	}
+
+	if (imsg_compose(ibuf, GOT_IMSG_TAG_REQUEST, 0, 0, fd, iobjp, len)
+	    == -1)
+		return got_error_from_errno();
+
+	return flush_imsg(ibuf);
+}
+
+const struct got_error *
 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
 {
 	if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index c6dc175..a195548 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -294,13 +294,21 @@ tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
     struct got_packidx *packidx, struct got_object_cache *objcache)
 {
 	const struct got_error *err = NULL;
+	struct got_imsg_packed_object iobj;
 	struct got_object *obj = NULL;
 	struct got_tag_object *tag = NULL;
 	uint8_t *buf;
 	size_t len;
+	struct got_object_id id;
+	size_t datalen;
 
-	err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
-	    GOT_OBJ_TYPE_TAG);
+	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
+	if (datalen != sizeof(iobj))
+		return got_error(GOT_ERR_PRIVSEP_LEN);
+	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;
 
diff --git a/libexec/got-read-tag/got-read-tag.c b/libexec/got-read-tag/got-read-tag.c
index b32627b..b4d7d9a 100644
--- a/libexec/got-read-tag/got-read-tag.c
+++ b/libexec/got-read-tag/got-read-tag.c
@@ -48,17 +48,18 @@ catch_sigint(int signo)
 }
 
 static const struct got_error *
-read_tag_object(struct got_tag_object **tag, struct got_object *obj,
-    FILE *f)
+read_tag_object(struct got_tag_object **tag, FILE *f)
 {
 	const struct got_error *err = NULL;
+	struct got_object *obj;
 	size_t len;
 	uint8_t *p;
 
-	if (obj->flags & GOT_OBJ_FLAG_PACKED)
-		err = got_read_file_to_mem(&p, &len, f);
-	else
-		err = got_inflate_to_mem(&p, &len, f);
+	err = got_inflate_to_mem(&p, &len, f);
+	if (err)
+		return err;
+
+	err = got_object_parse_header(&obj, p, len);
 	if (err)
 		return err;
 
@@ -70,8 +71,9 @@ read_tag_object(struct got_tag_object **tag, struct got_object *obj,
 	/* Skip object header. */
 	len -= obj->hdrlen;
 	err = got_object_parse_tag(tag, p + obj->hdrlen, len);
-	free(p);
 done:
+	free(p);
+	got_object_close(obj);
 	return err;
 }
 
@@ -79,9 +81,7 @@ int
 main(int argc, char *argv[])
 {
 	const struct got_error *err = NULL;
-	struct got_tag_object *tag = NULL;
 	struct imsgbuf ibuf;
-	size_t datalen;
 
 	signal(SIGINT, catch_sigint);
 
@@ -98,9 +98,8 @@ main(int argc, char *argv[])
 
 	while (1) {
 		struct imsg imsg;
-		struct got_imsg_object iobj;
 		FILE *f = NULL;
-		struct got_object *obj = NULL;
+		struct got_tag_object *tag = NULL;
 
 		if (sigint_received) {
 			err = got_error(GOT_ERR_CANCELLED);
@@ -122,32 +121,11 @@ main(int argc, char *argv[])
 			goto done;
 		}
 
-		datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
-		if (datalen != sizeof(iobj)) {
-			err = got_error(GOT_ERR_PRIVSEP_LEN);
-			goto done;
-		}
-
-		memcpy(&iobj, imsg.data, sizeof(iobj));
-		if (iobj.type != GOT_OBJ_TYPE_TAG) {
-			err = got_error(GOT_ERR_OBJ_TYPE);
-			goto done;
-		}
-
 		if (imsg.fd == -1) {
 			err = got_error(GOT_ERR_PRIVSEP_NO_FD);
 			goto done;
 		}
 
-		obj = calloc(1, sizeof(*obj));
-		if (obj == NULL) {
-			err = got_error_from_errno();
-			goto done;
-		}
-		obj->type = iobj.type;
-		obj->hdrlen = iobj.hdrlen;
-		obj->size = iobj.size;
-
 		/* Always assume file offset zero. */
 		f = fdopen(imsg.fd, "rb");
 		if (f == NULL) {
@@ -155,7 +133,7 @@ main(int argc, char *argv[])
 			goto done;
 		}
 
-		err = read_tag_object(&tag, obj, f);
+		err = read_tag_object(&tag, f);
 		if (err)
 			goto done;
 
@@ -166,8 +144,6 @@ done:
 		else if (imsg.fd != -1)
 			close(imsg.fd);
 		imsg_free(&imsg);
-		if (obj)
-			got_object_close(obj);
 		if (err)
 			break;
 	}