-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscan.hh
More file actions
80 lines (63 loc) · 1.9 KB
/
scan.hh
File metadata and controls
80 lines (63 loc) · 1.9 KB
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
#ifndef GITCODE_DIRECTORY_SCANNER_HH
#define GITCODE_DIRECTORY_SCANNER_HH
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include <vector>
namespace gitcode::scan {
struct IgnoreRule {
std::string pattern;
bool directory_only = false;
};
struct ScanRules {
bool include_hidden = false;
bool follow_symlinks = false;
std::vector<IgnoreRule> ignore_rules;
std::vector<std::string> allowed_extensions;
std::size_t max_files = 10000;
};
struct ScanEntry {
std::filesystem::path relative_path;
std::uintmax_t size = 0;
bool is_directory = false;
};
struct ScanStatistics {
std::size_t visited = 0;
std::size_t indexed = 0;
std::size_t skipped = 0;
};
struct ScanResult {
std::filesystem::path root;
std::vector<ScanEntry> entries;
std::vector<std::filesystem::path> skipped_paths;
ScanStatistics statistics;
};
class DirectoryScanner {
public:
using ProgressCallback = std::function<void(const ScanEntry& entry)>;
explicit DirectoryScanner(ScanRules rules = {});
void SetProgressCallback(ProgressCallback callback);
[[nodiscard]] const ScanRules& Rules() const;
[[nodiscard]] ScanResult Scan(const std::filesystem::path& root) const;
private:
[[nodiscard]] bool ShouldSkip(const std::filesystem::path& relative_path,
bool is_directory) const;
[[nodiscard]] bool MatchesExtension(const std::filesystem::path& path) const;
ScanRules rules_;
ProgressCallback callback_;
};
class IgnoreMatcher {
public:
explicit IgnoreMatcher(std::vector<IgnoreRule> rules = {});
[[nodiscard]] bool Matches(std::string_view relative_path,
bool is_directory) const;
[[nodiscard]] std::size_t RuleCount() const;
[[nodiscard]] bool Empty() const;
private:
std::vector<IgnoreRule> rules_;
};
} // namespace gitcode::scan
#endif // GITCODE_DIRECTORY_SCANNER_HH