Sources/OrgWorkspace/Discovery.swift
111 lines · 4577 bytes
1import Foundation
2import OrgIndex
3
4/// A folder the user added. Identified by its bookmark, so a moved folder is followed.
5public struct WorkspaceRoot: Sendable, Equatable, Codable {
6 public let bookmark: Data
7 /// The path when the bookmark was made or last refreshed, for display.
8 public var path: String
9
10 public init(url: URL) throws {
11 bookmark = try url.bookmarkData(options: [], includingResourceValuesForKeys: nil, relativeTo: nil)
12 path = url.standardizedFileURL.path
13 }
14
15 /// The folder's current location. `isStale` means the bookmark should be made again.
16 public func resolve() throws -> (url: URL, isStale: Bool) {
17 var isStale = false
18 let url = try URL(resolvingBookmarkData: bookmark, options: [], relativeTo: nil, bookmarkDataIsStale: &isStale)
19 _ = url.startAccessingSecurityScopedResource()
20 return (url.standardizedFileURL, isStale)
21 }
22}
23
24public struct DiscoveredFile: Sendable, Equatable {
25 public let url: URL
26 public let kind: FileKind
27 public let size: Int
28 public let mtime: Double
29}
30
31public struct Discovery: Sendable, Equatable {
32 public var files: [DiscoveredFile] = []
33 /// iCloud files not downloaded yet, by the name they will have.
34 public var placeholders: [URL] = []
35}
36
37public enum FileClassification: Sendable, Equatable {
38 case file(FileKind)
39 /// An iCloud placeholder (`.name.org.icloud`) for the file at the given name.
40 case placeholder(String)
41 case ignored
42}
43
44public enum WorkspaceScanner {
45 /// What a file name means to the workspace. Syncthing temp files, our own temp and backup
46 /// files, Emacs backups and auto-saves, and other hidden files are ignored; files that
47 /// aren't org files are `.other`.
48 public static func classify(_ name: String) -> FileClassification {
49 if name.hasPrefix(".") {
50 if name.hasSuffix(".icloud") {
51 let inner = String(name.dropFirst().dropLast(".icloud".count))
52 if case .file = classify(inner) { return .placeholder(inner) }
53 }
54 return .ignored
55 }
56 if name.hasSuffix("~") || name.hasPrefix("#") { return .ignored }
57 let conflict = name.contains(".sync-conflict-")
58 if name.hasSuffix(".org") { return .file(conflict ? .conflict : .org) }
59 if name.hasSuffix(".org_archive") { return .file(conflict ? .conflict : .archive) }
60 return .file(.other)
61 }
62
63 /// Every file under `root`, skipping hidden folders (`.git`, `.stfolder`, `.stversions`).
64 public static func scan(_ root: URL) throws -> Discovery {
65 let keys: [URLResourceKey] = [.isDirectoryKey, .fileSizeKey, .contentModificationDateKey]
66 guard let enumerator = FileManager.default.enumerator(at: root, includingPropertiesForKeys: keys) else {
67 return Discovery()
68 }
69 var discovery = Discovery()
70 for case let url as URL in enumerator {
71 let values = try url.resourceValues(forKeys: Set(keys))
72 let name = url.lastPathComponent
73 if values.isDirectory == true {
74 if name.hasPrefix(".") { enumerator.skipDescendants() }
75 continue
76 }
77 switch classify(name) {
78 case .file(let kind):
79 discovery.files.append(DiscoveredFile(
80 url: url.standardizedFileURL,
81 kind: kind,
82 size: values.fileSize ?? 0,
83 mtime: values.contentModificationDate?.timeIntervalSince1970 ?? 0
84 ))
85 case .placeholder(let inner):
86 discovery.placeholders.append(url.deletingLastPathComponent().appendingPathComponent(inner).standardizedFileURL)
87 case .ignored:
88 break
89 }
90 }
91 discovery.files.sort { $0.url.path < $1.url.path }
92 return discovery
93 }
94}
95
96/// Which files feed the agenda: `.org` files (not archives or conflict copies) matching one of
97/// the globs, relative to their root. No globs means every `.org` file.
98public struct AgendaScope: Sendable, Equatable {
99 public var globs: [String]
100
101 public init(globs: [String] = []) {
102 self.globs = globs
103 }
104
105 public func contains(relativePath: String, kind: FileKind) -> Bool {
106 guard kind == .org else { return false }
107 guard !globs.isEmpty else { return true }
108 // Without FNM_PATHNAME, `*` also matches `/`, so `work/*` covers nested folders.
109 return globs.contains { fnmatch($0, relativePath, 0) == 0 }
110 }
111}