241 lines
16 KiB
Swift
241 lines
16 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import Foundation
|
|
|
|
/// One bounded, content-based description of project inputs. Excluded directory
|
|
/// rows explain an entire subtree without reading or hashing its generated data.
|
|
struct CMOCProjectInputManifest: Equatable, Sendable {
|
|
struct Entry: Codable, Equatable, Sendable {
|
|
let path: String
|
|
let role: String
|
|
let reason: String
|
|
let included: Bool
|
|
let affectsRevision: Bool
|
|
let affectsBuild: Bool
|
|
let sha256: String?
|
|
let executable: Bool
|
|
var permissions: Int? = nil
|
|
var publicFields: [String: Any] {
|
|
["path": path, "role": role, "reason": reason, "included": included,
|
|
"affects_revision": affectsRevision, "affects_build": affectsBuild,
|
|
"sha256": sha256 as Any? ?? NSNull(), "executable": executable,
|
|
"permissions": permissions as Any? ?? NSNull()]
|
|
}
|
|
var control: Bool { ["recipe", "project_settings", "generator", "support", "pending_support_root", "executable"].contains(role) }
|
|
}
|
|
struct Change: Equatable, Sendable {
|
|
let path: String
|
|
let previous: Entry?
|
|
let current: Entry?
|
|
var requiresReview: Bool {
|
|
guard let previous, let current else { return true }
|
|
return previous.included != current.included || previous.role != current.role ||
|
|
previous.executable != current.executable || previous.control || current.control
|
|
}
|
|
var publicFields: [String: Any] {
|
|
["path": path, "previous": previous?.publicFields as Any? ?? NSNull(),
|
|
"current": current?.publicFields as Any? ?? NSNull(), "requires_review": requiresReview]
|
|
}
|
|
}
|
|
let configuration: CMOCProjectConfiguration
|
|
let files: [String: Data]
|
|
let permissions: [String: Int]
|
|
let modificationDates: [String: Date]
|
|
let overlayPaths: Set<String>
|
|
let support: CMOCBuildSupportSnapshot
|
|
let entries: [Entry]
|
|
var sharedPaths: [String] { files.keys.filter(CMOCProject.isEditable).sorted() }
|
|
var identity: String {
|
|
let encoder = JSONEncoder(); encoder.outputFormatting = [.sortedKeys]
|
|
let rows = entries.filter(\.included)
|
|
// Configuration is a value already validated at capture. Encoding these
|
|
// Codable values cannot depend on disk timestamps or dictionary order.
|
|
let data = (try? encoder.encode(rows)) ?? Data()
|
|
let config = (try? encoder.encode(configuration)) ?? Data()
|
|
return CMOCProject.sha256(data + config)
|
|
}
|
|
static func == (lhs: Self, rhs: Self) -> Bool { lhs.identity == rhs.identity }
|
|
/// Restricted assistant recipes use only reviewed C/header inputs (or the
|
|
/// fixed Hello Disk set), not the user's arbitrary Make resources/helpers.
|
|
func restricting(to paths: Set<String>) -> Self {
|
|
Self(configuration: configuration, files: files.filter { paths.contains($0.key) },
|
|
permissions: permissions.filter { paths.contains($0.key) },
|
|
modificationDates: modificationDates.filter { paths.contains($0.key) },
|
|
overlayPaths: overlayPaths.intersection(paths), support: .init(files: [:], permissions: [:]),
|
|
entries: entries.map { entry in
|
|
guard entry.included, !paths.contains(entry.path) else { return entry }
|
|
return Entry(path: entry.path, role: "excluded_scope", reason: "Not used by the approved restricted recipe.",
|
|
included: false, affectsRevision: false, affectsBuild: false, sha256: nil, executable: false)
|
|
})
|
|
}
|
|
func changes(from old: Self) -> [Change] {
|
|
let before = Dictionary(uniqueKeysWithValues: old.entries.map { ($0.path, $0) })
|
|
let after = Dictionary(uniqueKeysWithValues: entries.map { ($0.path, $0) })
|
|
return Set(before.keys).union(after.keys).sorted().compactMap { path in
|
|
let lhs = before[path], rhs = after[path]
|
|
guard lhs?.included == true || rhs?.included == true, lhs != rhs else { return nil }
|
|
return Change(path: path, previous: lhs, current: rhs)
|
|
}
|
|
}
|
|
|
|
static func capture(project: CMOCProject, overlays: [String: String] = [:],
|
|
approvedSupportDirectories: [String]? = nil,
|
|
readConfigurationFile: Bool = true, retainHostExecutables: Bool = false) throws -> Self {
|
|
let fm = FileManager.default
|
|
let root = try CMOCProject.physicalURL(project.directory)
|
|
var configuration = project.configuration
|
|
let settingsURL = try project.fileURL(relativePath: CMOCProject.configurationFilename)
|
|
if readConfigurationFile, let text = overlays[CMOCProject.configurationFilename] {
|
|
configuration = try JSONDecoder().decode(CMOCProjectConfiguration.self, from: Data(text.utf8))
|
|
} else if readConfigurationFile, fm.fileExists(atPath: settingsURL.path) {
|
|
let attributes = try fm.attributesOfItem(atPath: settingsURL.path)
|
|
guard ((attributes[.size] as? NSNumber)?.intValue ?? Int.max) <= CMOCProject.maximumFileBytes else {
|
|
throw CMOCBuildError.message("Project settings exceed the input limit.")
|
|
}
|
|
configuration = try JSONDecoder().decode(CMOCProjectConfiguration.self, from: Data(contentsOf: settingsURL))
|
|
}
|
|
configuration = try configuration.validated()
|
|
let effective = CMOCProject(directory: project.directory, configuration: configuration)
|
|
let recipe = configuration.makefileSettings
|
|
let generatedOutputs = Set(recipe?.generatedResources.flatMap(\.outputs) ?? [])
|
|
var products = generatedOutputs.union(configuration.playtestDisks)
|
|
for path in [configuration.binary, configuration.diskImage, configuration.debugManifest].compactMap({ $0 }) {
|
|
products.insert(path)
|
|
}
|
|
if let binary = configuration.binary {
|
|
let stem = (binary as NSString).deletingPathExtension
|
|
for ext in ["map", "link"] { products.insert(stem + "." + ext) }
|
|
}
|
|
let explicitSources = Set((recipe?.sources ?? []) + [configuration.entrySource])
|
|
let explicitResources = Set(configuration.resourceInputs + (recipe?.generatedResources.flatMap(\.inputs) ?? []) + (recipe?.diskFiles ?? []))
|
|
.subtracting(products)
|
|
let explicitGenerated = Set(configuration.generatedInputs)
|
|
let explicit = explicitSources.union(explicitResources).union(explicitGenerated)
|
|
.union([configuration.makefile, CMOCProject.configurationFilename])
|
|
let includeDirectories = recipe?.includePaths ?? []
|
|
let hard = Set([".git", ".svn", ".hg", ".ds_store"])
|
|
let incidental = Set(["captures", "screenshots", "evidence", "receipts", "logs", "test-results", "tmp", "temp", ".cache", ".build", ".vccplus-build"])
|
|
func within(_ path: String, _ directory: String) -> Bool { path == directory || path.hasPrefix(directory + "/") }
|
|
func explicitlyNeeded(_ path: String, directory: Bool) -> Bool {
|
|
explicit.contains(path) || (directory && explicit.contains(where: { $0.hasPrefix(path + "/") })) ||
|
|
includeDirectories.contains(where: { within(path, $0) || (directory && within($0, path)) })
|
|
}
|
|
var enumerationError: Error?
|
|
guard let enumerator = fm.enumerator(at: root,
|
|
includingPropertiesForKeys: [.isRegularFileKey, .isDirectoryKey, .isSymbolicLinkKey, .fileSizeKey],
|
|
options: [], errorHandler: { _, error in enumerationError = error; return false }) else {
|
|
throw CMOCBuildError.message("The project folder could not be read.")
|
|
}
|
|
var candidates: [String: URL] = [:], excluded: [Entry] = [], names = Set<String>()
|
|
func exclusion(_ path: String, _ role: String, _ reason: String) {
|
|
excluded.append(Entry(path: path, role: role, reason: reason, included: false,
|
|
affectsRevision: false, affectsBuild: false, sha256: nil, executable: false))
|
|
}
|
|
while let url = enumerator.nextObject() as? URL {
|
|
let path = String(url.path.dropFirst(root.path.count + 1))
|
|
try CMOCProject.validateRelativePath(path)
|
|
guard names.insert(path.lowercased()).inserted, names.count <= 20_000 else {
|
|
throw CMOCBuildError.message("Project input names are ambiguous or exceed the 20,000-entry limit.")
|
|
}
|
|
let value = try url.resourceValues(forKeys: [.isRegularFileKey, .isDirectoryKey, .isSymbolicLinkKey])
|
|
if hard.contains(url.lastPathComponent.lowercased()) {
|
|
enumerator.skipDescendants(); exclusion(path, "excluded", "Version-control or platform metadata; subtree excluded."); continue
|
|
}
|
|
guard value.isSymbolicLink != true else {
|
|
throw CMOCBuildError.message("Project inputs cannot traverse symbolic links: \(path)")
|
|
}
|
|
let directory = value.isDirectory == true
|
|
let soft = path.split(separator: "/").contains(where: { incidental.contains($0.lowercased()) }) ||
|
|
configuration.outputDirectories.contains(where: { within(path, $0) })
|
|
if soft && !explicitlyNeeded(path, directory: directory) {
|
|
enumerator.skipDescendants(); exclusion(path, "excluded_output", "Incidental or declared output directory; subtree excluded."); continue
|
|
}
|
|
if directory { continue }
|
|
guard value.isRegularFile == true else { throw CMOCBuildError.message("Unsupported project input: \(path)") }
|
|
candidates[path] = url
|
|
}
|
|
if let enumerationError { throw enumerationError }
|
|
for path in explicitResources.union(explicitGenerated) {
|
|
guard candidates[path] != nil else { throw CMOCBuildError.message("Missing declared project input: \(path)") }
|
|
}
|
|
var files: [String: Data] = [:], permissions: [String: Int] = [:], dates: [String: Date] = [:]
|
|
var entries = excluded, total = 0
|
|
let keys = Set(candidates.keys.map { $0.lowercased() })
|
|
for path in candidates.keys.sorted() {
|
|
let key = path.lowercased(), ext = (key as NSString).pathExtension
|
|
let stem = (key as NSString).deletingPathExtension
|
|
let declared = explicit.contains(path) || includeDirectories.contains(where: { within(path, $0) })
|
|
if products.contains(path) {
|
|
exclusion(path, "generated_output", "Declared build output; captured in the resulting artifact, not the input revision."); continue
|
|
}
|
|
if !declared && (["o", "d", "lst", "map", "link", "log"].contains(ext) ||
|
|
(ext == "s" && (keys.contains(stem + ".c") || keys.contains(stem + ".o") || keys.contains(stem + ".lst"))) ||
|
|
(ext == "bin" && (keys.contains(stem + ".c") || keys.contains(stem + ".asm"))) ||
|
|
key.hasSuffix("-receipt.json") || key.hasSuffix(".receipt.json") ||
|
|
(recipe != nil && key.hasPrefix("obj/vcc-dos-boot/"))) {
|
|
exclusion(path, "compiler_output", "Conventional compiler product, log or receipt; declare it explicitly if it is an input."); continue
|
|
}
|
|
let url = candidates[path]!
|
|
let attributes = try fm.attributesOfItem(atPath: url.path)
|
|
let mode = (attributes[.posixPermissions] as? NSNumber)?.intValue ?? 0o644
|
|
let originalSize = (attributes[.size] as? NSNumber)?.intValue ?? Int.max
|
|
guard originalSize <= CMOCProject.maximumFileBytes else { throw CMOCBuildError.message("Project input exceeds the 64 MB file limit: \(path)") }
|
|
let data = try overlays[path].map(CMOCProject.sourceBytes) ?? Data(contentsOf: url)
|
|
// A locally compiled host executable is an output, unless explicitly
|
|
// retained as a reviewed tool/resource. CoCo binaries are unaffected.
|
|
let magic = Array(data.prefix(4))
|
|
let hostExecutable = [[0x7f,0x45,0x4c,0x46], [0xcf,0xfa,0xed,0xfe], [0xce,0xfa,0xed,0xfe],
|
|
[0xfe,0xed,0xfa,0xcf], [0xfe,0xed,0xfa,0xce], [0xca,0xfe,0xba,0xbe]].contains(magic.map(Int.init))
|
|
if hostExecutable && !declared && !retainHostExecutables {
|
|
exclusion(path, "host_build_output", "Compiled host executable; declare it explicitly to retain it as a build input."); continue
|
|
}
|
|
total += data.count
|
|
guard data.count <= CMOCProject.maximumFileBytes, total <= CMOCProject.maximumProjectBytes else {
|
|
throw CMOCBuildError.message("Project inputs exceed the 512 MB snapshot limit.")
|
|
}
|
|
files[path] = data; permissions[path] = mode & 0o777
|
|
dates[path] = attributes[.modificationDate] as? Date
|
|
let role: String
|
|
if path == CMOCProject.configurationFilename { role = "project_settings" }
|
|
else if path == configuration.makefile || ["makefile", "gnumakefile"].contains((key as NSString).lastPathComponent) || ext == "mk" { role = "recipe" }
|
|
else if ["pl", "py", "sh", "rb"].contains(ext) { role = "generator" }
|
|
else if mode & 0o111 != 0 { role = "executable" }
|
|
else if explicitGenerated.contains(path) { role = "generated_input" }
|
|
else if ["h", "inc"].contains(ext) { role = "header" }
|
|
else if CMOCProject.isSource(path) { role = "source" }
|
|
else { role = "resource" }
|
|
entries.append(Entry(path: path, role: role,
|
|
reason: declared ? "Explicitly declared source, include, resource or generated input." : "Project input inside the approved root.",
|
|
included: true, affectsRevision: true, affectsBuild: true, sha256: CMOCProject.sha256(data), executable: mode & 0o111 != 0,
|
|
permissions: ["recipe", "project_settings", "generator", "executable"].contains(role) ? mode & 0o777 : nil))
|
|
}
|
|
// Exclusions collected during file classification were appended after
|
|
// the traversal rows. Include them without duplicating existing rows.
|
|
let seen = Set(entries.map(\.path))
|
|
entries += excluded.filter { !seen.contains($0.path) }
|
|
var supportProject = effective
|
|
if let approvedSupportDirectories {
|
|
let approved = Set(approvedSupportDirectories)
|
|
supportProject.configuration.buildSupportDirectories = configuration.buildSupportDirectories.filter(approved.contains)
|
|
for path in configuration.buildSupportDirectories where !approved.contains(path) {
|
|
// New sibling roots require native approval BEFORE enumeration,
|
|
// hashing or copying. Only their already-declared names are public.
|
|
entries.append(Entry(path: path, role: "pending_support_root",
|
|
reason: "New build-support root; contents remain unread until native approval authorizes the whole root.",
|
|
included: true, affectsRevision: true, affectsBuild: true, sha256: nil, executable: false))
|
|
}
|
|
}
|
|
let support = try supportProject.supportSnapshot()
|
|
guard files.count + support.files.count <= 20_000,
|
|
total + support.files.values.reduce(0, { $0 + $1.count }) <= CMOCProject.maximumProjectBytes else {
|
|
throw CMOCBuildError.message("Project and approved support inputs exceed snapshot limits.")
|
|
}
|
|
for (path, data) in support.files {
|
|
entries.append(Entry(path: path, role: "support", reason: "Explicitly approved build-support root.", included: true,
|
|
affectsRevision: true, affectsBuild: true, sha256: CMOCProject.sha256(data), executable: (support.permissions[path] ?? 0) & 0o111 != 0,
|
|
permissions: (support.permissions[path] ?? 0) & 0o777))
|
|
}
|
|
return Self(configuration: configuration, files: files, permissions: permissions, modificationDates: dates,
|
|
overlayPaths: Set(overlays.keys).intersection(files.keys), support: support, entries: entries.sorted { $0.path < $1.path })
|
|
}
|
|
}
|