mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 07:07:34 +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
|
@ -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 };
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue