diff options
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/archive.cc | 4 | ||||
-rw-r--r-- | src/libutil/util.cc | 22 | ||||
-rw-r--r-- | src/libutil/util.hh | 3 |
3 files changed, 15 insertions, 14 deletions
diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index e3ff7345f..00536c1e1 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -44,7 +44,7 @@ static void dumpContents(const Path & path, off_t size, { sink << "contents" << size; - AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); + AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)}; if (!fd) throw SysError("opening file '%1%'", path); std::vector<char> buf(65536); @@ -318,7 +318,7 @@ struct RestoreSink : ParseSink void createRegularFile(const Path & path) override { Path p = dstPath + path; - fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0666); + fd = AutoCloseFD{open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0666)}; if (!fd) throw SysError("creating file '%1%'", p); } diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 3849b1a2f..f69269096 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -379,7 +379,7 @@ std::string readFile(int fd) std::string readFile(const Path & path) { - AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); + AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)}; if (!fd) throw SysError("opening file '%1%'", path); return readFile(fd.get()); @@ -388,7 +388,7 @@ std::string readFile(const Path & path) void readFile(const Path & path, Sink & sink) { - AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); + AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)}; if (!fd) throw SysError("opening file '%s'", path); drainFD(fd.get(), sink); @@ -397,7 +397,7 @@ void readFile(const Path & path, Sink & sink) void writeFile(const Path & path, std::string_view s, mode_t mode, bool sync) { - AutoCloseFD fd = open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode); + AutoCloseFD fd{open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode)}; if (!fd) throw SysError("opening file '%1%'", path); try { @@ -417,7 +417,7 @@ void writeFile(const Path & path, std::string_view s, mode_t mode, bool sync) void writeFile(const Path & path, Source & source, mode_t mode, bool sync) { - AutoCloseFD fd = open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode); + AutoCloseFD fd{open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode)}; if (!fd) throw SysError("opening file '%1%'", path); @@ -444,7 +444,7 @@ void writeFile(const Path & path, Source & source, mode_t mode, bool sync) void syncParent(const Path & path) { - AutoCloseFD fd = open(dirOf(path).c_str(), O_RDONLY, 0); + AutoCloseFD fd{open(dirOf(path).c_str(), O_RDONLY, 0)}; if (!fd) throw SysError("opening file '%1%'", path); fd.fsync(); @@ -941,8 +941,8 @@ void Pipe::create() closeOnExec(fds[0]); closeOnExec(fds[1]); #endif - readSide = fds[0]; - writeSide = fds[1]; + readSide = AutoCloseFD{fds[0]}; + writeSide = AutoCloseFD{fds[1]}; } @@ -1718,11 +1718,11 @@ void saveMountNamespace() #if __linux__ static std::once_flag done; std::call_once(done, []() { - fdSavedMountNamespace = open("/proc/self/ns/mnt", O_RDONLY); + fdSavedMountNamespace = AutoCloseFD{open("/proc/self/ns/mnt", O_RDONLY)}; if (!fdSavedMountNamespace) throw SysError("saving parent mount namespace"); - fdSavedRoot = open("/proc/self/root", O_RDONLY); + fdSavedRoot = AutoCloseFD{open("/proc/self/root", O_RDONLY)}; }); #endif } @@ -1777,11 +1777,11 @@ void restoreProcessContext(bool restoreMounts) AutoCloseFD createUnixDomainSocket() { - AutoCloseFD fdSocket = socket(PF_UNIX, SOCK_STREAM + AutoCloseFD fdSocket{socket(PF_UNIX, SOCK_STREAM #ifdef SOCK_CLOEXEC | SOCK_CLOEXEC #endif - , 0); + , 0)}; if (!fdSocket) throw SysError("cannot create Unix domain socket"); closeOnExec(fdSocket.get()); diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 3cf5eb0b1..d47d7e0be 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -332,7 +332,7 @@ class AutoCloseFD int fd; public: AutoCloseFD(); - AutoCloseFD(int fd); + explicit AutoCloseFD(int fd); AutoCloseFD(const AutoCloseFD & fd) = delete; AutoCloseFD(AutoCloseFD&& fd); ~AutoCloseFD(); @@ -343,6 +343,7 @@ public: int release(); void close(); void fsync(); + void reset() { *this = {}; } }; |