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 {
11671167 for item in items {
11681168 guard case .folder(let path) = item.change else { continue }
11691169 var url = URL(fileURLWithPath: OrgPaths.expandingTilde(path)).standardizedFileURL
1170 var picked: URL?
11701171 if let chosenFolders {
11711172 if let workspace, Self.isRoot(url, in: workspace) { continue }
11721173 guard let chosen = chosenFolders[path] else {
@@ -1174,14 +1175,18 @@ extension EmacsImport {
11741175 continue
11751176 }
11761177 url = chosen.standardizedFileURL
1178 picked = chosen
11771179 }
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() } }
11781183 guard let workspace, FileManager.default.fileExists(atPath: url.path) else {
11791184 problems.append("\(path) doesn't exist")
11801185 continue
11811186 }
11821187 if Self.isRoot(url, in: workspace) { continue }
11831188 do {
1184 try await workspace.addRoot(url)
1189 try await workspace.addRoot(picked ?? url)
11851190 folders += 1
11861191 } catch {
11871192 problems.append("Can't add \(path): \(error.localizedDescription)")
Sources/OrgApp/FolderGrants.swift +3 −1
@@ -13,7 +13,9 @@ public enum SecurityBookmark {
1313 var stale = false
1414 guard let url = try? URL(resolvingBookmarkData: data, options: Sandbox.bookmarkResolutionOptions(sandboxed: sandboxed), bookmarkDataIsStale: &stale) else { return nil }
1515 _ = 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)
1719 }
1820}
1921
Sources/OrgApp/PublishFolders.swift +2 −3
@@ -30,11 +30,10 @@ public final class PublishFolders {
3030 /// The folder chosen for `project`, if any.
3131 public func folder(for project: String) -> URL? { folders[project] }
3232
33 /// Keeps `url`, a folder from a folder picker, as where `project` publishes. False when its
34 /// bookmark can't be made, so it lasts only until the app quits.
33 /// Keeps `url`, a folder from a folder picker, as where `project` publishes; making its bookmark
34 /// starts access. False when the bookmark can't be made, so it lasts only until the app quits.
3535 @discardableResult
3636 public func set(_ url: URL, for project: String) -> Bool {
37 _ = url.startAccessingSecurityScopedResource()
3837 folders[project] = url
3938 guard let data = SecurityBookmark.make(url, sandboxed: sandboxed) else { return false }
4039 var saved = defaults.dictionary(forKey: Self.key) as? [String: Data] ?? [:]
Sources/OrgstarMobile/EmacsImportScreen.swift +1 −2
@@ -66,8 +66,7 @@ struct EmacsImportScreen: View {
6666 // A second importer on the same view would replace the first.
6767 Color.clear.fileImporter(isPresented: $choosingFolder, allowedContentTypes: [.folder]) { chosen in
6868 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.
70 _ = url.startAccessingSecurityScopedResource()
69 // Access starts when the import adds it.
7170 folders[path] = url
7271 }
7372 }