"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
mkstemp -> mkstemps
To:
gameoftrees@openbsd.org
Date:
Tue, 1 Nov 2022 08:27:30 +0100

Download raw body.

Thread
Use mkstemps(3) instead of mkstemp(3) for opening named temporary files.
Allows 'got commit' to use a ".diff" suffix for temporary diff files.

ok?
 
diff 2a47b1e5852390eadc730c1dd2dd7caae011adfa 5a6ea38b34bfdf4408e04553b7287893d51b0ed0
commit - 2a47b1e5852390eadc730c1dd2dd7caae011adfa
commit + 5a6ea38b34bfdf4408e04553b7287893d51b0ed0
blob - cbfb69469133d43ff6d3185d620a4cfc74627a59
blob + c0c38ea47dfbd657ee0c14d0e8829767ad691ae6
--- got/got.c
+++ got/got.c
@@ -518,7 +518,7 @@ collect_import_msg(char **logmsg, char **logmsg_path, 
 		return got_error_from_errno("asprintf");
 
 	err = got_opentemp_named_fd(logmsg_path, &fd,
-	    GOT_TMPDIR_STR "/got-importmsg");
+	    GOT_TMPDIR_STR "/got-importmsg", "");
 	if (err)
 		goto done;
 
@@ -7166,7 +7166,7 @@ get_tag_message(char **tagmsg, char **tagmsg_path, con
 		goto done;
 	}
 
-	err = got_opentemp_named_fd(tagmsg_path, &fd, template);
+	err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
 	if (err)
 		goto done;
 
@@ -8442,7 +8442,7 @@ collect_commit_logmsg(struct got_pathlist_head *commit
 	if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
 		return got_error_from_errno("asprintf");
 
-	err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
+	err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
 	if (err)
 		goto done;
 
@@ -10732,7 +10732,7 @@ histedit_edit_logmsg(struct got_histedit_list_entry *h
 		goto done;
 
 	err = got_opentemp_named_fd(&logmsg_path, &fd,
-	    GOT_TMPDIR_STR "/got-logmsg");
+	    GOT_TMPDIR_STR "/got-logmsg", "");
 	if (err)
 		goto done;
 
@@ -10978,7 +10978,7 @@ histedit_edit_script(struct got_histedit_list *histedi
 	FILE *f = NULL;
 	char *path = NULL;
 
-	err = got_opentemp_named(&path, &f, "got-histedit");
+	err = got_opentemp_named(&path, &f, "got-histedit", "");
 	if (err)
 		return err;
 
blob - 59fe03f76be46a82ab9cd7fb0b540b1181364a76
blob + faaafb6f3ed653a3a12f8ac342ab54ce2f43d2c0
--- gotd/gotd.c
+++ gotd/gotd.c
@@ -906,7 +906,7 @@ recv_packfile(struct gotd_client *client)
 		goto done;
 	}
 
-	err = got_opentemp_named_fd(&pack_path, &packfd, basepath);
+	err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
 	if (err)
 		goto done;
 
@@ -918,7 +918,7 @@ recv_packfile(struct gotd_client *client)
 		basepath = NULL;
 		goto done;
 	}
-	err = got_opentemp_named_fd(&idx_path, &idxfd, basepath);
+	err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
 	if (err)
 		goto done;
 
blob - 5df60c352cb02c68869dfd77742c78669f368df6
blob + 8c49224c3f3618922f155e0671044215eba5d031
--- include/got_opentemp.h
+++ include/got_opentemp.h
@@ -33,10 +33,12 @@ const struct got_error *got_opentemp_named(char **, FI
 
 /* Open a new temporary file for writing.
  * The file is visible in the filesystem. */
-const struct got_error *got_opentemp_named(char **, FILE **, const char *);
+const struct got_error *got_opentemp_named(char **, FILE **, const char *,
+    const char *);
 
 /* Like got_opentemp_named() but returns a file descriptor instead of a FILE. */
-const struct got_error *got_opentemp_named_fd(char **, int *, const char *);
+const struct got_error *got_opentemp_named_fd(char **, int *, const char *,
+    const char *);
 
 /* Truncate a file. This is useful for re-using open temporary files. */
 const struct got_error *got_opentemp_truncate(FILE *);
blob - ca41b5e1c959efef486c9c9c470cd918d4390b7f
blob + 4570818661d209d2a903214f7ca0611c3da485eb
--- lib/diff3.c
+++ lib/diff3.c
@@ -222,7 +222,7 @@ diffreg(BUF **d, const char *path1, const char *path2,
 	}
 
 	err = got_opentemp_named(&outpath, &outfile,
-	    GOT_TMPDIR_STR "/got-diffreg");
+	    GOT_TMPDIR_STR "/got-diffreg", "");
 	if (err)
 		goto done;
 
