From 60f84f3154300ecf85f118f837d01d8f918eaa46 Mon Sep 17 00:00:00 2001 From: Timothy Date: Sun, 4 Jul 2021 14:42:51 +1000 Subject: [PATCH] LibCore: Add method to leak fd from File This will let other code use the fd while making sure the fd isn't automatically closed by Core::File's destructor --- Userland/Libraries/LibCore/File.cpp | 6 ++++++ Userland/Libraries/LibCore/File.h | 1 + 2 files changed, 7 insertions(+) diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index a8e62d05a0..0f30a995cb 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -92,6 +92,12 @@ bool File::open_impl(OpenMode mode, mode_t permissions) return true; } +int File::leak_fd() +{ + m_should_close_file_descriptor = ShouldCloseFileDescriptor::No; + return fd(); +} + bool File::is_device() const { struct stat stat; diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index 5ce8161ae8..cb81920d1d 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -74,6 +74,7 @@ public: Yes }; bool open(int fd, OpenMode, ShouldCloseFileDescriptor); + [[nodiscard]] int leak_fd(); static NonnullRefPtr standard_input(); static NonnullRefPtr standard_output();