From b6926d510f66368783a29448878d1452fc5d1362 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 29 May 2022 15:46:06 +0200 Subject: [PATCH] FileSaver: Don't add files in blacklisted paths We now resolve symlinks when adding, so we can properly check whether a path is excluded or not. This accidently also helps with duplicates. Excluded paths are hardcoded and can also be appended to by the user using the settings. Closes: #34 --- shared/common.cpp | 16 ++++++++++++++++ shared/common.h | 1 + shared/filesaver.cpp | 17 +++++++++++++---- shared/filesaver.h | 5 ++--- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/shared/common.cpp b/shared/common.cpp index ff33d63..f929ef0 100644 --- a/shared/common.cpp +++ b/shared/common.cpp @@ -17,6 +17,7 @@ #define SETTINGS_KEY_FIRSTRUN "firstrun" #define SETTINGS_KEY_IPCSOCKETPATH "ipcsocketpath" #define SETTINGS_KEY_PDFVIEWER "pdfviewer" +#define SETTINGS_KEY_EXCLUDEDPATHS "excludedpaths" inline void initResources() { @@ -162,3 +163,18 @@ QString Common::ipcSocketPath() // QSettings settings; // return settings.value(SETTINGS_KEY_IPCSOCKETPATH, "/tmp/.looqs/looqs-ipc-socket").toString(); } + +static QStringList excludedPaths = {"/proc", "/sys", "/dev", "/tmp", "/var/run", "/run"}; + +QStringList Common::excludedPaths() +{ + static int ran = false; + if(!ran) + { + QSettings settings; + QStringList userExcludedPaths = settings.value(SETTINGS_KEY_EXCLUDEDPATHS).toStringList(); + ran = true; + ::excludedPaths.append(userExcludedPaths); + } + return ::excludedPaths; +} diff --git a/shared/common.h b/shared/common.h index 4e0e888..ab0457a 100644 --- a/shared/common.h +++ b/shared/common.h @@ -11,5 +11,6 @@ void setPdfViewer(); QString findInPath(QString needle); bool initSqliteDatabase(QString path); void ensureConfigured(); +QStringList excludedPaths(); } // namespace Common #endif diff --git a/shared/filesaver.cpp b/shared/filesaver.cpp index 7362358..6f8043f 100644 --- a/shared/filesaver.cpp +++ b/shared/filesaver.cpp @@ -23,6 +23,7 @@ SaveFileResult FileSaver::addFile(QString path) { QFileInfo info(path); QString absPath = info.absoluteFilePath(); + auto mtime = info.lastModified().toSecsSinceEpoch(); if(this->dbService->fileExistsInDatabase(absPath, mtime)) { @@ -95,7 +96,7 @@ int FileSaver::processFiles(const QVector paths, std::function pageData; - QString absPath = fileInfo.absoluteFilePath(); + QString canonicalPath = fileInfo.canonicalFilePath(); int status = -1; @@ -106,9 +107,17 @@ SaveFileResult FileSaver::saveFile(const QFileInfo &fileInfo) if(fileInfo.isFile()) { + for(QString &excludedPath : this->excludedPaths) + { + if(canonicalPath.startsWith(excludedPath)) + { + return SKIPPED; + } + } + QProcess process; QStringList args; - args << "process" << absPath; + args << "process" << canonicalPath; process.setProcessChannelMode(QProcess::ForwardedErrorChannel); process.start("/proc/self/exe", args); process.waitForStarted(); @@ -132,7 +141,7 @@ SaveFileResult FileSaver::saveFile(const QFileInfo &fileInfo) status = process.exitCode(); if(status != 0 && status != NOTHING_PROCESSED) { - Logger::error() << "FileSaver::saveFile(): Error while processing" << absPath << ":" + Logger::error() << "FileSaver::saveFile(): Error while processing" << canonicalPath << ":" << "Exit code " << status << Qt::endl; return PROCESSFAIL; @@ -142,7 +151,7 @@ SaveFileResult FileSaver::saveFile(const QFileInfo &fileInfo) // Could happen if a file corrupted for example if(pageData.isEmpty() && status != NOTHING_PROCESSED) { - Logger::error() << "Could not get any content for " << absPath << Qt::endl; + Logger::error() << "Could not get any content for " << canonicalPath << Qt::endl; } return this->dbService->saveFile(fileInfo, pageData); diff --git a/shared/filesaver.h b/shared/filesaver.h index fb5b82a..58733ec 100644 --- a/shared/filesaver.h +++ b/shared/filesaver.h @@ -5,11 +5,12 @@ #include "pagedata.h" #include "filedata.h" #include "sqlitedbservice.h" - +#include "common.h" class FileSaver { private: SqliteDbService *dbService; + QStringList excludedPaths = Common::excludedPaths(); public: FileSaver(SqliteDbService &dbService); @@ -20,8 +21,6 @@ class FileSaver bool keepGoing, bool verbose); int addFiles(const QVector paths, bool keepGoing, bool verbose); int updateFiles(const QVector paths, bool keepGoing, bool verbose); - - ; }; #endif // FILESAVER_H