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