#ifndef APP_FILEHANDLE_H #define APP_FILEHANDLE_H #include #include #include #include #include #include #include #include #include class DirectoryWatcher : public QObject { Q_OBJECT public: explicit DirectoryWatcher(QObject *parent = nullptr) : QObject(parent), m_inotifyFd(-1), m_notifier(nullptr) { initInotify(); } ~DirectoryWatcher() { if (m_inotifyFd != -1) close(m_inotifyFd); } bool watchDirectory(const QString &path, uint32_t mask = IN_CLOSE_WRITE) { //IN_ALL_EVENTS // 1. 检查并创建目录 QDir targetDir(path); if (!targetDir.exists()) { LOG("目录不存在,尝试创建: {}", path.toStdString()); if (!targetDir.mkpath(".")) { // 递归创建目录 ERROR("目录创建失败: {}", path.toStdString()); return false; } } // 2. 添加inotify监控 int wd = inotify_add_watch(m_inotifyFd, path.toStdString().c_str(), mask | IN_DELETE_SELF); // 监控目录自身删除事件 if (wd == -1) { ERROR("添加监控失败: {} 错误: {}", path.toStdString(), strerror(errno)); return false; } // 3. 记录监控项 m_watchedPaths[wd] = path; LOG("开始监控目录: {}", path.toStdString()); return true; } signals: void directoryCreated(const QString &path); void directoryDeleted(const QString &path); void fileCreated(const QString &path); void fileDeleted(const QString &path); void fileModified(const QString &path); private: void initInotify() { m_inotifyFd = inotify_init1(IN_NONBLOCK); if (m_inotifyFd == -1) { ERROR("inotify初始化失败: {}", strerror(errno)); return; } m_notifier = new QSocketNotifier(m_inotifyFd, QSocketNotifier::Read, this); connect(m_notifier, &QSocketNotifier::activated, this, &DirectoryWatcher::handleInotifyEvent); } void handleInotifyEvent() { constexpr size_t EVENT_SIZE = sizeof(inotify_event); constexpr size_t BUF_LEN = 1024 * (EVENT_SIZE + 16); char buffer[BUF_LEN]; ssize_t len = read(m_inotifyFd, buffer, BUF_LEN); for (char *ptr = buffer; ptr < buffer + len; ) { auto *event = reinterpret_cast(ptr); processEvent(event); ptr += EVENT_SIZE + event->len; } } void processEvent(const inotify_event *event) { const QString path = m_watchedPaths.value(event->wd, ""); // 处理目录自身被删除的情况 if (event->mask & IN_DELETE_SELF) { WARN("监控目录被删除: {}", path.toStdString()); inotify_rm_watch(m_inotifyFd, event->wd); m_watchedPaths.remove(event->wd); emit directoryDeleted(path); return; } // 构建完整路径 const QString name = QString::fromUtf8(event->name); const QString fullPath = QDir(path).filePath(name); // 分发事件 if (event->mask & (IN_CREATE | IN_MOVED_TO)) { if (event->mask & IN_ISDIR) { LOG("新目录创建: {}", fullPath.toStdString()); emit directoryCreated(fullPath); } else { LOG("新文件创建: {}", fullPath.toStdString()); emit fileCreated(fullPath); } } if (event->mask & (IN_DELETE | IN_MOVED_FROM)) { if (event->mask & IN_ISDIR) { LOG("目录删除: {}", fullPath.toStdString()); emit directoryDeleted(fullPath); } else { LOG("文件删除: {}", fullPath.toStdString()); emit fileDeleted(fullPath); } } if (event->mask & IN_MODIFY) { LOG("文件修改: {}", fullPath.toStdString()); emit fileModified(fullPath); } if (event->mask & IN_CLOSE_WRITE) { LOG("文件写入到文件系统完成:{}",fullPath.toStdString()); emit fileCreated(fullPath); } } int m_inotifyFd; QSocketNotifier *m_notifier; QMap m_watchedPaths; }; #endif // APP_FILEHANDLE_H