aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2015-03-03 22:18:38 +0100
committerMax Kellermann <max@duempel.org>2015-03-03 23:04:25 +0100
commitcd08e5c7da399ce1fb2f84eb39af7fd684c3df80 (patch)
tree1ef4622f3322b8c8058221098e3d9e0c188fab3c
parentdd4beea44c6a574a25d1778e946f470683e1a0d6 (diff)
downloadmpd-cd08e5c7da399ce1fb2f84eb39af7fd684c3df80.tar.gz
mpd-cd08e5c7da399ce1fb2f84eb39af7fd684c3df80.tar.xz
mpd-cd08e5c7da399ce1fb2f84eb39af7fd684c3df80.zip
fs/io/FileOutputStream: use class FileDescriptor
-rw-r--r--src/fs/io/FileOutputStream.cxx31
-rw-r--r--src/fs/io/FileOutputStream.hxx8
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
}