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
|
#include "filescanner.hpp"
#include <cstring>
#include <algorithm>
#include <fstream>
using std::size_t;
FileScanner::FileScanner() : QObject(nullptr), maxmnlen(0)
{
}
void FileScanner::add_magic_number(const std::string &m)
{
mn.push_back(m);
if (m.length() > maxmnlen)
maxmnlen = m.length();
}
void FileScanner::add_path(const fs::path &p, bool recurse)
{
paths.emplace_back(p, recurse);
}
template <class T>
void dirit_foreach(T iter, std::function<void(const fs::directory_entry& p)> f)
{
std::for_each(fs::begin(iter), fs::end(iter), f);
}
void FileScanner::scan()
{
size_t fcnt = 0;
auto opt = std::filesystem::directory_options::skip_permission_denied;
auto count_files = [&fcnt](const fs::directory_entry& e){
if (e.is_regular_file()) ++fcnt;
};
auto scan_file = [&fcnt, this](const fs::directory_entry &e) {
if (!e.is_regular_file()) return;
std::fstream fst(e.path(), std::ios::binary | std::ios::in);
std::string buf(maxmnlen, '\0');
fst.read(buf.data(), maxmnlen);
buf.resize(fst.gcount());
for (auto &magic : mn)
if (!memcmp(magic.data(), buf.data(), magic.length()))
{
ret.push_back(e.path());
break;
}
Q_EMIT file_scanned(e.path(), ++fcnt);
};
auto for_all_paths = [opt, this](std::function<void(const fs::directory_entry&)> f) {
for (auto &pe : paths)
{
fs::path p;
bool recurse;
std::tie(p, recurse) = pe;
if (recurse)
dirit_foreach(fs::recursive_directory_iterator(p, opt), f);
else
dirit_foreach(fs::directory_iterator(p, opt), f);
}
};
for_all_paths(count_files);
Q_EMIT scan_done_prep(fcnt);
fcnt = 0;
for_all_paths(scan_file);
}
std::vector<fs::path> FileScanner::file_list()
{
return ret;
}
|