/
lftmanager.cpp
1278 lines (954 loc) · 35.2 KB
/
lftmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 ~ 2019 Deepin Technology Co., Ltd.
*
* Author: zccrs <zccrs@live.com>
*
* Maintainer: zccrs <zhangjide@deepin.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lftmanager.h"
#include "lftdisktool.h"
extern "C" {
#include "fs_buf.h"
#include "walkdir.h"
}
#include <dfmdiskmanager.h>
#include <dfmblockpartition.h>
#include <dfmdiskdevice.h>
#include <QtConcurrent>
#include <QFutureWatcher>
#include <QStandardPaths>
#include <QRegularExpression>
#include <QTimer>
#include <QLoggingCategory>
#include <unistd.h>
Q_GLOBAL_STATIC_WITH_ARGS(QLoggingCategory, normalLog, ("manager.normal"))
Q_GLOBAL_STATIC_WITH_ARGS(QLoggingCategory, changesLog, ("manager.changes", QtWarningMsg))
#define nDebug(...) qCDebug((*normalLog), __VA_ARGS__)
#define nInfo(...) qCInfo((*normalLog), __VA_ARGS__)
#define nWarning(...) qCWarning((*normalLog), __VA_ARGS__)
#define cDebug(...) qCDebug((*changesLog), __VA_ARGS__)
#define cWarning(...) qCWarning((*changesLog), __VA_ARGS__)
static QString _getCacheDir()
{
QString cachePath = QString("/var/cache/%1/deepin-anything").arg(qApp->organizationName());
if (getuid() != 0 && !QFileInfo(cachePath).isWritable()) {
cachePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
if (cachePath.isEmpty() || cachePath == "/") {
cachePath = QString("/tmp/%1/deepin-anything").arg(qApp->organizationName());
}
}
nInfo() << "Cache Dir:" << cachePath;
if (!QDir::home().mkpath(cachePath)) {
nWarning() << "Failed on create chache path";
}
return cachePath;
}
class _LFTManager : public LFTManager {};
Q_GLOBAL_STATIC(_LFTManager, _global_lftmanager)
typedef QMap<QString, fs_buf*> FSBufMap;
Q_GLOBAL_STATIC(FSBufMap, _global_fsBufMap)
typedef QMap<fs_buf*, QString> FSBufToFileMap;
Q_GLOBAL_STATIC(FSBufToFileMap, _global_fsBufToFileMap)
typedef QMap<QString, QFutureWatcher<fs_buf*>*> FSJobWatcherMap;
Q_GLOBAL_STATIC(FSJobWatcherMap, _global_fsWatcherMap)
typedef QSet<fs_buf*> FSBufList;
Q_GLOBAL_STATIC(FSBufList, _global_fsBufDirtyList)
Q_GLOBAL_STATIC_WITH_ARGS(QSettings, _global_settings, (_getCacheDir() + "/config.ini", QSettings::IniFormat))
static QSet<fs_buf*> fsBufList()
{
if (!_global_fsBufMap.exists())
return QSet<fs_buf*>();
return _global_fsBufMap->values().toSet();
}
static void clearFsBufMap()
{
for (fs_buf *buf : fsBufList()) {
if (buf)
free_fs_buf(buf);
}
if (_global_fsBufMap.exists())
_global_fsBufMap->clear();
if (_global_fsBufToFileMap)
_global_fsBufToFileMap->clear();
if (_global_fsWatcherMap.exists()) {
for (const QString &path : _global_fsWatcherMap->keys()) {
LFTManager::instance()->cancelBuild(path);
}
}
}
// 标记为脏文件, 定时清理(对象销毁时也会清理)
static void markLFTFileToDirty(fs_buf *buf)
{
_global_fsBufDirtyList->insert(buf);
}
// 删除脏文件
static bool doLFTFileToDirty(fs_buf *buf)
{
const QString &lft_file = _global_fsBufToFileMap->value(buf);
nDebug() << lft_file;
if (lft_file.isEmpty())
return false;
return QFile::remove(lft_file);
}
static void cleanDirtyLFTFiles()
{
if (!_global_fsBufDirtyList.exists())
return;
for (fs_buf *buf : _global_fsBufDirtyList.operator *()) {
doLFTFileToDirty(buf);
}
_global_fsBufDirtyList->clear();
}
LFTManager::~LFTManager()
{
sync();
clearFsBufMap();
// 删除剩余脏文件(可能是sync失败)
cleanDirtyLFTFiles();
}
LFTManager *LFTManager::instance()
{
return _global_lftmanager;
}
QString LFTManager::cacheDir()
{
static QString dir = _getCacheDir();
return dir;
}
QStringList LFTManager::logCategoryList()
{
QStringList list;
list << normalLog->categoryName()
<< changesLog->categoryName();
return list;
}
QByteArray LFTManager::setCodecNameForLocale(const QByteArray &codecName)
{
const QTextCodec *old_codec = QTextCodec::codecForLocale();
if (codecName.isEmpty())
QTextCodec::setCodecForLocale(nullptr);
else
QTextCodec::setCodecForLocale(QTextCodec::codecForName(codecName));
nDebug() << codecName << "old:" << old_codec->name();
return old_codec->name();
}
struct FSBufDeleter
{
static inline void cleanup(fs_buf *pointer)
{
free_fs_buf(pointer);
}
};
static int handle_build_fs_buf_progress(uint32_t file_count, uint32_t dir_count, const char* cur_dir, const char* cur_file, void* param)
{
Q_UNUSED(file_count)
Q_UNUSED(dir_count)
Q_UNUSED(cur_dir)
Q_UNUSED(cur_file)
QFutureWatcherBase *futureWatcher = static_cast<QFutureWatcherBase*>(param);
if (futureWatcher->isCanceled()) {
return 1;
}
return 0;
}
static fs_buf *buildFSBuf(QFutureWatcherBase *futureWatcher, const QString &path)
{
fs_buf *buf = new_fs_buf(1 << 24, path.toLocal8Bit().constData());
if (!buf)
return buf;
if (build_fstree(buf, false, handle_build_fs_buf_progress, futureWatcher) != 0) {
free_fs_buf(buf);
nWarning() << "Failed on build fs buffer of path: " << path;
return nullptr;
}
return buf;
}
static QString getLFTFileByPath(const QString &path, bool autoIndex)
{
QByteArray lft_file_name = LFTDiskTool::pathToSerialUri(path);
if (lft_file_name.isEmpty())
return QString();
//由自动索引机制创建的文件后缀名为LFT,否则为lft
lft_file_name += autoIndex ? ".LFT" : ".lft";
const QString &cache_path = LFTManager::cacheDir();
if (cache_path.isEmpty())
return QString();
return cache_path + "/" + QString::fromLocal8Bit(lft_file_name.toPercentEncoding(":", "/"));
}
static bool allowableBuf(LFTManager *manager, fs_buf *buf)
{
// 手动创建的buf不受属性autoIndexExternal/autoIndexInternal的控制
if (_global_fsBufToFileMap->value(buf).endsWith(".lft"))
return true;
const char *root = get_root_path(buf);
QStorageInfo info(QString::fromLocal8Bit(root));
if (!info.isValid()) {
return true;
}
QScopedPointer<DFMBlockPartition> device(LFTDiskTool::diskManager()->createBlockPartition(info, nullptr));
if (!device) {
return true;
}
QScopedPointer<DFMDiskDevice> disk(LFTDiskTool::diskManager()->createDiskDevice(device->drive()));
if (disk->removable()) {
return manager->autoIndexExternal();
} else {
return manager->autoIndexInternal();
}
}
bool LFTManager::addPath(QString path, bool autoIndex)
{
nDebug() << path << autoIndex;
if (!path.startsWith("/")) {
sendErrorReply(QDBusError::InvalidArgs, "The path must start with '/'");
return false;
}
if (_global_fsBufMap->contains(path)) {
sendErrorReply(QDBusError::InternalError, "Index data already exists for this path");
return false;
}
const QByteArray &serial_uri = LFTDiskTool::pathToSerialUri(path);
if (serial_uri.isEmpty()) {
sendErrorReply(QDBusError::InvalidArgs, "Unable to convert to serial uri");
return false;
}
QFutureWatcher<fs_buf*> *watcher = new QFutureWatcher<fs_buf*>(this);
// 此路径对应的设备可能被挂载到多个位置
const QByteArrayList &path_list = LFTDiskTool::fromSerialUri(serial_uri);
nDebug() << "Equivalent paths:" << path_list;
// 将路径改为相对于第一个挂载点的路径,vfs_monitor中所有文件的改动都是以设备第一个挂载点通知的
path = path_list.first();
// 保存信息,用于判断索引是否正在构建
for (const QByteArray &path_raw : path_list) {
const QString &path = QString::fromLocal8Bit(path_raw);
(*_global_fsBufMap)[path] = nullptr;
(*_global_fsWatcherMap)[path] = watcher;
}
connect(watcher, &QFutureWatcher<fs_buf*>::finished, this, [this, path_list, path, watcher, autoIndex] {
fs_buf *buf = watcher->result();
if (!_global_fsBufMap->contains(path) || (autoIndex && !allowableBuf(this, buf))) {
nWarning() << "Discarded index data of path:" << path;
free_fs_buf(buf);
buf = nullptr;
}
for (const QByteArray &path_raw : path_list) {
const QString &path = QString::fromLocal8Bit(path_raw);
if (buf) {
(*_global_fsBufMap)[path] = buf;
} else {
_global_fsBufMap->remove(path);
}
_global_fsWatcherMap->remove(path);
Q_EMIT addPathFinished(path, buf);
}
if (buf) {
_global_fsBufToFileMap->insert(buf, getLFTFileByPath(path, autoIndex));
}
watcher->deleteLater();
});
QFuture<fs_buf*> result = QtConcurrent::run(buildFSBuf, watcher, path.endsWith('/') ? path : path + "/");
watcher->setFuture(result);
return true;
}
static void removeBuf(fs_buf *buf, bool removeLFTFile = true)
{
nDebug() << get_root_path(buf) << removeLFTFile;
for (const QString &other_key : _global_fsBufMap->keys(buf)) {
nDebug() << "do remove:" << other_key;
_global_fsBufMap->remove(other_key);
}
if (removeLFTFile)
doLFTFileToDirty(buf);
_global_fsBufDirtyList->remove(buf);
_global_fsBufToFileMap->remove(buf);
free_fs_buf(buf);
}
bool LFTManager::removePath(const QString &path)
{
nDebug() << path;
if (fs_buf *buf = _global_fsBufMap->take(path)) {
if (_global_fsBufToFileMap->value(buf).endsWith(".LFT")) {
// 不允许通过此接口删除由自动索引创建的数据
sendErrorReply(QDBusError::NotSupported, "Deleting data created by automatic indexing is not supported");
return false;
}
removeBuf(buf);
//此处被删除的是手动添加的索引数据, 之后应该更新自动生成的索引数据, 因为此目录之前一直
//使用手动生成的数据,现在数据被删了,但目录本身有可能是满足为其自动生成索引数据的要求
QStorageInfo info(path);
if (info.isValid()) {
nDebug() << "will process mount point(do build lft data for it):" << info.rootPath();
onMountAdded(QString(), info.rootPath().toLocal8Bit());
}
}
sendErrorReply(QDBusError::InvalidArgs, "Not found the index data");
return false;
}
// 返回path对应的fs_buf对象,且将path转成为相对于fs_buf root_path的路径
static QList<QPair<QString, fs_buf*>> getFsBufByPath(const QString &path)
{
if (!_global_fsBufMap.exists())
return QList<QPair<QString, fs_buf*>>();
if (!path.startsWith("/"))
return QList<QPair<QString, fs_buf*>>();
QDir path_dir(path);
// 找到一个存在的路径
while (!path_dir.exists()) {
if (!path_dir.cdUp())
break;
}
QStorageInfo storage_info(path_dir);
QString result_path = path;
QList<QPair<QString, fs_buf*>> buf_list;
Q_FOREVER {
fs_buf *buf = _global_fsBufMap->value(result_path, (fs_buf*)0x01);
if (buf != (fs_buf*)0x01) {
if (!buf) {
buf_list << qMakePair(path, buf);
} else {
// path相对于此fs_buf root_path的路径
QString new_path = path.mid(result_path.size());
// 移除多余的 / 字符
if (new_path.startsWith("/"))
new_path = new_path.mid(1);
// fs_buf中的root_path以/结尾,所以此处直接拼接
new_path.prepend(QString::fromLocal8Bit(get_root_path(buf)));
// 移除多余的 / 字符
if (new_path.endsWith("/"))
new_path.chop(1);
buf_list << qMakePair(new_path, buf);
}
}
if (result_path == "/" || result_path == storage_info.rootPath())
break;
int last_dir_split_pos = result_path.lastIndexOf('/');
if (last_dir_split_pos < 0)
break;
result_path = result_path.left(last_dir_split_pos);
if (result_path.isEmpty())
result_path = "/";
};
return buf_list;
}
bool LFTManager::hasLFT(const QString &path) const
{
auto list = getFsBufByPath(path);
return !list.isEmpty() && list.first().second;
}
bool LFTManager::lftBuinding(const QString &path) const
{
// 对应fs_buf存在且为nullptr认为正在构建
auto list = getFsBufByPath(path);
return !list.isEmpty() && !list.first().second;
}
bool LFTManager::cancelBuild(const QString &path)
{
nDebug() << path;
if (QFutureWatcher<fs_buf*> *watcher = _global_fsWatcherMap->take(path)) {
watcher->cancel();
nDebug() << "will wait for finished";
watcher->waitForFinished();
// 清理其它等价的路径
for (const QString &path : _global_fsWatcherMap->keys(watcher)) {
qDebug() << "do remove:" << path;
_global_fsWatcherMap->remove(path);
}
return true;
}
return false;
}
QStringList LFTManager::allPath() const
{
if (!_global_fsBufMap.exists())
return QStringList();
QStringList list;
for (auto i = _global_fsBufMap->constBegin(); i != _global_fsBufMap->constEnd(); ++i) {
if (i.value()) { // 排除正在构建的目录
list << i.key();
}
}
return list;
}
QStringList LFTManager::hasLFTSubdirectories(QString path) const
{
if (!path.endsWith("/"))
path.append('/');
QStringList list;
for (auto i = _global_fsBufMap->constBegin(); i != _global_fsBufMap->constEnd(); ++i) {
if (i.value() && (i.key() + "/").startsWith(path))
list << i.key();
}
return list;
}
// 重新从磁盘加载lft文件
QStringList LFTManager::refresh(const QByteArray &serialUriFilter)
{
nDebug() << serialUriFilter;
const QString &cache_path = cacheDir();
QDirIterator dir_iterator(cache_path, {"*.lft", "*.LFT"});
QStringList path_list;
while (dir_iterator.hasNext()) {
const QString &lft_file = dir_iterator.next();
nDebug() << "found file:" << lft_file;
// 根据地址过滤,只加载此挂载路径或其子路径对应的索引文件
if (!serialUriFilter.isEmpty() && !dir_iterator.fileName().startsWith(serialUriFilter))
continue;
nDebug() << "will load:" << dir_iterator.fileName();
QByteArray file_name = dir_iterator.fileName().toLocal8Bit();
file_name.chop(4); // 去除 .lft 后缀
const QByteArrayList pathList = LFTDiskTool::fromSerialUri(QByteArray::fromPercentEncoding(file_name));
nDebug() << "path list of serial uri:" << pathList;
if (pathList.isEmpty()) {
continue;
}
fs_buf *buf = nullptr;
if (load_fs_buf(&buf, lft_file.toLocal8Bit().constData()) != 0) {
nWarning() << "Failed on load:" << lft_file;
continue;
}
if (!buf) {
nWarning() << "Failed on load:" << lft_file;
continue;
}
for (const QByteArray &path_raw : pathList) {
const QString path = QString::fromLocal8Bit(path_raw);
path_list << path;
// 清理旧的buf
if (fs_buf *buf = _global_fsBufMap->value(path)) {
removeBuf(buf, false);
}
(*_global_fsBufMap)[path] = buf;
}
_global_fsBufToFileMap->insert(buf, dir_iterator.fileName());
}
return path_list;
}
QStringList LFTManager::sync(const QString &mountPoint)
{
nDebug() << mountPoint;
QStringList path_list;
if (!_global_fsBufMap.exists()) {
return path_list;
}
if (!QDir::home().mkpath(cacheDir())) {
sendErrorReply(QDBusError::AccessDenied, "Failed on create path: " + cacheDir());
return path_list;
}
QList<fs_buf*> saved_buf_list;
for (auto buf_begin = _global_fsBufMap->constBegin(); buf_begin != _global_fsBufMap->constEnd(); ++buf_begin) {
fs_buf *buf = buf_begin.value();
if (!buf)
continue;
const QString &path = buf_begin.key();
nDebug() << "found buf, path:" << path;
// 只同步此挂载点的数据
if (!mountPoint.isEmpty()) {
if (!(path + "/").startsWith(mountPoint + '/')) {
continue;
}
}
if (saved_buf_list.contains(buf)) {
nDebug() << "buf is saved";
path_list << buf_begin.key();
continue;
}
const QString &lft_file = _global_fsBufToFileMap->value(buf);
nDebug() << "lft file:" << lft_file;
if (lft_file.isEmpty()) {
nWarning() << "Can't get the LFT file path of the fs_buf:" << get_root_path(buf);
continue;
}
if (save_fs_buf(buf, lft_file.toLocal8Bit().constData()) == 0) {
saved_buf_list.append(buf);
path_list << buf_begin.key();
// 从脏列表中移除
_global_fsBufDirtyList->remove(buf);
} else {
path_list << QString("Failed: \"%1\"->\"%2\"").arg(buf_begin.key()).arg(lft_file);
nWarning() << path_list.last();
}
}
return path_list;
}
static int compareString(const char *string, void *keyword)
{
return QString::fromLocal8Bit(string).indexOf(*static_cast<const QString*>(keyword), 0, Qt::CaseInsensitive) < 0;
}
static int compareStringRegExp(const char *string, void *re)
{
auto match = static_cast<QRegularExpression*>(re)->match(QString::fromLocal8Bit(string));
return !match.hasMatch();
}
static int timeoutGuard(uint32_t count, const char* cur_file, void* param)
{
Q_UNUSED(count)
Q_UNUSED(cur_file)
QPair<QElapsedTimer*, qint64> *data = static_cast<QPair<QElapsedTimer*, qint64>*>(param);
return data->first->elapsed() >= data->second; // 返回false时表示不终端搜索
}
QStringList LFTManager::search(const QString &path, const QString &keyword, bool useRegExp) const
{
quint32 start, end;
return search(-1, -1, 0, 0, path, keyword, useRegExp, start, end);
}
QStringList LFTManager::search(int maxCount, qint64 maxTime, quint32 startOffset, quint32 endOffset,
const QString &path, const QString &keyword, bool useRegExp,
quint32 &startOffsetReturn, quint32 &endOffsetReturn) const
{
nDebug() << maxCount << maxTime << startOffset << endOffset << path << keyword << useRegExp;
auto buf_list = getFsBufByPath(path);
if (buf_list.isEmpty()) {
sendErrorReply(QDBusError::InvalidArgs, "Not found the index data");
return QStringList();
}
fs_buf *buf = buf_list.first().second;
if (!buf) {
sendErrorReply(QDBusError::InternalError, "Index is being generated");
return QStringList();
}
// new_path 为path在fs_buf中对应的路径
const QString &new_path = buf_list.first().first;
if (startOffset == 0 || endOffset == 0) {
// 未指定有效的搜索区间时, 根据路径获取
uint32_t path_offset = 0;
get_path_range(buf, new_path.toLocal8Bit().constData(), &path_offset, &startOffset, &endOffset);
}
// 说明目录为空
if (startOffset == 0) {
nDebug() << "Empty directory:" << new_path;
return QStringList();
}
QRegularExpression re(keyword);
void *compare_param = nullptr;
comparator_fn compare = nullptr;
if (useRegExp) {
if (!re.isValid()) {
sendErrorReply(QDBusError::InvalidArgs, "Invalid regular expression: " + re.errorString());
return QStringList();
}
re.setPatternOptions(QRegularExpression::CaseInsensitiveOption
| QRegularExpression::DotMatchesEverythingOption
| QRegularExpression::OptimizeOnFirstUsageOption);
compare_param = &re;
compare = compareStringRegExp;
} else {
compare_param = const_cast<QString*>(&keyword);
compare = compareString;
}
#define MAX_RESULT_COUNT 100
uint32_t name_offsets[MAX_RESULT_COUNT];
uint32_t count = MAX_RESULT_COUNT;
QStringList list;
char tmp_path[PATH_MAX];
// root_path 以/结尾,所以此处需要多忽略一个字符
bool need_reset_root_path = path != new_path;
QElapsedTimer et;
progress_fn progress = nullptr;
QPair<QElapsedTimer*, qint64> progress_param;
if (maxTime >= 0) {
et.start();
progress = timeoutGuard;
progress_param.first = &et;
progress_param.second = maxTime;
}
do {
count = qMin(MAX_RESULT_COUNT, maxCount - list.count());
search_files(buf, &startOffset, endOffset, name_offsets, &count, compare, compare_param, progress, &progress_param);
for (uint32_t i = 0; i < count; ++i) {
const char *result = get_path_by_name_off(buf, name_offsets[i], tmp_path, sizeof(tmp_path));
const QString &origin_path = QString::fromLocal8Bit(result);
if (need_reset_root_path) {
list << path + origin_path.mid(new_path.size());
nDebug() << "need reset root path:" << origin_path << ", to:" << list.last();
} else {
list << origin_path;
}
}
if (maxTime >= 0 && et.elapsed() >= maxTime) {
break;
}
} while (count == MAX_RESULT_COUNT);
startOffsetReturn = startOffset;
endOffsetReturn = endOffset;
return list;
}
QStringList LFTManager::insertFileToLFTBuf(const QByteArray &file)
{
cDebug() << file;
auto list = getFsBufByPath(QString::fromLocal8Bit(file));
QStringList root_path_list;
if (list.isEmpty())
return root_path_list;
QFileInfo info(QString::fromLocal8Bit(file));
bool is_dir = info.isDir();
for (auto i : list) {
fs_buf *buf = i.second;
// 有可能索引正在构建
if (!buf) {
cDebug() << "index buinding";
// 正在构建索引时需要等待
if (QFutureWatcher<fs_buf*> *watcher = _global_fsWatcherMap->value(i.first)) {
cDebug() << "will be wait build finished";
watcher->waitForFinished();
buf = watcher->result();
}
if (!buf)
continue;
}
cDebug() << "do insert:" << i.first;
fs_change change;
int r = insert_path(buf, i.first.toLocal8Bit().constData(), is_dir, &change);
if (r == 0) {
// buf内容已改动,标记删除对应的lft文件
markLFTFileToDirty(buf);
root_path_list << QString::fromLocal8Bit(get_root_path(buf));
} else {
if (r == ERR_NO_MEM) {
cWarning() << "Failed(No Memory):" << i.first;
} else {
cDebug() << "Failed:" << i.first << ", result:" << r;
}
}
}
return root_path_list;
}
QStringList LFTManager::removeFileFromLFTBuf(const QByteArray &file)
{
cDebug() << file;
auto list = getFsBufByPath(QString::fromLocal8Bit(file));
QStringList root_path_list;
if (list.isEmpty())
return root_path_list;
for (auto i : list) {
fs_buf *buf = i.second;
// 有可能索引正在构建
if (!buf) {
cDebug() << "index buinding";
// 正在构建索引时需要等待
if (QFutureWatcher<fs_buf*> *watcher = _global_fsWatcherMap->value(i.first)) {
cDebug() << "will be wait build finished";
watcher->waitForFinished();
buf = watcher->result();
}
if (!buf)
continue;
}
cDebug() << "do remove:" << i.first;
fs_change changes[10];
uint32_t count = 10;
int r = remove_path(buf, i.first.toLocal8Bit().constData(), changes, &count);
if (r == 0) {
// buf内容已改动,标记删除对应的lft文件
markLFTFileToDirty(buf);
root_path_list << QString::fromLocal8Bit(get_root_path(buf));
} else {
if (r == ERR_NO_MEM) {
cWarning() << "Failed(No Memory):" << i.first;
} else {
cDebug() << "Failed:" << i.first << ", result:" << r;
}
}
}
return root_path_list;
}
QStringList LFTManager::renameFileOfLFTBuf(const QByteArray &oldFile, const QByteArray &newFile)
{
cDebug() << oldFile << newFile;
auto list = getFsBufByPath(QString::fromLocal8Bit(newFile));
QStringList root_path_list;
if (list.isEmpty())
return root_path_list;
for (int i = 0; i < list.count(); ++i) {
fs_buf *buf = list.at(i).second;
// 有可能索引正在构建
if (!buf) {
cDebug() << "index buinding";
// 正在构建索引时需要等待
if (QFutureWatcher<fs_buf*> *watcher = _global_fsWatcherMap->value(list.at(i).first)) {
cDebug() << "will be wait build finished";
watcher->waitForFinished();
buf = watcher->result();
}
if (!buf)
continue;
}
fs_change changes[10];
uint32_t change_count = 10;
// newFile相对于此buf的路径
const QByteArray &new_file_new_path = list.at(i).first.toLocal8Bit();
int valid_suffix_size = new_file_new_path.size() - strlen(get_root_path(buf));
int invalid_prefix_size = newFile.size() - valid_suffix_size;
QByteArray old_file_new_path = QByteArray(get_root_path(buf)).append(oldFile.mid(invalid_prefix_size));
cDebug() << "do rename:" << old_file_new_path << new_file_new_path;
int r = rename_path(buf, old_file_new_path.constData(), new_file_new_path.constData(), changes, &change_count);
if (r == 0) {
// buf内容已改动,标记删除对应的lft文件
markLFTFileToDirty(buf);
root_path_list << QString::fromLocal8Bit(get_root_path(buf));
} else {
if (r == ERR_NO_MEM) {
cWarning() << "Failed(No Memory)";
} else {
cDebug() << "Failed: result=" << r;
}
}
}
return root_path_list;
}
void LFTManager::quit()
{
qApp->quit();
}
bool LFTManager::autoIndexExternal() const
{
return _global_settings->value("autoIndexExternal", false).toBool();
}
bool LFTManager::autoIndexInternal() const
{
return _global_settings->value("autoIndexInternal", true).toBool();
}
int LFTManager::logLevel() const
{
if (normalLog->isEnabled(QtDebugMsg)) {
if (changesLog->isEnabled(QtDebugMsg)) {
return 2;
} else {
return 1;
}
}
return 0;
}
void LFTManager::setAutoIndexExternal(bool autoIndexExternal)
{
if (this->autoIndexExternal() == autoIndexExternal)
return;
_global_settings->setValue("autoIndexExternal", autoIndexExternal);
nDebug() << autoIndexExternal;
if (autoIndexExternal) {
_indexAll();
} else {