diff options
Diffstat (limited to 'src/fs/io/FileOutputStream.cxx')
-rw-r--r-- | src/fs/io/FileOutputStream.cxx | 31 |
1 files changed, 14 insertions, 17 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) |