aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2018-04-25 20:11:33 +0200
committerMarc André Tanner <mat@brain-dump.org>2018-05-16 13:24:01 +0200
commitb23116112e21fbdecc51e2029bfe01c0f19b5267 (patch)
treec583f45ecedad34f6e589acfef2b18669b64c8fd
parent3f31e37f488e7f063d6d341011facf1c59092287 (diff)
downloadvis-b23116112e21fbdecc51e2029bfe01c0f19b5267.tar.gz
vis-b23116112e21fbdecc51e2029bfe01c0f19b5267.tar.xz
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.
-rw-r--r--text.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/text.c b/text.c
index caf203b..9d24e5a 100644
--- 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;
}