import Foundation /// Protected, bounded immutable requests for the foreground app. Callers /// serialize access on MainActor. No credential belongs in a record. Unlike /// the share journal, every read requires the exact sign-in generation. public struct ActionJournal: Sendable { private let directory: URL private var file: URL { directory.appendingPathComponent("actions.json") } private struct Entry: Codable { let id: String; let binding: String; let body: Data; let created: Date } public init(directory: URL) { self.directory = directory } private func entries(now: Date) throws -> [Entry] { guard FileManager.default.fileExists(atPath: file.path) else { return [] } let handle = try FileHandle(forReadingFrom: file); defer { try? handle.close() } let raw = try handle.read(upToCount: 3 * 1024 * 1024 + 1) ?? Data() guard raw.count <= 3 * 1024 * 1024 else { throw ShareFailure.unconfirmed } let all = try JSONDecoder().decode([Entry].self, from: raw) guard all.count <= 32, all.allSatisfy({ $0.body.count <= 64 * 1024 }) else { throw ShareFailure.unconfirmed } let current = all.filter { now >= $0.created && now.timeIntervalSince($0.created) < 86400 } if current.count != all.count { try persist(current) } return current } private func persist(_ entries: [Entry]) throws { if entries.isEmpty { if FileManager.default.fileExists(atPath: file.path) { try FileManager.default.removeItem(at: file) } return } try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700]) var folder = directory; var values = URLResourceValues(); values.isExcludedFromBackup = true; try folder.setResourceValues(values) let data = try JSONEncoder().encode(entries) guard data.count <= 3 * 1024 * 1024 else { throw ShareFailure.unconfirmed } #if os(iOS) try data.write(to: file, options: [.atomic, .completeFileProtection]) #else try data.write(to: file, options: .atomic) #endif try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: file.path) } /// Restore only the active sign-in. Other generations cannot reappear /// after an account change, including a change while the app was closed. public func pending(binding: String, now: Date = Date()) throws -> [Data] { let all = try entries(now: now) let current = all.filter { $0.binding == binding } if all.count != current.count { try persist(current) } return current.map(\.body) } public func load(id: String, binding: String, now: Date = Date()) throws -> Data? { try entries(now: now).first { $0.id == id && $0.binding == binding }?.body } public func save(id: String, binding: String, body: Data, now: Date = Date()) throws { guard !id.isEmpty, id.utf8.count <= 128, !binding.isEmpty, binding.utf8.count <= 4096, body.count <= 64 * 1024 else { throw ShareFailure.unconfirmed } var all = try entries(now: now) if let existing = all.first(where: { $0.id == id && $0.binding == binding }) { guard existing.body == body else { throw ShareFailure.unconfirmed } return } guard all.count < 32 else { throw ShareFailure.unconfirmed } all.append(Entry(id: id, binding: binding, body: body, created: now)) try persist(all) } public func remove(id: String, binding: String, now: Date = Date()) throws { try persist(entries(now: now).filter { $0.id != id || $0.binding != binding }) } public func clear() { try? FileManager.default.removeItem(at: file) } }