diff options
-rw-r--r-- | src/fs/io/FileOutputStream.cxx | 31 | ||||
-rw-r--r-- | src/fs/io/FileOutputStream.hxx | 8 |
2 files changed, 20 insertions, 19 deletions
diff --git a/src/fs/io/FileOutputStream.cxx b/src/fs/io/FileOutputStream.cxx index 6cec024ad..85eb300e7 100644 --- a/src/fs/io/FileOutputStream.cxx +++ b/src/fs/io/FileOutputStream.cxx @@ -111,14 +111,14 @@ FileOutputStream::Cancel() /** * Open a file using Linux's O_TMPFILE for writing the given file. */ -static int -OpenTempFile(Path path) +static bool +OpenTempFile(FileDescriptor &fd, Path path) { const auto directory = path.GetDirectoryName(); if (directory.IsNull()) - return -1; + return false; - return OpenFile(directory, O_TMPFILE|O_WRONLY, 0666); + return fd.Open(directory.c_str(), O_TMPFILE|O_WRONLY, 0666); } #endif /* HAVE_LINKAT */ @@ -128,15 +128,13 @@ FileOutputStream::FileOutputStream(Path _path, Error &error) { #ifdef HAVE_LINKAT /* try Linux's O_TMPFILE first */ - fd = OpenTempFile(path); - is_tmpfile = fd >= 0; + is_tmpfile = OpenTempFile(fd, path); if (!is_tmpfile) { #endif /* fall back to plain POSIX */ - fd = OpenFile(path, - O_WRONLY|O_CREAT|O_TRUNC, - 0666); - if (fd < 0) + if (!fd.Open(path.c_str(), + O_WRONLY|O_CREAT|O_TRUNC, + 0666)) error.FormatErrno("Failed to create %s", path.c_str()); #ifdef HAVE_LINKAT } @@ -148,7 +146,7 @@ FileOutputStream::Write(const void *data, size_t size, Error &error) { assert(IsDefined()); - ssize_t nbytes = write(fd, data, size); + ssize_t nbytes = fd.Write(data, size); if (nbytes < 0) { error.FormatErrno("Failed to write to %s", path.c_str()); return false; @@ -172,18 +170,18 @@ FileOutputStream::Commit(Error &error) /* hard-link the temporary file to the final path */ char fd_path[64]; - snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", fd); + snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", + fd.Get()); if (linkat(AT_FDCWD, fd_path, AT_FDCWD, path.c_str(), AT_SYMLINK_FOLLOW) < 0) { error.FormatErrno("Failed to commit %s", path.c_str()); - close(fd); + fd.Close(); return false; } } #endif - bool success = close(fd) == 0; - fd = -1; + bool success = fd.Close(); if (!success) error.FormatErrno("Failed to commit %s", path.c_str()); @@ -195,8 +193,7 @@ FileOutputStream::Cancel() { assert(IsDefined()); - close(fd); - fd = -1; + fd.Close(); #ifdef HAVE_LINKAT if (!is_tmpfile) diff --git a/src/fs/io/FileOutputStream.hxx b/src/fs/io/FileOutputStream.hxx index f66554ec9..ff1ae31a4 100644 --- a/src/fs/io/FileOutputStream.hxx +++ b/src/fs/io/FileOutputStream.hxx @@ -25,6 +25,10 @@ #include "fs/AllocatedPath.hxx" #include "Compiler.h" +#ifndef WIN32 +#include "system/FileDescriptor.hxx" +#endif + #include <assert.h> #ifdef WIN32 @@ -39,7 +43,7 @@ class FileOutputStream final : public OutputStream { #ifdef WIN32 HANDLE handle; #else - int fd; + FileDescriptor fd; #endif #ifdef HAVE_LINKAT @@ -64,7 +68,7 @@ public: #ifdef WIN32 return handle != INVALID_HANDLE_VALUE; #else - return fd >= 0; + return fd.IsDefined(); #endif } |