mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 17:47:36 +00:00
LibCore+Everywhere: Move OpenMode out of IODevice
...and make it an enum class so people don't omit "OpenMode".
This commit is contained in:
parent
422ef7904e
commit
a91a49337c
113 changed files with 180 additions and 177 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 };
|
||||
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {};
|
||||
}
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue