diff --git a/FileWatch.hpp b/FileWatch.hpp index 4eba08b..d16cc4b 100644 --- a/FileWatch.hpp +++ b/FileWatch.hpp @@ -1003,7 +1003,7 @@ namespace filewatch { eventInfos[0].file = _filename; } else { - StringType absPath = pathOfFd(_file_fd); + StringType absPath = fullPathOfFd(_file_fd); PathParts split = splitPath(absPath); if (split.directory != _path) { @@ -1117,29 +1117,48 @@ namespace filewatch { _cv.notify_all(); } - static void handleFsEvent(__attribute__((unused)) ConstFSEventStreamRef streamFef, - void* clientCallBackInfo, - size_t numEvents, - CFArrayRef eventPaths, - const FSEventStreamEventFlags* eventFlags, - __attribute__((unused)) const FSEventStreamEventId* eventIds) { - FileWatch* self = (FileWatch*)clientCallBackInfo; - - for (size_t i = 0; i < numEvents; i++) { - FSEventStreamEventFlags flag = eventFlags[i]; - CFStringRef path = (CFStringRef)CFArrayGetValueAtIndex(eventPaths, i); + static void handleFsEvent(__attribute__((unused)) ConstFSEventStreamRef streamFef, + void *clientCallBackInfo, + size_t numEvents, + CFArrayRef eventPaths, + const FSEventStreamEventFlags *eventFlags, + __attribute__((unused)) const FSEventStreamEventId *eventIds) + { + FileWatch *self = (FileWatch *)clientCallBackInfo; - if (self->_watching_single_file) { - self->seeSingleFileChanges(); - } - else if (flag & kFSEventStreamEventFlagMustScanSubDirs) { - self->walkAndSeeChanges(); - } - else { - self->notify(path, flag); - } - } - } + for (size_t i = 0; i < numEvents; i++) + { + FSEventStreamEventFlags flag = eventFlags[i]; + CFStringRef path = (CFStringRef)CFArrayGetValueAtIndex(eventPaths, i); + + // Convert CFStringRef to StringType + CFIndex pathLength = CFStringGetLength(path); + char buffer[PATH_MAX + 1]; + CFStringGetCString(path, buffer, PATH_MAX, IsWChar::value ? kCFStringEncodingUTF32 : kCFStringEncodingUTF8); + StringType absolutePath = StringType { (const C *)buffer, static_cast(pathLength) }; + PathParts pathPair = self->splitPath(absolutePath); + + // Ensure the event belongs to the monitored file or directory + if (self->_watching_single_file) + { + if (pathPair.filename == self->_filename) + { + self->seeSingleFileChanges(); + } + } + else if (flag & kFSEventStreamEventFlagMustScanSubDirs) + { + if (isInDirectory(absolutePath, self->_path)) + { + self->walkAndSeeChanges(); + } + } + else + { + self->notify(path, flag); + } + } + } FSEventStreamRef openStream(const StringType& directory) { CFStringEncoding encoding = IsWChar::value?