From 2344bb6c5767bba3c4ab8f2097972acd0f489e33 Mon Sep 17 00:00:00 2001 From: Caoimhe Date: Fri, 2 Jun 2023 23:30:32 +0100 Subject: [PATCH] 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. --- Userland/Libraries/LibCore/File.cpp | 7 +++++++ Userland/Libraries/LibCore/File.h | 1 + 2 files changed, 8 insertions(+) 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 {