2021-06-10 20:43:57 +08:00
|
|
|
#include "inotify-watch.h"
|
2022-03-04 15:15:07 +08:00
|
|
|
#include "dir-watcher.h"
|
2021-06-10 20:43:57 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <malloc.h>
|
2021-06-25 16:30:46 +08:00
|
|
|
#include <errno.h>
|
2021-12-14 14:43:35 +08:00
|
|
|
using namespace UkuiSearch;
|
2021-06-10 20:43:57 +08:00
|
|
|
static InotifyWatch* global_instance_InotifyWatch = nullptr;
|
|
|
|
|
2022-03-04 15:15:07 +08:00
|
|
|
UkuiSearch::InotifyWatch *UkuiSearch::InotifyWatch::getInstance()
|
2021-06-10 20:43:57 +08:00
|
|
|
{
|
|
|
|
if(!global_instance_InotifyWatch) {
|
2022-03-04 15:15:07 +08:00
|
|
|
global_instance_InotifyWatch = new InotifyWatch();
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
return global_instance_InotifyWatch;
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:16:26 +08:00
|
|
|
UkuiSearch::InotifyWatch::InotifyWatch(): Traverse_BFS(), m_semaphore(INDEX_SEM, 0, QSystemSemaphore::AccessMode::Open)
|
2021-06-10 20:43:57 +08:00
|
|
|
{
|
|
|
|
qDebug() << "setInotifyMaxUserWatches start";
|
|
|
|
UkuiSearchQDBus usQDBus;
|
|
|
|
usQDBus.setInotifyMaxUserWatches();
|
|
|
|
qDebug() << "setInotifyMaxUserWatches end";
|
|
|
|
m_sharedMemory = new QSharedMemory("ukui-search-shared-map", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
InotifyWatch::~InotifyWatch()
|
|
|
|
{
|
|
|
|
if(m_notifier)
|
|
|
|
delete m_notifier;
|
|
|
|
m_notifier = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InotifyWatch::addWatch(const QString &path)
|
|
|
|
{
|
|
|
|
int ret = inotify_add_watch(m_inotifyFd, path.toStdString().c_str(), (IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE | IN_DELETE | IN_MODIFY));
|
|
|
|
if(ret == -1) {
|
|
|
|
qWarning() << "AddWatch error:" << path;
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-11 17:46:13 +08:00
|
|
|
m_pathMap[ret] = path;
|
2021-06-10 20:43:57 +08:00
|
|
|
// qDebug() << "Watch: " << path << "ret: " << ret;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InotifyWatch::removeWatch(const QString &path, bool removeFromDatabase)
|
|
|
|
{
|
2022-04-11 17:46:13 +08:00
|
|
|
inotify_rm_watch(m_inotifyFd, m_pathMap.key(path));
|
2021-06-10 20:43:57 +08:00
|
|
|
|
|
|
|
if(removeFromDatabase) {
|
2022-04-11 17:46:13 +08:00
|
|
|
for(QMap<int, QString>::Iterator i = m_pathMap.begin(); i != m_pathMap.end();) {
|
2021-06-10 20:43:57 +08:00
|
|
|
// qDebug() << i.value();
|
|
|
|
// if(i.value().length() > path.length()) {
|
2021-07-06 16:53:32 +08:00
|
|
|
if(FileUtils::isOrUnder(i.value(), path)) {
|
2021-06-10 20:43:57 +08:00
|
|
|
qDebug() << "remove path: " << i.value();
|
2022-04-11 17:46:13 +08:00
|
|
|
inotify_rm_watch(m_inotifyFd, m_pathMap.key(path));
|
2021-06-10 20:43:57 +08:00
|
|
|
PendingFile f(i.value());
|
|
|
|
f.setDeleted();
|
|
|
|
f.setIsDir();
|
|
|
|
PendingFileQueue::getInstance()->enqueue(f);
|
2022-04-11 17:46:13 +08:00
|
|
|
m_pathMap.erase(i++);
|
2021-06-10 20:43:57 +08:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-11 17:46:13 +08:00
|
|
|
for(QMap<int, QString>::Iterator i = m_pathMap.begin(); i != m_pathMap.end();) {
|
2021-06-10 20:43:57 +08:00
|
|
|
// qDebug() << i.value();
|
|
|
|
if(i.value().length() > path.length()) {
|
2021-07-06 16:53:32 +08:00
|
|
|
if(FileUtils::isOrUnder(i.value(), path)) {
|
|
|
|
// if(i.value().startsWith(path + "/")) {
|
2021-06-25 16:30:46 +08:00
|
|
|
// qDebug() << "remove path: " << i.value();
|
2022-04-11 17:46:13 +08:00
|
|
|
inotify_rm_watch(m_inotifyFd, m_pathMap.key(path));
|
|
|
|
m_pathMap.erase(i++);
|
2021-06-10 20:43:57 +08:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 17:46:13 +08:00
|
|
|
m_pathMap.remove(m_pathMap.key(path));
|
2021-06-10 20:43:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:16:26 +08:00
|
|
|
void InotifyWatch::work(const QFileInfo &info)
|
2021-06-10 20:43:57 +08:00
|
|
|
{
|
|
|
|
qDebug() << info.fileName() << "-------" << info.absoluteFilePath();
|
|
|
|
if(info.isDir() && (!info.isSymLink())) {
|
|
|
|
this->addWatch(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
PendingFile f(info.absoluteFilePath());
|
|
|
|
if(info.isDir()) {
|
|
|
|
f.setIsDir();
|
|
|
|
}
|
|
|
|
PendingFileQueue::getInstance()->enqueue(f);
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:16:26 +08:00
|
|
|
void InotifyWatch::firstTraverse(QStringList pathList, QStringList blockList)
|
2021-06-10 20:43:57 +08:00
|
|
|
{
|
2022-04-14 11:16:26 +08:00
|
|
|
if(pathList.isEmpty()) {
|
|
|
|
pathList = m_pathList;
|
|
|
|
}
|
|
|
|
if(blockList.isEmpty()) {
|
|
|
|
blockList = m_blockList;
|
|
|
|
}
|
|
|
|
|
2021-06-10 20:43:57 +08:00
|
|
|
QQueue<QString> bfs;
|
2022-04-14 11:16:26 +08:00
|
|
|
for(QString blockPath : blockList) {
|
|
|
|
for(QString path : pathList) {
|
2022-03-04 15:15:07 +08:00
|
|
|
if(FileUtils::isOrUnder(path, blockPath)) {
|
2022-04-14 11:16:26 +08:00
|
|
|
pathList.removeOne(path);
|
2022-03-04 15:15:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 11:16:26 +08:00
|
|
|
for(QString path : pathList) {
|
2022-03-04 15:15:07 +08:00
|
|
|
addWatch(path);
|
|
|
|
bfs.enqueue(path);
|
|
|
|
}
|
|
|
|
|
2021-06-10 20:43:57 +08:00
|
|
|
QFileInfoList list;
|
|
|
|
QDir dir;
|
2022-04-14 11:16:26 +08:00
|
|
|
QStringList tmpList = blockList;
|
2021-06-10 20:43:57 +08:00
|
|
|
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
dir.setSorting(QDir::DirsFirst);
|
|
|
|
while(!bfs.empty()) {
|
|
|
|
dir.setPath(bfs.dequeue());
|
|
|
|
list = dir.entryInfoList();
|
|
|
|
for(auto i : list) {
|
2022-03-04 15:15:07 +08:00
|
|
|
bool isBlocked = false;
|
|
|
|
for(QString path : tmpList) {
|
|
|
|
if(i.absoluteFilePath() == path) {
|
|
|
|
isBlocked = true;
|
|
|
|
tmpList.removeOne(path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isBlocked)
|
|
|
|
continue;
|
|
|
|
|
2021-06-10 20:43:57 +08:00
|
|
|
if(i.isDir() && (!(i.isSymLink()))) {
|
2022-03-04 15:15:07 +08:00
|
|
|
addWatch(i.absoluteFilePath());
|
2021-06-10 20:43:57 +08:00
|
|
|
bfs.enqueue(i.absoluteFilePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:16:26 +08:00
|
|
|
void InotifyWatch::addIndexPath(const QString path, const QStringList blockList)
|
|
|
|
{
|
|
|
|
this->firstTraverse(QStringList() << path, blockList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InotifyWatch::removeIndexPath(QString &path)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-10 20:43:57 +08:00
|
|
|
void InotifyWatch::stopWatch()
|
|
|
|
{
|
|
|
|
// if(this->isRunning()) {
|
|
|
|
// this->quit();
|
|
|
|
// if(m_notifier)
|
|
|
|
// delete m_notifier;
|
|
|
|
// m_notifier = nullptr;
|
|
|
|
// removeWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation), false);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "3");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InotifyWatch::run()
|
|
|
|
{
|
|
|
|
m_inotifyFd = inotify_init();
|
|
|
|
if (m_inotifyFd > 0) {
|
|
|
|
qDebug()<<"Inotify init success!";
|
|
|
|
} else {
|
2021-06-30 10:09:25 +08:00
|
|
|
qWarning() << "Inotify init fail! Now try add inotify_user_instances.";
|
|
|
|
UkuiSearchQDBus usQDBus;
|
|
|
|
usQDBus.addInotifyUserInstances(128);
|
|
|
|
m_inotifyFd = inotify_init();
|
|
|
|
if (m_inotifyFd > 0) {
|
|
|
|
qDebug()<<"Inotify init success!";
|
|
|
|
} else {
|
|
|
|
printf("errno=%d\n",errno);
|
|
|
|
printf("Mesg:%s\n",strerror(errno));
|
|
|
|
Q_ASSERT_X(0, "InotifyWatch", "Failed to initialize inotify");
|
|
|
|
}
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
|
2022-04-25 10:23:20 +08:00
|
|
|
setPath(DirWatcher::getDirWatcher()->currentIndexableDir());
|
2022-03-04 15:15:07 +08:00
|
|
|
setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex());
|
|
|
|
firstTraverse();
|
2021-06-10 20:43:57 +08:00
|
|
|
|
|
|
|
while(FileUtils::SearchMethod::INDEXSEARCH == FileUtils::searchMethod) {
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(m_inotifyFd, &fds);
|
|
|
|
int rc;
|
|
|
|
rc = select(m_inotifyFd + 1, &fds, NULL, NULL, NULL);
|
|
|
|
if(rc > 0) {
|
2021-11-09 10:07:41 +08:00
|
|
|
++FileUtils::indexStatus;
|
2021-06-10 20:43:57 +08:00
|
|
|
int avail;
|
|
|
|
if (ioctl(m_inotifyFd, FIONREAD, &avail) == EINVAL) {
|
|
|
|
qWarning() << "Did not receive an entire inotify event.";
|
2021-11-09 10:07:41 +08:00
|
|
|
--FileUtils::indexStatus;
|
2021-06-10 20:43:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buf = (char*)malloc(avail);
|
|
|
|
memset(buf, 0x00, avail);
|
|
|
|
|
|
|
|
const ssize_t len = read(m_inotifyFd, buf, avail);
|
|
|
|
if(len != avail) {
|
|
|
|
qWarning()<<"read event error";
|
|
|
|
// IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
while (i < len) {
|
|
|
|
const struct inotify_event* event = (struct inotify_event*)&buf[i];
|
|
|
|
if(event->name[0] != '.') {
|
|
|
|
// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
|
|
|
|
// qDebug("mask:0x%x,",event->mask);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += sizeof(struct inotify_event) + event->len;
|
|
|
|
}
|
|
|
|
if(i < len ) {
|
|
|
|
qDebug() << "fork";
|
|
|
|
slotEvent(buf, len);
|
|
|
|
free(buf);
|
|
|
|
}
|
2021-11-09 10:07:41 +08:00
|
|
|
--FileUtils::indexStatus;
|
2021-06-11 19:58:32 +08:00
|
|
|
} else if(rc < 0) {
|
|
|
|
// error
|
|
|
|
qWarning() << "select result < 0, error!";
|
|
|
|
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1");
|
|
|
|
assert(false);
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-25 16:30:46 +08:00
|
|
|
qDebug() << "Leave watch loop";
|
2021-06-10 20:43:57 +08:00
|
|
|
if(FileUtils::SearchMethod::DIRECTSEARCH == FileUtils::searchMethod) {
|
|
|
|
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "3");
|
2022-04-11 17:46:13 +08:00
|
|
|
for(QString path : m_pathMap) {
|
|
|
|
inotify_rm_watch(m_inotifyFd, m_pathMap.key(path));
|
2022-04-09 17:32:25 +08:00
|
|
|
}
|
2022-04-11 17:46:13 +08:00
|
|
|
m_pathMap.clear();
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
2021-06-25 16:30:46 +08:00
|
|
|
close(m_inotifyFd);
|
2021-06-10 20:43:57 +08:00
|
|
|
// fcntl(m_inotifyFd, F_SETFD, FD_CLOEXEC);
|
|
|
|
// m_notifier = new QSocketNotifier(m_inotifyFd, QSocketNotifier::Read);
|
|
|
|
// connect(m_notifier, &QSocketNotifier::activated, this, &InotifyWatch::slotEvent, Qt::DirectConnection);
|
|
|
|
// exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InotifyWatch::slotEvent(char *buf, ssize_t len)
|
|
|
|
{
|
|
|
|
// eventProcess(socket);
|
|
|
|
if(FileUtils::SearchMethod::INDEXSEARCH == FileUtils::searchMethod) {
|
|
|
|
pid_t pid;
|
|
|
|
pid = fork();
|
|
|
|
if(pid == 0) {
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
|
|
|
prctl(PR_SET_NAME, "inotify-index");
|
|
|
|
this->eventProcess(buf, len);
|
|
|
|
fd_set read_fds;
|
|
|
|
int rc;
|
|
|
|
timeval* read_timeout = (timeval*)malloc(sizeof(timeval));
|
|
|
|
read_timeout->tv_sec = 40;
|
|
|
|
read_timeout->tv_usec = 0;
|
|
|
|
for(;;) {
|
|
|
|
FD_ZERO(&read_fds);
|
|
|
|
FD_SET(m_inotifyFd, &read_fds);
|
|
|
|
rc = select(m_inotifyFd + 1, &read_fds, NULL, NULL, read_timeout);
|
|
|
|
if(rc < 0) {
|
|
|
|
// error
|
2021-06-11 19:58:32 +08:00
|
|
|
qWarning() << "fork select result < 0, error!";
|
2021-06-10 20:43:57 +08:00
|
|
|
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1");
|
|
|
|
assert(false);
|
|
|
|
} else if(rc == 0) {
|
|
|
|
qDebug() << "select timeout!";
|
|
|
|
::free(read_timeout);
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
QDataStream out(&buffer);
|
|
|
|
if (m_sharedMemory->isAttached()) {
|
|
|
|
m_sharedMemory->detach();
|
|
|
|
}
|
|
|
|
buffer.open(QBuffer::ReadWrite);
|
2022-04-11 17:46:13 +08:00
|
|
|
out << m_pathMap;
|
2021-06-10 20:43:57 +08:00
|
|
|
int size = buffer.size();
|
|
|
|
if (!m_sharedMemory->create(size)) {
|
|
|
|
qDebug() << "Create sharedMemory Error: " << m_sharedMemory->errorString();
|
|
|
|
} else {
|
|
|
|
m_sharedMemory->lock();
|
|
|
|
char *to = static_cast<char *>(m_sharedMemory->data());
|
|
|
|
const char *from = buffer.data().constData();
|
|
|
|
memcpy(to, from, qMin(size, m_sharedMemory->size()));
|
|
|
|
m_sharedMemory->unlock();
|
|
|
|
}
|
|
|
|
// GlobalSettings::getInstance()->forceSync();
|
|
|
|
PendingFileQueue::getInstance()->forceFinish();
|
|
|
|
PendingFileQueue::getInstance()->~PendingFileQueue();
|
|
|
|
::_exit(0);
|
|
|
|
} else {
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "Select remain:" <<read_timeout->tv_sec;
|
2021-06-10 20:43:57 +08:00
|
|
|
this->eventProcess(m_inotifyFd);
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "Select remain:" <<read_timeout->tv_sec;
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(pid > 0) {
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
if (!m_sharedMemory->attach()) {
|
|
|
|
qDebug() << "SharedMemory attach Error: " << m_sharedMemory->errorString();
|
|
|
|
} else {
|
|
|
|
QBuffer buffer;
|
|
|
|
QDataStream in(&buffer);
|
|
|
|
QMap<int, QString> pathMap;
|
|
|
|
m_sharedMemory->lock();
|
|
|
|
buffer.setData(static_cast<const char *>(m_sharedMemory->constData()), m_sharedMemory->size());
|
|
|
|
buffer.open(QBuffer::ReadWrite);
|
|
|
|
in >> pathMap;
|
|
|
|
m_sharedMemory->unlock();
|
|
|
|
m_sharedMemory->detach();
|
2022-04-11 17:46:13 +08:00
|
|
|
m_pathMap = pathMap;
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char * InotifyWatch::filter()
|
|
|
|
{
|
|
|
|
int avail;
|
|
|
|
if (ioctl(m_inotifyFd, FIONREAD, &avail) == EINVAL) {
|
|
|
|
qWarning() << "Did not receive an entire inotify event.";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer = (char*)malloc(avail);
|
|
|
|
memset(buffer, 0x00, avail);
|
|
|
|
|
|
|
|
const int len = read(m_inotifyFd, buffer, avail);
|
|
|
|
if(len != avail) {
|
|
|
|
qWarning()<<"read event error";
|
|
|
|
// IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
while (i < len) {
|
|
|
|
const struct inotify_event* event = (struct inotify_event*)&buffer[i];
|
|
|
|
if(event->name[0] == '.') {
|
|
|
|
// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
|
|
|
|
// qDebug("mask:0x%x,",event->mask);
|
|
|
|
i += sizeof(struct inotify_event) + event->len;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void InotifyWatch::eventProcess(int socket)
|
|
|
|
{
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug()<< "Enter eventProcess!";
|
2021-06-10 20:43:57 +08:00
|
|
|
int avail;
|
|
|
|
if (ioctl(socket, FIONREAD, &avail) == EINVAL) {
|
|
|
|
qWarning() << "Did not receive an entire inotify event.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer = (char*)malloc(avail);
|
|
|
|
memset(buffer, 0x00, avail);
|
|
|
|
|
|
|
|
const ssize_t len = read(socket, buffer, avail);
|
|
|
|
if(len != avail) {
|
|
|
|
qWarning()<<"read event error";
|
|
|
|
// IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "1");
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
while (i < len) {
|
|
|
|
const struct inotify_event* event = (struct inotify_event*)&buffer[i];
|
|
|
|
if(event->name[0] != '.') {
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
|
|
|
|
// qDebug("mask:0x%x,",event->mask);
|
2021-06-10 20:43:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += sizeof(struct inotify_event) + event->len;
|
|
|
|
}
|
|
|
|
if(i >= len) {
|
|
|
|
qDebug() << "There is nothing to do!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
eventProcess(buffer, len);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InotifyWatch::eventProcess(const char *buffer, ssize_t len)
|
|
|
|
{
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug()<< "Begin eventProcess! len:" << len;
|
2021-06-10 20:43:57 +08:00
|
|
|
|
|
|
|
char * p = const_cast<char*>(buffer);
|
|
|
|
while (p < buffer + len) {
|
|
|
|
const struct inotify_event* event = reinterpret_cast<inotify_event *>(p);
|
|
|
|
// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
|
|
|
|
// qDebug("mask:0x%x,",event->mask);
|
|
|
|
if(event->name[0] != '.') {
|
2022-04-11 17:46:13 +08:00
|
|
|
QString path = m_pathMap[event->wd] + '/' + event->name;
|
2022-03-04 15:15:07 +08:00
|
|
|
|
|
|
|
//过滤黑名单下的信号
|
|
|
|
for(QString i : m_blockList) {
|
|
|
|
if(FileUtils::isOrUnder(path, i))
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2021-06-10 20:43:57 +08:00
|
|
|
//Create top dir first, traverse it last.
|
|
|
|
if(event->mask & IN_CREATE) {
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "IN_CREATE";
|
2021-06-10 20:43:57 +08:00
|
|
|
PendingFile f(path);
|
|
|
|
if(event->mask & IN_ISDIR) {
|
|
|
|
f.setIsDir();
|
|
|
|
}
|
2022-04-07 15:45:40 +08:00
|
|
|
PendingFileQueue::getInstance()->enqueue(f);
|
2021-06-10 20:43:57 +08:00
|
|
|
|
|
|
|
if(event->mask & IN_ISDIR) {
|
|
|
|
if(!QFileInfo(path).isSymLink()){
|
|
|
|
addWatch(path);
|
2022-03-04 15:15:07 +08:00
|
|
|
setPath(QStringList() << path);
|
2021-06-10 20:43:57 +08:00
|
|
|
Traverse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)) {
|
|
|
|
qDebug() << "IN_DELETE or IN_MOVED_FROM";
|
|
|
|
if(event->mask & IN_ISDIR) {
|
|
|
|
removeWatch(path);
|
|
|
|
} else {
|
|
|
|
PendingFile f(path);
|
|
|
|
f.setDeleted();
|
|
|
|
PendingFileQueue::getInstance()->enqueue(f);
|
|
|
|
}
|
|
|
|
p += sizeof(struct inotify_event) + event->len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(event->mask & IN_MODIFY) {
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug() << "IN_MODIFY";
|
2021-06-10 20:43:57 +08:00
|
|
|
if(!(event->mask & IN_ISDIR)) {
|
|
|
|
PendingFileQueue::getInstance()->enqueue(PendingFile(path));
|
|
|
|
}
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->mask & IN_MOVED_TO) {
|
|
|
|
qDebug() << "IN_MOVED_TO";
|
|
|
|
if(event->mask & IN_ISDIR) {
|
|
|
|
removeWatch(path);
|
|
|
|
|
|
|
|
PendingFile f(path);
|
|
|
|
f.setIsDir();
|
|
|
|
PendingFileQueue::getInstance()->enqueue(f);
|
|
|
|
|
|
|
|
if(!QFileInfo(path).isSymLink()){
|
|
|
|
addWatch(path);
|
2022-03-04 15:15:07 +08:00
|
|
|
setPath(QStringList() << path);
|
2021-06-10 20:43:57 +08:00
|
|
|
Traverse();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Enqueue a deleted file to merge.
|
|
|
|
PendingFile f(path);
|
|
|
|
f.setDeleted();
|
|
|
|
PendingFileQueue::getInstance()->enqueue(f);
|
|
|
|
//Enqueue a new one.
|
|
|
|
PendingFileQueue::getInstance()->enqueue(PendingFile(path));
|
|
|
|
}
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next:
|
|
|
|
p += sizeof(struct inotify_event) + event->len;
|
|
|
|
}
|
2021-06-11 19:58:32 +08:00
|
|
|
// qDebug()<< "Finish eventProcess!";
|
2021-06-10 20:43:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|