aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--text.c31
-rw-r--r--text.h7
2 files changed, 29 insertions, 9 deletions
diff --git a/text.c b/text.c
index 9d24e5a..5d5f814 100644
--- a/text.c
+++ b/text.c
@@ -815,7 +815,8 @@ static bool preserve_selinux_context(int src, int dest) {
return true;
}
-/* Create a new file named `filename~` and try to preserve all important
+/* Create a new file named `.filename.vis.XXXXXX` (where `XXXXXX` is a
+ * randomly generated, unique suffix) and try to preserve all important
* meta data. After the file content has been written to this temporary
* file, text_save_commit_atomic will atomically move it to its final
* (possibly already existing) destination using rename(2).
@@ -844,14 +845,32 @@ static bool text_save_begin_atomic(TextSave *ctx) {
goto err;
}
- size_t namelen = strlen(ctx->filename) + 1 /* ~ */ + 1 /* \0 */;
- if (!(ctx->tmpname = calloc(1, namelen)))
+ char suffix[] = ".vis.XXXXXX";
+ size_t len = strlen(ctx->filename) + sizeof("./.") + sizeof(suffix);
+ char *dir = strdup(ctx->filename);
+ char *base = strdup(ctx->filename);
+
+ if (!(ctx->tmpname = malloc(len)) || !dir || !base) {
+ free(dir);
+ free(base);
goto err;
- snprintf(ctx->tmpname, namelen, "%s~", ctx->filename);
+ }
- if ((ctx->fd = open(ctx->tmpname, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, oldfd == -1 ? 0666 : oldmeta.st_mode)) == -1)
+ snprintf(ctx->tmpname, len, "%s/.%s%s", dirname(dir), basename(base), suffix);
+ free(dir);
+ free(base);
+
+ if ((ctx->fd = mkstemp(ctx->tmpname)) == -1)
goto err;
- if (oldfd != -1) {
+
+ if (oldfd == -1) {
+ mode_t mask = umask(0);
+ umask(mask);
+ if (fchmod(ctx->fd, 0666 & ~mask) == -1)
+ goto err;
+ } else {
+ if (fchmod(ctx->fd, oldmeta.st_mode) == -1)
+ goto err;
if (!preserve_acl(oldfd, ctx->fd) || !preserve_selinux_context(oldfd, ctx->fd))
goto err;
/* change owner if necessary */
diff --git a/text.h b/text.h
index 06b7843..2fa6aea 100644
--- a/text.h
+++ b/text.h
@@ -280,9 +280,10 @@ enum TextSaveMethod {
/**
* Save file atomically using `rename(2)`.
*
- * Creates a new file named `filename~` and tries to restore all important
- * meta data. After which it is atomically moved to its final
- * (possibly already existing) destination using `rename(2)`.
+ * Creates a temporary file, restores all important meta data,
+ * before moving it atomically to its final (possibly already
+ * existing) destination using `rename(2)`. For new files,
+ * permissions are set to `0666 & ~umask`.
*
* @rst
* .. warning:: This approach does not work if: