Skip to content

Commit

Permalink
Final?
Browse files Browse the repository at this point in the history
  • Loading branch information
KitRifty committed Jan 2, 2024
1 parent 9396320 commit 3e4b17c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
31 changes: 19 additions & 12 deletions extension/filesystemwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ bool SMDirectoryWatcher::Start()

watching = true;

if (onStarted && onStarted->IsRunnable())
{
onStarted->PushCell(handle);
onStarted->Execute(nullptr);
}

return true;
}

Expand All @@ -85,12 +79,6 @@ void SMDirectoryWatcher::Stop()

watching = false;
StopWatching();

if (onStopped && onStopped->IsRunnable())
{
onStopped->PushCell(handle);
onStopped->Execute(nullptr);
}
}

void SMDirectoryWatcher::OnGameFrame(bool simulating)
Expand Down Expand Up @@ -200,6 +188,25 @@ void SMDirectoryWatcher::OnProcessEvent(const NotifyEvent &event)

break;
}
case kStart:
{
if (onStarted && onStarted->IsRunnable())
{
onStarted->PushCell(handle);
onStarted->Execute(nullptr);
}

break;
}
case kStop:
{
if (onStopped && onStopped->IsRunnable())
{
onStopped->PushCell(handle);
onStopped->Execute(nullptr);
}
break;
}
}
}

Expand Down
39 changes: 26 additions & 13 deletions extension/tests/test-symlinks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,34 @@ TEST(SymbolicLinks, CreateRenameDeleteDir)
watcher.ProcessEvents();

ASSERT_EQ(watcher.events.size(), 5);
ASSERT_EQ(watcher.events[0].type, DirectoryWatcher::NotifyEventType::kStart);
ASSERT_EQ(watcher.events[0].path, dir.GetPath());

ASSERT_EQ(watcher.events[1].type, DirectoryWatcher::NotifyEventType::kFilesystem);
ASSERT_EQ(watcher.events[1].flags, DirectoryWatcher::NotifyFilterFlags::kCreated);
ASSERT_EQ(watcher.events[1].path, dir.GetPath() / "sym_link");
size_t i = 0;

ASSERT_EQ(watcher.events[2].type, DirectoryWatcher::NotifyEventType::kFilesystem);
ASSERT_EQ(watcher.events[2].flags, DirectoryWatcher::NotifyFilterFlags::kCreated);
ASSERT_EQ(watcher.events[2].path, dir.GetPath() / "sym_link" / "existing_file");
ASSERT_EQ(watcher.events[i].type, DirectoryWatcher::NotifyEventType::kStart);
ASSERT_EQ(watcher.events[i].path, dir.GetPath());

ASSERT_EQ(watcher.events[3].type, DirectoryWatcher::NotifyEventType::kFilesystem);
ASSERT_EQ(watcher.events[3].flags, DirectoryWatcher::NotifyFilterFlags::kModified);
ASSERT_EQ(watcher.events[3].path, dir.GetPath() / "sym_link" / "existing_file");
i++;

ASSERT_EQ(watcher.events[4].type, DirectoryWatcher::NotifyEventType::kStop);
ASSERT_EQ(watcher.events[4].path, dir.GetPath());
ASSERT_EQ(watcher.events[i].type, DirectoryWatcher::NotifyEventType::kFilesystem);
ASSERT_EQ(watcher.events[i].flags, DirectoryWatcher::NotifyFilterFlags::kCreated);
ASSERT_EQ(watcher.events[i].path, dir.GetPath() / "sym_link");

i++;

ASSERT_EQ(watcher.events[i].type, DirectoryWatcher::NotifyEventType::kFilesystem);
ASSERT_EQ(watcher.events[i].flags, DirectoryWatcher::NotifyFilterFlags::kCreated);
ASSERT_EQ(watcher.events[i].path, dir.GetPath() / "sym_link" / "existing_file");

i++;

ASSERT_EQ(watcher.events[i].type, DirectoryWatcher::NotifyEventType::kFilesystem);
ASSERT_EQ(watcher.events[i].flags, DirectoryWatcher::NotifyFilterFlags::kModified);
ASSERT_EQ(watcher.events[i].path, dir.GetPath() / "sym_link" / "existing_file");

i++;

