diff options
author | Riccardo Spagni <ric@spagni.net> | 2017-09-25 16:40:35 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2017-09-25 16:40:35 +0200 |
commit | 216395d43e41d2e9e6c81add94da98b3577e0542 (patch) | |
tree | f5d2c949efd6e4424a03ce08f0da1d8e926d6103 /src/common/util.h | |
parent | Merge pull request #2377 (diff) | |
parent | Do not create file when RPC user/pass is given and use file locking (diff) | |
download | monero-216395d43e41d2e9e6c81add94da98b3577e0542.tar.xz |
Merge pull request #2379
9c83f806 Do not create file when RPC user/pass is given and use file locking (Lee Clagett)
Diffstat (limited to 'src/common/util.h')
-rw-r--r-- | src/common/util.h | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/common/util.h b/src/common/util.h index 2452bc9d5..48bdbbc28 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -60,8 +60,30 @@ namespace tools } }; - //! \return File only readable by owner. nullptr if `filename` exists. - std::unique_ptr<std::FILE, close_file> create_private_file(const std::string& filename); + //! A file restricted to process owner AND process. Deletes file on destruction. + class private_file { + std::unique_ptr<std::FILE, close_file> m_handle; + std::string m_filename; + + private_file(std::FILE* handle, std::string&& filename) noexcept; + public: + + //! `handle() == nullptr && filename.empty()`. + private_file() noexcept; + + /*! \return File only readable by owner and only used by this process + OR `private_file{}` on error. */ + static private_file create(std::string filename); + + private_file(private_file&&) = default; + private_file& operator=(private_file&&) = default; + + //! Deletes `filename()` and closes `handle()`. + ~private_file() noexcept; + + std::FILE* handle() const noexcept { return m_handle.get(); } + const std::string& filename() const noexcept { return m_filename; } + }; /*! \brief Returns the default data directory. * |