import Darwin import Foundation struct NameIndexEntry { let path: String let mode: String let oid: String } enum NameGit { static func run(_ arguments: [String], root: URL, input: Data? = nil) throws -> Data { try DevProcess.checked("git", arguments, input: input, directory: root) } static func entries(root: URL) throws -> [NameIndexEntry] { try run(["ls-files", "--stage", "-z"], root: root).split(separator: 0).map { record in guard let tab = record.firstIndex(of: 9) else { throw DevError("Invalid Git index record") } let metadata = String(decoding: record[.. [String: Data] { guard !oids.isEmpty else { return [:] } let input = Data((oids.joined(separator: "\n") + "\n").utf8) let data = try run(["cat-file", "--batch"], root: root, input: input) var position = data.startIndex var result: [String: Data] = [:] for oid in oids { guard let end = data[position...].firstIndex(of: 10) else { throw DevError("Could not read indexed blob \(oid): invalid batch header") } let header = String(decoding: data[position..= 0 else { throw DevError("Could not read indexed blob \(oid): invalid batch header") } let start = end + 1 guard size < data.endIndex - start, data[start + size] == 10 else { throw DevError("Could not read indexed blob \(oid): truncated batch data") } result[oid] = data[start.. = [] for entry in try NameGit.entries(root: root) where selected.contains(entry.path) { guard entry.mode != "160000" else { throw DevError("Submodule contents cannot be checked: \(entry.path)") } entries[entry.path] = entry.oid if known.insert(entry.oid).inserted { oids.append(entry.oid) } } if let missing = selected.subtracting(entries.keys).sorted().first { throw DevError("Required input is missing from the index: \(missing)") } let blobs = try NameGit.blobs(oids, root: root) var files: [String: Data] = [:] for (path, oid) in entries { guard let blob = blobs[oid] else { throw DevError("Could not read indexed blob \(oid)") } files[path] = blob } self.files = files } static func relative(_ path: URL, root: URL) throws -> String? { let path = NamePaths.lexical(path) let prefix = root.path + "/" if path.path.hasPrefix(prefix) { return String(path.path.dropFirst(prefix.count)) } var parent = path.deletingLastPathComponent() var suffix = path.lastPathComponent while parent.path != "/" { if let resolved = try NamePaths.existingParent(parent) { if resolved == root { return suffix } if resolved.path.hasPrefix(prefix) { return String(resolved.path.dropFirst(prefix.count)) + "/" + suffix } } suffix = parent.lastPathComponent + "/" + suffix parent.deleteLastPathComponent() } return nil } func read(_ path: URL) throws -> Data { if let relative = try Self.relative(path, root: root) { guard let data = files[relative] else { throw DevError("Required policy input is missing from the index: \(relative)") } return data } return try Data(contentsOf: path) } } enum NameArtifacts { static let magic: Set = Set( [ [0xfe, 0xed, 0xfa, 0xce], [0xce, 0xfa, 0xed, 0xfe], [0xfe, 0xed, 0xfa, 0xcf], [0xcf, 0xfa, 0xed, 0xfe], [0xca, 0xfe, 0xba, 0xbe], [0xbe, 0xba, 0xfe, 0xca], [0xca, 0xfe, 0xba, 0xbf], [0xbf, 0xba, 0xfe, 0xca], ].map { Data($0) }) static func files(root: URL) throws -> Set { var result: Set = [] let background = root.appendingPathComponent("Packaging/background.png") if FileManager.default.fileExists(atPath: background.path) { result.insert(background) } let excluded: Set = [ "checkouts", "repositories", "artifacts", "ModuleCache", "index", "plugins", "Intermediates.noindex", ] func visit(_ directory: URL) throws { for path in try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) .sorted(by: { $0.path < $1.path }) { var info = stat() guard lstat(path.path, &info) == 0 else { throw DevError("Could not inspect artifact \(path.path): \(String(cString: strerror(errno)))") } let kind = info.st_mode & S_IFMT let name = path.lastPathComponent if kind == S_IFDIR { if !excluded.contains(name) && !name.hasSuffix(".build") && !name.hasSuffix(".dSYM") { try visit(path) } } else if kind == S_IFREG { if name == "background.png" { result.insert(path); continue } guard info.st_mode & 0o111 != 0 || ["dylib", "so"].contains(path.pathExtension) else { continue } let handle = try FileHandle(forReadingFrom: path) let header = try handle.read(upToCount: 4) ?? Data() try handle.close() if magic.contains(header) { result.insert(path) } } } } for name in ["build", ".build"] { let directory = root.appendingPathComponent(name) if FileManager.default.fileExists(atPath: directory.path) { try visit(directory) } } return result } static func source(_ source: String) -> String { var parts = source.split(separator: "/").map(String.init) if parts.count > 3 && parts[0] == ".build" && parts[1].hasSuffix("-apple-macosx") && ["debug", "release"].contains(parts[2]) { parts.remove(at: 1) return parts.joined(separator: "/") } return source } }