blob - 897d9da4c5d2cbbf863779e67af4ba3faeb6ae40
blob + 0dd641076040553ff06ef8320ae4686dabdd6786
--- lib/fetch.c
+++ lib/fetch.c
@@ -191,7 +191,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struc
 			err = got_error_from_errno("asprintf");
 			goto done;
 		}
-		err = got_opentemp_named_fd(&tmppackpath, &packfd, path);
+		err = got_opentemp_named_fd(&tmppackpath, &packfd, path, "");
 		free(path);
 		if (err)
 			goto done;
@@ -212,7 +212,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struc
 			err = got_error_from_errno("asprintf");
 			goto done;
 		}
-		err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
+		err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
 		free(path);
 		if (err)
 			goto done;
blob - 670e15b4f8e9326fc92edea6272e06bd3e072a60
blob + 8fa42a873d1dd3feb591275a17e1bd546c3dab59
--- lib/object_create.c
+++ lib/object_create.c
@@ -67,7 +67,7 @@ create_object_file(struct got_object_id *id, FILE *con
 	if (err)
 		return err;
 
-	err = got_opentemp_named(&tmppath, &tmpfile, objpath);
+	err = got_opentemp_named(&tmppath, &tmpfile, objpath, "");
 	if (err) {
 		char *parent_path;
 		if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
@@ -79,7 +79,7 @@ create_object_file(struct got_object_id *id, FILE *con
 		free(parent_path);
 		if (err)
 			goto done;
-		err = got_opentemp_named(&tmppath, &tmpfile, objpath);
+		err = got_opentemp_named(&tmppath, &tmpfile, objpath, "");
 		if (err)
 			goto done;
 	}
blob - 3c1f5eebe793a2f895a8529b13cdad48e3f9ccb0
blob + 0e4865ca9ef8a0f80cc03e76722c8280c20510bf
--- lib/opentemp.c
+++ lib/opentemp.c
@@ -63,21 +63,22 @@ got_opentemp_named(char **path, FILE **outfile, const 
 }
 
 const struct got_error *
-got_opentemp_named(char **path, FILE **outfile, const char *basepath)
+got_opentemp_named(char **path, FILE **outfile, const char *basepath,
+    const char *suffix)
 {
 	const struct got_error *err = NULL;
 	int fd;
 
 	*outfile = NULL;
 
-	if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
+	if (asprintf(path, "%s-XXXXXX%s", basepath, suffix) == -1) {
 		*path = NULL;
 		return got_error_from_errno("asprintf");
 	}
 
-	fd = mkstemp(*path);
+	fd = mkstemps(*path, strlen(suffix));
 	if (fd == -1) {
-		err = got_error_from_errno2("mkstemp", *path);
+		err = got_error_from_errno2("mkstemps", *path);
 		free(*path);
 		*path = NULL;
 		return err;
@@ -94,19 +95,20 @@ got_opentemp_named_fd(char **path, int *outfd, const c
 }
 
 const struct got_error *
-got_opentemp_named_fd(char **path, int *outfd, const char *basepath)
+got_opentemp_named_fd(char **path, int *outfd, const char *basepath,
+    const char *suffix)
 {
 	const struct got_error *err = NULL;
 	int fd;
 
 	*outfd = -1;
 
-	if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
+	if (asprintf(path, "%s-XXXXXX%s", basepath, suffix) == -1) {
 		*path = NULL;
 		return got_error_from_errno("asprintf");
 	}
 
-	fd = mkstemp(*path);
+	fd = mkstemps(*path, strlen(suffix));
 	if (fd == -1) {
 		err = got_error_from_errno("mkstemp");
 		free(*path);
blob - 8647dea45a83ad12f49ae63960dd903bd55fde98
blob + 6c31c4b5e3788850c30c78ed552e90f1eddd3982
--- lib/patch.c
+++ lib/patch.c
@@ -716,7 +716,8 @@ open_blob(char **path, FILE **fp, const char *blobid,
 	if (err)
 		goto done;
 
-	err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob");
+	err = got_opentemp_named(path, fp, GOT_TMPDIR_STR "/got-patch-blob",
+	    "");
 	if (err)
 		goto done;
 
@@ -848,7 +849,7 @@ apply_patch(int *overlapcnt, struct got_worktree *work
 	} else if (p->xbit)
 		mode |= (S_IXUSR | S_IXGRP | S_IXOTH);
 
-	err = got_opentemp_named(&tmppath, &tmpfile, template);
+	err = got_opentemp_named(&tmppath, &tmpfile, template, "");
 	if (err)
 		goto done;
 	outfd = fileno(tmpfile);
@@ -905,7 +906,7 @@ apply_patch(int *overlapcnt, struct got_worktree *work
 			tmpfile = t;
 		}
 
-		err = got_opentemp_named(&mergepath, &mergefile, template);
+		err = got_opentemp_named(&mergepath, &mergefile, template, "");
 		if (err)
 			goto done;
 		outfd = fileno(mergefile);
blob - b5f93d7b7eba07c7f32fdad87614319943f14305
blob + 63312071b7d892680d8127151e9958aed8e85ac8
--- lib/reference.c
+++ lib/reference.c
@@ -1188,7 +1188,7 @@ got_ref_write(struct got_reference *ref, struct got_re
 		goto done;
 	}
 
-	err = got_opentemp_named(&tmppath, &f, path);
+	err = got_opentemp_named(&tmppath, &f, path, "");
 	if (err) {
 		char *parent;
 		if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
@@ -1200,7 +1200,7 @@ got_ref_write(struct got_reference *ref, struct got_re
 		free(parent);
 		if (err)
 			goto done;
-		err = got_opentemp_named(&tmppath, &f, path);
+		err = got_opentemp_named(&tmppath, &f, path, "");
 		if (err)
 			goto done;
 	}
@@ -1296,7 +1296,7 @@ delete_packed_ref(struct got_reference *delref, struct
 	if (packed_refs_path == NULL)
 		return got_error_from_errno("got_repo_get_path_packed_refs");
 
-	err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
+	err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
 	if (err)
 		goto done;
 
blob - 8486282d50b2fc83c3f3b444373cd17540fd31dd
blob + fd1fae8ec1b8171b5cb576110a381201f1cb4ad9
--- lib/repository_admin.c
+++ lib/repository_admin.c
@@ -167,7 +167,7 @@ got_repo_pack_objects(FILE **packfile, struct got_obje
 		err = got_error_from_errno("asprintf");
 		goto done;
 	}
-	err = got_opentemp_named_fd(&tmpfile_path, &packfd, path);
+	err = got_opentemp_named_fd(&tmpfile_path, &packfd, path, "");
 	if (err)
 		goto done;
 
@@ -297,7 +297,7 @@ got_repo_index_pack(FILE *packfile, struct got_object_
 		err = got_error_from_errno("asprintf");
 		goto done;
 	}
-	err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path);
+	err = got_opentemp_named_fd(&tmpidxpath, &idxfd, path, "");
 	free(path);
 	if (err)
 		goto done;
blob - 3800e67fcce500f30b32b4c3590bdf0493827da1
blob + 8bcb3732e23d2041a0bd0790d942ce3377f22b68
--- lib/sigs.c
+++ lib/sigs.c
@@ -274,7 +274,7 @@ got_sigs_verify_tag_ssh(char **msg, struct got_tag_obj
 	*msg = NULL;
 
 	error = got_opentemp_named(&tmppath, &tmpsig,
-	    GOT_TMPDIR_STR "/got-tagsig");
+	    GOT_TMPDIR_STR "/got-tagsig", "");
 	if (error)
 		goto done;
 
blob - acbca91c5f5f7a0048daf2e2b2baffaf08c347d5
blob + 929c300ef8767589d0f0aa5a4db85d85655e794f
--- lib/worktree.c
+++ lib/worktree.c
@@ -94,7 +94,7 @@ update_meta_file(const char *path_got, const char *nam
 		goto done;
 	}
 
-	err = got_opentemp_named(&tmppath, &tmpfile, path);
+	err = got_opentemp_named(&tmppath, &tmpfile, path, "");
 	if (err)
 		goto done;
 
@@ -556,15 +556,15 @@ merge_binary_file(int *overlapcnt, int merged_fd,
 			goto done;
 		}
 		err = got_opentemp_named_fd(&path_orig, &fd_orig,
-		    base_path_orig);
+		    base_path_orig, "");
 		if (err)
 			goto done;
 		err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
-		    base_path_deriv);
+		    base_path_deriv, "");
 		if (err)
 			goto done;
 		err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
-		    base_path_deriv2);
+		    base_path_deriv2, "");
 		if (err)
 			goto done;
 		err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
@@ -664,7 +664,7 @@ merge_file(int *local_changes_subsumed, struct got_wor
 		goto done;
 	}
 
-	err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
+	err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
 	if (err)
 		goto done;
 
@@ -774,7 +774,7 @@ install_symlink_conflict(const char *deriv_target,
 		goto done;
 	}
 
-	err = got_opentemp_named(&path, &f, "got-symlink-conflict");
+	err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
 	if (err)
 		goto done;
 
@@ -992,7 +992,8 @@ merge_blob(int *local_changes_subsumed, struct got_wor
 			goto done;
 		}
 
-		err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
+		err = got_opentemp_named(&blob_orig_path, &f_orig,
+		    base_path, "");
 		if (err)
 			goto done;
 		err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
@@ -1019,7 +1020,7 @@ merge_blob(int *local_changes_subsumed, struct got_wor
 		goto done;
 	}
 
-	err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
+	err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
 	if (err)
 		goto done;
 	err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
@@ -1435,7 +1436,7 @@ install_blob(struct got_worktree *worktree, const char
 				goto done;
 			} else {
 				err = got_opentemp_named_fd(&tmppath, &fd,
-				    ondisk_path);
+				    ondisk_path, "");
 				if (err)
 					goto done;
 				update = 1;
@@ -2469,7 +2470,7 @@ sync_fileindex(struct got_fileindex *fileindex, const 
 	struct timespec timeout;
 
 	err = got_opentemp_named(&new_fileindex_path, &new_index,
-	    fileindex_path);
+	    fileindex_path, "");
 	if (err)
 		goto done;
 
@@ -4552,7 +4553,7 @@ create_patched_content(char **path_outfile, int revers
 	if (err)
 		goto done;
 
-	err = got_opentemp_named(&path1, &f1, "got-patched-blob");
+	err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
 	if (err)
 		goto done;
 
@@ -4565,7 +4566,8 @@ create_patched_content(char **path_outfile, int revers
 	if (err)
 		goto done;
 
-	err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
+	err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
+	    "");
 	if (err)
 		goto done;
 
@@ -6154,7 +6156,7 @@ got_worktree_commit(struct got_object_id **new_commit_
 	cc_arg.diff_header_shown = 0;
 	if (show_diff) {
 		err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
-		    GOT_TMPDIR_STR "/got-diff");
+		    GOT_TMPDIR_STR "/got", ".diff");
 		if (err)
 			goto done;
 		cc_arg.f1 = got_opentemp();
@@ -8611,7 +8613,7 @@ create_unstaged_content(char **path_unstaged_content,
 	if (err)
 		goto done;
 
-	err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
+	err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
 	if (err)
 		goto done;
 
@@ -8624,7 +8626,7 @@ create_unstaged_content(char **path_unstaged_content,
 	if (err)
 		goto done;
 
-	err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
+	err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
 	if (err)
 		goto done;
 
@@ -8638,11 +8640,11 @@ create_unstaged_content(char **path_unstaged_content,
 		goto done;
 
 	err = got_opentemp_named(path_unstaged_content, &outfile,
-	    "got-unstaged-content");
+	    "got-unstaged-content", "");
 	if (err)
 		goto done;
 	err = got_opentemp_named(path_new_staged_content, &rejectfile,
-	    "got-new-staged-content");
+	    "got-new-staged-content", "");
 	if (err)
 		goto done;
 
@@ -8801,7 +8803,8 @@ unstage_hunks(struct got_object_id *staged_blob_id,
 			goto done;
 		}
 
-		err = got_opentemp_named(&blob_base_path, &f_base, base_path);
+		err = got_opentemp_named(&blob_base_path, &f_base,
+		    base_path, "");
 		if (err)
 			goto done;
 		err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,