app_fileHandle.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef APP_FILEHANDLE_H
  2. #define APP_FILEHANDLE_H
  3. #include <QDir>
  4. #include <sys/inotify.h>
  5. #include <unistd.h>
  6. #include <QSocketNotifier>
  7. #include <QMap>
  8. #include <QDebug>
  9. #include <app_curlFunc.h>
  10. #include <m_loger.h>
  11. #include <main.h>
  12. class DirectoryWatcher : public QObject {
  13. Q_OBJECT
  14. public:
  15. explicit DirectoryWatcher(QObject *parent = nullptr)
  16. : QObject(parent), m_inotifyFd(-1), m_notifier(nullptr)
  17. {
  18. initInotify();
  19. }
  20. ~DirectoryWatcher() {
  21. if (m_inotifyFd != -1) close(m_inotifyFd);
  22. }
  23. bool watchDirectory(const QString &path, uint32_t mask = IN_CLOSE_WRITE) { //IN_ALL_EVENTS
  24. // 1. 检查并创建目录
  25. QDir targetDir(path);
  26. if (!targetDir.exists()) {
  27. LOG("目录不存在,尝试创建: {}", path.toStdString());
  28. if (!targetDir.mkpath(".")) { // 递归创建目录
  29. ERROR("目录创建失败: {}", path.toStdString());
  30. return false;
  31. }
  32. }
  33. // 2. 添加inotify监控
  34. int wd = inotify_add_watch(m_inotifyFd,
  35. path.toStdString().c_str(),
  36. mask | IN_DELETE_SELF); // 监控目录自身删除事件
  37. if (wd == -1) {
  38. ERROR("添加监控失败: {} 错误: {}", path.toStdString(), strerror(errno));
  39. return false;
  40. }
  41. // 3. 记录监控项
  42. m_watchedPaths[wd] = path;
  43. LOG("开始监控目录: {}", path.toStdString());
  44. return true;
  45. }
  46. signals:
  47. void directoryCreated(const QString &path);
  48. void directoryDeleted(const QString &path);
  49. void fileCreated(const QString &path);
  50. void fileDeleted(const QString &path);
  51. void fileModified(const QString &path);
  52. private:
  53. void initInotify() {
  54. m_inotifyFd = inotify_init1(IN_NONBLOCK);
  55. if (m_inotifyFd == -1) {
  56. ERROR("inotify初始化失败: {}", strerror(errno));
  57. return;
  58. }
  59. m_notifier = new QSocketNotifier(m_inotifyFd,
  60. QSocketNotifier::Read,
  61. this);
  62. connect(m_notifier, &QSocketNotifier::activated,
  63. this, &DirectoryWatcher::handleInotifyEvent);
  64. }
  65. void handleInotifyEvent() {
  66. constexpr size_t EVENT_SIZE = sizeof(inotify_event);
  67. constexpr size_t BUF_LEN = 1024 * (EVENT_SIZE + 16);
  68. char buffer[BUF_LEN];
  69. ssize_t len = read(m_inotifyFd, buffer, BUF_LEN);
  70. for (char *ptr = buffer; ptr < buffer + len; ) {
  71. auto *event = reinterpret_cast<inotify_event*>(ptr);
  72. processEvent(event);
  73. ptr += EVENT_SIZE + event->len;
  74. }
  75. }
  76. void processEvent(const inotify_event *event) {
  77. const QString path = m_watchedPaths.value(event->wd, "");
  78. // 处理目录自身被删除的情况
  79. if (event->mask & IN_DELETE_SELF) {
  80. WARN("监控目录被删除: {}", path.toStdString());
  81. inotify_rm_watch(m_inotifyFd, event->wd);
  82. m_watchedPaths.remove(event->wd);
  83. emit directoryDeleted(path);
  84. return;
  85. }
  86. // 构建完整路径
  87. const QString name = QString::fromUtf8(event->name);
  88. const QString fullPath = QDir(path).filePath(name);
  89. // 分发事件
  90. if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
  91. if (event->mask & IN_ISDIR) {
  92. LOG("新目录创建: {}", fullPath.toStdString());
  93. emit directoryCreated(fullPath);
  94. } else {
  95. LOG("新文件创建: {}", fullPath.toStdString());
  96. emit fileCreated(fullPath);
  97. }
  98. }
  99. if (event->mask & (IN_DELETE | IN_MOVED_FROM)) {
  100. if (event->mask & IN_ISDIR) {
  101. LOG("目录删除: {}", fullPath.toStdString());
  102. emit directoryDeleted(fullPath);
  103. } else {
  104. LOG("文件删除: {}", fullPath.toStdString());
  105. emit fileDeleted(fullPath);
  106. }
  107. }
  108. if (event->mask & IN_MODIFY) {
  109. LOG("文件修改: {}", fullPath.toStdString());
  110. emit fileModified(fullPath);
  111. }
  112. if (event->mask & IN_CLOSE_WRITE) {
  113. LOG("文件写入到文件系统完成:{}",fullPath.toStdString());
  114. emit fileCreated(fullPath);
  115. }
  116. }
  117. int m_inotifyFd;
  118. QSocketNotifier *m_notifier;
  119. QMap<int, QString> m_watchedPaths;
  120. };
  121. #endif // APP_FILEHANDLE_H