Mac: write config.toml through a symlink !222

merged merged by cmc on 2026-10-10 21:26 UTC · krz/orgstar:config-symlink into main

2 files changed, +29 −4

Layout: unified · split

Sources/OrgApp/ConfigSync.swift +10 −4
@@ -29,6 +29,9 @@ public final class ConfigSync {
29 @ObservationIgnored private var observer: NSObjectProtocol? 29 @ObservationIgnored private var observer: NSObjectProtocol?
30 @ObservationIgnored private var serial = 0 30 @ObservationIgnored private var serial = 0
31 31
32 /// Where `url` leads: a symlinked config.toml is written through, not replaced by a file.
33 private var target: URL { url.resolvingSymlinksInPath() }
34
32 public init(url: URL = UserConfig.configFile, defaults: UserDefaults = .standard) { 35 public init(url: URL = UserConfig.configFile, defaults: UserDefaults = .standard) {
33 self.url = url 36 self.url = url
34 self.defaults = defaults 37 self.defaults = defaults
@@ -49,13 +52,13 @@ public final class ConfigSync {
49 let migrated = UserConfig.usesLegacyNames(text) ? UserConfig.migratingLegacyNames(text) : text 52 let migrated = UserConfig.usesLegacyNames(text) ? UserConfig.migratingLegacyNames(text) : text
50 let completed = UserConfig.addingMissingSections(migrated, from: defaults) 53 let completed = UserConfig.addingMissingSections(migrated, from: defaults)
51 if completed != text { 54 if completed != text {
52 try? completed.write(to: url, atomically: true, encoding: .utf8) 55 try? completed.write(to: target, atomically: true, encoding: .utf8)
53 self.text = completed 56 self.text = completed
54 } 57 }
55 } 58 }
56 } else { 59 } else {
57 let template = UserConfig.template(from: defaults) 60 let template = UserConfig.template(from: defaults)
58 try? template.write(to: url, atomically: true, encoding: .utf8) 61 try? template.write(to: target, atomically: true, encoding: .utf8)
59 text = template 62 text = template
60 } 63 }
61 observer = NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: defaults, queue: .main) { [weak self] _ in 64 observer = NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: defaults, queue: .main) { [weak self] _ in
@@ -63,10 +66,13 @@ public final class ConfigSync {
63 } 66 }
64 guard watch else { return } 67 guard watch else { return }
65 let directory = url.deletingLastPathComponent().resolvingSymlinksInPath() 68 let directory = url.deletingLastPathComponent().resolvingSymlinksInPath()
69 // The linked file's folder too, when config.toml is a symlink.
70 let linked = target.deletingLastPathComponent().resolvingSymlinksInPath()
71 let watchesLinked = linked.path != directory.path
66 // Files still read from where earlier versions kept them reload when edited there too. 72 // Files still read from where earlier versions kept them reload when edited there too.
67 let legacy = UserConfig.legacyDirectory.resolvingSymlinksInPath() 73 let legacy = UserConfig.legacyDirectory.resolvingSymlinksInPath()
68 let watchesLegacy = legacy.path != directory.path && FileManager.default.fileExists(atPath: legacy.path) 74 let watchesLegacy = legacy.path != directory.path && FileManager.default.fileExists(atPath: legacy.path)
69 watcher = FSEventsWatcher(roots: [directory] + (watchesLegacy ? [legacy] : [])) { [weak self] events in 75 watcher = FSEventsWatcher(roots: [directory] + (watchesLinked ? [linked] : []) + (watchesLegacy ? [legacy] : [])) { [weak self] events in
70 let changed = Set(events.compactMap { event -> URL? in 76 let changed = Set(events.compactMap { event -> URL? in
71 if case .changed(let url) = event { return url } 77 if case .changed(let url) = event { return url }
72 return nil 78 return nil
@@ -130,7 +136,7 @@ public final class ConfigSync {
130 } 136 }
131 guard current != original else { return } 137 guard current != original else { return }
132 text = current 138 text = current
133 try? current.write(to: url, atomically: true, encoding: .utf8) 139 try? current.write(to: target, atomically: true, encoding: .utf8)
134 } 140 }
135} 141}
136#endif 142#endif
Tests/OrgAppTests/UserConfigTests.swift +19
@@ -119,6 +119,25 @@ struct UserConfigTests {
119 #expect(store.integer(forKey: "agendaStartOffset") == 0) 119 #expect(store.integer(forKey: "agendaStartOffset") == 0)
120 #expect(sync.problems.isEmpty) 120 #expect(sync.problems.isEmpty)
121 } 121 }
122
123 /// A config.toml linked to another folder's (as the iOS app's) stays a link when the
124 /// Settings window writes it.
125 @Test func writesThroughASymlink() throws {
126 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-config-\(UUID().uuidString)")
127 defer { try? FileManager.default.removeItem(at: folder) }
128 let shared = folder.appendingPathComponent("notes/config.toml")
129 let link = folder.appendingPathComponent("orgstar/config.toml")
130 try FileManager.default.createDirectory(at: shared.deletingLastPathComponent(), withIntermediateDirectories: true)
131 try FileManager.default.createDirectory(at: link.deletingLastPathComponent(), withIntermediateDirectories: true)
132 try "fill-column = 80\n".write(to: shared, atomically: true, encoding: .utf8)
133 try FileManager.default.createSymbolicLink(at: link, withDestinationURL: shared)
134 let store = defaults()
135 let sync = ConfigSync(url: link, defaults: store)
136 sync.start(watch: false)
137 store.set(66, forKey: "fillColumn")
138 #expect(try FileManager.default.destinationOfSymbolicLink(atPath: link.path) == shared.path)
139 #expect(try String(contentsOf: shared, encoding: .utf8).contains("fill-column = 66"))
140 }
122} 141}
123 142
124@MainActor 143@MainActor