diff --git a/Sources/PureMouseAppSupport/ConfigStore.swift b/Sources/PureMouseAppSupport/ConfigStore.swift index 969a8f6..cd1be41 100644 --- a/Sources/PureMouseAppSupport/ConfigStore.swift +++ b/Sources/PureMouseAppSupport/ConfigStore.swift @@ -117,7 +117,8 @@ public final class ConfigStore { undo?.clear() return } - guard data != diskBytes else { return } + // Identical bytes are a no-op, unless an error is showing: the file may have recovered. + guard data != diskBytes || configProblem != nil else { return } // Someone else changed the file: what the steps would put back no longer fits it. undo?.clear() // Edited by hand while a change of ours waits: save ours on top of the new file now. @@ -277,15 +278,26 @@ public final class ConfigStore { /// watch the directory; some editors write in place, which leaves the directory untouched, so /// watch the file too, following each new one. `reloadConfig` compares bytes, so a save seen /// by both costs one read. + /// + /// While the directory does not exist, the nearest existing ancestor is watched instead; each + /// event there rearms the watches, so they move down as the directories appear and settle on + /// the real directory (event-driven, no polling). public func startWatching() { directoryWatch?.cancel() directoryWatch = nil let directory = locations.config.resolvingSymlinksInPath().deletingLastPathComponent() - directoryWatch = watch(directory.path, [.write, .delete, .rename]) { store, events in - if !events.isDisjoint(with: [.delete, .rename]) { store.startWatching() } + var watched = directory + while watched.path != "/", !FileManager.default.fileExists(atPath: watched.path) { + watched = watched.deletingLastPathComponent() + } + let waiting = watched.path != directory.path + directoryWatch = watch(watched.path, [.write, .delete, .rename]) { store, events in + if waiting || !events.isDisjoint(with: [.delete, .rename]) { store.startWatching() } store.configChanged() } watchConfigFile() + // The file may have appeared between the last event and the new watch. + if !waiting, directoryWatch != nil, fileWatch != nil { reloadConfig() } } private func watchConfigFile() { diff --git a/Tests/PureMouseAppSupportTests/ConfigStoreTests.swift b/Tests/PureMouseAppSupportTests/ConfigStoreTests.swift index 38d7ac0..c1502b7 100644 --- a/Tests/PureMouseAppSupportTests/ConfigStoreTests.swift +++ b/Tests/PureMouseAppSupportTests/ConfigStoreTests.swift @@ -153,3 +153,38 @@ private func loadedStore(_ sandbox: Sandbox, clock: Clock? = nil) -> ConfigStore #expect(flushes == 1) #expect(!sandbox.exists(sandbox.config)) } + +@MainActor @Test func aReadFailureClearsOnceTheIdenticalFileIsBack() throws { + let sandbox = try Sandbox() + let text = #"{"dpi": 800}"# + try sandbox.write(text) + let store = loadedStore(sandbox) + #expect(!store.readOnly) + try FileManager.default.removeItem(at: sandbox.config) + try FileManager.default.createDirectory(at: sandbox.config, withIntermediateDirectories: false) + store.reloadConfig() + #expect(store.readOnly) + #expect(store.configProblem != nil) + try FileManager.default.removeItem(at: sandbox.config) + try sandbox.write(text) + store.reloadConfig() + #expect(!store.readOnly) + #expect(store.configProblem == nil) +} + +@MainActor @Test func watchingAMissingDirectoryFollowsItsCreation() async throws { + let sandbox = try Sandbox() + let missing = sandbox.directory.appending(path: "a").appending(path: "b") + let config = missing.appending(path: "config.json") + let locations = Locations(config: config, log: sandbox.locations.log, daemon: nil, + companion: sandbox.locations.companion) + let store = ConfigStore(locations: locations, clock: ManualClock().clock) + store.reloadConfig() + store.startWatching() + try FileManager.default.createDirectory(at: missing, withIntermediateDirectories: true) + try sandbox.write(#"{"dpi":800}"#, to: config) + for _ in 0..<100 where store.config.int(["dpi"]) != 800 { + try await Task.sleep(for: .milliseconds(50)) + } + #expect(store.config.int(["dpi"]) == 800) +} diff --git a/docs/companion.md b/docs/companion.md index 959c63a..e8c0313 100644 --- a/docs/companion.md +++ b/docs/companion.md @@ -135,6 +135,11 @@ so the file's own descriptor goes stale). It reloads when the file's bytes chang about 150 ms, exactly as for SIGHUP. Hand edits therefore apply on save. A failed load keeps the previous config and reports `config.error`. +The menu app and Settings (`ConfigStore`) watch the same directory, plus the file itself for +in-place editors. While the directory does not exist yet, they watch its nearest existing ancestor +and move the watch down as directories appear, so a config created later is still seen (events +only, no polling). After a read error, the same bytes back on disk clear the error. + Slider writes from the menu are debounced to about 300 ms. ## Versioning