diff options
Diffstat (limited to '')
-rw-r--r-- | src/fs/io/FileOutputStream.hxx | 80 |
1 files changed, 68 insertions, 12 deletions
diff --git a/src/fs/io/FileOutputStream.hxx b/src/fs/io/FileOutputStream.hxx index 7d30d95bb..ce53427e2 100644 --- a/src/fs/io/FileOutputStream.hxx +++ b/src/fs/io/FileOutputStream.hxx @@ -37,7 +37,7 @@ class Path; -class FileOutputStream final : public OutputStream { +class BaseFileOutputStream : public OutputStream { const AllocatedPath path; #ifdef WIN32 @@ -46,6 +46,73 @@ class FileOutputStream final : public OutputStream { FileDescriptor fd; #endif +protected: +#ifdef WIN32 + template<typename P> + BaseFileOutputStream(P &&_path) + :path(std::forward<P>(_path)), + handle(INVALID_HANDLE_VALUE) {} +#else + template<typename P> + BaseFileOutputStream(P &&_path) + :path(std::forward<P>(_path)), + fd(FileDescriptor::Undefined()) {} +#endif + + ~BaseFileOutputStream() { + assert(!IsDefined()); + } + +#ifdef WIN32 + void SetHandle(HANDLE _handle) { + assert(!IsDefined()); + + handle = _handle; + + assert(IsDefined()); + } +#else + FileDescriptor &SetFD() { + assert(!IsDefined()); + + return fd; + } + + const FileDescriptor &GetFD() const { + return fd; + } +#endif + + bool Close() { + assert(IsDefined()); + +#ifdef WIN32 + CloseHandle(handle); + handle = INVALID_HANDLE_VALUE; + return true; +#else + return fd.Close(); +#endif + } + +public: + bool IsDefined() const { +#ifdef WIN32 + return handle != INVALID_HANDLE_VALUE; +#else + return fd.IsDefined(); +#endif + } + + Path GetPath() const { + return path; + } + + /* virtual methods from class OutputStream */ + bool Write(const void *data, size_t size, Error &error) override; +}; + +class FileOutputStream final : public BaseFileOutputStream { #ifdef HAVE_LINKAT /** * Was O_TMPFILE used? If yes, then linkat() must be used to @@ -64,19 +131,8 @@ public: static FileOutputStream *Create(Path path, Error &error); - bool IsDefined() const { -#ifdef WIN32 - return handle != INVALID_HANDLE_VALUE; -#else - return fd.IsDefined(); -#endif - } - bool Commit(Error &error); void Cancel(); - - /* virtual methods from class OutputStream */ - bool Write(const void *data, size_t size, Error &error) override; }; #endif |