diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index 7ce5bb42dc..20fb0a33e9 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -182,4 +182,13 @@ ErrorOr File::truncate(size_t length) return System::ftruncate(m_fd, length); } +ErrorOr File::set_blocking(bool enabled) +{ + // NOTE: This works fine on Serenity, but some systems out there don't support changing the blocking state of certain POSIX objects (message queues, pipes, etc) after their creation. + // Therefore, this method shouldn't be used in Lagom. + // https://github.com/SerenityOS/serenity/pull/18965#discussion_r1207951840 + int value = enabled ? 0 : 1; + return System::ioctl(fd(), FIONBIO, &value); +} + } diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index 303730b103..2773ef910a 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -68,6 +68,13 @@ public: virtual ErrorOr seek(i64 offset, SeekMode) override; virtual ErrorOr truncate(size_t length) override; + // Sets the blocking mode of the file. If blocking mode is disabled, reads + // will fail with EAGAIN when there's no data available to read, and writes + // will fail with EAGAIN when the data cannot be written without blocking + // (due to the send buffer being full, for example). + // See also Socket::set_blocking. + ErrorOr set_blocking(bool enabled); + template VIP> int leak_fd(Badge) {