Commit 6e84c5315a
Verified · cmc
Layout: unified · split
Sources/OrgApp/DocumentSession.swift +13 −2
| @@ -960,8 +960,7 @@ public final class DocumentSession { | ||
| 960 | 960 | let output = try await run(job) |
| 961 | 961 | guard entries.contains(where: { $0 === entry }) else { return "The file was closed before the block finished." } |
| 962 | 962 | if let file = Babel.fileResult(job, output: output) { |
| 963 | let runDirectory = job.directory.map { URL(fileURLWithPath: $0, relativeTo: directory).standardizedFileURL } ?? directory | |
| 964 | let target = file.path.hasPrefix("/") ? URL(fileURLWithPath: file.path) : runDirectory.appendingPathComponent(file.path) | |
| 963 | let target = Self.resultFile(file.path, job: job, directory: directory) | |
| 965 | 964 | do { |
| 966 | 965 | try file.contents.write(to: target, atomically: true, encoding: .utf8) |
| 967 | 966 | } catch { |
| @@ -990,6 +989,18 @@ public final class DocumentSession { | ||
| 990 | 989 | } |
| 991 | 990 | } |
| 992 | 991 | |
| 992 | /// Where a block's `:file` result goes: relative to `:dir`, itself relative to `directory`. | |
| 993 | static func resultFile(_ name: String, job: BabelJob, directory: URL) -> URL { | |
| 994 | let runDirectory = job.directory.map { URL(fileURLWithPath: $0, relativeTo: directory).standardizedFileURL } ?? directory | |
| 995 | return name.hasPrefix("/") ? URL(fileURLWithPath: name) : runDirectory.appendingPathComponent(name) | |
| 996 | } | |
| 997 | ||
| 998 | /// The `:file` the block writes its result to, for the open file; nil without one. | |
| 999 | public func resultFile(of job: BabelJob) -> String? { | |
| 1000 | guard let path, let name = job.finish.file else { return nil } | |
| 1001 | return Self.resultFile(name, job: job, directory: URL(fileURLWithPath: path).deletingLastPathComponent()).path | |
| 1002 | } | |
| 1003 | ||
| 993 | 1004 | /// The block running now, to cancel. |
| 994 | 1005 | @ObservationIgnored private var babelTask: Task<String, Never>? |
| 995 | 1006 | public private(set) var isRunningBabel = false |
Sources/OrgApp/FolderGrants.swift added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | ||
| 4 | /// Bookmarks that give access back after relaunch: security-scoped in the Mac sandbox, plain | |
| 5 | /// elsewhere. | |
| 6 | public enum SecurityBookmark { | |
| 7 | public static func make(_ url: URL, sandboxed: Bool = Sandbox.isActive) -> Data? { | |
| 8 | try? url.bookmarkData(options: Sandbox.bookmarkCreationOptions(sandboxed: sandboxed)) | |
| 9 | } | |
| 10 | ||
| 11 | /// The bookmarked URL with access to it started, and new data when the bookmark was stale. | |
| 12 | public static func resolve(_ data: Data, sandboxed: Bool = Sandbox.isActive) -> (url: URL, renewed: Data?)? { | |
| 13 | var stale = false | |
| 14 | guard let url = try? URL(resolvingBookmarkData: data, options: Sandbox.bookmarkResolutionOptions(sandboxed: sandboxed), bookmarkDataIsStale: &stale) else { return nil } | |
| 15 | _ = url.startAccessingSecurityScopedResource() | |
| 16 | return (url, stale ? make(url, sandboxed: sandboxed) : nil) | |
| 17 | } | |
| 18 | } | |
| 19 | ||
| 20 | /// Folders outside the sidebar's folders that the Mac sandbox let orgstar into through an open | |
| 21 | /// panel: setup files, includes, tangle and export targets and the like. Kept as bookmarks and | |
| 22 | /// started when the store is first used. | |
| 23 | @MainActor | |
| 24 | public final class FolderGrants { | |
| 25 | public static let shared = FolderGrants() | |
| 26 | static let key = "folderGrants" | |
| 27 | ||
| 28 | private let defaults: UserDefaults | |
| 29 | private let sandboxed: Bool | |
| 30 | /// The granted folders, with access started. | |
| 31 | public private(set) var folders: [URL] = [] | |
| 32 | ||
| 33 | init(defaults: UserDefaults = .standard, sandboxed: Bool = Sandbox.isActive) { | |
| 34 | self.defaults = defaults | |
| 35 | self.sandboxed = sandboxed | |
| 36 | let saved = defaults.array(forKey: Self.key) as? [Data] ?? [] | |
| 37 | var kept: [Data] = [] | |
| 38 | for data in saved { | |
| 39 | // One that doesn't resolve now (an unmounted volume) is kept for later. | |
| 40 | guard let (url, renewed) = SecurityBookmark.resolve(data, sandboxed: sandboxed) else { | |
| 41 | kept.append(data) | |
| 42 | continue | |
| 43 | } | |
| 44 | folders.append(url) | |
| 45 | kept.append(renewed ?? data) | |
| 46 | } | |
| 47 | if kept != saved { defaults.set(kept, forKey: Self.key) } | |
| 48 | } | |
| 49 | ||
| 50 | /// Keeps `url`, a folder from an open panel, and starts access to it. | |
| 51 | public func add(_ url: URL) { | |
| 52 | _ = url.startAccessingSecurityScopedResource() | |
| 53 | guard !folders.contains(where: { $0.standardizedFileURL == url.standardizedFileURL }), | |
| 54 | let data = SecurityBookmark.make(url, sandboxed: sandboxed) else { return } | |
| 55 | folders.append(url) | |
| 56 | defaults.set((defaults.array(forKey: Self.key) as? [Data] ?? []) + [data], forKey: Self.key) | |
| 57 | } | |
| 58 | } | |
Sources/OrgApp/TangleWriter.swift +16 −2
| @@ -3,7 +3,7 @@ import OrgCore | ||
| 3 | 3 | |
| 4 | 4 | /// Writes what `Tangle.run` produced, as `org-babel-tangle` does: a file whose contents |
| 5 | 5 | /// haven't changed is left alone, `:mkdirp` makes its folder, a read-only file is replaced, |
| 6 | /// and the mode is set after writing. | |
| 6 | /// and the mode is set after writing. A mode that can't be set doesn't stop the others. | |
| 7 | 7 | public enum TangleWriter { |
| 8 | 8 | public struct Failure: Error, Equatable { |
| 9 | 9 | public let message: String |
| @@ -14,6 +14,7 @@ public enum TangleWriter { | ||
| 14 | 14 | public static func write(_ output: Tangle.Output, source: String) throws(Failure) -> [String] { |
| 15 | 15 | let manager = FileManager.default |
| 16 | 16 | var paths: [String] = [] |
| 17 | var modeFailure: Failure? | |
| 17 | 18 | for file in output.files { |
| 18 | 19 | let url = URL(fileURLWithPath: file.path) |
| 19 | 20 | let data = Data(file.contents.utf8) |
| @@ -37,12 +38,19 @@ public enum TangleWriter { | ||
| 37 | 38 | } |
| 38 | 39 | do { |
| 39 | 40 | try data.write(to: url) |
| 40 | if let mode = file.mode { try manager.setAttributes([.posixPermissions: mode], ofItemAtPath: file.path) } | |
| 41 | 41 | } catch { |
| 42 | 42 | throw Failure(message: "Can't write \(file.path): \(error.localizedDescription)") |
| 43 | 43 | } |
| 44 | if let mode = file.mode { | |
| 45 | do { | |
| 46 | try manager.setAttributes([.posixPermissions: mode], ofItemAtPath: file.path) | |
| 47 | } catch { | |
| 48 | modeFailure = modeFailure ?? Failure(message: "\(message(output, source: source)), but can't set the mode of \(file.path): \(error.localizedDescription)") | |
| 49 | } | |
| 50 | } | |
| 44 | 51 | paths.append(file.path) |
| 45 | 52 | } |
| 53 | if let modeFailure { throw modeFailure } | |
| 46 | 54 | return paths |
| 47 | 55 | } |
| 48 | 56 | |
| @@ -53,6 +61,12 @@ public enum TangleWriter { | ||
| 53 | 61 | } |
| 54 | 62 | |
| 55 | 63 | extension DocumentSession { |
| 64 | /// The files `tangle` would write. | |
| 65 | public func tangleFiles(_ scope: Tangle.Scope) -> [String] { | |
| 66 | guard let path, let document, case .success(let output) = Tangle.run(text: document.text, path: path, scope: scope, tree: document.tree) else { return [] } | |
| 67 | return output.files.map(\.path) | |
| 68 | } | |
| 69 | ||
| 56 | 70 | /// Tangles the open file: its text as it is now, written beside it. |
| 57 | 71 | public func tangle(_ scope: Tangle.Scope) -> String { |
| 58 | 72 | guard let path, let document else { return "No file is open" } |
Sources/OrgApp/WorkspaceModel.swift +26 −2
| @@ -136,9 +136,33 @@ public final class WorkspaceModel { | ||
| 136 | 136 | /// Resolved root paths, sorted. |
| 137 | 137 | public var rootPaths: [String] { resolvedRoots.keys.sorted() } |
| 138 | 138 | |
| 139 | /// Whether `url` is a root or inside one. | |
| 139 | /// Whether `url` is a root or a granted folder, or inside one. | |
| 140 | 140 | public func contains(_ url: URL) -> Bool { |
| 141 | Self.isInside(url, roots: Array(resolvedRoots.values)) | |
| 141 | Self.isInside(url, roots: Array(resolvedRoots.values) + FolderGrants.shared.folders) | |
| 142 | } | |
| 143 | ||
| 144 | /// The folders to ask for so that `paths` can be read and written: in the Mac sandbox, for | |
| 145 | /// each path outside the roots, the grants and the app's container, its nearest existing | |
| 146 | /// folder. `directories` takes the paths as folders rather than files. Empty unsandboxed. | |
| 147 | public func foldersToGrant(for paths: [String], directories: Bool = false) -> [URL] { | |
| 148 | guard Sandbox.isActive else { return [] } | |
| 149 | let reachable = Array(resolvedRoots.values) + FolderGrants.shared.folders + [URL(fileURLWithPath: NSHomeDirectory())] | |
| 150 | return Self.foldersToGrant(for: paths, directories: directories, reachable: reachable) | |
| 151 | } | |
| 152 | ||
| 153 | nonisolated static func foldersToGrant( | |
| 154 | for paths: [String], directories: Bool, reachable: [URL], | |
| 155 | exists: (String) -> Bool = { FileManager.default.fileExists(atPath: $0) } | |
| 156 | ) -> [URL] { | |
| 157 | var folders: [URL] = [] | |
| 158 | for path in paths where !path.isEmpty { | |
| 159 | let url = URL(fileURLWithPath: path).standardizedFileURL | |
| 160 | guard !isInside(url, roots: reachable) else { continue } | |
| 161 | var folder = directories ? url : url.deletingLastPathComponent() | |
| 162 | while folder.pathComponents.count > 1, !exists(folder.path) { folder.deleteLastPathComponent() } | |
| 163 | if !folders.contains(folder) { folders.append(folder) } | |
| 164 | } | |
| 165 | return folders | |
| 142 | 166 | } |
| 143 | 167 | |
| 144 | 168 | /// Whether `url` is one of `roots` or inside one, by whole path components, as written or |
Sources/OrgCore/Compute/Babel.swift +1 −1
| @@ -74,7 +74,7 @@ public struct BabelJob: Sendable, Equatable { | ||
| 74 | 74 | /// An inline block: the result goes after it, in `{{{results(…)}}}`. |
| 75 | 75 | var inline = false |
| 76 | 76 | /// `:file` (under `:output-dir`) with `:results file`, and `:file-desc`. |
| 77 | var file: String? | |
| 77 | public internal(set) var file: String? | |
| 78 | 78 | var fileDescription: String? |
| 79 | 79 | /// The program writes `file` itself, as graphics languages do. |
| 80 | 80 | var writesFile = false |
Sources/OrgCore/Export/ExportOptions.swift +27 −3
| @@ -116,9 +116,7 @@ public enum ExportPreprocess { | ||
| 116 | 116 | file = String(spec[..<r.lowerBound]) |
| 117 | 117 | target = String(spec[r.upperBound...]) |
| 118 | 118 | } |
| 119 | let url = (file as NSString).isAbsolutePath || directory == nil | |
| 120 | ? URL(fileURLWithPath: OrgPaths.expandingTilde(file)) | |
| 121 | : directory!.appendingPathComponent(file) | |
| 119 | let url = includeURL(file, directory: directory) | |
| 122 | 120 | guard var included = try? String(contentsOf: url, encoding: .utf8) else { |
| 123 | 121 | lines.append("") |
| 124 | 122 | continue |
| @@ -152,6 +150,32 @@ public enum ExportPreprocess { | ||
| 152 | 150 | return lines.joined(separator: "\n") |
| 153 | 151 | } |
| 154 | 152 | |
| 153 | /// The files `#+INCLUDE` names in `text`, and in the files they include, that can't be read. | |
| 154 | public static func unreadableIncludes(_ text: String, directory: URL?, depth: Int = 0) -> [String] { | |
| 155 | guard depth < 8, text.containsIgnoringASCIICase("#+INCLUDE:") else { return [] } | |
| 156 | var unreadable: [String] = [] | |
| 157 | for line in text.components(separatedBy: "\n") { | |
| 158 | guard let m = line.firstMatch(of: #/(?i)^[ \t]*#\+INCLUDE:[ \t]+"([^"]+)"/#) else { continue } | |
| 159 | let spec = String(m.1) | |
| 160 | let url = includeURL(spec.range(of: "::").map { String(spec[..<$0.lowerBound]) } ?? spec, directory: directory) | |
| 161 | guard let included = try? String(contentsOf: url, encoding: .utf8) else { | |
| 162 | if !unreadable.contains(url.path) { unreadable.append(url.path) } | |
| 163 | continue | |
| 164 | } | |
| 165 | for path in unreadableIncludes(included, directory: url.deletingLastPathComponent(), depth: depth + 1) where !unreadable.contains(path) { | |
| 166 | unreadable.append(path) | |
| 167 | } | |
| 168 | } | |
| 169 | return unreadable | |
| 170 | } | |
| 171 | ||
| 172 | /// An include's file, relative to `directory`. | |
| 173 | static func includeURL(_ file: String, directory: URL?) -> URL { | |
| 174 | (file as NSString).isAbsolutePath || directory == nil | |
| 175 | ? URL(fileURLWithPath: OrgPaths.expandingTilde(file)) | |
| 176 | : directory!.appendingPathComponent(file) | |
| 177 | } | |
| 178 | ||
| 155 | 179 | /// The subtree of `text` at `*Heading` or `#custom-id`. |
| 156 | 180 | static func subtree(_ text: String, _ target: String) -> String? { |
| 157 | 181 | let tree = OrgParser.parse(text) |
Sources/Orgstar/ContentView.swift +51 −4
| @@ -575,6 +575,7 @@ struct ContentView: View { | ||
| 575 | 575 | commands.show("No file is open") |
| 576 | 576 | return |
| 577 | 577 | } |
| 578 | guard readsIncludes(text, path: path) else { return } | |
| 578 | 579 | let url = URL(fileURLWithPath: path) |
| 579 | 580 | let base = url.deletingPathExtension() |
| 580 | 581 | let defaults = Preferences.orgSettings.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile) |
| @@ -673,11 +674,17 @@ struct ContentView: View { | ||
| 673 | 674 | } |
| 674 | 675 | } |
| 675 | 676 | |
| 676 | /// `url`; in the App Store build, when it is outside every folder, where a save panel puts it | |
| 677 | /// instead, as the sandbox lets orgstar write only there. Nil when the panel is canceled. | |
| 677 | /// `url`; in the App Store build, when the sandbox keeps orgstar out of its folder, `url` once | |
| 678 | /// that folder is granted, or where a save panel puts it instead. Nil when canceled. | |
| 678 | 679 | private func writable(_ url: URL) -> URL? { |
| 679 | 680 | #if APPSTORE |
| 680 | guard !workspace.contains(url) else { return url } | |
| 681 | guard let folder = workspace.foldersToGrant(for: [url.path]).first else { return url } | |
| 682 | let elsewhere = String(localized: "Save Elsewhere…", comment: "Button that asks where an export goes instead") | |
| 683 | switch GrantAccess.ask(folder, workspace: workspace, alternative: elsewhere) { | |
| 684 | case .granted: return url | |
| 685 | case .canceled: return nil | |
| 686 | case .alternative: break | |
| 687 | } | |
| 681 | 688 | let panel = NSSavePanel() |
| 682 | 689 | panel.directoryURL = url.deletingLastPathComponent() |
| 683 | 690 | panel.nameFieldStringValue = url.lastPathComponent |
| @@ -688,6 +695,39 @@ struct ContentView: View { | ||
| 688 | 695 | #endif |
| 689 | 696 | } |
| 690 | 697 | |
| 698 | /// In the App Store build, asks for the folders outside the sidebar's folders that `paths` | |
| 699 | /// need. False when one is still out of reach. | |
| 700 | private func reach(_ paths: @autoclosure () -> [String], directories: Bool = false) -> Bool { | |
| 701 | #if APPSTORE | |
| 702 | GrantAccess.reach(paths(), directories: directories, workspace: workspace) | |
| 703 | #else | |
| 704 | true | |
| 705 | #endif | |
| 706 | } | |
| 707 | ||
| 708 | /// In the App Store build, offers access to the setup files and includes of `text` that | |
| 709 | /// can't be read. False, with a message, when an include still can't be read, rather than | |
| 710 | /// exporting it as an empty line. | |
| 711 | private func readsIncludes(_ text: String, path: String) -> Bool { | |
| 712 | #if APPSTORE | |
| 713 | let directory = URL(fileURLWithPath: path).deletingLastPathComponent() | |
| 714 | var setupFiles: [String] = [] | |
| 715 | _ = Preferences.orgSettings.resolvingSetupFiles(in: text, path: path) { file in | |
| 716 | let contents = OrgSettings.readFile(file) | |
| 717 | if contents == nil { setupFiles.append(file) } | |
| 718 | return contents | |
| 719 | } | |
| 720 | let includes = ExportPreprocess.unreadableIncludes(text, directory: directory) | |
| 721 | guard !(setupFiles + includes).isEmpty else { return true } | |
| 722 | _ = reach(setupFiles + includes) | |
| 723 | guard let unreadable = ExportPreprocess.unreadableIncludes(text, directory: directory).first else { return true } | |
| 724 | commands.show(String(localized: "Can't include \(OrgPaths.abbreviatingWithTilde(unreadable)).", comment: "Export stopped by an #+INCLUDE file that can't be read")) | |
| 725 | return false | |
| 726 | #else | |
| 727 | return true | |
| 728 | #endif | |
| 729 | } | |
| 730 | ||
| 691 | 731 | /// Says a command doesn't run in this build. |
| 692 | 732 | static func unavailable(_ title: String) -> String { |
| 693 | 733 | String(localized: "\(CommandTitle.localized(title)) isn't available.", comment: "A command the Mac App Store build leaves out") |
| @@ -696,6 +736,7 @@ struct ContentView: View { | ||
| 696 | 736 | /// `org-icalendar-export-agenda-files`, or with `combined`, `org-icalendar-combine-agenda-files`. |
| 697 | 737 | private func exportAgendaCalendars(combined: Bool) { |
| 698 | 738 | let paths = workspace.agendaFilePaths() |
| 739 | guard reach(paths + (combined ? [OrgPreferences.icalendarCombinedFile()] : [])) else { return } | |
| 699 | 740 | let read = { (path: String) -> (text: String, settings: OrgSettings)? in |
| 700 | 741 | guard let text = session.openDocuments.first(where: { $0.path == path })?.document.text ?? (try? String(contentsOfFile: path, encoding: .utf8)) else { return nil } |
| 701 | 742 | return (text, Preferences.orgSettings.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile)) |
| @@ -848,6 +889,7 @@ struct ContentView: View { | ||
| 848 | 889 | } |
| 849 | 890 | #endif |
| 850 | 891 | if let url = PlotOutput.file(plot, directory: directory) { |
| 892 | guard reach([url.path]) else { return } | |
| 851 | 893 | do { |
| 852 | 894 | try PlotOutput.write(plot, to: url) |
| 853 | 895 | plotWritten(url, editor: editor) |
| @@ -877,6 +919,9 @@ struct ContentView: View { | ||
| 877 | 919 | } |
| 878 | 920 | let codeStyle = HTMLCodeStyle(highlighter: PaneView.highlighter, theme: theme) |
| 879 | 921 | func run(_ chosen: [PublishProject], only: String? = nil) { |
| 922 | // The chosen projects and their components read and write these folders. | |
| 923 | let involved = { loaded.projects.filter { project in chosen.contains { $0 == project || $0.components.contains(project.name) } } } | |
| 924 | guard reach(involved().flatMap { [$0.baseDirectory, $0.publishingDirectory] }, directories: true) else { return } | |
| 880 | 925 | do { |
| 881 | 926 | let report = try Publish.publish(chosen, all: loaded.projects, only: only, read: read) { function, text, path, bodyOnly in |
| 882 | 927 | let settings = Preferences.orgSettings.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile) |
| @@ -918,7 +963,7 @@ struct ContentView: View { | ||
| 918 | 963 | DispatchQueue.main.async { commands.editor?.perform(SparseTreeCommand(), answers: ["sparse": "/", "regexp": regexp]) } |
| 919 | 964 | } |
| 920 | 965 | case .tangle(let scope): |
| 921 | guard ProAccess.shared.allows(.babel) else { return } | |
| 966 | guard ProAccess.shared.allows(.babel), reach(session.tangleFiles(scope)) else { return } | |
| 922 | 967 | commands.show(session.tangle(scope)) |
| 923 | 968 | case .agenda(let start, let days): |
| 924 | 969 | commands.agendaRequest = (start, days) |
| @@ -952,6 +997,7 @@ struct ContentView: View { | ||
| 952 | 997 | case .babel(let job): |
| 953 | 998 | guard ProAccess.shared.allows(.babel) else { return } |
| 954 | 999 | let run = { |
| 1000 | guard reach(session.resultFile(of: job).map { [$0] } ?? []) else { return } | |
| 955 | 1001 | commands.show("Running \(job.language) block… (⌘. to cancel)") |
| 956 | 1002 | session.startBabel(job) { commands.show($0) } |
| 957 | 1003 | } |
| @@ -1259,6 +1305,7 @@ struct ContentView: View { | ||
| 1259 | 1305 | case let id where id.hasPrefix("app.crypt."): |
| 1260 | 1306 | crypt(id) |
| 1261 | 1307 | case let id where id.hasPrefix("app.attach."): |
| 1308 | guard reach(attachActions.entry().flatMap { Attachments.directory($0, noFSCheck: true) }.map { [$0] } ?? [], directories: true) else { return } | |
| 1262 | 1309 | attachActions.run(id) |
| 1263 | 1310 | case let id where id.hasPrefix("app.dailies.") || id.hasPrefix("app.node."): |
| 1264 | 1311 | roamActions.run(id) |
Sources/Orgstar/GrantAccess.swift added +52
| @@ -0,0 +1,52 @@ | ||
| 1 | #if APPSTORE | |
| 2 | import AppKit | |
| 3 | import OrgApp | |
| 4 | import OrgCore | |
| 5 | ||
| 6 | /// Access to folders outside the sidebar's folders, which the sandbox keeps orgstar out of | |
| 7 | /// until they're chosen in an open panel. | |
| 8 | @MainActor | |
| 9 | enum GrantAccess { | |
| 10 | enum Answer { | |
| 11 | case granted, alternative, canceled | |
| 12 | } | |
| 13 | ||
| 14 | /// Says orgstar can't reach `folder` and offers an open panel at it, plus `alternative` | |
| 15 | /// when given. A folder chosen in the panel is kept as a grant; `granted` once it covers | |
| 16 | /// `folder`. | |
| 17 | static func ask(_ folder: URL, workspace: WorkspaceModel, alternative: String? = nil) -> Answer { | |
| 18 | let alert = NSAlert() | |
| 19 | let name = OrgPaths.abbreviatingWithTilde(folder.path) | |
| 20 | alert.messageText = String(localized: "orgstar can't reach \(name).", comment: "Alert for a folder outside the sidebar's folders") | |
| 21 | alert.informativeText = String(localized: "It isn't in one of your folders. Grant access to it to continue.", comment: "Alert for a folder outside the sidebar's folders") | |
| 22 | alert.addButton(withTitle: String(localized: "Grant Access…", comment: "Button that opens a panel to give orgstar access to a folder")) | |
| 23 | if let alternative { alert.addButton(withTitle: alternative) } | |
| 24 | alert.addButton(withTitle: String(localized: "Cancel")) | |
| 25 | switch alert.runModal() { | |
| 26 | case .alertFirstButtonReturn: break | |
| 27 | case .alertSecondButtonReturn where alternative != nil: return .alternative | |
| 28 | default: return .canceled | |
| 29 | } | |
| 30 | let panel = NSOpenPanel() | |
| 31 | panel.canChooseDirectories = true | |
| 32 | panel.canChooseFiles = false | |
| 33 | panel.canCreateDirectories = true | |
| 34 | panel.directoryURL = folder | |
| 35 | panel.prompt = String(localized: "Grant Access", comment: "Open panel button that gives orgstar access to a folder") | |
| 36 | panel.message = String(localized: "Choose \(name) or a folder that contains it.", comment: "Open panel for giving orgstar access to a folder") | |
| 37 | guard panel.runModal() == .OK, let url = panel.url else { return .canceled } | |
| 38 | FolderGrants.shared.add(url) | |
| 39 | return workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty ? .granted : .canceled | |
| 40 | } | |
| 41 | ||
| 42 | /// Asks for each folder `paths` need, in turn. Whether all of them can now be reached. | |
| 43 | static func reach(_ paths: [String], directories: Bool = false, workspace: WorkspaceModel) -> Bool { | |
| 44 | for folder in workspace.foldersToGrant(for: paths, directories: directories) { | |
| 45 | // An earlier grant may cover this one. | |
| 46 | guard !workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty else { continue } | |
| 47 | guard ask(folder, workspace: workspace) == .granted else { return false } | |
| 48 | } | |
| 49 | return true | |
| 50 | } | |
| 51 | } | |
| 52 | #endif | |
Sources/Orgstar/OrgstarApp.swift +1
| @@ -335,6 +335,7 @@ struct OrgstarApp: App { | ||
| 335 | 335 | init() { |
| 336 | 336 | #if APPSTORE |
| 337 | 337 | AgendaSnapshot.useGroupContainer = true |
| 338 | _ = FolderGrants.shared | |
| 338 | 339 | ProStore.shared.start() |
| 339 | 340 | #else |
| 340 | 341 | DocumentSession.programs = Programs() |
Tests/OrgAppTests/FolderGrantsTests.swift added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | import Foundation | |
| 2 | import Testing | |
| 3 | @testable import OrgApp | |
| 4 | ||
| 5 | @MainActor | |
| 6 | struct FolderGrantsTests { | |
| 7 | func defaults() -> UserDefaults { | |
| 8 | let name = "orgstar-folder-grants-\(UUID().uuidString)" | |
| 9 | let defaults = UserDefaults(suiteName: name)! | |
| 10 | defaults.removePersistentDomain(forName: name) | |
| 11 | return defaults | |
| 12 | } | |
| 13 | ||
| 14 | @Test func grantsComeBackAtLaunch() throws { | |
| 15 | let store = defaults() | |
| 16 | let folder = try Folder() | |
| 17 | let grants = FolderGrants(defaults: store, sandboxed: false) | |
| 18 | #expect(grants.folders.isEmpty) | |
| 19 | grants.add(folder.url) | |
| 20 | grants.add(folder.url) | |
| 21 | #expect(grants.folders == [folder.url]) | |
| 22 | #expect((store.array(forKey: FolderGrants.key) as? [Data])?.count == 1) | |
| 23 | let relaunched = FolderGrants(defaults: store, sandboxed: false) | |
| 24 | #expect(relaunched.folders.map { $0.resolvingSymlinksInPath().path } == [folder.url.resolvingSymlinksInPath().path]) | |
| 25 | } | |
| 26 | ||
| 27 | @Test func bookmarksThatDontResolveAreKept() { | |
| 28 | let store = defaults() | |
| 29 | store.set([Data("not a bookmark".utf8)], forKey: FolderGrants.key) | |
| 30 | #expect(FolderGrants(defaults: store, sandboxed: false).folders.isEmpty) | |
| 31 | #expect((store.array(forKey: FolderGrants.key) as? [Data])?.count == 1) | |
| 32 | } | |
| 33 | ||
| 34 | @Test func fileBookmarkRoundTrip() throws { | |
| 35 | let folder = try Folder() | |
| 36 | let file = try folder.write("a.org", "* a\n") | |
| 37 | let data = try #require(SecurityBookmark.make(file, sandboxed: false)) | |
| 38 | let resolved = try #require(SecurityBookmark.resolve(data, sandboxed: false)) | |
| 39 | #expect(resolved.url.resolvingSymlinksInPath().path == file.resolvingSymlinksInPath().path) | |
| 40 | } | |
| 41 | ||
| 42 | @Test func foldersToGrantSkipReachablePaths() { | |
| 43 | let reachable = [URL(fileURLWithPath: "/notes"), URL(fileURLWithPath: "/container")] | |
| 44 | let existing: Set<String> = ["/", "/notes", "/container", "/other", "/other/site"] | |
| 45 | let folders = WorkspaceModel.foldersToGrant( | |
| 46 | for: ["/notes/a.org", "/container/x", "/other/setup.org", "/other/inc.org", "", "/other/site/new/out.html"], | |
| 47 | directories: false, reachable: reachable, exists: existing.contains) | |
| 48 | #expect(folders.map(\.path) == ["/other", "/other/site"]) | |
| 49 | } | |
| 50 | ||
| 51 | @Test func foldersToGrantForFolders() { | |
| 52 | let folders = WorkspaceModel.foldersToGrant( | |
| 53 | for: ["/out/public", "/out/missing/deep"], directories: true, reachable: [], | |
| 54 | exists: ["/", "/out", "/out/public"].contains) | |
| 55 | #expect(folders.map(\.path) == ["/out/public", "/out"]) | |
| 56 | } | |
| 57 | } | |
Tests/OrgAppTests/TangleWriterTests.swift +20 −1
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import Foundation |
| 2 | import OrgCore | |
| 3 | 2 | import Testing |
| 4 | 3 | @testable import OrgApp |
| 4 | @testable import OrgCore | |
| 5 | 5 | |
| 6 | 6 | struct TangleWriterTests { |
| 7 | 7 | @Test func writesMakesFoldersAndSetsModes() throws { |
| @@ -42,4 +42,23 @@ struct TangleWriterTests { | ||
| 42 | 42 | try TangleWriter.write(own, source: source.path) |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | ||
| 46 | @Test func aModeThatCantBeSetIsReportedAfterWriting() throws { | |
| 47 | let folder = try Folder() | |
| 48 | let script = folder.url.appendingPathComponent("run.sh").path | |
| 49 | // Anyone can write /dev/null, but only its owner can change its mode. | |
| 50 | let output = Tangle.Output(files: [ | |
| 51 | Tangle.File(path: "/dev/null", contents: "x\n", mode: 0o755, makeDirectories: false), | |
| 52 | Tangle.File(path: script, contents: "#!/bin/sh\n", mode: 0o755, makeDirectories: false), | |
| 53 | ], blocks: 2) | |
| 54 | let source = folder.url.appendingPathComponent("notes.org").path | |
| 55 | do { | |
| 56 | try TangleWriter.write(output, source: source) | |
| 57 | Issue.record("The mode of /dev/null was set") | |
| 58 | } catch { | |
| 59 | #expect(error.message.hasPrefix("Tangled 2 code blocks from notes.org, but can't set the mode of /dev/null")) | |
| 60 | } | |
| 61 | #expect(try String(contentsOfFile: script, encoding: .utf8) == "#!/bin/sh\n") | |
| 62 | #expect(try FileManager.default.attributesOfItem(atPath: script)[.posixPermissions] as? Int == 0o755) | |
| 63 | } | |
| 45 | 64 | } |
Tests/OrgCoreTests/ExportOptionsTests.swift +16
| @@ -104,4 +104,20 @@ struct ExportOptionsTests { | ||
| 104 | 104 | let ours = ExportPreprocess.macros(ExportPreprocess.includes(text, directory: folder), path: path) |
| 105 | 105 | #expect(ours == emacs.first, "ours:\n\(ours)\nemacs:\n\(emacs.first ?? "")") |
| 106 | 106 | } |
| 107 | ||
| 108 | @Test func unreadableIncludesAreListed() throws { | |
| 109 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-includes-\(UUID().uuidString)") | |
| 110 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) | |
| 111 | defer { try? FileManager.default.removeItem(at: folder) } | |
| 112 | try "#+INCLUDE: \"gone.org\"\ntext\n".write(to: folder.appendingPathComponent("part.org"), atomically: true, encoding: .utf8) | |
| 113 | let text = """ | |
| 114 | #+INCLUDE: "part.org::*Sub" :minlevel 1 | |
| 115 | #+INCLUDE: "missing.org" src sh | |
| 116 | #+INCLUDE: "missing.org" | |
| 117 | """ | |
| 118 | #expect(ExportPreprocess.unreadableIncludes(text, directory: folder) == [ | |
| 119 | folder.appendingPathComponent("gone.org").path, folder.appendingPathComponent("missing.org").path, | |
| 120 | ]) | |
| 121 | #expect(ExportPreprocess.unreadableIncludes("no includes", directory: folder).isEmpty) | |
| 122 | } | |
| 107 | 123 | } |
l10n/Localizable.xcstrings +245
| @@ -4707,6 +4707,41 @@ | ||
| 4707 | 4707 | } |
| 4708 | 4708 | } |
| 4709 | 4709 | }, |
| 4710 | "Can't include %@.": { | |
| 4711 | "comment": "Export stopped by an #+INCLUDE file that can't be read", | |
| 4712 | "localizations": { | |
| 4713 | "de": { | |
| 4714 | "stringUnit": { | |
| 4715 | "state": "translated", | |
| 4716 | "value": "%@ kann nicht eingebunden werden." | |
| 4717 | } | |
| 4718 | }, | |
| 4719 | "es": { | |
| 4720 | "stringUnit": { | |
| 4721 | "state": "translated", | |
| 4722 | "value": "No se puede incluir %@." | |
| 4723 | } | |
| 4724 | }, | |
| 4725 | "fr": { | |
| 4726 | "stringUnit": { | |
| 4727 | "state": "translated", | |
| 4728 | "value": "Impossible d'inclure %@." | |
| 4729 | } | |
| 4730 | }, | |
| 4731 | "ja": { | |
| 4732 | "stringUnit": { | |
| 4733 | "state": "translated", | |
| 4734 | "value": "%@ を取り込めません。" | |
| 4735 | } | |
| 4736 | }, | |
| 4737 | "pt-BR": { | |
| 4738 | "stringUnit": { | |
| 4739 | "state": "translated", | |
| 4740 | "value": "Não é possível incluir %@." | |
| 4741 | } | |
| 4742 | } | |
| 4743 | } | |
| 4744 | }, | |
| 4710 | 4745 | "Can't open the entry": { |
| 4711 | 4746 | "localizations": { |
| 4712 | 4747 | "de": { |
| @@ -5531,6 +5566,41 @@ | ||
| 5531 | 5566 | } |
| 5532 | 5567 | } |
| 5533 | 5568 | }, |
| 5569 | "Choose %@ or a folder that contains it.": { | |
| 5570 | "comment": "Open panel for giving orgstar access to a folder", | |
| 5571 | "localizations": { | |
| 5572 | "de": { | |
| 5573 | "stringUnit": { | |
| 5574 | "state": "translated", | |
| 5575 | "value": "Wähle %@ oder einen Ordner, der ihn enthält." | |
| 5576 | } | |
| 5577 | }, | |
| 5578 | "es": { | |
| 5579 | "stringUnit": { | |
| 5580 | "state": "translated", | |
| 5581 | "value": "Elige %@ o una carpeta que la contenga." | |
| 5582 | } | |
| 5583 | }, | |
| 5584 | "fr": { | |
| 5585 | "stringUnit": { | |
| 5586 | "state": "translated", | |
| 5587 | "value": "Choisissez %@ ou un dossier qui le contient." | |
| 5588 | } | |
| 5589 | }, | |
| 5590 | "ja": { | |
| 5591 | "stringUnit": { | |
| 5592 | "state": "translated", | |
| 5593 | "value": "%@ またはそれを含むフォルダを選択してください。" | |
| 5594 | } | |
| 5595 | }, | |
| 5596 | "pt-BR": { | |
| 5597 | "stringUnit": { | |
| 5598 | "state": "translated", | |
| 5599 | "value": "Escolha %@ ou uma pasta que a contenha." | |
| 5600 | } | |
| 5601 | } | |
| 5602 | } | |
| 5603 | }, | |
| 5534 | 5604 | "Choose Another Folder…": { |
| 5535 | 5605 | "localizations": { |
| 5536 | 5606 | "de": { |
| @@ -13292,6 +13362,76 @@ | ||
| 13292 | 13362 | } |
| 13293 | 13363 | } |
| 13294 | 13364 | }, |
| 13365 | "Grant Access": { | |
| 13366 | "comment": "Open panel button that gives orgstar access to a folder", | |
| 13367 | "localizations": { | |
| 13368 | "de": { | |
| 13369 | "stringUnit": { | |
| 13370 | "state": "translated", | |
| 13371 | "value": "Zugriff erlauben" | |
| 13372 | } | |
| 13373 | }, | |
| 13374 | "es": { | |
| 13375 | "stringUnit": { | |
| 13376 | "state": "translated", | |
| 13377 | "value": "Conceder acceso" | |
| 13378 | } | |
| 13379 | }, | |
| 13380 | "fr": { | |
| 13381 | "stringUnit": { | |
| 13382 | "state": "translated", | |
| 13383 | "value": "Autoriser l'accès" | |
| 13384 | } | |
| 13385 | }, | |
| 13386 | "ja": { | |
| 13387 | "stringUnit": { | |
| 13388 | "state": "translated", | |
| 13389 | "value": "アクセスを許可" | |
| 13390 | } | |
| 13391 | }, | |
| 13392 | "pt-BR": { | |
| 13393 | "stringUnit": { | |
| 13394 | "state": "translated", | |
| 13395 | "value": "Conceder Acesso" | |
| 13396 | } | |
| 13397 | } | |
| 13398 | } | |
| 13399 | }, | |
| 13400 | "Grant Access…": { | |
| 13401 | "comment": "Button that opens a panel to give orgstar access to a folder", | |
| 13402 | "localizations": { | |
| 13403 | "de": { | |
| 13404 | "stringUnit": { | |
| 13405 | "state": "translated", | |
| 13406 | "value": "Zugriff erlauben…" | |
| 13407 | } | |
| 13408 | }, | |
| 13409 | "es": { | |
| 13410 | "stringUnit": { | |
| 13411 | "state": "translated", | |
| 13412 | "value": "Conceder acceso…" | |
| 13413 | } | |
| 13414 | }, | |
| 13415 | "fr": { | |
| 13416 | "stringUnit": { | |
| 13417 | "state": "translated", | |
| 13418 | "value": "Autoriser l'accès…" | |
| 13419 | } | |
| 13420 | }, | |
| 13421 | "ja": { | |
| 13422 | "stringUnit": { | |
| 13423 | "state": "translated", | |
| 13424 | "value": "アクセスを許可…" | |
| 13425 | } | |
| 13426 | }, | |
| 13427 | "pt-BR": { | |
| 13428 | "stringUnit": { | |
| 13429 | "state": "translated", | |
| 13430 | "value": "Conceder Acesso…" | |
| 13431 | } | |
| 13432 | } | |
| 13433 | } | |
| 13434 | }, | |
| 13295 | 13435 | "Habit": { |
| 13296 | 13436 | "localizations": { |
| 13297 | 13437 | "de": { |
| @@ -15197,6 +15337,41 @@ | ||
| 15197 | 15337 | } |
| 15198 | 15338 | } |
| 15199 | 15339 | }, |
| 15340 | "It isn't in one of your folders. Grant access to it to continue.": { | |
| 15341 | "comment": "Alert for a folder outside the sidebar's folders", | |
| 15342 | "localizations": { | |
| 15343 | "de": { | |
| 15344 | "stringUnit": { | |
| 15345 | "state": "translated", | |
| 15346 | "value": "Er liegt in keinem deiner Ordner. Erlaube den Zugriff, um fortzufahren." | |
| 15347 | } | |
| 15348 | }, | |
| 15349 | "es": { | |
| 15350 | "stringUnit": { | |
| 15351 | "state": "translated", | |
| 15352 | "value": "No está en ninguna de tus carpetas. Concede acceso para continuar." | |
| 15353 | } | |
| 15354 | }, | |
| 15355 | "fr": { | |
| 15356 | "stringUnit": { | |
| 15357 | "state": "translated", | |
| 15358 | "value": "Il ne se trouve dans aucun de vos dossiers. Autorisez l'accès pour continuer." | |
| 15359 | } | |
| 15360 | }, | |
| 15361 | "ja": { | |
| 15362 | "stringUnit": { | |
| 15363 | "state": "translated", | |
| 15364 | "value": "どのフォルダにも含まれていません。続けるにはアクセスを許可してください。" | |
| 15365 | } | |
| 15366 | }, | |
| 15367 | "pt-BR": { | |
| 15368 | "stringUnit": { | |
| 15369 | "state": "translated", | |
| 15370 | "value": "Ela não está em nenhuma das suas pastas. Conceda acesso para continuar." | |
| 15371 | } | |
| 15372 | } | |
| 15373 | } | |
| 15374 | }, | |
| 15200 | 15375 | "Italic": { |
| 15201 | 15376 | "comment": "Command title", |
| 15202 | 15377 | "localizations": { |
| @@ -25107,6 +25282,41 @@ | ||
| 25107 | 25282 | } |
| 25108 | 25283 | } |
| 25109 | 25284 | }, |
| 25285 | "Save Elsewhere…": { | |
| 25286 | "comment": "Button that asks where an export goes instead", | |
| 25287 | "localizations": { | |
| 25288 | "de": { | |
| 25289 | "stringUnit": { | |
| 25290 | "state": "translated", | |
| 25291 | "value": "Woanders sichern…" | |
| 25292 | } | |
| 25293 | }, | |
| 25294 | "es": { | |
| 25295 | "stringUnit": { | |
| 25296 | "state": "translated", | |
| 25297 | "value": "Guardar en otro lugar…" | |
| 25298 | } | |
| 25299 | }, | |
| 25300 | "fr": { | |
| 25301 | "stringUnit": { | |
| 25302 | "state": "translated", | |
| 25303 | "value": "Enregistrer ailleurs…" | |
| 25304 | } | |
| 25305 | }, | |
| 25306 | "ja": { | |
| 25307 | "stringUnit": { | |
| 25308 | "state": "translated", | |
| 25309 | "value": "別の場所に保存…" | |
| 25310 | } | |
| 25311 | }, | |
| 25312 | "pt-BR": { | |
| 25313 | "stringUnit": { | |
| 25314 | "state": "translated", | |
| 25315 | "value": "Salvar em Outro Lugar…" | |
| 25316 | } | |
| 25317 | } | |
| 25318 | } | |
| 25319 | }, | |
| 25110 | 25320 | "Save Field": { |
| 25111 | 25321 | "localizations": { |
| 25112 | 25322 | "de": { |
| @@ -34611,6 +34821,41 @@ | ||
| 34611 | 34821 | } |
| 34612 | 34822 | } |
| 34613 | 34823 | }, |
| 34824 | "orgstar can't reach %@.": { | |
| 34825 | "comment": "Alert for a folder outside the sidebar's folders", | |
| 34826 | "localizations": { | |
| 34827 | "de": { | |
| 34828 | "stringUnit": { | |
| 34829 | "state": "translated", | |
| 34830 | "value": "orgstar kann nicht auf %@ zugreifen." | |
| 34831 | } | |
| 34832 | }, | |
| 34833 | "es": { | |
| 34834 | "stringUnit": { | |
| 34835 | "state": "translated", | |
| 34836 | "value": "orgstar no puede acceder a %@." | |
| 34837 | } | |
| 34838 | }, | |
| 34839 | "fr": { | |
| 34840 | "stringUnit": { | |
| 34841 | "state": "translated", | |
| 34842 | "value": "orgstar ne peut pas accéder à %@." | |
| 34843 | } | |
| 34844 | }, | |
| 34845 | "ja": { | |
| 34846 | "stringUnit": { | |
| 34847 | "state": "translated", | |
| 34848 | "value": "orgstarは%@にアクセスできません。" | |
| 34849 | } | |
| 34850 | }, | |
| 34851 | "pt-BR": { | |
| 34852 | "stringUnit": { | |
| 34853 | "state": "translated", | |
| 34854 | "value": "O orgstar não consegue acessar %@." | |
| 34855 | } | |
| 34856 | } | |
| 34857 | } | |
| 34858 | }, | |
| 34614 | 34859 | "orgstar edits a folder of plain-text org files in place. Add the folder your notes are in, or start a new one. Nothing is copied or moved, and you can add more folders later with File ▸ Add Folder….": { |
| 34615 | 34860 | "localizations": { |
| 34616 | 34861 | "de": { |