From b63a794fbe88136d0cbee724e8ce500bacaa5f04 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 11 Jan 2024 16:59:44 +0100 Subject: [PATCH] io/FileDescriptor: add [[nodiscard]] --- src/fs/FileSystem.hxx | 2 +- src/io/FileDescriptor.hxx | 28 ++++++++++++++++++++++++++++ src/mixer/plugins/OssMixerPlugin.cxx | 3 +-- src/system/EventPipe.cxx | 2 +- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/fs/FileSystem.hxx b/src/fs/FileSystem.hxx index c67baffad..de5ab7f13 100644 --- a/src/fs/FileSystem.hxx +++ b/src/fs/FileSystem.hxx @@ -23,7 +23,7 @@ static inline UniqueFileDescriptor OpenFile(Path file, int flags, int mode) { UniqueFileDescriptor fd; - fd.Open(file.c_str(), flags, mode); + (void)fd.Open(file.c_str(), flags, mode); return fd; } diff --git a/src/io/FileDescriptor.hxx b/src/io/FileDescriptor.hxx index 6da440a35..64ae824ce 100644 --- a/src/io/FileDescriptor.hxx +++ b/src/io/FileDescriptor.hxx @@ -27,7 +27,10 @@ protected: int fd; public: + [[nodiscard]] FileDescriptor() = default; + + [[nodiscard]] explicit constexpr FileDescriptor(int _fd) noexcept:fd(_fd) {} constexpr bool operator==(FileDescriptor other) const noexcept { @@ -80,6 +83,7 @@ public: fd = _fd; } + [[nodiscard]] int Steal() noexcept { return std::exchange(fd, -1); } @@ -88,37 +92,46 @@ public: fd = -1; } + [[nodiscard]] static constexpr FileDescriptor Undefined() noexcept { return FileDescriptor(-1); } #ifdef __linux__ + [[nodiscard]] bool Open(FileDescriptor dir, const char *pathname, int flags, mode_t mode=0666) noexcept; #endif + [[nodiscard]] bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept; #ifdef _WIN32 + [[nodiscard]] bool Open(const wchar_t *pathname, int flags, mode_t mode=0666) noexcept; #endif + [[nodiscard]] bool OpenReadOnly(const char *pathname) noexcept; #ifdef __linux__ + [[nodiscard]] bool OpenReadOnly(FileDescriptor dir, const char *pathname) noexcept; #endif #ifndef _WIN32 + [[nodiscard]] bool OpenNonBlocking(const char *pathname) noexcept; #endif #ifdef __linux__ + [[nodiscard]] static bool CreatePipe(FileDescriptor &r, FileDescriptor &w, int flags) noexcept; #endif + [[nodiscard]] static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept; #ifdef _WIN32 @@ -126,6 +139,7 @@ public: void DisableCloseOnExec() const noexcept {} void SetBinaryMode() const noexcept; #else + [[nodiscard]] static bool CreatePipeNonBlock(FileDescriptor &r, FileDescriptor &w) noexcept; @@ -159,11 +173,13 @@ public: * @return the new file descriptor or UniqueFileDescriptor{} * on error */ + [[nodiscard]] UniqueFileDescriptor Duplicate() const noexcept; /** * Duplicate the file descriptor onto the given file descriptor. */ + [[nodiscard]] bool Duplicate(FileDescriptor new_fd) const noexcept { return ::dup2(Get(), new_fd.Get()) != -1; } @@ -189,12 +205,15 @@ public: /** * Rewind the pointer to the beginning of the file. */ + [[nodiscard]] bool Rewind() const noexcept; + [[nodiscard]] off_t Seek(off_t offset) const noexcept { return lseek(Get(), offset, SEEK_SET); } + [[nodiscard]] off_t Skip(off_t offset) const noexcept { return lseek(Get(), offset, SEEK_CUR); } @@ -211,16 +230,19 @@ public: off_t GetSize() const noexcept; #ifndef _WIN32 + [[nodiscard]] ssize_t ReadAt(off_t offset, void *buffer, std::size_t length) const noexcept { return ::pread(fd, buffer, length, offset); } #endif + [[nodiscard]] ssize_t Read(std::span dest) const noexcept { return ::read(fd, dest.data(), dest.size()); } + [[nodiscard]] ssize_t Read(void *buffer, std::size_t length) const noexcept { return ::read(fd, buffer, length); } @@ -231,10 +253,12 @@ public: */ void FullRead(std::span dest) const; + [[nodiscard]] ssize_t Write(std::span src) const noexcept { return ::write(fd, src.data(), src.size()); } + [[nodiscard]] ssize_t Write(const void *buffer, std::size_t length) const noexcept { return ::write(fd, buffer, length); } @@ -246,9 +270,13 @@ public: void FullWrite(std::span src) const; #ifndef _WIN32 + [[nodiscard]] int Poll(short events, int timeout) const noexcept; + [[nodiscard]] int WaitReadable(int timeout) const noexcept; + + [[nodiscard]] int WaitWritable(int timeout) const noexcept; [[gnu::pure]] diff --git a/src/mixer/plugins/OssMixerPlugin.cxx b/src/mixer/plugins/OssMixerPlugin.cxx index 0c03dcab6..a9756bc9d 100644 --- a/src/mixer/plugins/OssMixerPlugin.cxx +++ b/src/mixer/plugins/OssMixerPlugin.cxx @@ -97,8 +97,7 @@ OssMixer::Close() noexcept void OssMixer::Open() { - device_fd.OpenReadOnly(device); - if (!device_fd.IsDefined()) + if (!device_fd.OpenReadOnly(device)) throw FmtErrno("failed to open {}", device); try { diff --git a/src/system/EventPipe.cxx b/src/system/EventPipe.cxx index e6d83ceb5..ce5d399d6 100644 --- a/src/system/EventPipe.cxx +++ b/src/system/EventPipe.cxx @@ -48,7 +48,7 @@ EventPipe::Write() noexcept assert(w.IsDefined()); static constexpr std::byte buffer[1]{}; - w.Write(buffer); + (void)w.Write(buffer); } #ifdef _WIN32