// SPDX-License-Identifier: GPL-3.0-or-later import Foundation import Darwin /// Completed, owned build snapshots only. Working caches and development disks /// are deliberately outside this inventory. Cleanup is explicit and recoverable. final class CMOCBuildHistory: @unchecked Sendable { struct Entry: Identifiable, Sendable { let id: UUID let directory: URL let createdAt: Date let configurationName: String let action: String let byteCount: Int64 let hasBinary: Bool let hasDisk: Bool let protectionReason: String? } struct Removal: Sendable { var removedIDs: Set = [] var retained: [UUID: String] = [:] } private struct Record: Codable { let version: Int let id: UUID let project: String let createdAt: Date let configurationName: String let action: String let hasBinary: Bool let hasDisk: Bool let byteCount: Int64 let hashes: [String: String] } static let manifestName = ".vccplus-build-history.json" static let leaseName = ".vccplus-build-history.lock" private let root: URL private let lock = NSLock() private var leases: [UUID: Int32] = [:] private let trash: @Sendable (URL) throws -> Void init(root: URL, trash: @escaping @Sendable (URL) throws -> Void = { url in try FileManager.default.trashItem(at: url, resultingItemURL: nil) }) { self.root = root.standardizedFileURL.resolvingSymlinksInPath() self.trash = trash } deinit { for descriptor in leases.values { close(descriptor) } } func record(_ artifact: CMOCBuildArtifact, project: URL) throws { lock.lock(); defer { lock.unlock() } let directory = try checkedDirectory(artifact.id) guard try CMOCProject.physicalURL(artifact.directory) == directory else { throw failure("Build result is outside its owned history folder.") } let manager = FileManager.default for name in [Self.manifestName, Self.leaseName] { guard !manager.fileExists(atPath: directory.appendingPathComponent(name).path) else { throw failure("The build recipe used a reserved history filename: \(name)") } } let files = try contents(directory) let descriptor = open(directory.appendingPathComponent(Self.leaseName).path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0o600) guard descriptor >= 0 else { throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) } var retained = false defer { if !retained { close(descriptor); try? manager.removeItem(at: directory.appendingPathComponent(Self.leaseName)) } } guard flock(descriptor, LOCK_SH | LOCK_NB) == 0 else { throw failure("The new build could not be protected from cleanup.") } let record = Record(version: 1, id: artifact.id, project: canonicalProject(project), createdAt: Date(), configurationName: artifact.configurationName, action: artifact.action.rawValue, hasBinary: artifact.hasBinary, hasDisk: artifact.hasDisk, byteCount: files.values.reduce(Int64(0)) { $0 + Int64($1.count) }, hashes: files.mapValues(CMOCProject.sha256)) let temporary = directory.appendingPathComponent(".vccplus-history-" + UUID().uuidString) defer { try? manager.removeItem(at: temporary) } try JSONEncoder().encode(record).write(to: temporary, options: .withoutOverwriting) guard renamex_np(temporary.path, directory.appendingPathComponent(Self.manifestName).path, UInt32(RENAME_EXCL)) == 0 else { throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) } leases[artifact.id] = descriptor; retained = true } func entries(project: URL, protectedIDs: Set = []) throws -> [Entry] { lock.lock(); defer { lock.unlock() } guard FileManager.default.fileExists(atPath: root.path) else { return [] } let canonical = canonicalProject(project) return try FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: nil).compactMap { url in guard let id = UUID(uuidString: url.lastPathComponent), let record = try? read(id), record.project == canonical else { return nil } guard let directory = try? checkedDirectory(id) else { return nil } let reason: String? if protectedIDs.contains(id) { reason = "Protected build or assigned media" } else if let invalid = validationProblem(record, directory: directory) { reason = invalid } else if leases[id] == nil, !leaseAvailable(directory) { reason = "In use by another app session" } else { reason = nil } return Entry(id: id, directory: directory, createdAt: record.createdAt, configurationName: record.configurationName, action: record.action, byteCount: record.byteCount, hasBinary: record.hasBinary, hasDisk: record.hasDisk, protectionReason: reason) }.sorted { $0.createdAt == $1.createdAt ? $0.id.uuidString < $1.id.uuidString : $0.createdAt > $1.createdAt } } func remove(ids: Set, project: URL, protectedIDs: Set) throws -> Removal { lock.lock(); defer { lock.unlock() } var result = Removal() let canonical = canonicalProject(project) for id in ids { do { guard !protectedIDs.contains(id) else { throw failure("Protected build or assigned media") } let record = try read(id), directory = try checkedDirectory(id) guard record.project == canonical else { throw failure("This build belongs to a different project.") } let owned = leases[id] let descriptor = owned ?? open(directory.appendingPathComponent(Self.leaseName).path, O_RDWR | O_NOFOLLOW | O_CLOEXEC) guard descriptor >= 0 else { throw failure("Build ownership could not be verified.") } defer { if let owned, leases[id] != nil { _ = flock(owned, LOCK_SH) } if owned == nil { close(descriptor) } } guard validLease(descriptor, directory: directory) else { throw failure("Build ownership file changed; kept for recovery") } guard flock(descriptor, LOCK_EX | LOCK_NB) == 0 else { throw failure("In use by another app session") } if let problem = validationProblem(record, directory: directory) { throw failure(problem) } // Trash retains a recovery path even if a file was edited in the // brief interval after validation. Never remove a run disk here. try trash(directory) if let owned { leases.removeValue(forKey: id); close(owned) } result.removedIDs.insert(id) } catch { result.retained[id] = error.localizedDescription } } return result } private func canonicalProject(_ project: URL) -> String { project.standardizedFileURL.resolvingSymlinksInPath().path } private func failure(_ message: String) -> CMOCBuildError { .message(message) } private func checkedDirectory(_ id: UUID) throws -> URL { let rootValues = try root.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey]) guard rootValues.isDirectory == true, rootValues.isSymbolicLink != true else { throw failure("Build history folder ownership could not be verified.") } let canonicalRoot = try CMOCProject.physicalURL(root) let directory = canonicalRoot.appendingPathComponent(id.uuidString) let values = try directory.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey]) guard values.isDirectory == true, values.isSymbolicLink != true, try CMOCProject.physicalURL(directory).deletingLastPathComponent() == canonicalRoot else { throw failure("Build folder ownership could not be verified.") } return directory } private func read(_ id: UUID) throws -> Record { let directory = try checkedDirectory(id), url = directory.appendingPathComponent(Self.manifestName) let values = try url.resourceValues(forKeys: [.isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey]) guard values.isRegularFile == true, values.isSymbolicLink != true, let size = values.fileSize, size > 0, size <= 8 * 1024 * 1024 else { throw failure("Build history is missing or invalid.") } let record = try JSONDecoder().decode(Record.self, from: Data(contentsOf: url)) guard record.version == 1, record.id == id, record.hashes.count <= 20_000, record.byteCount >= 0, record.byteCount <= Int64(CMOCProject.maximumProjectBytes), record.configurationName.count <= 1024, CMOCBuildAction(rawValue: record.action) != nil, record.project.hasPrefix("/"), record.project.count <= 4096 else { throw failure("Build history is missing or invalid.") } for path in record.hashes.keys { try CMOCProject.validateRelativePath(path) } return record } private func leaseAvailable(_ directory: URL) -> Bool { let descriptor = open(directory.appendingPathComponent(Self.leaseName).path, O_RDWR | O_NOFOLLOW | O_CLOEXEC) guard descriptor >= 0 else { return false } defer { close(descriptor) } return validLease(descriptor, directory: directory) && flock(descriptor, LOCK_EX | LOCK_NB) == 0 } private func validLease(_ descriptor: Int32, directory: URL) -> Bool { var held = stat(), current = stat() guard fstat(descriptor, &held) == 0, lstat(directory.appendingPathComponent(Self.leaseName).path, ¤t) == 0 else { return false } return (current.st_mode & S_IFMT) == S_IFREG && current.st_size == 0 && held.st_dev == current.st_dev && held.st_ino == current.st_ino } private func validationProblem(_ record: Record, directory: URL) -> String? { do { if let descriptor = leases[record.id], !validLease(descriptor, directory: directory) { return "Build ownership file changed; kept for recovery" } let files = try contents(directory) guard files.mapValues(CMOCProject.sha256) == record.hashes else { return "Files changed since this build; kept for recovery" } return nil } catch { return "Files are missing, linked or unreadable; kept for recovery" } } /// Unlike project enumeration, this includes hidden/user-added files. Any /// changed, added or missing file makes a completed snapshot ineligible. private func contents(_ directory: URL) throws -> [String: Data] { var enumerationError: Error? guard let enumerator = FileManager.default.enumerator(at: directory, includingPropertiesForKeys: [.isDirectoryKey, .isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey], errorHandler: { _, error in enumerationError = error; return false }) else { throw failure("Build files could not be inspected.") } var files: [String: Data] = [:], total = 0 while let url = enumerator.nextObject() as? URL { let relative = String(url.path.dropFirst(directory.path.count + 1)) if relative == Self.manifestName || relative == Self.leaseName { continue } try CMOCProject.validateRelativePath(relative) let values = try url.resourceValues(forKeys: [.isDirectoryKey, .isRegularFileKey, .isSymbolicLinkKey, .fileSizeKey]) guard values.isSymbolicLink != true else { throw failure("Linked build file") } if values.isDirectory == true { continue } // Finder may add this bookkeeping when revealing build products. // It is not a user source or media change. Links still fail above. if url.lastPathComponent == ".DS_Store" { continue } guard values.isRegularFile == true, let size = values.fileSize, size <= CMOCProject.maximumFileBytes, files.count < 20_000, total + size <= CMOCProject.maximumProjectBytes else { throw failure("Build files exceed inspection limits.") } let data = try Data(contentsOf: url) guard data.count <= CMOCProject.maximumFileBytes, total + data.count <= CMOCProject.maximumProjectBytes else { throw failure("Build files changed during inspection.") } total += data.count; files[relative] = data } if let enumerationError { throw enumerationError } return files } }