forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIndexScanPolicy.swift
More file actions
160 lines (143 loc) · 5.01 KB
/
Copy pathFileIndexScanPolicy.swift
File metadata and controls
160 lines (143 loc) · 5.01 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
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
import Foundation
struct FileIndexScanPolicy {
enum DirectoryEntryPlan: Equatable {
case skipSubtree
case descend
case indexPackage(fileExtension: String, fileType: String)
}
static let standard = FileIndexScanPolicy()
let skipFolders: Set<String>
let packageExtensions: Set<String>
let maxDepth: Int
let maxFileSize: Int64
init(
skipFolders: Set<String> = [
".Trash", "node_modules", ".git", "__pycache__", ".venv", "venv",
".cache", ".npm", ".yarn", "Pods", "DerivedData", ".build",
"build", "dist", ".next", ".nuxt", "target", "vendor",
"Library", ".local", ".cargo", ".rustup",
],
packageExtensions: Set<String> = [
"app", "framework", "bundle", "plugin", "kext",
"xcodeproj", "xcworkspace", "playground",
],
maxDepth: Int = 3,
maxFileSize: Int64 = 500 * 1024 * 1024
) {
self.skipFolders = skipFolders
self.packageExtensions = packageExtensions
self.maxDepth = maxDepth
self.maxFileSize = maxFileSize
}
func standardScanRoots(
homeURL: URL,
applicationsURL: URL = URL(fileURLWithPath: "/Applications", isDirectory: true)
) -> [URL] {
[
homeURL.appendingPathComponent("Downloads", isDirectory: true),
homeURL.appendingPathComponent("Documents", isDirectory: true),
homeURL.appendingPathComponent("Desktop", isDirectory: true),
homeURL.appendingPathComponent("Developer", isDirectory: true),
homeURL.appendingPathComponent("Projects", isDirectory: true),
homeURL.appendingPathComponent("Code", isDirectory: true),
homeURL.appendingPathComponent("src", isDirectory: true),
homeURL.appendingPathComponent("repos", isDirectory: true),
homeURL.appendingPathComponent("Sites", isDirectory: true),
applicationsURL,
homeURL.appendingPathComponent("Applications", isDirectory: true),
]
}
/// Home subfolders macOS protects with per-folder TCC (Files and Folders):
/// enumerating them without Full Disk Access raises a system consent dialog.
static let tccProtectedFolderNames: Set<String> = ["Documents", "Desktop", "Downloads"]
/// Roots the **automatic** indexer (initial backfill, periodic rescan) may
/// enumerate. Without Full Disk Access the protected roots are dropped rather
/// than scanned — a background scan must never be the thing that throws a
/// "grant access to Documents?" sheet at the user. Explicit user-initiated scans
/// (Settings → Rescan files) keep the full root set.
func automaticScanRoots(
homeURL: URL,
applicationsURL: URL = URL(fileURLWithPath: "/Applications", isDirectory: true),
fullDiskAccessGranted: Bool
) -> [URL] {
let roots = standardScanRoots(homeURL: homeURL, applicationsURL: applicationsURL)
guard !fullDiskAccessGranted else { return roots }
return roots.filter { root in
guard root.deletingLastPathComponent().standardizedFileURL == homeURL.standardizedFileURL else {
return true
}
return !Self.tccProtectedFolderNames.contains(root.lastPathComponent)
}
}
func shouldScanDirectory(atDepth depth: Int) -> Bool {
depth <= maxDepth
}
func planDirectoryEntry(_ url: URL) -> DirectoryEntryPlan {
let name = url.lastPathComponent
if skipFolders.contains(name) {
return .skipSubtree
}
let ext = url.pathExtension.lowercased()
if packageExtensions.contains(ext) {
return .indexPackage(fileExtension: ext, fileType: ext == "app" ? "application" : "package")
}
return .descend
}
func makePackageRecord(
for url: URL,
folderName: String,
homePath: String,
depth: Int,
createdAt: Date?,
modifiedAt: Date?
) -> IndexedFileRecord? {
guard case .indexPackage(let ext, let fileType) = planDirectoryEntry(url) else {
return nil
}
return IndexedFileRecord(
path: relativePath(for: url, homePath: homePath),
filename: url.lastPathComponent,
fileExtension: ext,
fileType: fileType,
sizeBytes: 0,
folder: folderName,
depth: depth,
createdAt: createdAt,
modifiedAt: modifiedAt
)
}
func makeFileRecord(
for url: URL,
folderName: String,
homePath: String,
depth: Int,
isRegularFile: Bool,
sizeBytes: Int64,
createdAt: Date?,
modifiedAt: Date?
) -> IndexedFileRecord? {
guard isRegularFile, sizeBytes > 0, sizeBytes <= maxFileSize else {
return nil
}
let ext = url.pathExtension.isEmpty ? nil : url.pathExtension.lowercased()
let fileType = FileTypeCategory.from(extension: ext)
return IndexedFileRecord(
path: relativePath(for: url, homePath: homePath),
filename: url.lastPathComponent,
fileExtension: ext,
fileType: fileType.rawValue,
sizeBytes: sizeBytes,
folder: folderName,
depth: depth,
createdAt: createdAt,
modifiedAt: modifiedAt
)
}
func relativePath(for url: URL, homePath: String) -> String {
var path = url.path
if path.hasPrefix(homePath) {
path = "~" + path.dropFirst(homePath.count)
}
return path
}
}