Sane C++ Libraries
C++ Platform Abstraction Libraries
FileSystemWatcher.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Async/Async.h" // AsyncLoopWakeUp
6#include "../Containers/IntrusiveDoubleLinkedList.h"
7#include "../Foundation/Function.h"
8#include "../Foundation/OpaqueObject.h"
9#include "../Foundation/Result.h"
10#include "../Strings/SmallString.h"
11#include "../Threading/Threading.h" // EventObject
12
13namespace SC
14{
15struct FileSystemWatcher;
16struct String;
17} // namespace SC
18
21
24
45{
46 private:
47 struct Internal;
48 struct ThreadRunnerInternal;
49 struct ThreadRunnerDefinition
50 {
51 static constexpr int MaxWatchablePaths = 1024;
52 static constexpr int Windows =
53 (2 * MaxWatchablePaths) * sizeof(void*) + sizeof(uint64_t) + sizeof(Thread) + sizeof(Action);
54 static constexpr int Apple = sizeof(void*);
55 static constexpr int Linux = sizeof(Thread) + sizeof(void*) * 2;
56 static constexpr int Default = Linux;
57
58 static constexpr size_t Alignment = alignof(void*);
59
60 using Object = ThreadRunnerInternal;
61 };
62
63 struct FolderWatcherInternal;
64 struct FolderWatcherSizes
65 {
66 static constexpr int MaxNumberOfSubdirs = 128; // Max number of subfolders tracked in a watcher
67 static constexpr int MaxChangesBufferSize = 1024;
68 static constexpr int Windows =
69 MaxChangesBufferSize + sizeof(void*) + sizeof(FileDescriptor) + sizeof(AsyncFilePoll);
70 static constexpr int Apple = sizeof(void*);
71 static constexpr int Linux = sizeof(Array<int64_t, MaxNumberOfSubdirs>) + sizeof(Vector<char>) + sizeof(void*);
72 static constexpr int Default = Linux;
73
74 static constexpr size_t Alignment = alignof(void*);
75
76 using Object = FolderWatcherInternal;
77 };
78 struct InternalDefinition
79 {
80 static constexpr int Windows = 3 * sizeof(void*);
81 static constexpr int Apple = 43 * sizeof(void*) + sizeof(Mutex);
82 static constexpr int Linux = sizeof(void*) * 4;
83 static constexpr int Default = Linux;
84
85 static constexpr size_t Alignment = alignof(void*);
86
87 using Object = Internal;
88 };
89
91
92 InternalOpaque internal;
93
94 public:
97 enum class Operation
98 {
99 Modified,
101 };
102
105 {
109
114 SC::Result getFullPath(String& bufferString, StringView& outFullPath) const;
115
116 private:
117 friend struct Internal;
118#if SC_PLATFORM_APPLE
119 StringView fullPath;
120#endif
121 };
122
127 {
129
133
134 private:
135 friend struct FileSystemWatcher;
137 FileSystemWatcher* parent = nullptr;
138 FolderWatcher* next = nullptr;
139 FolderWatcher* prev = nullptr;
141
143 };
144
147 {
148
149 private:
150 friend struct FileSystemWatcher;
151 AsyncEventLoop* eventLoop = nullptr;
152#if SC_PLATFORM_APPLE
153 AsyncLoopWakeUp eventLoopAsync = {};
154 EventObject eventObject = {};
155#elif SC_PLATFORM_LINUX
156 AsyncFilePoll asyncPoll = {};
157#endif
158 };
159
162
166 [[nodiscard]] Result init(ThreadRunner& runner);
167
172 [[nodiscard]] Result init(EventLoopRunner& runner, AsyncEventLoop& eventLoop);
173
176 [[nodiscard]] Result close();
177
183 [[nodiscard]] Result watch(FolderWatcher& watcher, StringView path);
184
185 private:
186 friend decltype(internal);
187 friend decltype(FolderWatcher::internal);
189};
190
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
A contiguous sequence of elements kept inside its inline storage.
Definition: Array.h:43
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition: Async.h:956
Starts an handle polling operation.
Definition: Async.h:906
Starts a wake-up operation, allowing threads to execute callbacks on loop thread.
Definition: Async.h:369
An automatically reset event object to synchronize two threads.
Definition: Threading.h:174
File Descriptor (use SC::File to open and use it with strings and containers).
Definition: FileDescriptor.h:52
Delivers notifications using Async (SC::AsyncEventLoop).
Definition: FileSystemWatcher.h:147
Represents a single folder being watched.
Definition: FileSystemWatcher.h:127
Function< void(const Notification &)> notifyCallback
Function that will be called on a notification.
Definition: FileSystemWatcher.h:128
Result stopWatching()
Stop watching this directory.
Notification holding type and path.
Definition: FileSystemWatcher.h:105
SC::Result getFullPath(String &bufferString, StringView &outFullPath) const
Get the full path of the file being watched.
StringView basePath
Reference to the watched directory.
Definition: FileSystemWatcher.h:106
Operation operation
Notification type.
Definition: FileSystemWatcher.h:108
StringView relativePath
Relative path of the file being notified from basePath
Definition: FileSystemWatcher.h:107
Notifies about events (add, remove, rename, modified) on files and directories.
Definition: FileSystemWatcher.h:45
Result close()
Stops all watchers and frees the ThreadRunner or EventLoopRunner passed in init.
Result init(ThreadRunner &runner)
Setup watcher to receive notifications from a background thread.
Result watch(FolderWatcher &watcher, StringView path)
Starts watching a single directory, calling FolderWatcher::notifyCallback on file events.
Result init(EventLoopRunner &runner, AsyncEventLoop &eventLoop)
Setup watcher to receive async notifications on SC::AsyncEventLoop.
Operation
Specifies the event classes.
Definition: FileSystemWatcher.h:98
@ Modified
A file or directory has been modified in its contents and/or timestamp.
@ AddRemoveRename
A file or directory has been added, removed or renamed.
Wraps function pointers, member functions and lambdas without ever allocating.
Definition: Function.h:50
An Intrusive Double Linked List.
Definition: IntrusiveDoubleLinkedList.h:22
A native OS mutex to synchronize access to shared resources.
Definition: Threading.h:28
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
String with compile time configurable inline storage (small string optimization)
Definition: SmallString.h:21
A non-modifiable owning string with associated encoding.
Definition: String.h:30
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
A native OS thread.
Definition: Threading.h:118
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51