1/*
2 This file is part of the KDE Baloo project.
3 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "unindexedfileiterator.h"
9#include "fileindexerconfig.h"
10#include "idutils.h"
11#include "transaction.h"
12#include "baloodebug.h"
13
14#include <QFileInfo>
15#include <QDateTime>
16
17using namespace Baloo;
18
19UnIndexedFileIterator::UnIndexedFileIterator(const FileIndexerConfig* config, Transaction* transaction, const QString& folder)
20 : m_config(config)
21 , m_transaction(transaction)
22 , m_iter(config, folder, FilteredDirIterator::FilesAndDirs)
23 , m_mTimeChanged(false)
24 , m_cTimeChanged(false)
25{
26}
27
28UnIndexedFileIterator::~UnIndexedFileIterator()
29{
30}
31
32QString UnIndexedFileIterator::filePath() const
33{
34 return m_iter.filePath();
35}
36
37QString UnIndexedFileIterator::mimetype() const
38{
39 return m_mimetype;
40}
41
42bool UnIndexedFileIterator::mTimeChanged() const
43{
44 return m_mTimeChanged;
45}
46
47bool UnIndexedFileIterator::cTimeChanged() const
48{
49 return m_cTimeChanged;
50}
51
52QString UnIndexedFileIterator::next()
53{
54 while (1) {
55 const QString filePath = m_iter.next();
56 m_mTimeChanged = false;
57 m_cTimeChanged = false;
58
59 if (filePath.isEmpty()) {
60 m_mimetype.clear();
61 return QString();
62 }
63
64 if (shouldIndex(filePath)) {
65 return filePath;
66 }
67 }
68}
69
70bool UnIndexedFileIterator::shouldIndex(const QString& filePath)
71{
72 const QFileInfo fileInfo = m_iter.fileInfo();
73 if (!fileInfo.exists()) {
74 return false;
75 }
76
77 quint64 fileId = filePathToId(filePath: QFile::encodeName(fileName: filePath));
78 if (!fileId) {
79 // stat has failed, e.g. when file has been deleted after iteration
80 return false;
81 }
82
83 DocumentTimeDB::TimeInfo timeInfo = m_transaction->documentTimeInfo(id: fileId);
84 if ((timeInfo.mTime == 0) && (timeInfo.cTime == 0) && !m_transaction->hasDocument(id: fileId)) {
85 m_mTimeChanged = true;
86 m_cTimeChanged = true;
87 } else {
88 if (timeInfo.mTime != fileInfo.lastModified().toSecsSinceEpoch()) {
89 m_mTimeChanged = true;
90 }
91 if (timeInfo.cTime != fileInfo.metadataChangeTime().toSecsSinceEpoch()) {
92 m_cTimeChanged = true;
93 }
94 }
95
96 if (fileInfo.isDir()) {
97 // The folder ctime changes when the file is created, when the folder is
98 // renamed, or when the xattrs (tags, comments, ...) change
99 if (m_cTimeChanged) {
100 qCDebug(BALOO) << filePath << "ctime changed:"
101 << timeInfo.cTime << "->" << fileInfo.metadataChangeTime().toSecsSinceEpoch();
102 m_mimetype = QStringLiteral("inode/directory");
103 return true;
104 }
105 // The mtime changes when an object inside the folder is added/removed/renamed,
106 // there is no need to reindex the folder when that happens
107 return false;
108 }
109
110 if (m_mTimeChanged || m_cTimeChanged) {
111 // This mimetype may not be completely accurate, but that's okay. This is
112 // just the initial phase of indexing. The second phase can try to find
113 // a more accurate mimetype.
114 m_mimetype = m_mimeDb.mimeTypeForFile(fileName: filePath, mode: QMimeDatabase::MatchExtension).name();
115
116 qCDebug(BALOO) << filePath << "mtime/ctime changed:"
117 << timeInfo.mTime << "/" << timeInfo.cTime << "->"
118 << fileInfo.lastModified().toSecsSinceEpoch() << "/"
119 << fileInfo.metadataChangeTime().toSecsSinceEpoch();
120 return true;
121 }
122
123 return false;
124}
125

source code of baloo/src/file/unindexedfileiterator.cpp