@eduardo191labs/watcher 0.0.0-stage → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +82 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/binding.gyp +125 -0
- package/index.d.ts +49 -0
- package/index.js +55 -0
- package/index.js.flow +45 -0
- package/package.json +61 -3
- package/scripts/build-from-source.js +16 -0
- package/src/Backend.cc +186 -0
- package/src/Backend.hh +37 -0
- package/src/Debounce.cc +113 -0
- package/src/Debounce.hh +49 -0
- package/src/DirTree.cc +164 -0
- package/src/DirTree.hh +50 -0
- package/src/Event.hh +109 -0
- package/src/Glob.cc +22 -0
- package/src/Glob.hh +34 -0
- package/src/PromiseRunner.hh +101 -0
- package/src/Signal.hh +46 -0
- package/src/Watcher.cc +241 -0
- package/src/Watcher.hh +73 -0
- package/src/binding.cc +268 -0
- package/src/kqueue/KqueueBackend.cc +306 -0
- package/src/kqueue/KqueueBackend.hh +35 -0
- package/src/linux/InotifyBackend.cc +236 -0
- package/src/linux/InotifyBackend.hh +34 -0
- package/src/macos/FSEventsBackend.cc +338 -0
- package/src/macos/FSEventsBackend.hh +20 -0
- package/src/shared/BruteForceBackend.cc +41 -0
- package/src/shared/BruteForceBackend.hh +25 -0
- package/src/unix/fts.cc +50 -0
- package/src/unix/legacy.cc +77 -0
- package/src/wasm/WasmBackend.cc +132 -0
- package/src/wasm/WasmBackend.hh +34 -0
- package/src/wasm/include.h +74 -0
- package/src/watchman/BSER.cc +302 -0
- package/src/watchman/BSER.hh +69 -0
- package/src/watchman/IPC.hh +175 -0
- package/src/watchman/WatchmanBackend.cc +342 -0
- package/src/watchman/WatchmanBackend.hh +35 -0
- package/src/windows/WindowsBackend.cc +282 -0
- package/src/windows/WindowsBackend.hh +18 -0
- package/src/windows/win_utils.cc +44 -0
- package/src/windows/win_utils.hh +11 -0
- package/wrapper.js +88 -0
package/src/Backend.cc
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
#ifdef FS_EVENTS
|
|
2
|
+
#include "macos/FSEventsBackend.hh"
|
|
3
|
+
#endif
|
|
4
|
+
#ifdef WATCHMAN
|
|
5
|
+
#include "watchman/WatchmanBackend.hh"
|
|
6
|
+
#endif
|
|
7
|
+
#ifdef WINDOWS
|
|
8
|
+
#include "windows/WindowsBackend.hh"
|
|
9
|
+
#endif
|
|
10
|
+
#ifdef INOTIFY
|
|
11
|
+
#include "linux/InotifyBackend.hh"
|
|
12
|
+
#endif
|
|
13
|
+
#ifdef KQUEUE
|
|
14
|
+
#include "kqueue/KqueueBackend.hh"
|
|
15
|
+
#endif
|
|
16
|
+
#ifdef __wasm32__
|
|
17
|
+
#include "wasm/WasmBackend.hh"
|
|
18
|
+
#endif
|
|
19
|
+
#include "shared/BruteForceBackend.hh"
|
|
20
|
+
|
|
21
|
+
#include "Backend.hh"
|
|
22
|
+
#include <unordered_map>
|
|
23
|
+
|
|
24
|
+
static std::unordered_map<std::string, std::shared_ptr<Backend>>& getSharedBackends() {
|
|
25
|
+
static std::unordered_map<std::string, std::shared_ptr<Backend>>* sharedBackends =
|
|
26
|
+
new std::unordered_map<std::string, std::shared_ptr<Backend>>();
|
|
27
|
+
return *sharedBackends;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
std::shared_ptr<Backend> getBackend(std::string backend) {
|
|
31
|
+
// Use FSEvents on macOS by default.
|
|
32
|
+
// Use watchman by default if available on other platforms.
|
|
33
|
+
// Fall back to brute force.
|
|
34
|
+
#ifdef FS_EVENTS
|
|
35
|
+
if (backend == "fs-events" || backend == "default") {
|
|
36
|
+
return std::make_shared<FSEventsBackend>();
|
|
37
|
+
}
|
|
38
|
+
#endif
|
|
39
|
+
#ifdef WATCHMAN
|
|
40
|
+
if ((backend == "watchman" || backend == "default") && WatchmanBackend::checkAvailable()) {
|
|
41
|
+
return std::make_shared<WatchmanBackend>();
|
|
42
|
+
}
|
|
43
|
+
#endif
|
|
44
|
+
#ifdef WINDOWS
|
|
45
|
+
if (backend == "windows" || backend == "default") {
|
|
46
|
+
return std::make_shared<WindowsBackend>();
|
|
47
|
+
}
|
|
48
|
+
#endif
|
|
49
|
+
#ifdef INOTIFY
|
|
50
|
+
if (backend == "inotify" || backend == "default") {
|
|
51
|
+
return std::make_shared<InotifyBackend>();
|
|
52
|
+
}
|
|
53
|
+
#endif
|
|
54
|
+
#ifdef KQUEUE
|
|
55
|
+
if (backend == "kqueue" || backend == "default") {
|
|
56
|
+
return std::make_shared<KqueueBackend>();
|
|
57
|
+
}
|
|
58
|
+
#endif
|
|
59
|
+
#ifdef __wasm32__
|
|
60
|
+
if (backend == "wasm" || backend == "default") {
|
|
61
|
+
return std::make_shared<WasmBackend>();
|
|
62
|
+
}
|
|
63
|
+
#endif
|
|
64
|
+
if (backend == "brute-force" || backend == "default") {
|
|
65
|
+
return std::make_shared<BruteForceBackend>();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return nullptr;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
std::shared_ptr<Backend> Backend::getShared(std::string backend) {
|
|
72
|
+
auto found = getSharedBackends().find(backend);
|
|
73
|
+
if (found != getSharedBackends().end()) {
|
|
74
|
+
return found->second;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
auto result = getBackend(backend);
|
|
78
|
+
if (!result) {
|
|
79
|
+
return getShared("default");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
result->run();
|
|
83
|
+
getSharedBackends().emplace(backend, result);
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
void removeShared(Backend *backend) {
|
|
88
|
+
for (auto it = getSharedBackends().begin(); it != getSharedBackends().end(); it++) {
|
|
89
|
+
if (it->second.get() == backend) {
|
|
90
|
+
getSharedBackends().erase(it);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Free up memory.
|
|
96
|
+
if (getSharedBackends().size() == 0) {
|
|
97
|
+
getSharedBackends().rehash(0);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
void Backend::run() {
|
|
102
|
+
#ifndef __wasm32__
|
|
103
|
+
mThread = std::thread([this] () {
|
|
104
|
+
try {
|
|
105
|
+
start();
|
|
106
|
+
} catch (std::exception &err) {
|
|
107
|
+
handleError(err);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
if (mThread.joinable()) {
|
|
112
|
+
mStartedSignal.wait();
|
|
113
|
+
}
|
|
114
|
+
#else
|
|
115
|
+
try {
|
|
116
|
+
start();
|
|
117
|
+
} catch (std::exception &err) {
|
|
118
|
+
handleError(err);
|
|
119
|
+
}
|
|
120
|
+
#endif
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
void Backend::notifyStarted() {
|
|
124
|
+
mStartedSignal.notify();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
void Backend::start() {
|
|
128
|
+
notifyStarted();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
Backend::~Backend() {
|
|
132
|
+
#ifndef __wasm32__
|
|
133
|
+
// Wait for thread to stop
|
|
134
|
+
if (mThread.joinable()) {
|
|
135
|
+
// If the backend is being destroyed from the thread itself, detach, otherwise join.
|
|
136
|
+
if (mThread.get_id() == std::this_thread::get_id()) {
|
|
137
|
+
mThread.detach();
|
|
138
|
+
} else {
|
|
139
|
+
mThread.join();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
#endif
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
void Backend::watch(WatcherRef watcher) {
|
|
146
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
147
|
+
auto res = mSubscriptions.find(watcher);
|
|
148
|
+
if (res == mSubscriptions.end()) {
|
|
149
|
+
try {
|
|
150
|
+
this->subscribe(watcher);
|
|
151
|
+
mSubscriptions.insert(watcher);
|
|
152
|
+
} catch (std::exception&) {
|
|
153
|
+
unref();
|
|
154
|
+
throw;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
void Backend::unwatch(WatcherRef watcher) {
|
|
160
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
161
|
+
size_t deleted = mSubscriptions.erase(watcher);
|
|
162
|
+
if (deleted > 0) {
|
|
163
|
+
this->unsubscribe(watcher);
|
|
164
|
+
unref();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
void Backend::unref() {
|
|
169
|
+
if (mSubscriptions.size() == 0) {
|
|
170
|
+
removeShared(this);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
void Backend::handleWatcherError(WatcherError &err) {
|
|
175
|
+
unwatch(err.mWatcher);
|
|
176
|
+
err.mWatcher->notifyError(err);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
void Backend::handleError(std::exception &err) {
|
|
180
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
181
|
+
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); it++) {
|
|
182
|
+
(*it)->notifyError(err);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
removeShared(this);
|
|
186
|
+
}
|
package/src/Backend.hh
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#ifndef BACKEND_H
|
|
2
|
+
#define BACKEND_H
|
|
3
|
+
|
|
4
|
+
#include "Event.hh"
|
|
5
|
+
#include "Watcher.hh"
|
|
6
|
+
#include "Signal.hh"
|
|
7
|
+
#include <thread>
|
|
8
|
+
|
|
9
|
+
class Backend {
|
|
10
|
+
public:
|
|
11
|
+
virtual ~Backend();
|
|
12
|
+
void run();
|
|
13
|
+
void notifyStarted();
|
|
14
|
+
|
|
15
|
+
virtual void start();
|
|
16
|
+
virtual void writeSnapshot(WatcherRef watcher, std::string *snapshotPath) = 0;
|
|
17
|
+
virtual void getEventsSince(WatcherRef watcher, std::string *snapshotPath) = 0;
|
|
18
|
+
virtual void subscribe(WatcherRef watcher) = 0;
|
|
19
|
+
virtual void unsubscribe(WatcherRef watcher) = 0;
|
|
20
|
+
|
|
21
|
+
static std::shared_ptr<Backend> getShared(std::string backend);
|
|
22
|
+
|
|
23
|
+
void watch(WatcherRef watcher);
|
|
24
|
+
void unwatch(WatcherRef watcher);
|
|
25
|
+
void unref();
|
|
26
|
+
void handleWatcherError(WatcherError &err);
|
|
27
|
+
|
|
28
|
+
std::mutex mMutex;
|
|
29
|
+
std::thread mThread;
|
|
30
|
+
private:
|
|
31
|
+
std::unordered_set<WatcherRef> mSubscriptions;
|
|
32
|
+
Signal mStartedSignal;
|
|
33
|
+
|
|
34
|
+
void handleError(std::exception &err);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
#endif
|
package/src/Debounce.cc
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#include "Debounce.hh"
|
|
2
|
+
|
|
3
|
+
#ifdef __wasm32__
|
|
4
|
+
extern "C" void on_timeout(void *ctx) {
|
|
5
|
+
Debounce *debounce = (Debounce *)ctx;
|
|
6
|
+
debounce->notify();
|
|
7
|
+
}
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
std::shared_ptr<Debounce> Debounce::getShared() {
|
|
11
|
+
static std::weak_ptr<Debounce> sharedInstance;
|
|
12
|
+
std::shared_ptr<Debounce> shared = sharedInstance.lock();
|
|
13
|
+
if (!shared) {
|
|
14
|
+
shared = std::make_shared<Debounce>();
|
|
15
|
+
sharedInstance = shared;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return shared;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
Debounce::Debounce() {
|
|
22
|
+
mRunning = true;
|
|
23
|
+
#ifndef __wasm32__
|
|
24
|
+
mThread = std::thread([this] () {
|
|
25
|
+
loop();
|
|
26
|
+
});
|
|
27
|
+
#endif
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Debounce::~Debounce() {
|
|
31
|
+
mRunning = false;
|
|
32
|
+
#ifndef __wasm32__
|
|
33
|
+
mWaitSignal.notify();
|
|
34
|
+
mThread.join();
|
|
35
|
+
#endif
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
void Debounce::add(void *key, std::function<void()> cb) {
|
|
39
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
40
|
+
mCallbacks.emplace(key, cb);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
void Debounce::remove(void *key) {
|
|
44
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
45
|
+
mCallbacks.erase(key);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
void Debounce::trigger() {
|
|
49
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
50
|
+
#ifdef __wasm32__
|
|
51
|
+
notifyIfReady();
|
|
52
|
+
#else
|
|
53
|
+
mWaitSignal.notify();
|
|
54
|
+
#endif
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#ifndef __wasm32__
|
|
58
|
+
void Debounce::loop() {
|
|
59
|
+
while (mRunning) {
|
|
60
|
+
mWaitSignal.wait();
|
|
61
|
+
if (!mRunning) {
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
notifyIfReady();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
#endif
|
|
69
|
+
|
|
70
|
+
void Debounce::notifyIfReady() {
|
|
71
|
+
if (!mRunning) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// If we haven't seen an event in more than the maximum wait time, notify callbacks immediately
|
|
76
|
+
// to ensure that we don't wait forever. Otherwise, wait for the minimum wait time and batch
|
|
77
|
+
// subsequent fast changes. This also means the first file change in a batch is notified immediately,
|
|
78
|
+
// separately from the rest of the batch. This seems like an acceptable tradeoff if the common case
|
|
79
|
+
// is that only a single file was updated at a time.
|
|
80
|
+
auto time = std::chrono::steady_clock::now();
|
|
81
|
+
if ((time - mLastTime) > std::chrono::milliseconds(MAX_WAIT_TIME)) {
|
|
82
|
+
mLastTime = time;
|
|
83
|
+
notify();
|
|
84
|
+
} else {
|
|
85
|
+
wait();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
void Debounce::wait() {
|
|
90
|
+
#ifdef __wasm32__
|
|
91
|
+
clear_timeout(mTimeout);
|
|
92
|
+
mTimeout = set_timeout(MIN_WAIT_TIME, this);
|
|
93
|
+
#else
|
|
94
|
+
auto status = mWaitSignal.waitFor(std::chrono::milliseconds(MIN_WAIT_TIME));
|
|
95
|
+
if (mRunning && (status == std::cv_status::timeout)) {
|
|
96
|
+
notify();
|
|
97
|
+
}
|
|
98
|
+
#endif
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
void Debounce::notify() {
|
|
102
|
+
std::unique_lock<std::mutex> lock(mMutex);
|
|
103
|
+
|
|
104
|
+
mLastTime = std::chrono::steady_clock::now();
|
|
105
|
+
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
106
|
+
auto cb = it->second;
|
|
107
|
+
cb();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#ifndef __wasm32__
|
|
111
|
+
mWaitSignal.reset();
|
|
112
|
+
#endif
|
|
113
|
+
}
|
package/src/Debounce.hh
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#ifndef DEBOUNCE_H
|
|
2
|
+
#define DEBOUNCE_H
|
|
3
|
+
|
|
4
|
+
#include <thread>
|
|
5
|
+
#include <unordered_map>
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include "Signal.hh"
|
|
8
|
+
|
|
9
|
+
#define MIN_WAIT_TIME 50
|
|
10
|
+
#define MAX_WAIT_TIME 500
|
|
11
|
+
|
|
12
|
+
#ifdef __wasm32__
|
|
13
|
+
extern "C" {
|
|
14
|
+
int set_timeout(int ms, void *ctx);
|
|
15
|
+
void clear_timeout(int timeout);
|
|
16
|
+
void on_timeout(void *ctx);
|
|
17
|
+
};
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
class Debounce {
|
|
21
|
+
public:
|
|
22
|
+
static std::shared_ptr<Debounce> getShared();
|
|
23
|
+
|
|
24
|
+
Debounce();
|
|
25
|
+
~Debounce();
|
|
26
|
+
|
|
27
|
+
void add(void *key, std::function<void()> cb);
|
|
28
|
+
void remove(void *key);
|
|
29
|
+
void trigger();
|
|
30
|
+
void notify();
|
|
31
|
+
|
|
32
|
+
private:
|
|
33
|
+
bool mRunning;
|
|
34
|
+
std::mutex mMutex;
|
|
35
|
+
#ifdef __wasm32__
|
|
36
|
+
int mTimeout;
|
|
37
|
+
#else
|
|
38
|
+
Signal mWaitSignal;
|
|
39
|
+
std::thread mThread;
|
|
40
|
+
#endif
|
|
41
|
+
std::unordered_map<void *, std::function<void()>> mCallbacks;
|
|
42
|
+
std::chrono::time_point<std::chrono::steady_clock> mLastTime;
|
|
43
|
+
|
|
44
|
+
void loop();
|
|
45
|
+
void notifyIfReady();
|
|
46
|
+
void wait();
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
#endif
|
package/src/DirTree.cc
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
#include "DirTree.hh"
|
|
2
|
+
#include <inttypes.h>
|
|
3
|
+
|
|
4
|
+
// "Meyer's singleton", construction is ordered by use, likewise (reverse) for destruction.
|
|
5
|
+
// https://stackoverflow.com/a/17713799
|
|
6
|
+
// https://laristra.github.io/flecsi/src/developer-guide/patterns/meyers_singleton.html
|
|
7
|
+
static std::mutex& mDirCacheMutex() {
|
|
8
|
+
static std::mutex mutex;
|
|
9
|
+
return mutex;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static std::unordered_map<std::string, std::weak_ptr<DirTree>>& dirTreeCache() {
|
|
13
|
+
static std::unordered_map<std::string, std::weak_ptr<DirTree>> cache;
|
|
14
|
+
return cache;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
struct DirTreeDeleter {
|
|
18
|
+
void operator()(DirTree *tree) {
|
|
19
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
20
|
+
std::unordered_map<std::string, std::weak_ptr<DirTree>> &cache = dirTreeCache();
|
|
21
|
+
cache.erase(tree->root);
|
|
22
|
+
delete tree;
|
|
23
|
+
|
|
24
|
+
// Free up memory.
|
|
25
|
+
if (cache.size() == 0) {
|
|
26
|
+
cache.rehash(0);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
std::shared_ptr<DirTree> DirTree::getCached(std::string root) {
|
|
32
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
33
|
+
std::unordered_map<std::string, std::weak_ptr<DirTree>> &cache = dirTreeCache();
|
|
34
|
+
|
|
35
|
+
auto found = cache.find(root);
|
|
36
|
+
std::shared_ptr<DirTree> tree;
|
|
37
|
+
|
|
38
|
+
// Use cached tree, or create an empty one.
|
|
39
|
+
if (found != cache.end()) {
|
|
40
|
+
tree = found->second.lock();
|
|
41
|
+
} else {
|
|
42
|
+
tree = std::shared_ptr<DirTree>(new DirTree(root), DirTreeDeleter());
|
|
43
|
+
cache.emplace(root, tree);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return tree;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
DirTree::DirTree(std::string root, FILE *f) : root(root), isComplete(true) {
|
|
50
|
+
size_t size;
|
|
51
|
+
if (fscanf(f, "%zu", &size)) {
|
|
52
|
+
for (size_t i = 0; i < size; i++) {
|
|
53
|
+
DirEntry entry(f);
|
|
54
|
+
entries.emplace(entry.path, entry);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Internal find method that has no lock
|
|
60
|
+
DirEntry *DirTree::_find(std::string path) {
|
|
61
|
+
auto found = entries.find(path);
|
|
62
|
+
if (found == entries.end()) {
|
|
63
|
+
return NULL;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return &found->second;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
DirEntry *DirTree::add(std::string path, uint64_t mtime, bool isDir) {
|
|
70
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
71
|
+
|
|
72
|
+
DirEntry entry(path, mtime, isDir);
|
|
73
|
+
auto it = entries.emplace(entry.path, entry);
|
|
74
|
+
return &it.first->second;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
DirEntry *DirTree::find(std::string path) {
|
|
78
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
79
|
+
return _find(path);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
DirEntry *DirTree::update(std::string path, uint64_t mtime) {
|
|
83
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
84
|
+
|
|
85
|
+
DirEntry *found = _find(path);
|
|
86
|
+
if (found) {
|
|
87
|
+
found->mtime = mtime;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return found;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
void DirTree::remove(std::string path) {
|
|
94
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
95
|
+
|
|
96
|
+
DirEntry *found = _find(path);
|
|
97
|
+
|
|
98
|
+
// Remove all sub-entries if this is a directory
|
|
99
|
+
if (found && found->isDir) {
|
|
100
|
+
std::string pathStart = path + DIR_SEP;
|
|
101
|
+
for (auto it = entries.begin(); it != entries.end();) {
|
|
102
|
+
if (it->first.rfind(pathStart, 0) == 0) {
|
|
103
|
+
it = entries.erase(it);
|
|
104
|
+
} else {
|
|
105
|
+
it++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
entries.erase(path);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void DirTree::write(FILE *f) {
|
|
114
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
115
|
+
|
|
116
|
+
fprintf(f, "%zu\n", entries.size());
|
|
117
|
+
for (auto it = entries.begin(); it != entries.end(); it++) {
|
|
118
|
+
it->second.write(f);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
void DirTree::getChanges(DirTree *snapshot, EventList &events) {
|
|
123
|
+
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
124
|
+
std::lock_guard<std::mutex> snapshotLock(snapshot->mMutex);
|
|
125
|
+
|
|
126
|
+
for (auto it = entries.begin(); it != entries.end(); it++) {
|
|
127
|
+
auto found = snapshot->entries.find(it->first);
|
|
128
|
+
if (found == snapshot->entries.end()) {
|
|
129
|
+
events.create(it->second.path);
|
|
130
|
+
} else if (found->second.mtime != it->second.mtime && !found->second.isDir && !it->second.isDir) {
|
|
131
|
+
events.update(it->second.path);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (auto it = snapshot->entries.begin(); it != snapshot->entries.end(); it++) {
|
|
136
|
+
size_t count = entries.count(it->first);
|
|
137
|
+
if (count == 0) {
|
|
138
|
+
events.remove(it->second.path);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
DirEntry::DirEntry(std::string p, uint64_t t, bool d) {
|
|
144
|
+
path = p;
|
|
145
|
+
mtime = t;
|
|
146
|
+
isDir = d;
|
|
147
|
+
state = NULL;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
DirEntry::DirEntry(FILE *f) {
|
|
151
|
+
size_t size;
|
|
152
|
+
if (fscanf(f, "%zu", &size)) {
|
|
153
|
+
path.resize(size);
|
|
154
|
+
if (fread(&path[0], sizeof(char), size, f)) {
|
|
155
|
+
int d = 0;
|
|
156
|
+
fscanf(f, "%" PRIu64 " %d\n", &mtime, &d);
|
|
157
|
+
isDir = d == 1;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
void DirEntry::write(FILE *f) const {
|
|
163
|
+
fprintf(f, "%zu%s%" PRIu64 " %d\n", path.size(), path.c_str(), mtime, isDir);
|
|
164
|
+
}
|
package/src/DirTree.hh
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#ifndef DIR_TREE_H
|
|
2
|
+
#define DIR_TREE_H
|
|
3
|
+
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <unordered_map>
|
|
6
|
+
#include <memory>
|
|
7
|
+
#include "Event.hh"
|
|
8
|
+
|
|
9
|
+
#ifdef _WIN32
|
|
10
|
+
#define DIR_SEP "\\"
|
|
11
|
+
#else
|
|
12
|
+
#define DIR_SEP "/"
|
|
13
|
+
#endif
|
|
14
|
+
|
|
15
|
+
struct DirEntry {
|
|
16
|
+
std::string path;
|
|
17
|
+
uint64_t mtime;
|
|
18
|
+
bool isDir;
|
|
19
|
+
mutable void *state;
|
|
20
|
+
|
|
21
|
+
DirEntry(std::string p, uint64_t t, bool d);
|
|
22
|
+
DirEntry(FILE *f);
|
|
23
|
+
void write(FILE *f) const;
|
|
24
|
+
bool operator==(const DirEntry &other) const {
|
|
25
|
+
return path == other.path;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
class DirTree {
|
|
30
|
+
public:
|
|
31
|
+
static std::shared_ptr<DirTree> getCached(std::string root);
|
|
32
|
+
DirTree(std::string root) : root(root), isComplete(false) {}
|
|
33
|
+
DirTree(std::string root, FILE *f);
|
|
34
|
+
DirEntry *add(std::string path, uint64_t mtime, bool isDir);
|
|
35
|
+
DirEntry *find(std::string path);
|
|
36
|
+
DirEntry *update(std::string path, uint64_t mtime);
|
|
37
|
+
void remove(std::string path);
|
|
38
|
+
void write(FILE *f);
|
|
39
|
+
void getChanges(DirTree *snapshot, EventList &events);
|
|
40
|
+
|
|
41
|
+
std::mutex mMutex;
|
|
42
|
+
std::string root;
|
|
43
|
+
bool isComplete;
|
|
44
|
+
std::unordered_map<std::string, DirEntry> entries;
|
|
45
|
+
|
|
46
|
+
private:
|
|
47
|
+
DirEntry *_find(std::string path);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
#endif
|
package/src/Event.hh
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#ifndef EVENT_H
|
|
2
|
+
#define EVENT_H
|
|
3
|
+
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <node_api.h>
|
|
6
|
+
#include "wasm/include.h"
|
|
7
|
+
#include <napi.h>
|
|
8
|
+
#include <mutex>
|
|
9
|
+
#include <map>
|
|
10
|
+
#include <optional>
|
|
11
|
+
|
|
12
|
+
using namespace Napi;
|
|
13
|
+
|
|
14
|
+
struct Event {
|
|
15
|
+
std::string path;
|
|
16
|
+
bool isCreated;
|
|
17
|
+
bool isDeleted;
|
|
18
|
+
Event(std::string path) : path(path), isCreated(false), isDeleted(false) {}
|
|
19
|
+
|
|
20
|
+
Value toJS(const Env& env) {
|
|
21
|
+
EscapableHandleScope scope(env);
|
|
22
|
+
Object res = Object::New(env);
|
|
23
|
+
std::string type = isCreated ? "create" : isDeleted ? "delete" : "update";
|
|
24
|
+
res.Set(String::New(env, "path"), String::New(env, path.c_str()));
|
|
25
|
+
res.Set(String::New(env, "type"), String::New(env, type.c_str()));
|
|
26
|
+
return scope.Escape(res);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
class EventList {
|
|
31
|
+
public:
|
|
32
|
+
void create(std::string path) {
|
|
33
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
34
|
+
Event *event = internalUpdate(path);
|
|
35
|
+
if (event->isDeleted) {
|
|
36
|
+
// Assume update event when rapidly removed and created
|
|
37
|
+
// https://github.com/parcel-bundler/watcher/issues/72
|
|
38
|
+
event->isDeleted = false;
|
|
39
|
+
} else {
|
|
40
|
+
event->isCreated = true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
Event *update(std::string path) {
|
|
45
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
46
|
+
return internalUpdate(path);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void remove(std::string path) {
|
|
50
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
51
|
+
Event *event = internalUpdate(path);
|
|
52
|
+
event->isDeleted = true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
size_t size() {
|
|
56
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
57
|
+
return mEvents.size();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
std::vector<Event> getEvents() {
|
|
61
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
62
|
+
std::vector<Event> eventsCloneVector;
|
|
63
|
+
for(auto it = mEvents.begin(); it != mEvents.end(); ++it) {
|
|
64
|
+
if (!(it->second.isCreated && it->second.isDeleted)) {
|
|
65
|
+
eventsCloneVector.push_back(it->second);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return eventsCloneVector;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
void clear() {
|
|
72
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
73
|
+
mEvents.clear();
|
|
74
|
+
mError.reset();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
void error(std::string err) {
|
|
78
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
79
|
+
if (!mError.has_value()) {
|
|
80
|
+
mError.emplace(err);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
bool hasError() {
|
|
85
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
86
|
+
return mError.has_value();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
std::string getError() {
|
|
90
|
+
std::lock_guard<std::mutex> l(mMutex);
|
|
91
|
+
return mError.value_or("");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private:
|
|
95
|
+
mutable std::mutex mMutex;
|
|
96
|
+
std::map<std::string, Event> mEvents;
|
|
97
|
+
std::optional<std::string> mError;
|
|
98
|
+
Event *internalUpdate(std::string path) {
|
|
99
|
+
auto found = mEvents.find(path);
|
|
100
|
+
if (found == mEvents.end()) {
|
|
101
|
+
auto it = mEvents.emplace(path, Event(path));
|
|
102
|
+
return &it.first->second;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return &found->second;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
#endif
|