Skip to content

Commit

Permalink
Cleaned up FileSystemWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Oct 1, 2023
1 parent c0b17ad commit 6f697b4
Showing 1 changed file with 2 additions and 77 deletions.
79 changes: 2 additions & 77 deletions Source/Utility/FileSystemWatcher.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,78 +27,6 @@ For more information visit www.rabiensoftware.com
#include <sys/time.h>
#endif

#if JUCE_MAC
class FileSystemWatcher::Impl
{
public:
Impl (FileSystemWatcher& o, File f) : owner (o), folder (f)
{
NSString* newPath = [NSString stringWithUTF8String:folder.getFullPathName().toRawUTF8()];

paths = [[NSArray arrayWithObject:newPath] retain];
context.version = 0L;
context.info = this;
context.retain = nil;
context.release = nil;
context.copyDescription = nil;

stream = FSEventStreamCreate (kCFAllocatorDefault, callback, &context, (CFArrayRef)paths, kFSEventStreamEventIdSinceNow, 0.05,
kFSEventStreamCreateFlagNoDefer | kFSEventStreamCreateFlagFileEvents);
if (stream)
{
FSEventStreamScheduleWithRunLoop (stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart (stream);
}

}

~Impl()
{
if (stream)
{
FSEventStreamStop (stream);
FSEventStreamUnscheduleFromRunLoop (stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamInvalidate (stream);
FSEventStreamRelease (stream);
}
}

static void callback (ConstFSEventStreamRef streamRef, void* clientCallBackInfo, size_t numEvents, void* eventPaths,
const FSEventStreamEventFlags* eventFlags, const FSEventStreamEventId* eventIds)
{
ignoreUnused (streamRef, numEvents, eventIds, eventPaths, eventFlags);

Impl* impl = (Impl*)clientCallBackInfo;
impl->owner.folderChanged (impl->folder);

char** files = (char**)eventPaths;

for (int i = 0; i < int (numEvents); i++)
{
char* file = files[i];
FSEventStreamEventFlags evt = eventFlags[i];

File path = String::fromUTF8 (file);
if (evt & kFSEventStreamEventFlagItemModified)
impl->owner.fileChanged (path, FileSystemEvent::fileUpdated);
else if (evt & kFSEventStreamEventFlagItemRemoved)
impl->owner.fileChanged (path, FileSystemEvent::fileDeleted);
else if (evt & kFSEventStreamEventFlagItemRenamed)
impl->owner.fileChanged (path, path.exists() ? FileSystemEvent::fileRenamedNewName : FileSystemEvent::fileRenamedOldName);
else if (evt & kFSEventStreamEventFlagItemCreated)
impl->owner.fileChanged (path, FileSystemEvent::fileCreated);
}
}

FileSystemWatcher& owner;
const File folder;

NSArray* paths;
FSEventStreamRef stream;
struct FSEventStreamContext context;
};
#endif

#ifdef JUCE_LINUX
#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))

Expand Down Expand Up @@ -136,7 +64,6 @@ class FileSystemWatcher::Impl : public Thread,

~Impl()
{
shouldQuit = true;
signalThreadShouldExit();
inotify_rm_watch (fd, wd);
close (fd);
Expand All @@ -152,7 +79,7 @@ class FileSystemWatcher::Impl : public Thread,
const struct inotify_event* iNotifyEvent;
char* ptr;

while (!shouldQuit)
while (!threadShouldExit())
{
int numRead = read (fd, buf, BUF_LEN);

Expand Down Expand Up @@ -194,9 +121,8 @@ class FileSystemWatcher::Impl : public Thread,

void handleAsyncUpdate() override
{
if(shouldQuit) return;

ScopedLock sl (lock);
if(threadShouldExit()) return;

owner.folderChanged (folder);

Expand All @@ -206,7 +132,6 @@ class FileSystemWatcher::Impl : public Thread,
events.clear();
}

std::atomic<bool> shouldQuit = false;
FileSystemWatcher& owner;
File folder;

Expand Down

0 comments on commit 6f697b4

Please sign in to comment.