Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import Darwinimport Foundation
@MainActorfinal class RealityConfigTransaction { private struct FileState: Equatable { let contents: Data let permissions: Int let inode: UInt64 let device: UInt64 }
private struct Backup: Codable { let file: URL let contents: Data? let permissions: Int? }
let file: URL private let storage: RealityStorage private let original: FileState? private var expected: FileState? private let receipt: URL
init(storage: RealityStorage, directory: URL) throws { try storage.validate() self.storage = storage self.file = storage.fileURL("parcel.toml") original = try Self.read(self.file) expected = original receipt = directory.appendingPathComponent("config-restoration.json") let backup = directory.appendingPathComponent("config-backup.json") try encodeJSON( Backup(file: self.file, contents: original?.contents, permissions: original?.permissions), to: backup) try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: backup.path) }
func write(_ contents: Data) throws { try requireUnchanged() try contents.write(to: file, options: .atomic) let permissions = original?.permissions ?? 0o600 try FileManager.default.setAttributes([.posixPermissions: permissions], ofItemAtPath: file.path) let written = try Self.read(file) guard written?.contents == contents, written?.permissions == permissions else { throw RealityError("Safety stop: scenario configuration write did not read back") } expected = written }
func restore() throws { try requireUnchanged() if let original { try original.contents.write(to: file, options: .atomic) try FileManager.default.setAttributes([.posixPermissions: original.permissions], ofItemAtPath: file.path) } else if expected != nil { try FileManager.default.removeItem(at: file) } let restored = try Self.read(file) guard restored?.contents == original?.contents, restored?.permissions == original?.permissions else { throw RealityError("Safety stop: original configuration restoration did not read back") } expected = restored try encodeJSON(["restored": true], to: receipt) }
private func requireUnchanged() throws { try storage.validate() guard try Self.read(file) == expected else { throw RealityError("Safety stop: configuration changed outside the scenario; private backup retained") } }
private static func read(_ file: URL) throws -> FileState? { guard file.resolvingSymlinksInPath() == file else { throw RealityError("Safety stop: configuration test refuses symbolic links") } guard let parent = try metadata(file.deletingLastPathComponent()), parent.st_mode & S_IFMT == S_IFDIR else { throw RealityError("Safety stop: configuration directory must already exist") } guard let attributes = try metadata(file) else { return nil } guard attributes.st_mode & S_IFMT == S_IFREG, attributes.st_nlink == 1, attributes.st_uid == getuid(), let device = UInt64(exactly: attributes.st_dev) else { throw RealityError("Safety stop: configuration must be a regular, unshared file owned by this user") } return FileState( contents: try Data(contentsOf: file), permissions: Int(attributes.st_mode & 0o7777), inode: attributes.st_ino, device: device) }
private static func metadata(_ file: URL) throws -> stat? { var value = stat() guard file.path.withCString({ lstat($0, &value) }) == 0 else { let code = errno if code == ENOENT { return nil } throw NSError(domain: NSPOSIXErrorDomain, code: Int(code)) } return value }}