From b23116112e21fbdecc51e2029bfe01c0f19b5267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 25 Apr 2018 20:11:33 +0200 Subject: 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. --- text.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'text.c') 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; } -- cgit v1.2.3