From d84f96a571caebaf2417db8f0545bcefc2d71e9e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 4 Sep 2016 13:08:52 +0200 Subject: [PATCH] fs/io/FileOutputStream: add mode CREATE_VISIBLE --- src/fs/io/FileOutputStream.cxx | 13 +++++++++---- src/fs/io/FileOutputStream.hxx | 9 ++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/fs/io/FileOutputStream.cxx b/src/fs/io/FileOutputStream.cxx index 421fac249..d6384630b 100644 --- a/src/fs/io/FileOutputStream.cxx +++ b/src/fs/io/FileOutputStream.cxx @@ -26,7 +26,11 @@ FileOutputStream::FileOutputStream(Path _path, Mode _mode) { switch (mode) { case Mode::CREATE: - OpenCreate(); + OpenCreate(false); + break; + + case Mode::CREATE_VISIBLE: + OpenCreate(true); break; case Mode::APPEND_EXISTING: @@ -42,7 +46,7 @@ FileOutputStream::FileOutputStream(Path _path, Mode _mode) #ifdef WIN32 inline void -FileOutputStream::OpenCreate() +FileOutputStream::OpenCreate(gcc_unused bool visible) { handle = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, @@ -147,11 +151,11 @@ OpenTempFile(FileDescriptor &fd, Path path) #endif /* HAVE_LINKAT */ inline void -FileOutputStream::OpenCreate() +FileOutputStream::OpenCreate(bool visible) { #ifdef HAVE_LINKAT /* try Linux's O_TMPFILE first */ - is_tmpfile = OpenTempFile(fd, GetPath()); + is_tmpfile = !visible && OpenTempFile(fd, GetPath()); if (!is_tmpfile) { #endif /* fall back to plain POSIX */ @@ -241,6 +245,7 @@ FileOutputStream::Cancel() unlink(GetPath().c_str()); break; + case Mode::CREATE_VISIBLE: case Mode::APPEND_EXISTING: case Mode::APPEND_OR_CREATE: /* can't roll this back */ diff --git a/src/fs/io/FileOutputStream.hxx b/src/fs/io/FileOutputStream.hxx index ff4acaf1e..c1fcccc1e 100644 --- a/src/fs/io/FileOutputStream.hxx +++ b/src/fs/io/FileOutputStream.hxx @@ -64,6 +64,13 @@ public: */ CREATE, + /** + * Like #CREATE, but no attempt is made to hide file + * contents during the transaction (e.g. via O_TMPFILE + * or a hidden temporary file). + */ + CREATE_VISIBLE, + /** * Append to a file that already exists. If it does * not, an exception is thrown. @@ -103,7 +110,7 @@ public: void Cancel(); private: - void OpenCreate(); + void OpenCreate(bool visible); void OpenAppend(bool create); bool Close() {