forked from openkylin/ukui-search
Merge pull request #102 from MouseZhangZh/0122-dev
🤡🤡🤡Removed inotify watch on home and modified index status
This commit is contained in:
commit
65d2414dc5
|
@ -15,7 +15,7 @@
|
|||
|
||||
size_t FileUtils::_max_index_count = 0;
|
||||
size_t FileUtils::_current_index_count = 0;
|
||||
unsigned short FileUtils::_index_status = INITIAL_STATE;
|
||||
unsigned short FileUtils::_index_status = 0;
|
||||
QMap<QString, QStringList> FileUtils::map_chinese2pinyin = QMap<QString, QStringList>();
|
||||
|
||||
FileUtils::FileUtils()
|
||||
|
|
|
@ -12,9 +12,9 @@
|
|||
#include <QIcon>
|
||||
#include <QMap>
|
||||
#include "libsearch_global.h"
|
||||
#define INITIAL_STATE 0
|
||||
#define CREATING_INDEX 1
|
||||
#define FINISH_CREATING_INDEX 2
|
||||
//#define INITIAL_STATE 0
|
||||
//#define CREATING_INDEX 1
|
||||
//#define FINISH_CREATING_INDEX 2
|
||||
|
||||
#define UKUI_SEARCH_PIPE_PATH "/tmp/ukuisearch"
|
||||
|
||||
|
|
|
@ -106,13 +106,14 @@ void FirstIndex::run(){
|
|||
// this->p_indexGenerator->creatAllIndex(this->q_content_index);
|
||||
|
||||
|
||||
++FileUtils::_index_status;
|
||||
|
||||
pid_t pid;
|
||||
pid = fork();
|
||||
if(pid == 0)
|
||||
{
|
||||
prctl(PR_SET_PDEATHSIG, SIGKILL);
|
||||
prctl(PR_SET_NAME,"first-index");
|
||||
FileUtils::_index_status = CREATING_INDEX;
|
||||
QSemaphore sem(5);
|
||||
QMutex mutex1, mutex2, mutex3;
|
||||
mutex1.lock();
|
||||
|
@ -122,6 +123,7 @@ void FirstIndex::run(){
|
|||
QtConcurrent::run([&](){
|
||||
sem.acquire(1);
|
||||
mutex1.unlock();
|
||||
this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
|
||||
this->Traverse();
|
||||
FileUtils::_max_index_count = this->q_index->length();
|
||||
sem.release(5);
|
||||
|
@ -181,6 +183,7 @@ void FirstIndex::run(){
|
|||
else
|
||||
{
|
||||
waitpid(pid,NULL,0);
|
||||
--FileUtils::_index_status;
|
||||
}
|
||||
|
||||
int retval = write(fifo_fd, buffer, strlen(buffer));
|
||||
|
@ -190,7 +193,6 @@ void FirstIndex::run(){
|
|||
}
|
||||
printf("write data ok!\n");
|
||||
|
||||
FileUtils::_index_status = FINISH_CREATING_INDEX;
|
||||
|
||||
//quit() is shit!!!
|
||||
// return;
|
||||
|
|
|
@ -42,6 +42,7 @@ bool IndexGenerator::setIndexdataPath()
|
|||
//文件名索引
|
||||
bool IndexGenerator::creatAllIndex(QQueue<QVector<QString> > *messageList)
|
||||
{
|
||||
// FileUtils::_index_status |= 0x1;
|
||||
HandlePathList(messageList);
|
||||
try
|
||||
{
|
||||
|
@ -67,8 +68,10 @@ bool IndexGenerator::creatAllIndex(QQueue<QVector<QString> > *messageList)
|
|||
qWarning()<<"creatAllIndex fail!"<<QString::fromStdString(e.get_description());
|
||||
//need a record
|
||||
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"1");
|
||||
// FileUtils::_index_status &= ~0x1;
|
||||
assert(false);
|
||||
}
|
||||
// FileUtils::_index_status &= ~0x1;
|
||||
_doc_list_path->clear();
|
||||
delete _doc_list_path;
|
||||
_doc_list_path = nullptr;
|
||||
|
@ -77,6 +80,7 @@ bool IndexGenerator::creatAllIndex(QQueue<QVector<QString> > *messageList)
|
|||
//文件内容索引
|
||||
bool IndexGenerator::creatAllIndex(QQueue<QString> *messageList)
|
||||
{
|
||||
// FileUtils::_index_status |= 0x2;
|
||||
HandlePathList(messageList);
|
||||
int size = _doc_list_content->size();
|
||||
if(!size == 0)
|
||||
|
@ -97,8 +101,10 @@ bool IndexGenerator::creatAllIndex(QQueue<QString> *messageList)
|
|||
{
|
||||
qWarning()<<"creat content Index fail!"<<QString::fromStdString(e.get_description());
|
||||
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"1");
|
||||
// FileUtils::_index_status &= ~0x2;
|
||||
assert(false);
|
||||
}
|
||||
// FileUtils::_index_status &= ~0x2;
|
||||
_doc_list_content->clear();
|
||||
delete _doc_list_content;
|
||||
_doc_list_content = nullptr;
|
||||
|
|
|
@ -1,23 +1,5 @@
|
|||
#include "inotify-index.h"
|
||||
|
||||
void handler(int){
|
||||
qDebug() << "Recieved SIGTERM!";
|
||||
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2");
|
||||
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2");
|
||||
GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2");
|
||||
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
|
||||
|
||||
|
||||
qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString();
|
||||
qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString();
|
||||
_exit(0);
|
||||
|
||||
// InotifyIndex::getInstance("/home")->~InotifyIndex();
|
||||
|
||||
//wait linux kill this thread forcedly
|
||||
// while (true);
|
||||
}
|
||||
|
||||
InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path)
|
||||
{
|
||||
/*-------------ukuisearchdbus Test start-----------------*/
|
||||
|
@ -33,7 +15,8 @@ InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path)
|
|||
m_fd = inotify_init();
|
||||
qDebug() << "m_fd----------->" <<m_fd;
|
||||
|
||||
this->AddWatch("/home");
|
||||
this->AddWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
|
||||
this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
|
||||
this->Traverse();
|
||||
|
||||
|
||||
|
@ -123,8 +106,6 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){
|
|||
// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
|
||||
if(event->name[0] != '.'){
|
||||
qDebug() << QString(currentPath[event->wd] + '/' + event->name);
|
||||
FileUtils::_index_status = CREATING_INDEX;
|
||||
|
||||
// switch (event->mask) {
|
||||
if (event->mask & IN_CREATE){
|
||||
if (event->mask & IN_ISDIR){
|
||||
|
@ -265,7 +246,6 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){
|
|||
// }
|
||||
// }
|
||||
/*--------------------------------*/
|
||||
FileUtils::_index_status = FINISH_CREATING_INDEX;
|
||||
}
|
||||
next:
|
||||
p += sizeof(struct inotify_event) + event->len;
|
||||
|
@ -308,18 +288,31 @@ void InotifyIndex::run(){
|
|||
}
|
||||
unlink(UKUI_SEARCH_PIPE_PATH);
|
||||
|
||||
qDebug() << "sigset start!";
|
||||
sigset( SIGTERM, handler);
|
||||
qDebug() << "sigset end!";
|
||||
|
||||
char buf[BUF_LEN] __attribute__((aligned(8)));
|
||||
|
||||
ssize_t numRead;
|
||||
|
||||
for (;;) { /* Read events forever */
|
||||
read:
|
||||
numRead = read(m_fd, buf, BUF_LEN);
|
||||
|
||||
|
||||
char * tmp = const_cast<char*>(buf);
|
||||
|
||||
for (; tmp < buf + numRead;) {
|
||||
struct inotify_event * event = reinterpret_cast<inotify_event *>(tmp);
|
||||
// qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
|
||||
if(event->name[0] != '.'){
|
||||
goto fork;
|
||||
}
|
||||
tmp += sizeof(struct inotify_event) + event->len;
|
||||
|
||||
}
|
||||
goto read;
|
||||
|
||||
fork:
|
||||
++FileUtils::_index_status;
|
||||
|
||||
pid_t pid;
|
||||
pid = fork();
|
||||
if(pid == 0)
|
||||
|
@ -338,23 +331,28 @@ void InotifyIndex::run(){
|
|||
liveTime->setInterval(60000);
|
||||
liveTime->start();
|
||||
|
||||
//I don't know how to use QTimer, wish someone can fix it!
|
||||
//MouseZhangZh
|
||||
|
||||
// connect(liveTime, &QTimer::timeout, [ = ](){
|
||||
//// _exit(0);
|
||||
// *b_timeout = 1;
|
||||
// });
|
||||
for (;;){
|
||||
// qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime();
|
||||
numRead = read(m_fd, buf, BUF_LEN);
|
||||
liveTime->stop();
|
||||
this->eventProcess(buf, numRead);
|
||||
if (liveTime->remainingTime() < 1){
|
||||
qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime();
|
||||
_exit(0);
|
||||
}
|
||||
liveTime->start();
|
||||
}
|
||||
}
|
||||
else if (pid > 0){
|
||||
memset(buf, 0x00, BUF_LEN);
|
||||
waitpid(pid, NULL, 0);
|
||||
|
||||
--FileUtils::_index_status;
|
||||
}
|
||||
else{
|
||||
assert(false);
|
||||
|
|
28
src/main.cpp
28
src/main.cpp
|
@ -34,6 +34,25 @@
|
|||
#include "xatom-helper.h"
|
||||
|
||||
|
||||
void handler(int){
|
||||
qDebug() << "Recieved SIGTERM!";
|
||||
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2");
|
||||
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2");
|
||||
GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2");
|
||||
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
|
||||
|
||||
|
||||
qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString();
|
||||
qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString();
|
||||
_exit(0);
|
||||
|
||||
// InotifyIndex::getInstance("/home")->~InotifyIndex();
|
||||
|
||||
//wait linux kill this thread forcedly
|
||||
// while (true);
|
||||
}
|
||||
|
||||
|
||||
void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||
{
|
||||
QByteArray localMsg = msg.toLocal8Bit();
|
||||
|
@ -126,9 +145,10 @@ int main(int argc, char *argv[])
|
|||
}*/
|
||||
|
||||
|
||||
//here need to be modified
|
||||
/*-------------ukuisearchdbus Test start-----------------*/
|
||||
// UkuiSearchQDBus usQDBus;
|
||||
// usQDBus.setInotifyMaxUserWatches();
|
||||
UkuiSearchQDBus usQDBus;
|
||||
usQDBus.setInotifyMaxUserWatches();
|
||||
|
||||
/*-------------ukuisearchdbus Test End-----------------*/
|
||||
|
||||
|
@ -206,6 +226,10 @@ int main(int argc, char *argv[])
|
|||
// InotifyIndex ii("/home");
|
||||
ii->start();
|
||||
|
||||
qDebug() << "sigset start!";
|
||||
sigset( SIGTERM, handler);
|
||||
qDebug() << "sigset end!";
|
||||
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
|
|
@ -209,7 +209,8 @@ void SettingsWidget::clearLayout(QLayout * layout) {
|
|||
*/
|
||||
void SettingsWidget::refreshIndexState()
|
||||
{
|
||||
if (FileUtils::_index_status == CREATING_INDEX) {
|
||||
// qDebug()<<"FileUtils::_index_status: "<<FileUtils::_index_status;
|
||||
if (FileUtils::_index_status != 0) {
|
||||
this->setIndexState(true);
|
||||
} else {
|
||||
this->setIndexState(false);
|
||||
|
@ -217,7 +218,8 @@ void SettingsWidget::refreshIndexState()
|
|||
m_indexNumLabel->setText(QString("%1/%2").arg(QString::number(FileSearcher::getCurrentIndexCount())).arg(QString::number(FileUtils::_max_index_count)));
|
||||
m_timer = new QTimer;
|
||||
connect(m_timer, &QTimer::timeout, this, [ = ]() {
|
||||
if (FileUtils::_index_status == CREATING_INDEX) {
|
||||
qDebug()<<"FileUtils::_index_status: "<<FileUtils::_index_status;
|
||||
if (FileUtils::_index_status != 0) {
|
||||
this->setIndexState(true);
|
||||
} else {
|
||||
this->setIndexState(false);
|
||||
|
|
Loading…
Reference in New Issue