Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gloomman committed Jun 14, 2024
1 parent b0f6c71 commit 04dc87e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ BraceWrapping:
SplitEmptyRecord: true
BeforeWhile: true
BreakBeforeBraces: Custom
ColumnLimit: 120
ColumnLimit: 140
Cpp11BracedListStyle: false
FixNamespaceComments: true
IndentCaseLabels: true
Expand Down
27 changes: 16 additions & 11 deletions src/Event.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ struct Event {
bool isDeleted;
bool isMoved;

Event(const std::string& path) : path(path), isCreated(false), isDeleted(false), isMoved(false) {}
Event(const std::string &path)
: path(path)
, isCreated(false)
, isDeleted(false)
, isMoved(false) {
}

Value toJS(const Env& env) {
Value toJS(const Env &env) {
EscapableHandleScope scope(env);
Object res = Object::New(env);
res.Set(String::New(env, "path"), String::New(env, path.c_str()));
Expand All @@ -31,9 +36,9 @@ struct Event {

class EventList {
public:
void create(const std::string& path) {
void create(const std::string &path) {
std::lock_guard<std::mutex> l(mMutex);
Event* event = internalUpdate(path);
Event *event = internalUpdate(path);
if (event->isDeleted) {
// Assume update event when rapidly removed and created
// https://github.com/parcel-bundler/watcher/issues/72
Expand All @@ -44,20 +49,20 @@ public:
}
}

Event* update(const std::string& path) {
Event *update(const std::string &path) {
std::lock_guard<std::mutex> l(mMutex);
return internalUpdate(path);
}

void move(const std::string& path) {
void move(const std::string &path) {
std::lock_guard<std::mutex> l(mMutex);
Event* event = internalUpdate(path);
Event *event = internalUpdate(path);
event->isMoved = true;
}

void remove(const std::string& path) {
void remove(const std::string &path) {
std::lock_guard<std::mutex> l(mMutex);
Event* event = internalUpdate(path);
Event *event = internalUpdate(path);
event->isDeleted = true;
}

Expand All @@ -69,7 +74,7 @@ public:
std::vector<Event> getEvents() {
std::lock_guard<std::mutex> l(mMutex);
std::vector<Event> eventsCloneVector;
for (auto& e : mEvents) {
for (auto &e : mEvents) {
if (!(e.second.isCreated && e.second.isDeleted)) {
eventsCloneVector.push_back(e.second);
}
Expand All @@ -85,7 +90,7 @@ public:
private:
mutable std::mutex mMutex;
std::map<std::string, Event> mEvents;
Event* internalUpdate(const std::string& path) {
Event *internalUpdate(const std::string &path) {
auto found = mEvents.find(path);
if (found == mEvents.end()) {
auto it = mEvents.emplace(path, Event(path));
Expand Down
9 changes: 5 additions & 4 deletions src/linux/FAnotifyBackend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ struct FAnotifySubscription {
int mountFd;
};

constexpr const uint64_t FANOTIFY_MASK = FAN_CREATE | FAN_DELETE | FAN_MODIFY | FAN_RENAME | FAN_EVENT_ON_CHILD | FAN_ATTRIB | FAN_DELETE_SELF | FAN_MOVED_FROM | FAN_MOVED_TO | FAN_MOVE_SELF | FAN_ONDIR;
constexpr const uint64_t FANOTIFY_MASK = FAN_CREATE | FAN_DELETE | FAN_MODIFY | FAN_RENAME | FAN_EVENT_ON_CHILD | FAN_ATTRIB |
FAN_DELETE_SELF | FAN_MOVED_FROM | FAN_MOVED_TO | FAN_MOVE_SELF | FAN_ONDIR;

class FAnotifyBackend : public BruteForceBackend {
protected:
static std::string toHex(const char* data, size_t len) {
static std::string toHex(const char *data, size_t len) {
static char abc[] = "0123456789abcdef";
std::string result;

Expand All @@ -29,8 +30,8 @@ protected:
return result;
}

static std::string toString(fsid_t* fsid) {
return toHex(reinterpret_cast<char*>(fsid), sizeof(fsid_t));
static std::string toString(fsid_t *fsid) {
return toHex(reinterpret_cast<char *>(fsid), sizeof(fsid_t));
}
};

Expand Down
34 changes: 16 additions & 18 deletions src/linux/FAnotifyCrawlBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void FAnotifyCrawlBackend::subscribe(WatcherRef watcher) {
// Build a full directory tree recursively, and watch each directory.
std::shared_ptr<DirTree> tree = getTree(watcher);

for (auto& e : tree->entries) {
for (auto &e : tree->entries) {
if (e.second.isDir) {
bool success = watchDir(watcher, e.second.path, tree);
if (!success) {
Expand All @@ -90,12 +90,8 @@ void FAnotifyCrawlBackend::subscribe(WatcherRef watcher) {
}
}

bool FAnotifyCrawlBackend::watchDir(WatcherRef watcher, const std::string& path, std::shared_ptr<DirTree> tree) {
auto markRc = fanotify_mark(mFAnotifyFd
, FAN_MARK_ADD | FAN_MARK_DONT_FOLLOW | FAN_MARK_ONLYDIR
, FANOTIFY_MASK
, AT_FDCWD
, path.c_str());
bool FAnotifyCrawlBackend::watchDir(WatcherRef watcher, const std::string &path, std::shared_ptr<DirTree> tree) {
auto markRc = fanotify_mark(mFAnotifyFd, FAN_MARK_ADD | FAN_MARK_DONT_FOLLOW | FAN_MARK_ONLYDIR, FANOTIFY_MASK, AT_FDCWD, path.c_str());

if (markRc != 0) {
return false;
Expand Down Expand Up @@ -139,7 +135,7 @@ void FAnotifyCrawlBackend::handleEvents() {
break;
}

auto event = reinterpret_cast<fanotify_event_metadata*>(buf);
auto event = reinterpret_cast<fanotify_event_metadata *>(buf);
while (FAN_EVENT_OK(event, n)) {
if (event->vers != FANOTIFY_METADATA_VERSION) {
throw std::runtime_error("fanotify: wrong version");
Expand All @@ -153,10 +149,10 @@ void FAnotifyCrawlBackend::handleEvents() {
unsigned processedLen = event->metadata_len;

while (eventLen > processedLen) {
auto header = (fanotify_event_info_header*)((char*)event + processedLen);
auto header = (fanotify_event_info_header *)((char *)event + processedLen);

if (FAN_EVENT_INFO_TYPE == header->info_type) {
auto fid = (fanotify_event_info_fid*)header;
auto fid = (fanotify_event_info_fid *)header;
handleEvent(event, fid, watchers);
}

Expand All @@ -167,38 +163,40 @@ void FAnotifyCrawlBackend::handleEvents() {
}
}

for (auto& w : watchers) {
for (auto &w : watchers) {
w->notify();
}
}

void FAnotifyCrawlBackend::handleEvent(fanotify_event_metadata* metadata, fanotify_event_info_fid* fid, std::unordered_set<WatcherRef>& watchers) {
void FAnotifyCrawlBackend::handleEvent(
fanotify_event_metadata *metadata, fanotify_event_info_fid *fid, std::unordered_set<WatcherRef> &watchers) {
std::unique_lock<std::mutex> lock(mMutex);

// Find the subscriptions for this watch descriptor
auto range = mSubscriptions.equal_range(toString((fsid_t*)&fid->fsid));
auto range = mSubscriptions.equal_range(toString((fsid_t *)&fid->fsid));
std::unordered_set<std::shared_ptr<FAnotifySubscription>> set;
for (auto it = range.first; it != range.second; ++it) {
set.insert(it->second);
}

for (auto& s : set) {
for (auto &s : set) {
if (handleSubscription(metadata, fid, s)) {
watchers.insert(s->watcher);
}
}
}

bool FAnotifyCrawlBackend::handleSubscription(fanotify_event_metadata* metadata, fanotify_event_info_fid* fid, std::shared_ptr<FAnotifySubscription> sub) {
bool FAnotifyCrawlBackend::handleSubscription(
fanotify_event_metadata *metadata, fanotify_event_info_fid *fid, std::shared_ptr<FAnotifySubscription> sub) {
// Build full path and check if its in our ignore list.
auto watcher = sub->watcher;
std::string path(sub->path);
bool isDir = (metadata->mask & FAN_ONDIR) == FAN_ONDIR;

#ifdef FAN_EVENT_INFO_TYPE_DFID_NAME
if (FAN_EVENT_INFO_TYPE_DFID_NAME == fid->hdr.info_type) {
auto handle = (file_handle*)fid->handle;
path.append("/").append((char*)handle + sizeof(file_handle) + handle->handle_bytes);
auto handle = (file_handle *)fid->handle;
path.append("/").append((char *)handle + sizeof(file_handle) + handle->handle_bytes);
}
#endif

Expand All @@ -215,7 +213,7 @@ bool FAnotifyCrawlBackend::handleSubscription(fanotify_event_metadata* metadata,
// Use lstat to avoid resolving symbolic links that we cannot watch anyway
// https://github.com/parcel-bundler/watcher/issues/76
lstat(path.c_str(), &st);
DirEntry* entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
DirEntry *entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));

if (entry->isDir) {
bool success = watchDir(watcher, path, sub->tree);
Expand Down
6 changes: 3 additions & 3 deletions src/linux/FAnotifyCrawlBackend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ private:
std::unordered_multimap<std::string, std::shared_ptr<FAnotifySubscription>> mSubscriptions;
Signal mEndedSignal;

bool watchDir(WatcherRef watcher, const std::string& path, std::shared_ptr<DirTree> tree);
bool watchDir(WatcherRef watcher, const std::string &path, std::shared_ptr<DirTree> tree);
void handleEvents();
void handleEvent(fanotify_event_metadata* metadata, fanotify_event_info_fid* fid, std::unordered_set<WatcherRef>& watchers);
bool handleSubscription(fanotify_event_metadata* metadata, fanotify_event_info_fid* fid, std::shared_ptr<FAnotifySubscription> sub);
void handleEvent(fanotify_event_metadata *metadata, fanotify_event_info_fid *fid, std::unordered_set<WatcherRef> &watchers);
bool handleSubscription(fanotify_event_metadata *metadata, fanotify_event_info_fid *fid, std::shared_ptr<FAnotifySubscription> sub);
};

#endif
2 changes: 1 addition & 1 deletion test/since.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('since', () => {

describe('files', () => {
it('should emit when a file is created', async function () {
this.timeout(5000);
this.timeout(10000);
let f = getFilename();
await watcher.writeSnapshot(tmpDir, snapshotPath, {backend});
if (isSecondPrecision) {
Expand Down

0 comments on commit 04dc87e

Please sign in to comment.