From 7c0b561a16a9e115ae0da679a2e27066aae8ce8a Mon Sep 17 00:00:00 2001
From: Max Kellermann <mk@cm4all.com>
Date: Fri, 13 Oct 2023 18:29:16 +0200
Subject: [PATCH] event/InotifyEvent: add method TryAddWatch()

---
 src/event/InotifyEvent.cxx | 10 ++++++++--
 src/event/InotifyEvent.hxx |  6 ++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/event/InotifyEvent.cxx b/src/event/InotifyEvent.cxx
index 643190159..80db3577b 100644
--- a/src/event/InotifyEvent.cxx
+++ b/src/event/InotifyEvent.cxx
@@ -34,11 +34,17 @@ InotifyEvent::~InotifyEvent() noexcept
 	Close();
 }
 
+int
+InotifyEvent::TryAddWatch(const char *pathname, uint32_t mask) noexcept
+{
+	return inotify_add_watch(event.GetFileDescriptor().Get(),
+				 pathname, mask);
+}
+
 int
 InotifyEvent::AddWatch(const char *pathname, uint32_t mask)
 {
-	int wd = inotify_add_watch(event.GetFileDescriptor().Get(),
-				   pathname, mask);
+	int wd = TryAddWatch(pathname, mask);
 	if (wd < 0)
 		throw FmtErrno("inotify_add_watch('{}') failed", pathname);
 
diff --git a/src/event/InotifyEvent.hxx b/src/event/InotifyEvent.hxx
index 5c1d634e2..ee1354957 100644
--- a/src/event/InotifyEvent.hxx
+++ b/src/event/InotifyEvent.hxx
@@ -84,6 +84,12 @@ public:
 	 */
 	int AddWatch(const char *pathname, uint32_t mask);
 
+	/**
+	 * Like AddWatch(), but returns -1 instead of throwing on
+	 * error.
+	 */
+	int TryAddWatch(const char *pathname, uint32_t mask) noexcept;
+
 	/**
 	 * Wrapper for AddWatch(pathname, IN_MODIFY).
 	 */