Commit 5a950d099a15fa3dc29590f3a8e90de9d6e1972b

Stefan Sperling 2022-06-15T14:27:20

using a local packfd variable in open_worktree() is sufficient

diff --git a/lib/got_lib_worktree.h b/lib/got_lib_worktree.h
index 414cf45..a358845 100644
--- a/lib/got_lib_worktree.h
+++ b/lib/got_lib_worktree.h
@@ -22,7 +22,6 @@ struct got_worktree {
 	struct got_object_id *base_commit_id;
 	char *head_ref_name;
 	uuid_t uuid;
-	int *pack_fds;
 
 	/*
 	 * File descriptor for the lock file, open while a work tree is open.
diff --git a/lib/worktree_open.c b/lib/worktree_open.c
index 8178c5f..529f4b6 100644
--- a/lib/worktree_open.c
+++ b/lib/worktree_open.c
@@ -116,6 +116,7 @@ open_worktree(struct got_worktree **worktree, const char *path)
 	int version, fd = -1;
 	const char *errstr;
 	struct got_repository *repo = NULL;
+	int *pack_fds = NULL;
 	uint32_t uuid_status;
 
 	*worktree = NULL;
@@ -190,12 +191,11 @@ open_worktree(struct got_worktree **worktree, const char *path)
 		goto done;
 	}
 
-	err = got_repo_pack_fds_open(&(*worktree)->pack_fds);
+	err = got_repo_pack_fds_open(&pack_fds);
 	if (err)
 		goto done;
 
-	err = got_repo_open(&repo, (*worktree)->repo_path, NULL,
-	    (*worktree)->pack_fds);
+	err = got_repo_open(&repo, (*worktree)->repo_path, NULL, pack_fds);
 	if (err)
 		goto done;
 
@@ -231,6 +231,12 @@ done:
 		if (err == NULL)
 			err = close_err;
 	}
+	if (pack_fds) {
+		const struct got_error *pack_err =
+		    got_repo_pack_fds_close(pack_fds);
+		if (err == NULL)
+			err = pack_err;
+	}
 	free(path_got);
 	free(path_lock);
 	free(base_commit_id_str);
@@ -301,12 +307,6 @@ got_worktree_close(struct got_worktree *worktree)
 	if (close(worktree->root_fd) == -1 && err == NULL)
 		err = got_error_from_errno2("close",
 		    got_worktree_get_root_path(worktree));
-	if (worktree->pack_fds) {
-		const struct got_error *pack_err =
-		    got_repo_pack_fds_close(worktree->pack_fds);
-		if (err == NULL)
-			err = pack_err;
-	}
 	free(worktree->repo_path);
 	free(worktree->path_prefix);
 	free(worktree->base_commit_id);