diff options
-rw-r--r-- | Userland/Libraries/LibCore/Stream.cpp | 29 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Stream.h | 2 |
2 files changed, 19 insertions, 12 deletions
diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index 5731580ca0..74fe90245f 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -117,32 +117,37 @@ ErrorOr<NonnullOwnPtr<File>> File::adopt_fd(int fd, OpenMode mode) return file; } -ErrorOr<void> File::open_path(StringView filename, mode_t permissions) +int File::open_mode_to_options(OpenMode mode) { - VERIFY(m_fd == -1); - int flags = 0; - if (has_flag(m_mode, OpenMode::ReadWrite)) { + if (has_flag(mode, OpenMode::ReadWrite)) { flags |= O_RDWR | O_CREAT; - } else if (has_flag(m_mode, OpenMode::Read)) { + } else if (has_flag(mode, OpenMode::Read)) { flags |= O_RDONLY; - } else if (has_flag(m_mode, OpenMode::Write)) { + } else if (has_flag(mode, OpenMode::Write)) { flags |= O_WRONLY | O_CREAT; - bool should_truncate = !has_any_flag(m_mode, OpenMode::Append | OpenMode::MustBeNew); + bool should_truncate = !has_any_flag(mode, OpenMode::Append | OpenMode::MustBeNew); if (should_truncate) flags |= O_TRUNC; } - if (has_flag(m_mode, OpenMode::Append)) + if (has_flag(mode, OpenMode::Append)) flags |= O_APPEND; - if (has_flag(m_mode, OpenMode::Truncate)) + if (has_flag(mode, OpenMode::Truncate)) flags |= O_TRUNC; - if (has_flag(m_mode, OpenMode::MustBeNew)) + if (has_flag(mode, OpenMode::MustBeNew)) flags |= O_EXCL; - if (!has_flag(m_mode, OpenMode::KeepOnExec)) + if (!has_flag(mode, OpenMode::KeepOnExec)) flags |= O_CLOEXEC; - if (!has_flag(m_mode, OpenMode::Nonblocking)) + if (!has_flag(mode, OpenMode::Nonblocking)) flags |= O_NONBLOCK; + return flags; +} + +ErrorOr<void> File::open_path(StringView filename, mode_t permissions) +{ + VERIFY(m_fd == -1); + auto flags = open_mode_to_options(m_mode); m_fd = TRY(System::open(filename.characters_without_null_termination(), flags, permissions)); return {}; diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index 4eb13eb64e..8a6ad35369 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -205,6 +205,8 @@ public: virtual ~File() override { close(); } + static int open_mode_to_options(OpenMode mode); + private: File(OpenMode mode) : m_mode(mode) |