fs/FileSystem: OpenFile() returns UniqueFileDescriptor
This commit is contained in:
parent
eb0ff32efb
commit
df5cc3f0f6
|
@ -62,7 +62,7 @@ open_log_file(void)
|
|||
{
|
||||
assert(!out_path.IsNull());
|
||||
|
||||
return OpenFile(out_path, O_CREAT | O_WRONLY | O_APPEND, 0666);
|
||||
return OpenFile(out_path, O_CREAT | O_WRONLY | O_APPEND, 0666).Steal();
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "AllocatedPath.hxx"
|
||||
#include "Limits.hxx"
|
||||
#include "system/Error.hxx"
|
||||
#include "system/fd_util.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
|
|
@ -22,9 +22,8 @@
|
|||
|
||||
#include "check.h"
|
||||
#include "Traits.hxx"
|
||||
#include "system/fd_util.h"
|
||||
|
||||
#include "Path.hxx"
|
||||
#include "system/UniqueFileDescriptor.hxx"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <fileapi.h>
|
||||
|
@ -53,14 +52,12 @@ FOpen(Path file, PathTraitsFS::const_pointer_type mode)
|
|||
/**
|
||||
* Wrapper for open_cloexec() that uses #Path names.
|
||||
*/
|
||||
static inline int
|
||||
static inline UniqueFileDescriptor
|
||||
OpenFile(Path file, int flags, int mode)
|
||||
{
|
||||
#ifdef WIN32
|
||||
return _topen(file.c_str(), flags, mode);
|
||||
#else
|
||||
return open_cloexec(file.c_str(), flags, mode);
|
||||
#endif
|
||||
UniqueFileDescriptor fd;
|
||||
fd.Open(file.c_str(), flags, mode);
|
||||
return fd;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -153,12 +153,12 @@ FifoOutput::OpenFifo()
|
|||
try {
|
||||
Check();
|
||||
|
||||
input = OpenFile(path, O_RDONLY|O_NONBLOCK|O_BINARY, 0);
|
||||
input = OpenFile(path, O_RDONLY|O_NONBLOCK|O_BINARY, 0).Steal();
|
||||
if (input < 0)
|
||||
throw FormatErrno("Could not open FIFO \"%s\" for reading",
|
||||
path_utf8.c_str());
|
||||
|
||||
output = OpenFile(path, O_WRONLY|O_NONBLOCK|O_BINARY, 0);
|
||||
output = OpenFile(path, O_WRONLY|O_NONBLOCK|O_BINARY, 0).Steal();
|
||||
if (output < 0)
|
||||
throw FormatErrno("Could not open FIFO \"%s\" for writing",
|
||||
path_utf8.c_str());
|
||||
|
|
|
@ -38,7 +38,7 @@ public:
|
|||
if (path.IsNull())
|
||||
return;
|
||||
|
||||
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
||||
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666).Steal();
|
||||
if (fd < 0) {
|
||||
const std::string utf8 = path.ToUTF8();
|
||||
FormatFatalSystemError("Failed to create pid file \"%s\"",
|
||||
|
@ -90,14 +90,14 @@ gcc_pure
|
|||
static inline pid_t
|
||||
ReadPidFile(Path path) noexcept
|
||||
{
|
||||
int fd = OpenFile(path, O_RDONLY, 0);
|
||||
if (fd < 0)
|
||||
auto fd = OpenFile(path, O_RDONLY, 0);
|
||||
if (!fd.IsDefined())
|
||||
return -1;
|
||||
|
||||
pid_t pid = -1;
|
||||
|
||||
char buffer[32];
|
||||
auto nbytes = read(fd, buffer, sizeof(buffer) - 1);
|
||||
auto nbytes = fd.Read(buffer, sizeof(buffer) - 1);
|
||||
if (nbytes > 0) {
|
||||
buffer[nbytes] = 0;
|
||||
|
||||
|
@ -107,7 +107,6 @@ ReadPidFile(Path path) noexcept
|
|||
pid = value;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue