Changeset View
Changeset View
Standalone View
Standalone View
src/engine/experimental/databasesanitizer.cpp
Show All 15 Lines | |||||
16 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. | ||
17 | * | 17 | * | ||
18 | * You should have received a copy of the GNU General Public License | 18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "databasesanitizer.h" | 22 | #include "databasesanitizer.h" | ||
23 | #include "documenturldb.h" | 23 | #include "documenturldb.h" | ||
24 | #include "fsutils.h" | | |||
25 | #include "idutils.h" | 24 | #include "idutils.h" | ||
26 | 25 | | |||
27 | #include <sys/sysmacros.h> | 26 | #include <sys/sysmacros.h> | ||
28 | 27 | | |||
29 | #include <KLocalizedString> | 28 | #include <KLocalizedString> | ||
30 | #include <QFileInfo> | 29 | #include <QFileInfo> | ||
30 | #include <QStorageInfo> | ||||
31 | #include <QDebug> | 31 | #include <QDebug> | ||
32 | 32 | | |||
33 | namespace Baloo | 33 | namespace Baloo | ||
34 | { | 34 | { | ||
35 | 35 | | |||
36 | class DatabaseSanitizerImpl { | 36 | class DatabaseSanitizerImpl { | ||
37 | public: | 37 | public: | ||
38 | DatabaseSanitizerImpl(const Database& db, Transaction::TransactionType type) | 38 | DatabaseSanitizerImpl(const Database& db, Transaction::TransactionType type) | ||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 103 | for (quint64 id: keys) { | |||
117 | ) { | 117 | ) { | ||
118 | continue; | 118 | continue; | ||
119 | } | 119 | } | ||
120 | result.append(info); | 120 | result.append(info); | ||
121 | } | 121 | } | ||
122 | return result; | 122 | return result; | ||
123 | } | 123 | } | ||
124 | 124 | | |||
125 | struct DeviceInfo { | 125 | QStorageInfo getStorageInfo(const quint32 id) { | ||
126 | quint32 id = 0; | 126 | static QMap<quint32, QStorageInfo> storageInfos = []() { | ||
127 | int items = 0; | 127 | QMap<quint32, QStorageInfo> result; | ||
128 | FSUtils::DeviceInfo fsInfo = {}; | 128 | const auto volumes = QStorageInfo::mountedVolumes(); | ||
129 | bool mounted = false; | 129 | for (const auto& vol : volumes) { | ||
130 | }; | 130 | const QByteArray rootPath = QFile::encodeName(vol.rootPath()); | ||
131 | 131 | const auto fsinfo = filePathToStat(rootPath); | |||
132 | int fillInDeviceInfo(const quint32 id, DeviceInfo& deviceInfo) { | | |||
133 | static QMap<quint32, DeviceInfo> deviceInfos = []() { | | |||
134 | QMap<quint32, DeviceInfo> result; | | |||
135 | const auto devices = FSUtils::attachedDevices(); | | |||
136 | for (const auto& dev : devices) { | | |||
137 | const QByteArray filePath = QFile::encodeName(dev.mountpoint); | | |||
138 | const auto fsinfo = filePathToStat(filePath); | | |||
139 | const quint32 id = static_cast<quint32>(fsinfo.st_dev); | 132 | const quint32 id = static_cast<quint32>(fsinfo.st_dev); | ||
140 | DeviceInfo devInfo; | 133 | // qDebug() << vol; | ||
141 | devInfo.id = id; | 134 | result[id] = vol; | ||
142 | devInfo.fsInfo = dev; | | |||
143 | devInfo.mounted = true && (dev.filesystem != QLatin1String("tmpfs")); | | |||
144 | qDebug() << "filesystem" << dev.filesystem; | | |||
145 | result[id] = devInfo; | | |||
146 | } | 135 | } | ||
147 | return result; | 136 | return result; | ||
148 | }(); | 137 | }(); | ||
149 | 138 | | |||
150 | deviceInfo.id = id; | 139 | QStorageInfo info = storageInfos.value(id); | ||
151 | if (deviceInfos.count(id) == 1) { | 140 | return info; | ||
152 | const DeviceInfo devInf = deviceInfos[deviceInfo.id]; | | |||
153 | deviceInfo.fsInfo = devInf.fsInfo ; | | |||
154 | deviceInfo.mounted = devInf.mounted; | | |||
155 | return 0; | | |||
156 | } | | |||
157 | return 1; | | |||
158 | } | | |||
159 | | ||||
160 | QMap<quint32, DeviceInfo> createDeviceList(const QVector<FileInfo>& infos) | | |||
161 | { | | |||
162 | QMap<quint32, DeviceInfo> usedDevices; | | |||
163 | for (const auto& info : infos) { | | |||
164 | usedDevices[info.deviceId].items++; | | |||
165 | } | | |||
166 | for (auto it = usedDevices.begin(), end = usedDevices.end(); it != end; it++) { | | |||
167 | fillInDeviceInfo(it.key(), it.value()); | | |||
168 | } | | |||
169 | return usedDevices; | | |||
170 | } | 141 | } | ||
171 | 142 | | |||
172 | private: | 143 | private: | ||
173 | Transaction* m_transaction; | 144 | Transaction* m_transaction; | ||
174 | }; | 145 | }; | ||
175 | } | 146 | } | ||
176 | 147 | | |||
177 | using namespace Baloo; | 148 | using namespace Baloo; | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 183 | { | |||
230 | } | 201 | } | ||
231 | err << i18n("Found %1 matching items", infos.count()) << endl; | 202 | err << i18n("Found %1 matching items", infos.count()) << endl; | ||
232 | 203 | | |||
233 | } | 204 | } | ||
234 | 205 | | |||
235 | void DatabaseSanitizer::printDevices(const QVector<qint64>& deviceIds, const bool missingOnly) | 206 | void DatabaseSanitizer::printDevices(const QVector<qint64>& deviceIds, const bool missingOnly) | ||
236 | { | 207 | { | ||
237 | auto infos = m_pimpl->createList(deviceIds, false, nullptr); | 208 | auto infos = m_pimpl->createList(deviceIds, false, nullptr); | ||
238 | auto usedDevices = m_pimpl->createDeviceList(infos); | 209 | | ||
210 | QMap<quint32, quint64> useCount; | ||||
211 | for (const auto& info : infos) { | ||||
212 | useCount[info.deviceId]++; | ||||
213 | } | ||||
239 | 214 | | |||
240 | const auto sep = QLatin1Char(' '); | 215 | const auto sep = QLatin1Char(' '); | ||
241 | QTextStream out(stdout); | 216 | QTextStream out(stdout); | ||
242 | QTextStream err(stderr); | 217 | QTextStream err(stderr); | ||
243 | int matchCount = 0; | 218 | int matchCount = 0; | ||
244 | for (const auto& dev : usedDevices) { | 219 | | ||
245 | if (missingOnly && dev.mounted) { | 220 | for (auto it = useCount.cbegin(); it != useCount.cend(); it++) { | ||
221 | auto id = it.key(); | ||||
222 | auto info = m_pimpl->getStorageInfo(id); | ||||
223 | auto mounted = info.isValid(); | ||||
224 | | ||||
225 | if (missingOnly && mounted) { | ||||
246 | continue; | 226 | continue; | ||
247 | } | 227 | } | ||
248 | matchCount++; | 228 | matchCount++; | ||
249 | // TODO coloring would be nice, but "...|grep '^!'" does not work with it. | 229 | // TODO coloring would be nice, but "...|grep '^!'" does not work with it. | ||
250 | // out << QStringLiteral("%1").arg(dev.mounted ? "+" : "\033[1;31m!") | 230 | // out << QStringLiteral("%1").arg(dev.mounted ? "+" : "\033[1;31m!") | ||
251 | // Can be done, see: https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/global/qlogging.cpp#n263 | 231 | // Can be done, see: https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/global/qlogging.cpp#n263 | ||
252 | out << QStringLiteral("%1").arg(dev.mounted ? "+" : "!") | 232 | out << QStringLiteral("%1").arg(mounted ? "+" : "!") | ||
253 | << sep << QStringLiteral("device:%1").arg(dev.id) | 233 | << sep << QStringLiteral("device:%1").arg(id) | ||
254 | << sep << QStringLiteral("[%1:%2]") | 234 | << sep << QStringLiteral("[%1:%2]") | ||
255 | .arg(major(dev.id), 4, 16, QLatin1Char('0')) | 235 | .arg(major(id), 4, 16, QLatin1Char('0')) | ||
256 | .arg(minor(dev.id), 4, 16, QLatin1Char('0')) | 236 | .arg(minor(id), 4, 16, QLatin1Char('0')) | ||
257 | << sep << QStringLiteral("indexed-items:%1").arg(dev.items); | 237 | << sep << QStringLiteral("indexed-items:%1").arg(it.value()); | ||
258 | 238 | | |||
259 | if (dev.mounted) { | 239 | if (mounted) { | ||
260 | out | 240 | out | ||
261 | << sep << QStringLiteral("fstype:%1").arg(dev.fsInfo.filesystem) | 241 | << sep << QStringLiteral("fstype:%1").arg(info.fileSystemType().toPercentEncoding().constData()) | ||
262 | << sep << QStringLiteral("fsname:%1").arg(dev.fsInfo.name) | 242 | << sep << QStringLiteral("device:%1").arg(info.device().constData()) | ||
263 | << sep << QStringLiteral("mount:%1").arg(dev.fsInfo.mountpoint) | 243 | << sep << QStringLiteral("path:%1").arg(info.rootPath()) | ||
264 | ; | 244 | ; | ||
265 | } | 245 | } | ||
266 | // TODO: see above | 246 | // TODO: see above | ||
267 | // out << QStringLiteral("\033[0m") << endl; | 247 | // out << QStringLiteral("\033[0m") << endl; | ||
268 | out << endl; | 248 | out << endl; | ||
269 | } | 249 | } | ||
270 | 250 | | |||
271 | err << i18n("Found %1 matching in %2 devices", matchCount, usedDevices.size()) << endl; | 251 | err << i18n("Found %1 matching in %2 devices", matchCount, useCount.size()) << endl; | ||
272 | } | 252 | } |