diff options
author | Max Kellermann <max@musicpd.org> | 2020-05-05 14:11:13 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2020-05-05 14:27:03 +0200 |
commit | 9815d101372ffd7386f076c1fffa6a39b15d7e51 (patch) | |
tree | 1314a5563308586e315d3e10b8988c5e84672a1e /src/system | |
parent | 97f7270aa8fa8b1618545f5eca4a9a1872f3d661 (diff) |
system/FileDescriptor: move to io/
Diffstat (limited to 'src/system')
-rw-r--r-- | src/system/EpollFD.hxx | 4 | ||||
-rw-r--r-- | src/system/EventFD.hxx | 2 | ||||
-rw-r--r-- | src/system/EventPipe.cxx | 2 | ||||
-rw-r--r-- | src/system/FileDescriptor.cxx | 343 | ||||
-rw-r--r-- | src/system/FileDescriptor.hxx | 257 | ||||
-rw-r--r-- | src/system/Open.cxx | 122 | ||||
-rw-r--r-- | src/system/Open.hxx | 68 | ||||
-rw-r--r-- | src/system/SignalFD.hxx | 2 | ||||
-rw-r--r-- | src/system/UniqueFileDescriptor.hxx | 94 | ||||
-rw-r--r-- | src/system/meson.build | 3 |
10 files changed, 6 insertions, 891 deletions
diff --git a/src/system/EpollFD.hxx b/src/system/EpollFD.hxx index 4161b54f5..6349bd254 100644 --- a/src/system/EpollFD.hxx +++ b/src/system/EpollFD.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2013-2020 Max Kellermann <max.kellermann@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -30,7 +30,7 @@ #ifndef EPOLL_FD_HXX #define EPOLL_FD_HXX -#include "UniqueFileDescriptor.hxx" +#include "io/UniqueFileDescriptor.hxx" #include <cstdint> diff --git a/src/system/EventFD.hxx b/src/system/EventFD.hxx index 79d1af0c7..337947472 100644 --- a/src/system/EventFD.hxx +++ b/src/system/EventFD.hxx @@ -30,7 +30,7 @@ #ifndef EVENT_FD_HXX #define EVENT_FD_HXX -#include "UniqueFileDescriptor.hxx" +#include "io/UniqueFileDescriptor.hxx" /** * A class that wraps eventfd(). diff --git a/src/system/EventPipe.cxx b/src/system/EventPipe.cxx index 462cc4a1c..d388451a2 100644 --- a/src/system/EventPipe.cxx +++ b/src/system/EventPipe.cxx @@ -18,7 +18,7 @@ */ #include "EventPipe.hxx" -#include "FileDescriptor.hxx" +#include "io/FileDescriptor.hxx" #include "system/Error.hxx" #include "util/Compiler.h" diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx deleted file mode 100644 index 27e66e95d..000000000 --- a/src/system/FileDescriptor.cxx +++ /dev/null @@ -1,343 +0,0 @@ -/* - * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "FileDescriptor.hxx" -#include "system/Error.hxx" - -#include <cassert> -#include <cstdint> - -#include <sys/stat.h> -#include <fcntl.h> - -#ifndef _WIN32 -#include <poll.h> -#endif - -#ifdef __linux__ -#include <sys/eventfd.h> -#include <sys/signalfd.h> -#include <sys/inotify.h> -#endif - -#ifndef O_NOCTTY -#define O_NOCTTY 0 -#endif - -#ifndef O_CLOEXEC -#define O_CLOEXEC 0 -#endif - -#ifndef _WIN32 - -bool -FileDescriptor::IsValid() const noexcept -{ - return IsDefined() && fcntl(fd, F_GETFL) >= 0; -} - -bool -FileDescriptor::IsRegularFile() const noexcept -{ - struct stat st; - return IsDefined() && fstat(fd, &st) == 0 && S_ISREG(st.st_mode); -} - -bool -FileDescriptor::IsPipe() const noexcept -{ - struct stat st; - return IsDefined() && fstat(fd, &st) == 0 && S_ISFIFO(st.st_mode); -} - -bool -FileDescriptor::IsSocket() const noexcept -{ - struct stat st; - return IsDefined() && fstat(fd, &st) == 0 && S_ISSOCK(st.st_mode); -} - -#endif - -#ifdef __linux__ - -bool -FileDescriptor::Open(FileDescriptor dir, const char *pathname, - int flags, mode_t mode) noexcept -{ - fd = ::openat(dir.Get(), pathname, flags | O_NOCTTY | O_CLOEXEC, mode); - return IsDefined(); -} - -#endif - -bool -FileDescriptor::Open(const char *pathname, int flags, mode_t mode) noexcept -{ - fd = ::open(pathname, flags | O_NOCTTY | O_CLOEXEC, mode); - return IsDefined(); -} - -#ifdef _WIN32 - -bool -FileDescriptor::Open(const wchar_t *pathname, int flags, mode_t mode) noexcept -{ - fd = ::_wopen(pathname, flags | O_NOCTTY | O_CLOEXEC, mode); - return IsDefined(); -} - -#endif - -bool -FileDescriptor::OpenReadOnly(const char *pathname) noexcept -{ - return Open(pathname, O_RDONLY); -} - -#ifndef _WIN32 - -bool -FileDescriptor::OpenNonBlocking(const char *pathname) noexcept -{ - return Open(pathname, O_RDWR | O_NONBLOCK); -} - -#endif - -#ifdef __linux__ - -bool -FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w, - int flags) noexcept -{ - int fds[2]; - const int result = pipe2(fds, flags); - if (result < 0) - return false; - - r = FileDescriptor(fds[0]); - w = FileDescriptor(fds[1]); - return true; -} - -#endif - -bool -FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept -{ -#ifdef __linux__ - return CreatePipe(r, w, O_CLOEXEC); -#else - int fds[2]; - -#ifdef _WIN32 - const int result = _pipe(fds, 512, _O_BINARY); -#else - const int result = pipe(fds); -#endif - - if (result < 0) - return false; - - r = FileDescriptor(fds[0]); - w = FileDescriptor(fds[1]); - return true; -#endif -} - -#ifndef _WIN32 - -bool -FileDescriptor::CreatePipeNonBlock(FileDescriptor &r, - FileDescriptor &w) noexcept -{ -#ifdef __linux__ - return CreatePipe(r, w, O_CLOEXEC|O_NONBLOCK); -#else - if (!CreatePipe(r, w)) - return false; - - r.SetNonBlocking(); - w.SetNonBlocking(); - return true; -#endif -} - -void -FileDescriptor::SetNonBlocking() noexcept -{ - assert(IsDefined()); - - int flags = fcntl(fd, F_GETFL); - fcntl(fd, F_SETFL, flags | O_NONBLOCK); -} - -void -FileDescriptor::SetBlocking() noexcept -{ - assert(IsDefined()); - - int flags = fcntl(fd, F_GETFL); - fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); -} - -void -FileDescriptor::EnableCloseOnExec() noexcept -{ - assert(IsDefined()); - - const int old_flags = fcntl(fd, F_GETFD, 0); - fcntl(fd, F_SETFD, old_flags | FD_CLOEXEC); -} - -void -FileDescriptor::DisableCloseOnExec() noexcept -{ - assert(IsDefined()); - - const int old_flags = fcntl(fd, F_GETFD, 0); - fcntl(fd, F_SETFD, old_flags & ~FD_CLOEXEC); -} - -bool -FileDescriptor::CheckDuplicate(FileDescriptor new_fd) noexcept -{ - if (*this == new_fd) { - DisableCloseOnExec(); - return true; - } else - return Duplicate(new_fd); -} - -#endif - -#ifdef __linux__ - -bool -FileDescriptor::CreateEventFD(unsigned initval) noexcept -{ - fd = ::eventfd(initval, EFD_NONBLOCK|EFD_CLOEXEC); - return fd >= 0; -} - -bool -FileDescriptor::CreateSignalFD(const sigset_t *mask) noexcept -{ - int new_fd = ::signalfd(fd, mask, SFD_NONBLOCK|SFD_CLOEXEC); - if (new_fd < 0) - return false; - - fd = new_fd; - return true; -} - -bool -FileDescriptor::CreateInotify() noexcept -{ - int new_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); - if (new_fd < 0) - return false; - - fd = new_fd; - return true; -} - -#endif - -bool -FileDescriptor::Rewind() noexcept -{ - assert(IsDefined()); - - return lseek(fd, 0, SEEK_SET) == 0; -} - -off_t -FileDescriptor::GetSize() const noexcept -{ - struct stat st; - return ::fstat(fd, &st) >= 0 - ? (long)st.st_size - : -1; -} - -void -FileDescriptor::FullRead(void *_buffer, size_t length) -{ - auto *buffer = (uint8_t *)_buffer; - - while (length > 0) { - ssize_t nbytes = Read(buffer, length); - if (nbytes <= 0) { - if (nbytes < 0) - throw MakeErrno("Failed to read"); - throw std::runtime_error("Unexpected end of file"); - } - - buffer += nbytes; - length -= nbytes; - } -} - -#ifndef _WIN32 - -int -FileDescriptor::Poll(short events, int timeout) const noexcept -{ - assert(IsDefined()); - - struct pollfd pfd; - pfd.fd = fd; - pfd.events = events; - int result = poll(&pfd, 1, timeout); - return result > 0 - ? pfd.revents - : result; -} - -int -FileDescriptor::WaitReadable(int timeout) const noexcept -{ - return Poll(POLLIN, timeout); -} - -int -FileDescriptor::WaitWritable(int timeout) const noexcept -{ - return Poll(POLLOUT, timeout); -} - -bool -FileDescriptor::IsReadyForWriting() const noexcept -{ - return WaitWritable(0) > 0; -} - -#endif diff --git a/src/system/FileDescriptor.hxx b/src/system/FileDescriptor.hxx deleted file mode 100644 index 3faec2ce4..000000000 --- a/src/system/FileDescriptor.hxx +++ /dev/null @@ -1,257 +0,0 @@ -/* - * Copyright 2012-2019 Max Kellermann <max.kellermann@gmail.com> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef FILE_DESCRIPTOR_HXX -#define FILE_DESCRIPTOR_HXX - -#include "util/Compiler.h" - -#include <utility> - -#include <unistd.h> -#include <sys/types.h> - -#ifdef __linux__ -#include <csignal> -#endif - -#ifdef _WIN32 -#include <wchar.h> -#endif - -/** - * An OO wrapper for a UNIX file descriptor. - * - * This class is unmanaged and trivial; for a managed version, see - * #UniqueFileDescriptor. - */ -class FileDescriptor { -protected: - int fd; - -public: - FileDescriptor() = default; - explicit constexpr FileDescriptor(int _fd) noexcept:fd(_fd) {} - - constexpr bool operator==(FileDescriptor other) const noexcept { - return fd == other.fd; - } - - constexpr bool operator!=(FileDescriptor other) const noexcept { - return !(*this == other); - } - - constexpr bool IsDefined() const noexcept { - return fd >= 0; - } - -#ifndef _WIN32 - /** - * Ask the kernel whether this is a valid file descriptor. - */ - gcc_pure - bool IsValid() const noexcept; - - /** - * Ask the kernel whether this is a regular file. - */ - gcc_pure - bool IsRegularFile() const noexcept; - - /** - * Ask the kernel whether this is a pipe. - */ - gcc_pure - bool IsPipe() const noexcept; - - /** - * Ask the kernel whether this is a socket descriptor. - */ - gcc_pure - bool IsSocket() const noexcept; -#endif - - /** - * Returns the file descriptor. This may only be called if - * IsDefined() returns true. - */ - constexpr int Get() const noexcept { - return fd; - } - - void Set(int _fd) noexcept { - fd = _fd; - } - - int Steal() noexcept { - return std::exchange(fd, -1); - } - - void SetUndefined() noexcept { - fd = -1; - } - - static constexpr FileDescriptor Undefined() noexcept { - return FileDescriptor(-1); - } - -#ifdef __linux__ - bool Open(FileDescriptor dir, const char *pathname, - int flags, mode_t mode=0666) noexcept; -#endif - - bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept; - -#ifdef _WIN32 - bool Open(const wchar_t *pathname, int flags, mode_t mode=0666) noexcept; -#endif - - bool OpenReadOnly(const char *pathname) noexcept; - -#ifndef _WIN32 - bool OpenNonBlocking(const char *pathname) noexcept; -#endif - -#ifdef __linux__ - static bool CreatePipe(FileDescriptor &r, FileDescriptor &w, - int flags) noexcept; -#endif - - static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept; - -#ifdef _WIN32 - void EnableCloseOnExec() noexcept {} - void DisableCloseOnExec() noexcept {} -#else - static bool CreatePipeNonBlock(FileDescriptor &r, - FileDescriptor &w) noexcept; - - /** - * Enable non-blocking mode on this file descriptor. - */ - void SetNonBlocking() noexcept; - - /** - * Enable blocking mode on this file descriptor. - */ - void SetBlocking() noexcept; - - /** - * Auto-close this file descriptor when a new program is - * executed. - */ - void EnableCloseOnExec() noexcept; - - /** - * Do not auto-close this file descriptor when a new program - * is executed. - */ - void DisableCloseOnExec() noexcept; - - /** - * Duplicate the file descriptor onto the given file descriptor. - */ - bool Duplicate(FileDescriptor new_fd) const noexcept { - return ::dup2(Get(), new_fd.Get()) == 0; - } - - /** - * Similar to Duplicate(), but if destination and source file - * descriptor are equal, clear the close-on-exec flag. Use - * this method to inject file descriptors into a new child - * process, to be used by a newly executed program. - */ - bool CheckDuplicate(FileDescriptor new_fd) noexcept; -#endif - -#ifdef __linux__ - bool CreateEventFD(unsigned initval=0) noexcept; - bool CreateSignalFD(const sigset_t *mask) noexcept; - bool CreateInotify() noexcept; -#endif - - /** - * Close the file descriptor. It should not be called on an - * "undefined" object. After this call, IsDefined() is guaranteed - * to return false, and this object may be reused. - */ - bool Close() noexcept { - return ::close(Steal()) == 0; - } - - /** - * Rewind the pointer to the beginning of the file. - */ - bool Rewind() noexcept; - - off_t Seek(off_t offset) noexcept { - return lseek(Get(), offset, SEEK_SET); - } - - off_t Skip(off_t offset) noexcept { - return lseek(Get(), offset, SEEK_CUR); - } - - gcc_pure - off_t Tell() const noexcept { - return lseek(Get(), 0, SEEK_CUR); - } - - /** - * Returns the size of the file in bytes, or -1 on error. - */ - gcc_pure - off_t GetSize() const noexcept; - - ssize_t Read(void *buffer, size_t length) noexcept { - return ::read(fd, buffer, length); - } - - /** - * Read until all of the given buffer has been filled. Throws - * on error. - */ - void FullRead(void *buffer, size_t length); - - ssize_t Write(const void *buffer, size_t length) noexcept { - return ::write(fd, buffer, length); - } - -#ifndef _WIN32 - int Poll(short events, int timeout) const noexcept; - - int WaitReadable(int timeout) const noexcept; - int WaitWritable(int timeout) const noexcept; - - gcc_pure - bool IsReadyForWriting() const noexcept; -#endif -}; - -#endif diff --git a/src/system/Open.cxx b/src/system/Open.cxx deleted file mode 100644 index e7bea24a2..000000000 --- a/src/system/Open.cxx +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright 2018 Max Kellermann <max.kellermann@gmail.com> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "Open.hxx" -#include "Error.hxx" -#include "UniqueFileDescriptor.hxx" - -#include <fcntl.h> - -UniqueFileDescriptor -OpenReadOnly(const char *path) -{ - UniqueFileDescriptor fd; - if (!fd.OpenReadOnly(path)) - throw FormatErrno("Failed to open '%s'", path); - - return fd; -} - -UniqueFileDescriptor -OpenWriteOnly(const char *path, int flags) -{ - UniqueFileDescriptor fd; - if (!fd.Open(path, O_WRONLY|flags)) - throw FormatErrno("Failed to open '%s'", path); - - return fd; -} - -#ifndef _WIN32 - -UniqueFileDescriptor -OpenDirectory(const char *path, int flags) -{ - UniqueFileDescriptor fd; - if (!fd.Open(path, O_DIRECTORY|O_RDONLY|flags)) - throw FormatErrno("Failed to open '%s'", path); - - return fd; -} - -#endif - -#ifdef __linux__ - -UniqueFileDescriptor -OpenPath(const char *path, int flags) -{ - UniqueFileDescriptor fd; - if (!fd.Open(path, O_PATH|flags)) - throw FormatErrno("Failed to open '%s'", path); - - return fd; -} - -UniqueFileDescriptor -OpenPath(FileDescriptor directory, const char *name, int flags) -{ - UniqueFileDescriptor fd; - if (!fd.Open(directory, name, O_PATH|flags)) - throw FormatErrno("Failed to open '%s'", name); - - return fd; -} - -UniqueFileDescriptor -OpenReadOnly(FileDescriptor directory, const char *name, int flags) -{ - UniqueFileDescriptor fd; - if (!fd.Open(directory, name, O_RDONLY|flags)) - throw FormatErrno("Failed to open '%s'", name); - - return fd; -} - -UniqueFileDescriptor -OpenWriteOnly(FileDescriptor directory, const char *name, int flags) -{ - UniqueFileDescriptor fd; - if (!fd.Open(directory, name, O_WRONLY|flags)) - throw FormatErrno("Failed to open '%s'", name); - - return fd; -} - -UniqueFileDescriptor -OpenDirectory(FileDescriptor directory, const char *name, int flags) -{ - UniqueFileDescriptor fd; - if (!fd.Open(directory, name, O_DIRECTORY|O_RDONLY|flags)) - throw FormatErrno("Failed to open '%s'", name); - - return fd; -} - -#endif diff --git a/src/system/Open.hxx b/src/system/Open.hxx deleted file mode 100644 index 08694a3ed..000000000 --- a/src/system/Open.hxx +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2018 Max Kellermann <max.kellermann@gmail.com> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef OPEN_HXX -#define OPEN_HXX - -class FileDescriptor; -class UniqueFileDescriptor; - -UniqueFileDescriptor -OpenReadOnly(const char *path); - -UniqueFileDescriptor -OpenWriteOnly(const char *path, int flags=0); - -#ifndef _WIN32 - -UniqueFileDescriptor -OpenDirectory(const char *name, int flags=0); - -#endif - -#ifdef __linux__ - -UniqueFileDescriptor -OpenPath(const char *path, int flags=0); - -UniqueFileDescriptor -OpenPath(FileDescriptor directory, const char *name, int flags=0); - -UniqueFileDescriptor -OpenReadOnly(FileDescriptor directory, const char *name, int flags=0); - -UniqueFileDescriptor -OpenWriteOnly(FileDescriptor directory, const char *name, int flags=0); - -UniqueFileDescriptor -OpenDirectory(FileDescriptor directory, const char *name, int flags=0); - -#endif - -#endif diff --git a/src/system/SignalFD.hxx b/src/system/SignalFD.hxx index 6d45d060f..90d628aea 100644 --- a/src/system/SignalFD.hxx +++ b/src/system/SignalFD.hxx @@ -30,7 +30,7 @@ #ifndef SIGNAL_FD_HXX #define SIGNAL_FD_HXX -#include "UniqueFileDescriptor.hxx" +#include "io/UniqueFileDescriptor.hxx" #include <csignal> diff --git a/src/system/UniqueFileDescriptor.hxx b/src/system/UniqueFileDescriptor.hxx deleted file mode 100644 index 8e808b231..000000000 --- a/src/system/UniqueFileDescriptor.hxx +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2012-2018 Max Kellermann <max.kellermann@gmail.com> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef UNIQUE_FILE_DESCRIPTOR_HXX -#define UNIQUE_FILE_DESCRIPTOR_HXX - -#include "FileDescriptor.hxx" - -#include <cassert> -#include <utility> - -/** - * An OO wrapper for a UNIX file descriptor. - */ -class UniqueFileDescriptor : public FileDescriptor { -public: - UniqueFileDescriptor() noexcept - :FileDescriptor(FileDescriptor::Undefined()) {} - - explicit UniqueFileDescriptor(int _fd) noexcept - :FileDescriptor(_fd) {} - - explicit UniqueFileDescriptor(FileDescriptor _fd) noexcept - :FileDescriptor(_fd) {} - - UniqueFileDescriptor(const UniqueFileDescriptor &) = delete; - - UniqueFileDescriptor(UniqueFileDescriptor &&other) noexcept - :FileDescriptor(other.Steal()) {} - - ~UniqueFileDescriptor() noexcept { - Close(); - } - - UniqueFileDescriptor &operator=(UniqueFileDescriptor &&other) noexcept { - using std::swap; - swap(fd, other.fd); - return *this; - } - -protected: - void Set(int _fd) noexcept { - assert(!IsDefined()); - assert(_fd >= 0); - - FileDescriptor::Set(_fd); - } - -public: -#ifndef _WIN32 - static bool CreatePipe(UniqueFileDescriptor &r, UniqueFileDescriptor &w) noexcept { - return FileDescriptor::CreatePipe(r, w); - } - - static bool CreatePipeNonBlock(UniqueFileDescriptor &r, - UniqueFileDescriptor &w) noexcept { - return FileDescriptor::CreatePipeNonBlock(r, w); - } - - static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept; -#endif - - bool Close() noexcept { - return IsDefined() && FileDescriptor::Close(); - } -}; - -#endif diff --git a/src/system/meson.build b/src/system/meson.build index 4babb4274..509783463 100644 --- a/src/system/meson.build +++ b/src/system/meson.build @@ -1,7 +1,5 @@ system_sources = [ 'FatalError.cxx', - 'FileDescriptor.cxx', - 'Open.cxx', 'EventPipe.cxx', 'Clock.cxx', ] @@ -29,6 +27,7 @@ endif system_dep = declare_dependency( link_with: system, dependencies: [ + io_dep, winsock_dep, ], ) |