| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #ifndef APP_FILEHANDLE_H
- #define APP_FILEHANDLE_H
- #include <QDir>
- #include <sys/inotify.h>
- #include <unistd.h>
- #include <QSocketNotifier>
- #include <QMap>
- #include <QDebug>
- #include <app_curlFunc.h>
- #include <m_loger.h>
- #include <main.h>
- 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<inotify_event*>(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<int, QString> m_watchedPaths;
- };
- #endif // APP_FILEHANDLE_H
|