| 1 | import Foundation |
| 2 | |
| 3 | /// Protected, bounded immutable requests for the foreground app. Callers |
| 4 | /// serialize access on MainActor. No credential belongs in a record. Unlike |
| 5 | /// the share journal, every read requires the exact sign-in generation. |
| 6 | public struct ActionJournal: Sendable { |
| 7 | private let directory: URL |
| 8 | private var file: URL { directory.appendingPathComponent("actions.json") } |
| 9 | private struct Entry: Codable { let id: String; let binding: String; let body: Data; let created: Date } |
| 10 | public init(directory: URL) { self.directory = directory } |
| 11 | private func entries(now: Date) throws -> [Entry] { |
| 12 | guard FileManager.default.fileExists(atPath: file.path) else { return [] } |
| 13 | let handle = try FileHandle(forReadingFrom: file); defer { try? handle.close() } |
| 14 | let raw = try handle.read(upToCount: 3 * 1024 * 1024 + 1) ?? Data() |
| 15 | guard raw.count <= 3 * 1024 * 1024 else { throw ShareFailure.unconfirmed } |
| 16 | let all = try JSONDecoder().decode([Entry].self, from: raw) |
| 17 | guard all.count <= 32, all.allSatisfy({ $0.body.count <= 64 * 1024 }) else { throw ShareFailure.unconfirmed } |
| 18 | let current = all.filter { now >= $0.created && now.timeIntervalSince($0.created) < 86400 } |
| 19 | if current.count != all.count { try persist(current) } |
| 20 | return current |
| 21 | } |
| 22 | private func persist(_ entries: [Entry]) throws { |
| 23 | if entries.isEmpty { |
| 24 | if FileManager.default.fileExists(atPath: file.path) { try FileManager.default.removeItem(at: file) } |
| 25 | return |
| 26 | } |
| 27 | try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700]) |
| 28 | var folder = directory; var values = URLResourceValues(); values.isExcludedFromBackup = true; try folder.setResourceValues(values) |
| 29 | let data = try JSONEncoder().encode(entries) |
| 30 | guard data.count <= 3 * 1024 * 1024 else { throw ShareFailure.unconfirmed } |
| 31 | #if os(iOS) |
| 32 | try data.write(to: file, options: [.atomic, .completeFileProtection]) |
| 33 | #else |
| 34 | try data.write(to: file, options: .atomic) |
| 35 | #endif |
| 36 | try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: file.path) |
| 37 | } |
| 38 | /// Restore only the active sign-in. Other generations cannot reappear |
| 39 | /// after an account change, including a change while the app was closed. |
| 40 | public func pending(binding: String, now: Date = Date()) throws -> [Data] { |
| 41 | let all = try entries(now: now) |
| 42 | let current = all.filter { $0.binding == binding } |
| 43 | if all.count != current.count { try persist(current) } |
| 44 | return current.map(\.body) |
| 45 | } |
| 46 | public func load(id: String, binding: String, now: Date = Date()) throws -> Data? { |
| 47 | try entries(now: now).first { $0.id == id && $0.binding == binding }?.body |
| 48 | } |
| 49 | public func save(id: String, binding: String, body: Data, now: Date = Date()) throws { |
| 50 | guard !id.isEmpty, id.utf8.count <= 128, !binding.isEmpty, binding.utf8.count <= 4096, body.count <= 64 * 1024 else { throw ShareFailure.unconfirmed } |
| 51 | var all = try entries(now: now) |
| 52 | if let existing = all.first(where: { $0.id == id && $0.binding == binding }) { |
| 53 | guard existing.body == body else { throw ShareFailure.unconfirmed } |
| 54 | return |
| 55 | } |
| 56 | guard all.count < 32 else { throw ShareFailure.unconfirmed } |
| 57 | all.append(Entry(id: id, binding: binding, body: body, created: now)) |
| 58 | try persist(all) |
| 59 | } |
| 60 | public func remove(id: String, binding: String, now: Date = Date()) throws { |
| 61 | try persist(entries(now: now).filter { $0.id != id || $0.binding != binding }) |
| 62 | } |
| 63 | public func clear() { try? FileManager.default.removeItem(at: file) } |
| 64 | } |
| 65 | |