Commit df08e11a9f

df08e11a9fb1f9717c01a86714c7756190943f15

parent: 8fa7e99e67

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-10 21:26 UTC

Mac: write config.toml through a symlink

An atomic write replaced a symlinked config.toml with a file. Writes go to the link's target, and the target's folder is watched too.

Layout: unified · split

Sources/OrgApp/ConfigSync.swift +10 −4
@@ -29,6 +29,9 @@ public final class ConfigSync {
2929 @ObservationIgnored private var observer: NSObjectProtocol?
3030 @ObservationIgnored private var serial = 0
3131
32 /// Where `url` leads: a symlinked config.toml is written through, not replaced by a file.
33 private var target: URL { url.resolvingSymlinksInPath() }
34
3235 public init(url: URL = UserConfig.configFile, defaults: UserDefaults = .standard) {
3336 self.url = url
3437 self.defaults = defaults
@@ -49,13 +52,13 @@ public final class ConfigSync {
4952 let migrated = UserConfig.usesLegacyNames(text) ? UserConfig.migratingLegacyNames(text) : text
5053 let completed = UserConfig.addingMissingSections(migrated, from: defaults)
5154 if completed != text {
52 try? completed.write(to: url, atomically: true, encoding: .utf8)
55 try? completed.write(to: target, atomically: true, encoding: .utf8)
5356 self.text = completed
5457 }
5558 }
5659 } else {
5760 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)
5962 text = template
6063 }
6164 observer = NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: defaults, queue: .main) { [weak self] _ in
@@ -63,10 +66,13 @@ public final class ConfigSync {
6366 }
6467 guard watch else { return }
6568 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
6672 // Files still read from where earlier versions kept them reload when edited there too.
6773 let legacy = UserConfig.legacyDirectory.resolvingSymlinksInPath()
6874 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
7076 let changed = Set(events.compactMap { event -> URL? in
7177 if case .changed(let url) = event { return url }
7278 return nil
@@ -130,7 +136,7 @@ public final class ConfigSync {
130136 }
131137 guard current != original else { return }
132138 text = current
133 try? current.write(to: url, atomically: true, encoding: .utf8)
139 try? current.write(to: target, atomically: true, encoding: .utf8)
134140 }
135141}
136142#endif
Tests/OrgAppTests/UserConfigTests.swift +19
@@ -119,6 +119,25 @@ struct UserConfigTests {
119119 #expect(store.integer(forKey: "agendaStartOffset") == 0)
120120 #expect(sync.problems.isEmpty)
121121 }
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 }
122141}
123142
124143@MainActor