ASSERT_EQ(watcher.events[i].type, DirectoryWatcher::NotifyEventType::kStop);
ASSERT_EQ(watcher.events[i].path, dir.GetPath());

i++;
}
29 changes: 17 additions & 12 deletions extension/watcher/watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
namespace fs = std::filesystem;

DirectoryWatcher::Worker::Worker(
bool isRoot,
const std::filesystem::path &path,
const WatchOptions &_options,
EventQueue *eventsBuffer,
std::mutex *eventsBufferMutex) : basePath(path.lexically_normal()),
std::mutex *eventsBufferMutex) : isRootWorker(isRoot),
basePath(path.lexically_normal()),
eventsBuffer(eventsBuffer),
eventsBufferMutex(eventsBufferMutex),
options(_options)
Expand Down Expand Up @@ -106,7 +108,7 @@ DirectoryWatcher::Worker::Worker(
{
if (fs::is_symlink(entry))
{
workers.push_back(std::make_unique<Worker>(fs::path(entry), options, eventsBuffer, eventsBufferMutex));
workers.push_back(std::make_unique<Worker>(false, fs::path(entry), options, eventsBuffer, eventsBufferMutex));
}
else
{
Expand Down Expand Up @@ -214,6 +216,7 @@ void DirectoryWatcher::Worker::ThreadProc()
overlapped.hEvent = watchEvent;
#endif

if (isRootWorker)
{
std::lock_guard<std::mutex> lock(*eventsBufferMutex);

Expand Down Expand Up @@ -468,7 +471,7 @@ void DirectoryWatcher::Worker::ThreadProc()

if (options.subtree && options.symlinks && fs::is_symlink(path) && fs::is_directory(path))
{
workers.push_back(std::make_unique<Worker>(path, options, eventsBuffer, eventsBufferMutex));
workers.push_back(std::make_unique<Worker>(false, path, options, eventsBuffer, eventsBufferMutex));
}

break;
Expand Down Expand Up @@ -543,7 +546,7 @@ void DirectoryWatcher::Worker::ThreadProc()

if (options.symlinks && fs::is_symlink(path) && fs::is_directory(path))
{
workers.push_back(std::make_unique<Worker>(path, options, eventsBuffer, eventsBufferMutex));
workers.push_back(std::make_unique<Worker>(false, path, options, eventsBuffer, eventsBufferMutex));
}
}

Expand Down Expand Up @@ -581,15 +584,16 @@ void DirectoryWatcher::Worker::ThreadProc()
}
#endif

{
std::lock_guard<std::mutex> lock(*eventsBufferMutex);
if (isRootWorker)
{
std::lock_guard<std::mutex> lock(*eventsBufferMutex);

auto change = std::make_unique<NotifyEvent>();
change->type = kStop;
change->path = basePath.string();
auto change = std::make_unique<NotifyEvent>();
change->type = kStop;
change->path = basePath.string();

eventsBuffer->push(std::move(change));
}
eventsBuffer->push(std::move(change));
}
}

DirectoryWatcher::DirectoryWatcher()
Expand All @@ -610,7 +614,8 @@ bool DirectoryWatcher::Watch(const std::filesystem::path &absPath, const WatchOp
return false;
}

workers.push_back(std::make_unique<Worker>(absPath, options, eventsBuffer.get(), eventsBufferMutex.get()));
auto worker = std::make_unique<Worker>(true, absPath, options, eventsBuffer.get(), eventsBufferMutex.get());
workers.push_back(std::move(worker));

return true;
}
Expand Down
3 changes: 2 additions & 1 deletion extension/watcher/watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class DirectoryWatcher
class Worker
{
public:
Worker(const std::filesystem::path &path, const WatchOptions &options, EventQueue *eventsBuffer, std::mutex *eventsBufferMutex);
Worker(bool isRoot, const std::filesystem::path &path, const WatchOptions &options, EventQueue *eventsBuffer, std::mutex *eventsBufferMutex);
~Worker();
inline bool IsRunning() const { return thread.joinable(); }

Expand All @@ -117,6 +117,7 @@ class DirectoryWatcher
void ThreadProc();

public:
bool isRootWorker;
const std::filesystem::path basePath;

private:
Expand Down

0 comments on commit 3e4b17c

Please sign in to comment.