diff options
author | Caoimhe <caoimhebyrne06@gmail.com> | 2023-06-02 23:30:32 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-06-03 05:52:16 +0200 |
commit | 2344bb6c5767bba3c4ab8f2097972acd0f489e33 (patch) | |
tree | 2531ff381b23194abb46d0b441cf6d98a48b83b1 | |
parent | 9e6d91032eceb987e32ceb4860e7a34023ac1807 (diff) | |
download | serenity-2344bb6c5767bba3c4ab8f2097972acd0f489e33.zip |
LibCore: Add `File::OpenMode::DontCreate`
Some applications may not want to have the ability to create a
file if it doesn't exist, but still be able to read and write
from it. The easy solution here would be just to not apply
O_CREAT when creating the flags, but to prevent breaking a ton
of applications, having a `DontCreate` mode is the best for now.
-rw-r--r-- | Userland/Libraries/LibCore/File.cpp | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/File.h | 1 |
2 files changed, 8 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index 20fb0a33e9..2d9cec95cd 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -87,6 +87,13 @@ int File::open_mode_to_options(OpenMode mode) flags |= O_CLOEXEC; if (!has_flag(mode, OpenMode::Nonblocking)) flags |= O_NONBLOCK; + + // Some open modes, like `ReadWrite` imply the ability to create the file if it doesn't exist. + // Certain applications may not want this privledge, and for compability reasons, this is + // the easiest way to add this option. + if (has_flag(mode, OpenMode::DontCreate)) + flags &= ~O_CREAT; + return flags; } diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index 2773ef910a..c6de26d9c4 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -31,6 +31,7 @@ public: MustBeNew = 16, KeepOnExec = 32, Nonblocking = 64, + DontCreate = 128, }; enum class ShouldCloseFileDescriptor { |