diff src/core/filesystem.cc @ 413:192da585a0a8 default tip

*: fix formatting the clang-format file was broken andjust using LLVM defaults. now it's fixed and is more in line with what I actually prefer my code to look like -- esp. with regards to tabulators
author Paper <paper@tflc.us>
date Sat, 25 Jul 2026 14:54:32 -0400
parents 8d06825d96d1
children
line wrap: on
line diff
--- a/src/core/filesystem.cc	Sat Jul 25 14:33:03 2026 -0400
+++ b/src/core/filesystem.cc	Sat Jul 25 14:54:32 2026 -0400
@@ -12,8 +12,8 @@
 #elif defined(HAVE_INOTIFY)
 /* ehhhh */
 # include <fcntl.h>
+# include <sys/inotify.h>
 # include <unistd.h>
-# include <sys/inotify.h>
 #elif defined(__MACH__) && defined(__APPLE__)
 # include <CoreFoundation/CoreFoundation.h>
 # include <CoreServices/CoreServices.h>
@@ -80,7 +80,7 @@
 class Watcher : public IWatcher {
 public:
 	Watcher(void *opaque, const std::filesystem::path &path, EventHandler handler)
-	    : path_(path), handler_(handler), opaque_(opaque)
+		: path_(path), handler_(handler), opaque_(opaque)
 	{
 	}
 
@@ -107,7 +107,7 @@
 resort to old-fashioned recursion.  --paper
 */
 static void IterateDirectory(const std::filesystem::path &path, bool recursive,
-                             const std::function<void(const std::filesystem::path &path)> &func)
+	const std::function<void(const std::filesystem::path &path)> &func)
 {
 	std::error_code ec;
 	static const std::filesystem::directory_iterator end;
@@ -130,7 +130,7 @@
 class StdFilesystemWatcher : public Watcher {
 public:
 	StdFilesystemWatcher(void *opaque, const std::filesystem::path &path, EventHandler handler, bool recursive)
-	    : Watcher(opaque, path, handler), recursive_(recursive)
+		: Watcher(opaque, path, handler), recursive_(recursive)
 	{
 	}
 
@@ -205,7 +205,7 @@
 class Win32Watcher : public StdFilesystemWatcher {
 public:
 	Win32Watcher(void *opaque, const std::filesystem::path &path, IWatcher::EventHandler handler, bool recursive)
-	    : StdFilesystemWatcher(opaque, path, handler, recursive), dirwatcher_(INVALID_HANDLE_VALUE), first_(true)
+		: StdFilesystemWatcher(opaque, path, handler, recursive), dirwatcher_(INVALID_HANDLE_VALUE), first_(true)
 	{
 	}
 
@@ -239,8 +239,8 @@
 protected:
 	bool TryCreateDirWatcher()
 	{
-		dirwatcher_ = FindFirstChangeNotificationW(Watcher::path_.wstring().c_str(), recursive_,
-		                                           FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME);
+		dirwatcher_ = FindFirstChangeNotificationW(
+			Watcher::path_.wstring().c_str(), recursive_, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME);
 
 		return (dirwatcher_ != INVALID_HANDLE_VALUE);
 	}
@@ -258,7 +258,7 @@
 class Win32WatcherVista : public Win32Watcher {
 public:
 	Win32WatcherVista(void *opaque, const std::filesystem::path &path, IWatcher::EventHandler handler, bool recursive)
-	    : Win32Watcher(opaque, path, handler, recursive), dirhandle_(INVALID_HANDLE_VALUE)
+		: Win32Watcher(opaque, path, handler, recursive), dirhandle_(INVALID_HANDLE_VALUE)
 	{
 		ZeroMemory(&overlapped_, sizeof(overlapped_));
 		overlapped_.hEvent = nullptr;
@@ -285,7 +285,7 @@
 				 * directory tree into memory. Instead, iterate through the directory
 				 * ourselves. */
 				IterateDirectory(path_, recursive_,
-				                 [this](const std::filesystem::path &p) { handler_(opaque_, p, Event::Created); });
+					[this](const std::filesystem::path &p) { handler_(opaque_, p, Event::Created); });
 			} else {
 				/* Uh oh; we might have to fall back to Win32Watcher. Call into it to
 				 * load the tree into memory. */
@@ -353,8 +353,8 @@
 
 		if (dirhandle_ == INVALID_HANDLE_VALUE) {
 			dirhandle_ =
-			    CreateFileW(Watcher::path_.wstring().c_str(), FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE,
-			                nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr);
+				CreateFileW(Watcher::path_.wstring().c_str(), FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE,
+					nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr);
 			if (dirhandle_ == INVALID_HANDLE_VALUE)
 				return false;
 		}
@@ -366,8 +366,7 @@
 	bool QueueDirectoryRead()
 	{
 		return ReadDirectoryChangesW(dirhandle_, change_buf_, sizeof(change_buf_), TRUE,
-		                             FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME, nullptr, &overlapped_,
-		                             nullptr);
+			FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME, nullptr, &overlapped_, nullptr);
 	}
 
 	HANDLE dirhandle_;
@@ -381,11 +380,8 @@
 class InotifyWatcher : public StdFilesystemWatcher {
 public:
 	InotifyWatcher(void *opaque, const std::filesystem::path &path, IWatcher::EventHandler handler, bool recursive)
-	    : StdFilesystemWatcher(opaque, path, handler, recursive)
-	    , first_(true)
-	    , giveup_(false)
-	    , ev_(nullptr)
-	    , ev_size_(0)
+		: StdFilesystemWatcher(opaque, path, handler, recursive), first_(true), giveup_(false), ev_(nullptr),
+		  ev_size_(0)
 	{
 	}
 
@@ -467,14 +463,14 @@
 					continue;
 				}
 
-				if (ev->mask & (IN_MOVE|IN_CREATE|IN_DELETE)) {
+				if (ev->mask & (IN_MOVE | IN_CREATE | IN_DELETE)) {
 					std::filesystem::path p = wds_[ev->wd] / ev->name;
 
-					if (ev->mask & (IN_MOVED_TO|IN_CREATE)) {
+					if (ev->mask & (IN_MOVED_TO | IN_CREATE)) {
 						if (std::filesystem::is_directory(p))
 							AddWatchDescriptor(p);
 						handler_(opaque_, p, Event::Created);
-					} else if (ev->mask & (IN_MOVED_FROM|IN_DELETE)) {
+					} else if (ev->mask & (IN_MOVED_FROM | IN_DELETE)) {
 
 						handler_(opaque_, wds_[ev->wd] / ev->name, Event::Deleted);
 					}
@@ -490,7 +486,7 @@
 	 * but has a function for toggling non-blocking */
 	struct FileDescriptor {
 	public:
-		FileDescriptor() : fd_(-1) { }
+		FileDescriptor() : fd_(-1) {}
 		~FileDescriptor() { reset(); }
 
 		int get() { return fd_; }
@@ -536,19 +532,19 @@
 			return false;
 
 		if (!fd_) {
-#ifdef HAVE_INOTIFY_INIT1
+# ifdef HAVE_INOTIFY_INIT1
 			fd_.reset(inotify_init1(IN_NONBLOCK));
-#else
+# else
 			fd_.reset(inotify_init());
-#endif
+# endif
 			if (!fd_)
 				return false;
 
-#ifndef HAVE_INOTIFY_INIT1
+# ifndef HAVE_INOTIFY_INIT1
 			/* Very old linux */
 			if (!fd_.SetNonBlocking(true))
 				return false;
-#endif
+# endif
 		}
 
 		return !!fd_;
@@ -559,7 +555,8 @@
 		if (!fd_ || giveup_)
 			return false;
 
-		int wd = inotify_add_watch(fd_.get(), p.string().c_str(), IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE_SELF);
+		int wd = inotify_add_watch(
+			fd_.get(), p.string().c_str(), IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE_SELF);
 		if (wd < 0) {
 			/* Don't even try to watch any more */
 			giveup_ = true;
@@ -596,8 +593,7 @@
 class FSEventsWatcher : public StdFilesystemWatcher {
 public:
 	FSEventsWatcher(void *opaque, const std::filesystem::path &path, IWatcher::EventHandler handler, bool recursive)
-		: StdFilesystemWatcher(opaque, path, handler, recursive)
-		, first_(true)
+		: StdFilesystemWatcher(opaque, path, handler, recursive), first_(true)
 	{
 		FSEventStreamContext ctx;
 
@@ -608,9 +604,11 @@
 		ctx.version = 0;
 
 		CFStringRef str = Strings::ToCFString(path.u8string());
-		CFArrayRef arr = CFArrayCreate(kCFAllocatorDefault, reinterpret_cast<const void **>(&str), 1, &kCFTypeArrayCallBacks);
+		CFArrayRef arr =
+			CFArrayCreate(kCFAllocatorDefault, reinterpret_cast<const void **>(&str), 1, &kCFTypeArrayCallBacks);
 
-		stream_ = FSEventStreamCreate(kCFAllocatorDefault, callback_static, &ctx, arr, kFSEventStreamEventIdSinceNow, 0.5, 0);
+		stream_ =
+			FSEventStreamCreate(kCFAllocatorDefault, callback_static, &ctx, arr, kFSEventStreamEventIdSinceNow, 0.5, 0);
 
 		// kill these off now
 		CFRelease(str);
@@ -652,11 +650,12 @@
 	}
 
 private:
-	void callback(ConstFSEventStreamRef streamRef, std::size_t numEvents, void *eventPaths, const FSEventStreamEventFlags *eventFlags, const FSEventStreamEventId *eventIds)
+	void callback(ConstFSEventStreamRef streamRef, std::size_t numEvents, void *eventPaths,
+		const FSEventStreamEventFlags *eventFlags, const FSEventStreamEventId *eventIds)
 	{
 		// assert(streamRef == stream_);
 
-#if 0
+# if 0
 		for (std::size_t i = 0; i < numEvents; i++) {
 			if ((eventFlags[i] == 0) || (eventFlags[i] & (kFSEventStreamEventFlagItemCreated|kFSEventStreamEventFlagItemRemoved|kFSEventStreamEventFlagMustScanSubDirs))) {
 				EventInfo ev;
@@ -679,16 +678,18 @@
 				}
 			}
 		}
-#else
+# else
 		/* I only evr get eventFlags[i] == 0 so I think this is
 		 * probably the best way ??? */
 		rescan_.push_back(Watcher::path_);
-#endif
+# endif
 	}
 
-	static void callback_static(ConstFSEventStreamRef streamRef, void *clientCallBackInfo, std::size_t numEvents, void *eventPaths, const FSEventStreamEventFlags *eventFlags, const FSEventStreamEventId *eventIds)
+	static void callback_static(ConstFSEventStreamRef streamRef, void *clientCallBackInfo, std::size_t numEvents,
+		void *eventPaths, const FSEventStreamEventFlags *eventFlags, const FSEventStreamEventId *eventIds)
 	{
-		reinterpret_cast<FSEventsWatcher *>(clientCallBackInfo)->callback(streamRef, numEvents, eventPaths, eventFlags, eventIds);
+		reinterpret_cast<FSEventsWatcher *>(clientCallBackInfo)
+			->callback(streamRef, numEvents, eventPaths, eventFlags, eventIds);
 	}
 
 	void runloop_thread()
@@ -707,10 +708,7 @@
 		// will handle everything from here
 	}
 
-	static void runloop_thread_static(FSEventsWatcher *e)
-	{
-		e->runloop_thread();
-	}
+	static void runloop_thread_static(FSEventsWatcher *e) { e->runloop_thread(); }
 
 	struct EventInfo {
 		enum Event type;