From ebfc83dac5eb5d82f26a7e6123260f201db17187 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@ionos.com>
Date: Thu, 29 Aug 2024 08:52:50 +0200
Subject: [PATCH] io/FileDescriptor: add method SetPipeCapacity()

---
 src/io/FileDescriptor.cxx | 15 +++++++++++++++
 src/io/FileDescriptor.hxx |  9 +++++++++
 2 files changed, 24 insertions(+)

diff --git a/src/io/FileDescriptor.cxx b/src/io/FileDescriptor.cxx
index 45c740ee2..08c38ca6c 100644
--- a/src/io/FileDescriptor.cxx
+++ b/src/io/FileDescriptor.cxx
@@ -213,6 +213,21 @@ FileDescriptor::DisableCloseOnExec() const noexcept
 	fcntl(fd, F_SETFD, old_flags & ~FD_CLOEXEC);
 }
 
+#ifdef __linux__
+
+void
+FileDescriptor::SetPipeCapacity(unsigned capacity) const noexcept
+{
+	/* the canonical type for buffer sizes is "size_t", but since
+           F_SETPIPE_SZ expects an "int" parameter, "size_t" would
+           have the wrong size; "unsigned" is always the same size as
+           "int", but using a signed integer would suggest that
+           negative values are okay when they're not */
+	fcntl(fd, F_SETPIPE_SZ, capacity);
+}
+
+#endif
+
 UniqueFileDescriptor
 FileDescriptor::Duplicate() const noexcept
 {
diff --git a/src/io/FileDescriptor.hxx b/src/io/FileDescriptor.hxx
index edd009440..a77488a4d 100644
--- a/src/io/FileDescriptor.hxx
+++ b/src/io/FileDescriptor.hxx
@@ -163,6 +163,15 @@ public:
 	 */
 	void DisableCloseOnExec() const noexcept;
 
+#ifdef __linux__
+	/**
+	 * Set the capacity of the pipe.
+	 *
+	 * This method ignores errors.
+	 */
+	void SetPipeCapacity(unsigned capacity) const noexcept;
+#endif
+
 	/**
 	 * Duplicate this file descriptor.
 	 *