diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-05-12 13:56:43 +0430 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-12 11:00:45 +0100 |
commit | a91a49337c5992d64b30f493eea1eb492792b667 (patch) | |
tree | b665e2c0b31a9935ca8a8f8b5b71bbadf476e32f /Userland/Libraries/LibCore | |
parent | 422ef7904e9eb6be1182421a7529ed0d5e00991f (diff) | |
download | serenity-a91a49337c5992d64b30f493eea1eb492792b667.zip |
LibCore+Everywhere: Move OpenMode out of IODevice
...and make it an enum class so people don't omit "OpenMode".
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r-- | Userland/Libraries/LibCore/Command.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/ConfigFile.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/File.cpp | 32 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/File.h | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/FileStream.h | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/IODevice.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/IODevice.h | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/LocalSocket.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/ProcessStatisticsReader.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Socket.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/TCPSocket.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/UDPSocket.cpp | 2 |
12 files changed, 56 insertions, 53 deletions
diff --git a/Userland/Libraries/LibCore/Command.cpp b/Userland/Libraries/LibCore/Command.cpp index fd3e5abd59..8466152b58 100644 --- a/Userland/Libraries/LibCore/Command.cpp +++ b/Userland/Libraries/LibCore/Command.cpp @@ -80,7 +80,7 @@ String command(const String& program, const Vector<String>& arguments, Optional< auto read_all_from_pipe = [](int pipe[2]) { auto result_file = Core::File::construct(); - if (!result_file->open(pipe[0], Core::IODevice::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) { + if (!result_file->open(pipe[0], Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) { perror("open"); VERIFY_NOT_REACHED(); } diff --git a/Userland/Libraries/LibCore/ConfigFile.cpp b/Userland/Libraries/LibCore/ConfigFile.cpp index 5bbf8fae21..e856998b63 100644 --- a/Userland/Libraries/LibCore/ConfigFile.cpp +++ b/Userland/Libraries/LibCore/ConfigFile.cpp @@ -57,7 +57,7 @@ void ConfigFile::reparse() m_groups.clear(); auto file = File::construct(m_filename); - if (!file->open(IODevice::OpenMode::ReadOnly)) + if (!file->open(OpenMode::ReadOnly)) return; HashMap<String, String>* current_group = nullptr; diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index bd3d417d9e..90d0d24133 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -26,7 +26,7 @@ namespace Core { -Result<NonnullRefPtr<File>, String> File::open(String filename, IODevice::OpenMode mode, mode_t permissions) +Result<NonnullRefPtr<File>, String> File::open(String filename, OpenMode mode, mode_t permissions) { auto file = File::construct(move(filename)); if (!file->open_impl(mode, permissions)) @@ -42,11 +42,11 @@ File::File(String filename, Object* parent) File::~File() { - if (m_should_close_file_descriptor == ShouldCloseFileDescriptor::Yes && mode() != NotOpen) + if (m_should_close_file_descriptor == ShouldCloseFileDescriptor::Yes && mode() != OpenMode::NotOpen) close(); } -bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescriptor should_close) +bool File::open(int fd, OpenMode mode, ShouldCloseFileDescriptor should_close) { set_fd(fd); set_mode(mode); @@ -54,30 +54,30 @@ bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescriptor shoul return true; } -bool File::open(IODevice::OpenMode mode) +bool File::open(OpenMode mode) { return open_impl(mode, 0666); } -bool File::open_impl(IODevice::OpenMode mode, mode_t permissions) +bool File::open_impl(OpenMode mode, mode_t permissions) { VERIFY(!m_filename.is_null()); int flags = 0; - if ((mode & IODevice::ReadWrite) == IODevice::ReadWrite) { + if (has_flag(mode, OpenMode::ReadWrite)) { flags |= O_RDWR | O_CREAT; - } else if (mode & IODevice::ReadOnly) { + } else if (has_flag(mode, OpenMode::ReadOnly)) { flags |= O_RDONLY; - } else if (mode & IODevice::WriteOnly) { + } else if (has_flag(mode, OpenMode::WriteOnly)) { flags |= O_WRONLY | O_CREAT; - bool should_truncate = !((mode & IODevice::Append) || (mode & IODevice::MustBeNew)); + bool should_truncate = !(has_flag(mode, OpenMode::Append) || has_flag(mode, OpenMode::MustBeNew)); if (should_truncate) flags |= O_TRUNC; } - if (mode & IODevice::Append) + if (has_flag(mode, OpenMode::Append)) flags |= O_APPEND; - if (mode & IODevice::Truncate) + if (has_flag(mode, OpenMode::Truncate)) flags |= O_TRUNC; - if (mode & IODevice::MustBeNew) + if (has_flag(mode, OpenMode::MustBeNew)) flags |= O_EXCL; int fd = ::open(m_filename.characters(), flags, permissions); if (fd < 0) { @@ -238,7 +238,7 @@ NonnullRefPtr<File> File::standard_input() { if (!stdin_file) { stdin_file = File::construct(); - stdin_file->open(STDIN_FILENO, IODevice::ReadOnly, ShouldCloseFileDescriptor::No); + stdin_file->open(STDIN_FILENO, OpenMode::ReadOnly, ShouldCloseFileDescriptor::No); } return *stdin_file; } @@ -247,7 +247,7 @@ NonnullRefPtr<File> File::standard_output() { if (!stdout_file) { stdout_file = File::construct(); - stdout_file->open(STDOUT_FILENO, IODevice::WriteOnly, ShouldCloseFileDescriptor::No); + stdout_file->open(STDOUT_FILENO, OpenMode::WriteOnly, ShouldCloseFileDescriptor::No); } return *stdout_file; } @@ -256,7 +256,7 @@ NonnullRefPtr<File> File::standard_error() { if (!stderr_file) { stderr_file = File::construct(); - stderr_file->open(STDERR_FILENO, IODevice::WriteOnly, ShouldCloseFileDescriptor::No); + stderr_file->open(STDERR_FILENO, OpenMode::WriteOnly, ShouldCloseFileDescriptor::No); } return *stderr_file; } @@ -297,7 +297,7 @@ Result<void, File::CopyError> File::copy_file_or_directory(const String& dst_pat } } - auto source_or_error = File::open(src_path, IODevice::ReadOnly); + auto source_or_error = File::open(src_path, OpenMode::ReadOnly); if (source_or_error.is_error()) return CopyError { OSError(errno), false }; diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index 0a56255ef9..5ce8161ae8 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -19,7 +19,7 @@ class File final : public IODevice { public: virtual ~File() override; - static Result<NonnullRefPtr<File>, String> open(String filename, IODevice::OpenMode, mode_t = 0644); + static Result<NonnullRefPtr<File>, String> open(String filename, OpenMode, mode_t = 0644); String filename() const { return m_filename; } void set_filename(const String filename) { m_filename = move(filename); } @@ -67,13 +67,13 @@ public: }; static Result<void, RemoveError> remove(const String& path, RecursionMode, bool force); - virtual bool open(IODevice::OpenMode) override; + virtual bool open(OpenMode) override; enum class ShouldCloseFileDescriptor { No = 0, Yes }; - bool open(int fd, IODevice::OpenMode, ShouldCloseFileDescriptor); + bool open(int fd, OpenMode, ShouldCloseFileDescriptor); static NonnullRefPtr<File> standard_input(); static NonnullRefPtr<File> standard_output(); @@ -86,7 +86,7 @@ private: } explicit File(String filename, Object* parent = nullptr); - bool open_impl(IODevice::OpenMode, mode_t); + bool open_impl(OpenMode, mode_t); String m_filename; ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes }; diff --git a/Userland/Libraries/LibCore/FileStream.h b/Userland/Libraries/LibCore/FileStream.h index 7270f33280..e7bdde05e5 100644 --- a/Userland/Libraries/LibCore/FileStream.h +++ b/Userland/Libraries/LibCore/FileStream.h @@ -20,9 +20,9 @@ public: { } - static Result<InputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644) + static Result<InputFileStream, String> open(StringView filename, OpenMode mode = OpenMode::ReadOnly, mode_t permissions = 0644) { - VERIFY((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); + VERIFY(has_flag(mode, OpenMode::ReadOnly)); auto file_result = File::open(filename, mode, permissions); @@ -32,9 +32,9 @@ public: return InputFileStream { file_result.value() }; } - static Result<Buffered<InputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644) + static Result<Buffered<InputFileStream>, String> open_buffered(StringView filename, OpenMode mode = OpenMode::ReadOnly, mode_t permissions = 0644) { - VERIFY((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); + VERIFY(has_flag(mode, OpenMode::ReadOnly)); auto file_result = File::open(filename, mode, permissions); @@ -84,9 +84,9 @@ public: { } - static Result<OutputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644) + static Result<OutputFileStream, String> open(StringView filename, OpenMode mode = OpenMode::WriteOnly, mode_t permissions = 0644) { - VERIFY((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); + VERIFY(has_flag(mode, OpenMode::WriteOnly)); auto file_result = File::open(filename, mode, permissions); @@ -96,9 +96,9 @@ public: return OutputFileStream { file_result.value() }; } - static Result<Buffered<OutputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644) + static Result<Buffered<OutputFileStream>, String> open_buffered(StringView filename, OpenMode mode = OpenMode::WriteOnly, mode_t permissions = 0644) { - VERIFY((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite); + VERIFY(has_flag(mode, OpenMode::WriteOnly)); auto file_result = File::open(filename, mode, permissions); diff --git a/Userland/Libraries/LibCore/IODevice.cpp b/Userland/Libraries/LibCore/IODevice.cpp index 8d7c837c44..cde5c55548 100644 --- a/Userland/Libraries/LibCore/IODevice.cpp +++ b/Userland/Libraries/LibCore/IODevice.cpp @@ -209,7 +209,7 @@ bool IODevice::populate_read_buffer() const bool IODevice::close() { - if (fd() < 0 || mode() == NotOpen) + if (fd() < 0 || m_mode == OpenMode::NotOpen) return false; int rc = ::close(fd()); if (rc < 0) { @@ -217,7 +217,7 @@ bool IODevice::close() return false; } set_fd(-1); - set_mode(IODevice::NotOpen); + set_mode(OpenMode::NotOpen); return true; } diff --git a/Userland/Libraries/LibCore/IODevice.h b/Userland/Libraries/LibCore/IODevice.h index a61302b0a2..eb3805d80b 100644 --- a/Userland/Libraries/LibCore/IODevice.h +++ b/Userland/Libraries/LibCore/IODevice.h @@ -6,6 +6,7 @@ #pragma once +#include <AK/EnumBits.h> #include <AK/Forward.h> #include <LibCore/Object.h> @@ -33,24 +34,26 @@ private: String m_buffer; }; +enum class OpenMode : unsigned { + NotOpen = 0, + ReadOnly = 1, + WriteOnly = 2, + ReadWrite = 3, + Append = 4, + Truncate = 8, + MustBeNew = 16, +}; + +AK_ENUM_BITWISE_OPERATORS(OpenMode) + class IODevice : public Object { C_OBJECT_ABSTRACT(IODevice) public: - enum OpenMode { - NotOpen = 0, - ReadOnly = 1, - WriteOnly = 2, - ReadWrite = 3, - Append = 4, - Truncate = 8, - MustBeNew = 16, - }; - virtual ~IODevice() override; int fd() const { return m_fd; } - unsigned mode() const { return m_mode; } - bool is_open() const { return m_mode != NotOpen; } + OpenMode mode() const { return m_mode; } + bool is_open() const { return m_mode != OpenMode::NotOpen; } bool eof() const { return m_eof; } int error() const { return m_error; } @@ -81,7 +84,7 @@ public: bool seek(i64, SeekMode = SeekMode::SetPosition, off_t* = nullptr); - virtual bool open(IODevice::OpenMode) = 0; + virtual bool open(OpenMode) = 0; virtual bool close(); LineIterator line_begin() & { return LineIterator(*this); } @@ -102,7 +105,7 @@ private: bool can_read_from_fd() const; int m_fd { -1 }; - OpenMode m_mode { NotOpen }; + OpenMode m_mode { OpenMode::NotOpen }; mutable int m_error { 0 }; mutable bool m_eof { false }; mutable Vector<u8> m_buffered_data; diff --git a/Userland/Libraries/LibCore/LocalSocket.cpp b/Userland/Libraries/LibCore/LocalSocket.cpp index 58f43a367a..27034e6c41 100644 --- a/Userland/Libraries/LibCore/LocalSocket.cpp +++ b/Userland/Libraries/LibCore/LocalSocket.cpp @@ -24,7 +24,7 @@ LocalSocket::LocalSocket(int fd, Object* parent) // NOTE: This constructor is used by LocalServer::accept(), so the socket is already connected. m_connected = true; set_fd(fd); - set_mode(IODevice::ReadWrite); + set_mode(OpenMode::ReadWrite); set_error(0); } @@ -44,7 +44,7 @@ LocalSocket::LocalSocket(Object* parent) set_error(errno); } else { set_fd(fd); - set_mode(IODevice::ReadWrite); + set_mode(OpenMode::ReadWrite); set_error(0); } } diff --git a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp index f92fc8628a..9c7b679996 100644 --- a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp +++ b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp @@ -26,7 +26,7 @@ Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_a } } else { proc_all_file = Core::File::construct("/proc/all"); - if (!proc_all_file->open(Core::IODevice::ReadOnly)) { + if (!proc_all_file->open(Core::OpenMode::ReadOnly)) { fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", proc_all_file->error_string()); return {}; } diff --git a/Userland/Libraries/LibCore/Socket.h b/Userland/Libraries/LibCore/Socket.h index 016ed427e2..58823b7fbd 100644 --- a/Userland/Libraries/LibCore/Socket.h +++ b/Userland/Libraries/LibCore/Socket.h @@ -58,7 +58,7 @@ protected: virtual bool common_connect(const struct sockaddr*, socklen_t); private: - virtual bool open(IODevice::OpenMode) override { VERIFY_NOT_REACHED(); } + virtual bool open(OpenMode) override { VERIFY_NOT_REACHED(); } void ensure_read_notifier(); Type m_type { Type::Invalid }; diff --git a/Userland/Libraries/LibCore/TCPSocket.cpp b/Userland/Libraries/LibCore/TCPSocket.cpp index 7eab7f7834..e2838750aa 100644 --- a/Userland/Libraries/LibCore/TCPSocket.cpp +++ b/Userland/Libraries/LibCore/TCPSocket.cpp @@ -20,7 +20,7 @@ TCPSocket::TCPSocket(int fd, Object* parent) // NOTE: This constructor is used by TCPServer::accept(), so the socket is already connected. m_connected = true; set_fd(fd); - set_mode(IODevice::ReadWrite); + set_mode(OpenMode::ReadWrite); set_error(0); } @@ -38,7 +38,7 @@ TCPSocket::TCPSocket(Object* parent) set_error(errno); } else { set_fd(fd); - set_mode(IODevice::ReadWrite); + set_mode(OpenMode::ReadWrite); set_error(0); } } diff --git a/Userland/Libraries/LibCore/UDPSocket.cpp b/Userland/Libraries/LibCore/UDPSocket.cpp index 41d977b49c..3898739c0f 100644 --- a/Userland/Libraries/LibCore/UDPSocket.cpp +++ b/Userland/Libraries/LibCore/UDPSocket.cpp @@ -29,7 +29,7 @@ UDPSocket::UDPSocket(Object* parent) set_error(errno); } else { set_fd(fd); - set_mode(IODevice::ReadWrite); + set_mode(OpenMode::ReadWrite); set_error(0); } } |