Commit 24bd979c0d
24bd979c0d7bb1e32744354d5c99bd7f54e1ed2f
parent: 6cb60eeae9
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-21 11:19 UTC
Start security-scoped access once per kept folder
Layout: unified · split
Sources/OrgApp/EmacsImport.swift
+6 −1
| @@ -1167,6 +1167,7 @@ extension EmacsImport { |
| 1167 | for item in items { |
1167 | for item in items { |
| 1168 | guard case .folder(let path) = item.change else { continue } |
1168 | guard case .folder(let path) = item.change else { continue } |
| 1169 | var url = URL(fileURLWithPath: OrgPaths.expandingTilde(path)).standardizedFileURL |
1169 | var url = URL(fileURLWithPath: OrgPaths.expandingTilde(path)).standardizedFileURL |
| |
1170 | var picked: URL? |
| 1170 | if let chosenFolders { |
1171 | if let chosenFolders { |
| 1171 | if let workspace, Self.isRoot(url, in: workspace) { continue } |
1172 | if let workspace, Self.isRoot(url, in: workspace) { continue } |
| 1172 | guard let chosen = chosenFolders[path] else { |
1173 | guard let chosen = chosenFolders[path] else { |
| @@ -1174,14 +1175,18 @@ extension EmacsImport { |
| 1174 | continue |
1175 | continue |
| 1175 | } |
1176 | } |
| 1176 | url = chosen.standardizedFileURL |
1177 | url = chosen.standardizedFileURL |
| |
1178 | picked = chosen |
| 1177 | } |
1179 | } |
| |
1180 | // A folder from a picker is security-scoped; the root's bookmark keeps its own access. |
| |
1181 | let accessing = picked?.startAccessingSecurityScopedResource() ?? false |
| |
1182 | defer { if accessing { picked?.stopAccessingSecurityScopedResource() } } |
| 1178 | guard let workspace, FileManager.default.fileExists(atPath: url.path) else { |
1183 | guard let workspace, FileManager.default.fileExists(atPath: url.path) else { |
| 1179 | problems.append("\(path) doesn't exist") |
1184 | problems.append("\(path) doesn't exist") |
| 1180 | continue |
1185 | continue |
| 1181 | } |
1186 | } |
| 1182 | if Self.isRoot(url, in: workspace) { continue } |
1187 | if Self.isRoot(url, in: workspace) { continue } |
| 1183 | do { |
1188 | do { |
| 1184 | try await workspace.addRoot(url) |
1189 | try await workspace.addRoot(picked ?? url) |
| 1185 | folders += 1 |
1190 | folders += 1 |
| 1186 | } catch { |
1191 | } catch { |
| 1187 | problems.append("Can't add \(path): \(error.localizedDescription)") |
1192 | problems.append("Can't add \(path): \(error.localizedDescription)") |
Sources/OrgApp/FolderGrants.swift
+3 −1
| @@ -13,7 +13,9 @@ public enum SecurityBookmark { |
| 13 | var stale = false |
13 | var stale = false |
| 14 | guard let url = try? URL(resolvingBookmarkData: data, options: Sandbox.bookmarkResolutionOptions(sandboxed: sandboxed), bookmarkDataIsStale: &stale) else { return nil } |
14 | guard let url = try? URL(resolvingBookmarkData: data, options: Sandbox.bookmarkResolutionOptions(sandboxed: sandboxed), bookmarkDataIsStale: &stale) else { return nil } |
| 15 | _ = url.startAccessingSecurityScopedResource() |
15 | _ = url.startAccessingSecurityScopedResource() |
| 16 | return (url, stale ? make(url, sandboxed: sandboxed) : nil) |
16 | // Not `make`, which would start access a second time. |
| |
17 | let renewed = stale ? try? url.bookmarkData(options: Sandbox.bookmarkCreationOptions(sandboxed: sandboxed), includingResourceValuesForKeys: nil, relativeTo: nil) : nil |
| |
18 | return (url, renewed) |
| 17 | } |
19 | } |
| 18 | } |
20 | } |
| 19 | |
21 | |
Sources/OrgApp/PublishFolders.swift
+2 −3
| @@ -30,11 +30,10 @@ public final class PublishFolders { |
| 30 | /// The folder chosen for `project`, if any. |
30 | /// The folder chosen for `project`, if any. |
| 31 | public func folder(for project: String) -> URL? { folders[project] } |
31 | public func folder(for project: String) -> URL? { folders[project] } |
| 32 | |
32 | |
| 33 | /// Keeps `url`, a folder from a folder picker, as where `project` publishes. False when its |
33 | /// Keeps `url`, a folder from a folder picker, as where `project` publishes; making its bookmark |
| 34 | /// bookmark can't be made, so it lasts only until the app quits. |
34 | /// starts access. False when the bookmark can't be made, so it lasts only until the app quits. |
| 35 | @discardableResult |
35 | @discardableResult |
| 36 | public func set(_ url: URL, for project: String) -> Bool { |
36 | public func set(_ url: URL, for project: String) -> Bool { |
| 37 | _ = url.startAccessingSecurityScopedResource() |
| |
| 38 | folders[project] = url |
37 | folders[project] = url |
| 39 | guard let data = SecurityBookmark.make(url, sandboxed: sandboxed) else { return false } |
38 | guard let data = SecurityBookmark.make(url, sandboxed: sandboxed) else { return false } |
| 40 | var saved = defaults.dictionary(forKey: Self.key) as? [String: Data] ?? [:] |
39 | var saved = defaults.dictionary(forKey: Self.key) as? [String: Data] ?? [:] |
Sources/OrgstarMobile/EmacsImportScreen.swift
+1 −2
| @@ -66,8 +66,7 @@ struct EmacsImportScreen: View { |
| 66 | // A second importer on the same view would replace the first. |
66 | // A second importer on the same view would replace the first. |
| 67 | Color.clear.fileImporter(isPresented: $choosingFolder, allowedContentTypes: [.folder]) { chosen in |
67 | Color.clear.fileImporter(isPresented: $choosingFolder, allowedContentTypes: [.folder]) { chosen in |
| 68 | guard let path = folderFor, case .success(let url) = chosen else { return } |
68 | guard let path = folderFor, case .success(let url) = chosen else { return } |
| 69 | // Kept for the bookmark the import makes; access stays on for the session. |
69 | // Access starts when the import adds it. |
| 70 | _ = url.startAccessingSecurityScopedResource() |
| |
| 71 | folders[path] = url |
70 | folders[path] = url |
| 72 | } |
71 | } |
| 73 | } |
72 | } |