void UDirectoryWatcher::Watch(const FString& InDirectory) { Unwatch(); FDirectoryWatcherModule& DirectoryWatcherModule = FModuleManager::Get().LoadModuleChecked<FDirectoryWatcherModule>(TEXT("DirectoryWatcher")); IDirectoryWatcher* DirectoryWatcher = DirectoryWatcherModule.Get(); Directory = FPaths::IsRelative(InDirectory) ? FPaths::ConvertRelativePathToFull(InDirectory) : InDirectory; Changed = IDirectoryWatcher::FDirectoryChanged::CreateLambda([&](const TArray<FFileChangeData>& FileChanges){ for (auto Change : FileChanges) { FPaths::NormalizeFilename(Change.Filename); switch (Change.Action) { case FFileChangeData::FCA_Added: Added.Add(Change.Filename); break; case FFileChangeData::FCA_Modified: Modified.Add(Change.Filename); break; case FFileChangeData::FCA_Removed: Removed.Add(Change.Filename); break; } } OnChanged.Broadcast(); Added.Empty(); Modified.Empty(); Removed.Empty(); }); if (IFileManager::Get().DirectoryExists(*Directory)) { DirectoryWatcher->RegisterDirectoryChangedCallback_Handle(Directory, Changed, DelegateHandle, true); } }
bool InotifyDir::EventPoll(Event& event) { for (;;) { if (!m_pendingEv.empty()) { event = m_pendingEv.front(); m_pendingEv.pop_front(); return true; } if (m_buffIdx >= m_buffLen) { m_buffIdx = 0; m_buffLen = read(m_wfd, m_buff, sizeof(m_buff)); if (m_buffLen <= 0) { return false; } } inotify_event* ev = (inotify_event*)&m_buff[m_buffIdx]; m_buffIdx += ev->len + sizeof(inotify_event); if (ev->mask & (IN_IGNORED | IN_UNMOUNT)) { Unwatch(ev->wd); continue; } if (ev->mask & (IN_CREATE | IN_MOVED_TO)) { if (ev->mask & IN_ISDIR) { std::string s = m_watchFd[ev->wd] + ev->name; if (ev->mask & IN_MOVED_TO) { AddPathEvent(s.c_str(), ADD); } continue; } } if (ev->mask & IN_MOVED_FROM && ev->mask & IN_ISDIR) { //event can't catch continue; } if (ev->mask & IN_MOVE_SELF) { continue; } if (ev->mask & IN_DELETE) { if (ev->mask & IN_ISDIR) { continue; } } event.clear(); if (ev->mask & (IN_MOVED_TO | IN_CREATE)) { event.type = ADD; } else if (ev->mask & (IN_MOVED_FROM | IN_DELETE)) { event.type = DELETE; } else if (ev->mask & IN_MODIFY) { event.type = MODIFY; } else { event.type = UNKNOWN; } event.name = m_watchFd[ev->wd] + ev->name; return true; } }
void UDirectoryWatcher::BeginDestroy() { Super::BeginDestroy(); Unwatch(); }