Prevent symlink loop when indexing

This commit is contained in:
Dušan Poizl 2024-02-03 15:32:34 +01:00
parent 3f7e3689e8
commit 53c9a58125
2 changed files with 17 additions and 8 deletions

@ -156,23 +156,29 @@ int Database::checkVersion()
static QStringList nameFilters = {"*.fit", "*.fits", "*.xisf"};
static int countFiles(const QDir &dir, int count = 0)
static int countFiles(const QDir &dir, QStringList &scannedDirs)
{
count += dir.entryList(nameFilters, QDir::Files).size();
if(scannedDirs.contains(dir.canonicalPath()))return 0;
scannedDirs.append(dir.canonicalPath());
int count = dir.entryList(nameFilters, QDir::Files).size();
QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for(const QString &d : dirs)
count += countFiles(dir.filePath(d));
count += countFiles(dir.filePath(d), scannedDirs);
return count;
}
void Database::indexDir(const QDir &dir, QProgressDialog *progress)
{
m_progress = 0;
int count = countFiles(dir);
QStringList scannedDirs;
int count = countFiles(dir, scannedDirs);
progress->setMaximum(count);
QSqlDatabase database = QSqlDatabase::database();
database.transaction();
if(indexDir2(dir, progress))
scannedDirs.clear();
if(indexDir2(dir, progress, scannedDirs))
{
database.commit();
emit databaseChanged();
@ -225,14 +231,17 @@ QStringList Database::getFitsKeywords()
return keywords;
}
bool Database::indexDir2(const QDir &dir, QProgressDialog *progress)
bool Database::indexDir2(const QDir &dir, QProgressDialog *progress, QStringList &scannedDirs)
{
if(scannedDirs.contains(dir.canonicalPath()))return true;
scannedDirs.append(dir.canonicalPath());
QFileInfoList files = dir.entryInfoList(nameFilters, QDir::Files);
QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for(const QString &d : dirs)
{
if(!indexDir2(dir.filePath(d), progress))
if(!indexDir2(dir.filePath(d), progress, scannedDirs))
return false;
}
for(const QFileInfo &file : files)

@ -37,7 +37,7 @@ public:
void reindex(QProgressDialog *progress);
QStringList getFitsKeywords();
protected:
bool indexDir2(const QDir &dir, QProgressDialog *progress);
bool indexDir2(const QDir &dir, QProgressDialog *progress, QStringList &scannedDirs);
bool indexFile(const QFileInfo &file);
bool checkError(QSqlQuery &query);
int checkVersion();