Commit 883f04699494bd53a6d4ddf6e53bd3156429b622

Stefan Sperling 2018-06-23T17:57:39

make struct got_tree_object opaque

diff --git a/include/got_object.h b/include/got_object.h
index df0f4ad..52bd46f 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -17,6 +17,7 @@
 struct got_object_id;
 
 struct got_blob_object;
+struct got_tree_object;
 
 struct got_tree_entry {
 	SIMPLEQ_ENTRY(got_tree_entry) entry;
@@ -25,11 +26,11 @@ struct got_tree_entry {
 	struct got_object_id *id;
 };
 
-struct got_tree_object {
-	int nentries;
-	SIMPLEQ_HEAD(, got_tree_entry) entries;
+SIMPLEQ_HEAD(got_tree_entries_queue, got_tree_entry);
 
-	int refcnt; /* for internal use only */
+struct got_tree_entries {
+	int nentries;
+	struct got_tree_entries_queue head;
 };
 
 struct got_object_qid {
@@ -141,6 +142,10 @@ const struct got_error *got_object_tree_open(struct got_tree_object **,
 /* Dispose of a tree object. */
 void got_object_tree_close(struct got_tree_object *);
 
+/* Get the entries of a tree object. */
+const struct got_tree_entries *got_object_tree_get_entries(
+    struct got_tree_object *);
+
 /*
  * Attempt to open a blob object in a repository.
  * The provided object must be of type GOT_OBJ_TYPE_BLOB.
diff --git a/lib/diff.c b/lib/diff.c
index e4f7dbb..abdee82 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -152,8 +152,10 @@ struct got_tree_entry *
 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
 {
 	struct got_tree_entry *te2;
+	const struct got_tree_entries *entries2;
 
-	SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
+	entries2 = got_object_tree_get_entries(tree2); 
+	SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
 		if (strcmp(te1->name, te2->name) == 0)
 			return te2;
 	}
@@ -423,10 +425,16 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
 	struct got_tree_entry *te1 = NULL;
 	struct got_tree_entry *te2 = NULL;
 
-	if (tree1)
-		te1 = SIMPLEQ_FIRST(&tree1->entries);
-	if (tree2)
-		te2 = SIMPLEQ_FIRST(&tree2->entries);
+	if (tree1) {
+		const struct got_tree_entries *entries;
+		entries = got_object_tree_get_entries(tree1);
+		te1 = SIMPLEQ_FIRST(&entries->head);
+	}
+	if (tree2) {
+		const struct got_tree_entries *entries;
+		entries = got_object_tree_get_entries(tree2);
+		te2 = SIMPLEQ_FIRST(&entries->head);
+	}
 
 	do {
 		if (te1) {
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index ed4dac1..9f5b22c 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -34,6 +34,11 @@ struct got_object {
 	int refcnt;		/* > 0 if open and/or cached */
 };
 
+struct got_tree_object {
+	struct got_tree_entries entries;
+	int refcnt;
+};
+
 struct got_blob_object {
 	FILE *f;
 	struct got_zstream_buf zb;
diff --git a/lib/object.c b/lib/object.c
index 3a8d465..a55e09d 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -782,7 +782,7 @@ parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
 	if (*tree == NULL)
 		return got_error_from_errno();
 
-	SIMPLEQ_INIT(&(*tree)->entries);
+	SIMPLEQ_INIT(&(*tree)->entries.head);
 
 	while (remain > 0) {
 		struct got_tree_entry *te;
@@ -791,8 +791,8 @@ parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
 		err = parse_tree_entry(&te, &elen, buf, remain);
 		if (err)
 			return err;
-		(*tree)->nentries++;
-		SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
+		(*tree)->entries.nentries++;
+		SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
 		buf += elen;
 		remain -= elen;
 	}
@@ -1200,15 +1200,21 @@ got_object_tree_close(struct got_tree_object *tree)
 			return;
 	}
 
-	while (!SIMPLEQ_EMPTY(&tree->entries)) {
-		te = SIMPLEQ_FIRST(&tree->entries);
-		SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
+	while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
+		te = SIMPLEQ_FIRST(&tree->entries.head);
+		SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
 		tree_entry_close(te);
 	}
 
 	free(tree);
 }
 
+const struct got_tree_entries *
+got_object_tree_get_entries(struct got_tree_object *tree)
+{
+	return &tree->entries;
+}
+
 static const struct got_error *
 read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
 {
@@ -1464,7 +1470,7 @@ find_entry_by_name(struct got_tree_object *tree, const char *name)
 {
 	struct got_tree_entry *te;
 
-	SIMPLEQ_FOREACH(te, &tree->entries, entry) {
+	SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
 		if (strcmp(te->name, name) == 0)
 			return te;
 	}
diff --git a/lib/privsep.c b/lib/privsep.c
index 2fc13c0..a0f4d62 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -462,7 +462,7 @@ got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
 	struct got_imsg_tree_object itree;
 	struct got_tree_entry *te;
 
-	itree.nentries = tree->nentries;
+	itree.nentries = tree->entries.nentries;
 	if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
 	    == -1)
 		return got_error_from_errno();
@@ -471,7 +471,7 @@ got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
 	if (err)
 		return err;
 
-	SIMPLEQ_FOREACH(te, &tree->entries, entry) {
+	SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
 		struct got_imsg_tree_entry ite;
 		uint8_t *buf = NULL;
 		size_t len = sizeof(ite) + strlen(te->name);
@@ -528,7 +528,7 @@ get_more:
 
 		n = imsg_get(ibuf, &imsg);
 		if (n == 0) {
-			if (*tree && (*tree)->nentries != nentries)
+			if (*tree && (*tree)->entries.nentries != nentries)
 				goto get_more;
 			break;
 		}
@@ -558,8 +558,8 @@ get_more:
 				err = got_error_from_errno();
 				break;
 			}
-			(*tree)->nentries = itree.nentries;
-			SIMPLEQ_INIT(&(*tree)->entries);
+			(*tree)->entries.nentries = itree.nentries;
+			SIMPLEQ_INIT(&(*tree)->entries.head);
 			break;
 		case GOT_IMSG_TREE_ENTRY:
 			/* This message should be preceeded by GOT_IMSG_TREE. */
@@ -596,7 +596,7 @@ get_more:
 
 			memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
 			te->mode = ite.mode;
-			SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
+			SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
 			nentries++;
 			break;
 		default:
@@ -607,7 +607,7 @@ get_more:
 		imsg_free(&imsg);
 	}
 done:
-	if (*tree && (*tree)->nentries != nentries) {
+	if (*tree && (*tree)->entries.nentries != nentries) {
 		if (err == NULL)
 			err = got_error(GOT_ERR_PRIVSEP_LEN);
 		got_object_tree_close(*tree);
diff --git a/lib/worktree.c b/lib/worktree.c
index 176d82f..dc82efb 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -550,6 +550,7 @@ tree_checkout(struct got_worktree *worktree,
     got_worktree_checkout_cb progress_cb, void *progress_arg)
 {
 	const struct got_error *err = NULL;
+	const struct got_tree_entries *entries;
 	struct got_tree_entry *te;
 	size_t len;
 
@@ -558,7 +559,8 @@ tree_checkout(struct got_worktree *worktree,
 	if (strncmp(path, worktree->path_prefix, len) != 0)
 		return NULL;
 
-	SIMPLEQ_FOREACH(te, &tree->entries, entry) {
+	entries = got_object_tree_get_entries(tree);
+	SIMPLEQ_FOREACH(te, &entries->head, entry) {
 		err = tree_checkout_entry(worktree, fileindex, te, path, repo,
 		    progress_cb, progress_arg);
 		if (err)
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 021825f..8b5fcb7 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -88,6 +88,7 @@ print_tree_object(struct got_object *obj, char *parent,
     struct got_repository *repo)
 {
 	struct got_tree_object *tree;
+	const struct got_tree_entries *entries;
 	struct got_tree_entry *te;
 	const struct got_error *err;
 
@@ -95,7 +96,8 @@ print_tree_object(struct got_object *obj, char *parent,
 	if (err != NULL)
 		return err;
 
-	SIMPLEQ_FOREACH(te, &tree->entries, entry) {
+	entries = got_object_tree_get_entries(tree);
+	SIMPLEQ_FOREACH(te, &entries->head, entry) {
 		struct got_object *treeobj;
 		char *next_parent;
 		char *hex;