vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 3ba0be3dd450744b6b9513603c9bd3d774fa67b4 parent a50a6916a05f21cc326adadecffa7a025e9ca74d Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 25 Apr 2018 20:11:33 +0200 text: do not unlink existing `file~` when saving to `file` Fail atomic save if temporary file already exists. A follow up commit will use `mkstemp(3)` for temporary file creation. Diffstat:
| M | text.c | | | 4 | +++- |
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/text.c b/text.c @@ -849,7 +849,7 @@ static bool text_save_begin_atomic(TextSave *ctx) { goto err; snprintf(ctx->tmpname, namelen, "%s~", ctx->filename); - if ((ctx->fd = open(ctx->tmpname, O_CREAT|O_WRONLY|O_TRUNC, oldfd == -1 ? 0666 : oldmeta.st_mode)) == -1) + if ((ctx->fd = open(ctx->tmpname, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, oldfd == -1 ? 0666 : oldmeta.st_mode)) == -1) goto err; if (oldfd != -1) { if (!preserve_acl(oldfd, ctx->fd) || !preserve_selinux_context(oldfd, ctx->fd)) @@ -873,6 +873,8 @@ err: if (ctx->fd != -1) close(ctx->fd); ctx->fd = -1; + free(ctx->tmpname); + ctx->tmpname = NULL; errno = saved_errno; return false; }