Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import Darwinimport Foundation
struct RealityEditorProfile: Sendable { struct Receipt: Codable, Sendable { let url: URL let inode: UInt64 let device: Int32 let owner: UInt32 }
static let socketPathLimit = 103 static let socketSuffixBudget = 32 private static let prefix = "parcel-e-" private static let parent = URL(fileURLWithPath: "/tmp", isDirectory: true).resolvingSymlinksInPath() let receipt: Receipt var url: URL { receipt.url }
init() throws { let url = Self.parent.appendingPathComponent(Self.prefix + UUID().uuidString, isDirectory: true) try Self.validatePath(url) try FileManager.default.createDirectory( at: url, withIntermediateDirectories: false, attributes: [.posixPermissions: 0o700]) let metadata = try Self.metadata(url) receipt = Receipt(url: url, inode: metadata.st_ino, device: metadata.st_dev, owner: metadata.st_uid) try validate() }
static func validatePath(_ url: URL) throws { guard url.isFileURL, url.standardizedFileURL.path == url.path, url.deletingLastPathComponent().path == parent.path, url.lastPathComponent.hasPrefix(prefix), UUID(uuidString: String(url.lastPathComponent.dropFirst(prefix.count))) != nil, url.path.utf8.count + socketSuffixBudget < socketPathLimit else { throw RealityError("Editor profile is not an exact short owned scratch path") } }
func validate() throws { try Self.validatePath(url) let value = try Self.metadata(url) guard value.st_mode & S_IFMT == S_IFDIR, value.st_mode & 0o7777 == 0o700, value.st_uid == getuid(), value.st_uid == receipt.owner, value.st_dev == receipt.device, value.st_ino == receipt.inode, url.resolvingSymlinksInPath().path == url.path else { throw RealityError("Editor profile identity or private directory permissions changed") } }
func remove() throws { do { try validate() } catch let error as NSError where error.domain == NSPOSIXErrorDomain && error.code == Int(ENOENT) { return } try FileManager.default.removeItem(at: url) guard !FileManager.default.fileExists(atPath: url.path) else { throw RealityError("Owned editor profile survived removal") } }
private static func metadata(_ url: URL) throws -> stat { var value = stat() guard url.path.withCString({ lstat($0, &value) }) == 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } return value }}