Repository Public License
1// ORIX Convert — Packages/ConvertKit/Sources/ConvertCLI/DoctorCommand.swift2// Description: `orix-convert doctor`: engines and their availability, bundled tools with their versions and3// locations, linked libraries, and a check that nothing resolves outside the bundle or the repository.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import MachO7import ConvertCore8import ConvertKit910enum Repository {11 /// Root of the source repository (the folder holding `EmbeddedEngines` and `Packages`), found by walking up from12 /// the running executable, then from this source file. `nil` in an installed app.13 static func root() -> URL? {14 var starts: [URL] = []15 if let executable = ToolEnvironment.executableURL() { starts.append(executable.deletingLastPathComponent()) }16 starts.append(URL(fileURLWithPath: #filePath).deletingLastPathComponent())17 for start in starts {18 var directory = start19 for _ in 0..<12 {20 let fm = FileManager.default21 if fm.fileExists(atPath: directory.appendingPathComponent("EmbeddedEngines").path),22 fm.fileExists(atPath: directory.appendingPathComponent("Packages/ORIXConvertCore").path) { return directory }23 directory = directory.deletingLastPathComponent()24 }25 }26 return nil27 }28}2930enum DoctorCommand: Command {31 static let spec = CommandSpec("doctor", summary: "Engines, bundled tools and their versions; checks nothing comes from outside.",32 usage: "doctor [--json]", flags: ["json"])3334 struct Engine: Codable { let id: String, name: String, package: String; let available: Bool; let reason: String?; let paths: Int; let heavy: Bool }35 struct Directory: Codable { let path: String; let origin: String; let exists: Bool }36 struct Tool: Codable {37 let name: String, relativePath: String38 let path: String?, resolvedPath: String?39 let origin: String?40 let version: String?41 let sizeBytes: Int64?42 let usedBy: String43 }44 struct Library: Codable { let name: String; let version: String?; let linkage: String; let usedBy: String }45 struct Image: Codable { let path: String; let inside: Bool }46 struct Output: Codable {47 let version: String48 let executable: String49 let insideAppBundle: Bool50 let toolsEnvironment: String?51 let wiredPackages: [String], notWiredPackages: [String]52 let engines: [Engine]53 let searchDirectories: [Directory]54 let tools: [Tool]55 let libraries: [Library]56 let loadedImagesOutsideSystem: [Image]57 let registry: RegistryCounts58 let diagnostics: [String]59 let problems: [String]60 }6162 /// Bundled helper executables the engines look for.63 static let knownTools: [(name: String, relativePath: String, usedBy: String)] = [64 ("ffmpeg", "ffmpeg", "video, audio"),65 ("ffprobe", "ffprobe", "video, audio"),66 ("pandoc", "pandoc", "documents, ebooks"),67 ("LibreOffice", "LibreOffice.app/Contents/MacOS/soffice", "documents, presentations, data (legacy spreadsheets)"),68 ("orix-model3d-helper", "orix-model3d-helper", "model3d (isolated import)"),69 ("orix-hdf5-probe", "orix-hdf5-probe", "scientific (isolated HDF5 probe)"),70 ].filter { tool in71 // Helpers of engine packages that are not in this build are not expected.72 (tool.name != "orix-model3d-helper" || ORIXEngines.wiredPackages.contains("EngineModel3D"))73 && (tool.name != "orix-hdf5-probe" || ORIXEngines.wiredPackages.contains("EngineScientific"))74 }7576 /// Folders a tool must never come from.77 static let foreignPrefixes = ["/opt/homebrew", "/usr/local", "/opt/local", "/usr/bin", "/bin", "/usr/sbin", "/sbin", "/Library/Frameworks", "/nix"]7879 static func collect() async -> Output {80 let registry = Shared.registry()81 var problems: [String] = []82 let executable = ToolEnvironment.executableURL()83 let executableDirectory = executable?.deletingLastPathComponent()84 let insideBundle = executable?.path.contains(".app/Contents/") ?? false85 let repository = Repository.root()86 let ownBundle: String? = executable.flatMap { url in87 url.path.range(of: ".app/Contents/").map { String(url.path[..<$0.lowerBound]) + ".app" }88 }8990 let byPackage = ORIXEngines.byPackage()91 var package: [String: String] = [:]92 for entry in byPackage { for engine in entry.engines { package[engine.id] = entry.package } }93 let engines = registry.engines.map { engine -> Engine in94 var reason: String?95 if case .unavailable(let r)? = registry.availability(ofEngine: engine.id) { reason = r }96 if let reason { problems.append("Engine '\(engine.id)' is unavailable: \(reason)") }97 return Engine(id: engine.id, name: engine.displayName, package: package[engine.id] ?? "?", available: reason == nil, reason: reason,98 paths: engine.paths.count, heavy: ORIXEngines.heavyEngineIDs.contains(engine.id))99 }100101 func origin(of path: String) -> String {102 if let env = ProcessInfo.processInfo.environment["ORIX_CONVERT_TOOLS"], !env.isEmpty, path == URL(fileURLWithPath: env).standardizedFileURL.path {103 return path.contains(".app/Contents/Helpers") ? "app bundle (ORIX_CONVERT_TOOLS)" : "ORIX_CONVERT_TOOLS"104 }105 if path.hasSuffix(".app/Contents/Helpers") || path.contains(".app/Contents/") { return "app bundle" }106 if let repository, path.hasPrefix(repository.path + "/") { return "repository" }107 return "other"108 }109 func isForeign(_ path: String) -> Bool {110 if foreignPrefixes.contains(where: { path == $0 || path.hasPrefix($0 + "/") }) { return true }111 if path.hasPrefix("/Applications/") || path.contains("/Applications/") {112 if let ownBundle, path.hasPrefix(ownBundle + "/") { return false }113 return true114 }115 return false116 }117118 let directories = BundledTools.searchDirectories().map { url -> Directory in119 let path = url.standardizedFileURL.path120 if isForeign(path) { problems.append("Tool search directory \(path) is outside the app bundle and the repository.") }121 return Directory(path: path, origin: origin(of: path), exists: FileManager.default.fileExists(atPath: path))122 }123124 var tools: [Tool] = []125 for known in knownTools {126 guard let url = BundledTools.url(for: known.relativePath) else {127 tools.append(Tool(name: known.name, relativePath: known.relativePath, path: nil, resolvedPath: nil, origin: nil, version: nil, sizeBytes: nil, usedBy: known.usedBy))128 continue129 }130 let resolved = url.resolvingSymlinksInPath().path131 let directory = BundledTools.searchDirectories().first { url.path.hasPrefix($0.path) }?.standardizedFileURL.path ?? ""132 if isForeign(url.path) || isForeign(resolved) {133 problems.append("\(known.name) resolves to \(resolved), outside the app bundle and the repository.")134 } else if let repository, !(resolved.hasPrefix(repository.path + "/") || resolved.contains(".app/Contents/") || (executableDirectory.map { resolved.hasPrefix($0.path + "/") } ?? false)135 || resolved.hasPrefix(URL(fileURLWithPath: directory).resolvingSymlinksInPath().path + "/")) {136 problems.append("\(known.name) is a link leaving its tools folder: \(resolved).")137 }138 let size = (try? FileManager.default.attributesOfItem(atPath: resolved)[.size] as? NSNumber)?.int64Value139 tools.append(Tool(name: known.name, relativePath: known.relativePath, path: url.path, resolvedPath: resolved == url.path ? nil : resolved,140 origin: origin(of: directory), version: await version(of: known.name, at: url), sizeBytes: size, usedBy: known.usedBy))141 }142143 var images: [Image] = []144 for index in 0..<_dyld_image_count() {145 guard let name = _dyld_get_image_name(index) else { continue }146 let path = String(cString: name)147 if path.hasPrefix("/System/") || path.hasPrefix("/usr/lib/") { continue }148 let resolved = URL(fileURLWithPath: path).resolvingSymlinksInPath().path149 let inside = !isForeign(resolved) && ((executableDirectory.map { resolved.hasPrefix($0.resolvingSymlinksInPath().path + "/") } ?? false)150 || (ownBundle.map { resolved.hasPrefix($0 + "/") } ?? false) || (repository.map { resolved.hasPrefix($0.path + "/") } ?? false))151 if !inside { problems.append("A library is loaded from outside the bundle and the repository: \(resolved)") }152 images.append(Image(path: resolved, inside: inside))153 }154155 let notWired = ORIXEngines.knownPackages.filter { !ORIXEngines.wiredPackages.contains($0) }156 return Output(version: CLI.version, executable: executable?.path ?? "?", insideAppBundle: insideBundle,157 toolsEnvironment: ProcessInfo.processInfo.environment["ORIX_CONVERT_TOOLS"],158 wiredPackages: ORIXEngines.wiredPackages, notWiredPackages: notWired, engines: engines, searchDirectories: directories,159 tools: tools, libraries: libraries(), loadedImagesOutsideSystem: images, registry: registry.counts,160 diagnostics: registry.diagnostics, problems: problems + registry.diagnostics)161 }162163 /// Version of a bundled tool, asked from the tool itself (or read from the LibreOffice bundle, to avoid starting it).164 static func version(of name: String, at url: URL) async -> String? {165 switch name {166 case "ffmpeg", "ffprobe":167 guard let result = try? await ProcessRunner.run(url, arguments: ["-version"], timeout: 20) else { return nil }168 return result.stdoutString.split(whereSeparator: \.isNewline).first.map { line in169 line.split(separator: " ").dropFirst(2).first.map(String.init) ?? String(line)170 }171 case "pandoc":172 guard let result = try? await ProcessRunner.run(url, arguments: ["--version"], timeout: 20) else { return nil }173 return result.stdoutString.split(whereSeparator: \.isNewline).first.map { $0.replacingOccurrences(of: "pandoc ", with: "") }174 case "LibreOffice":175 let plist = url.deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Info.plist")176 guard let data = try? Data(contentsOf: plist),177 let info = (try? PropertyListSerialization.propertyList(from: data, format: nil)) as? [String: Any] else { return nil }178 return info["CFBundleShortVersionString"] as? String ?? info["CFBundleVersion"] as? String179 default:180 return nil181 }182 }183184 /// Versions of the libraries linked into this executable, read from the libraries themselves when they export a185 /// version function. A statically linked library whose version symbol was stripped reports `nil`.186 static func libraries() -> [Library] {187 typealias StringFunction = @convention(c) () -> UnsafePointer<CChar>?188 typealias IntFunction = @convention(c) () -> Int32189 typealias UIntFunction = @convention(c) () -> UInt32190 let handle = UnsafeMutableRawPointer(bitPattern: -2) // RTLD_DEFAULT191 func string(_ symbol: String) -> String? {192 guard let pointer = dlsym(handle, symbol) else { return nil }193 return unsafeBitCast(pointer, to: StringFunction.self)().map { String(cString: $0) }194 }195 var list: [Library] = []196 if ORIXEngines.wiredPackages.contains("EngineData") {197 list.append(Library(name: "DuckDB", version: string("duckdb_library_version"), linkage: "bundled dynamic library (libduckdb.dylib)", usedBy: "data"))198 list.append(Library(name: "SQLite", version: string("sqlite3_libversion"), linkage: "macOS system library", usedBy: "data, geo, history"))199 }200 if ORIXEngines.wiredPackages.contains("EngineArchive") {201 list.append(Library(name: "libarchive", version: string("archive_version_string"), linkage: "static (CArchive.xcframework)", usedBy: "archive"))202 }203 if ORIXEngines.wiredPackages.contains("EngineImage") {204 var version: String?205 if let pointer = dlsym(handle, "WebPGetEncoderVersion") {206 let v = unsafeBitCast(pointer, to: IntFunction.self)()207 version = "\((v >> 16) & 0xFF).\((v >> 8) & 0xFF).\(v & 0xFF)"208 }209 list.append(Library(name: "libwebp", version: version, linkage: "static (CWebP.xcframework)", usedBy: "image"))210 }211 if ORIXEngines.wiredPackages.contains("EngineModel3D") {212 var version: String?213 if let major = dlsym(handle, "aiGetVersionMajor"), let minor = dlsym(handle, "aiGetVersionMinor"), let patch = dlsym(handle, "aiGetVersionPatch") {214 version = "\(unsafeBitCast(major, to: UIntFunction.self)()).\(unsafeBitCast(minor, to: UIntFunction.self)()).\(unsafeBitCast(patch, to: UIntFunction.self)())"215 }216 list.append(Library(name: "Assimp", version: version, linkage: "static (CAssimp.xcframework)", usedBy: "model3d"))217 }218 if ORIXEngines.wiredPackages.contains("EngineFont") {219 list.append(Library(name: "woff2 + brotli", version: nil, linkage: "static (CWOFF2.xcframework)", usedBy: "font"))220 }221 if ORIXEngines.wiredPackages.contains("EngineScientific") {222 typealias VersionFunction = @convention(c) (UnsafeMutablePointer<UInt32>, UnsafeMutablePointer<UInt32>, UnsafeMutablePointer<UInt32>) -> Int32223 var version: String?224 if let pointer = dlsym(handle, "H5get_libversion") {225 var major: UInt32 = 0, minor: UInt32 = 0, release: UInt32 = 0226 if unsafeBitCast(pointer, to: VersionFunction.self)(&major, &minor, &release) >= 0 { version = "\(major).\(minor).\(release)" }227 }228 list.append(Library(name: "HDF5", version: version, linkage: "static (CHDF5.xcframework)", usedBy: "scientific"))229 }230 return list231 }232233 static func run(_ arguments: ParsedArguments, io: CommandIO) async throws -> ExitCode {234 let output = await collect()235 if arguments.flag("json") {236 io.out(try Shared.json(output))237 return output.problems.isEmpty ? .success : .checkFailed238 }239 io.out("orix-convert \(output.version)")240 io.out("Executable: \(output.executable)" + (output.insideAppBundle ? " (inside an app bundle)" : " (development build, outside an app bundle)"))241 io.out("")242 io.out("Engines (\(output.engines.count), from \(output.wiredPackages.count) package(s))")243 Shared.table(["ENGINE", "NAME", "PACKAGE", "PATHS", "HEAVY", "AVAILABLE"], output.engines.map {244 [$0.id, $0.name, $0.package, String($0.paths), $0.heavy ? "yes" : "—", $0.available ? "yes" : "NO — \($0.reason ?? "")"]245 }).forEach { io.out(" " + $0) }246 if !output.notWiredPackages.isEmpty { io.out(" Not in this build: \(output.notWiredPackages.joined(separator: ", "))") }247 let c = output.registry248 io.out(" Registry: \(c.formatsWithPath) formats, \(c.distinctPairs) source → destination pairs, \(c.availablePaths) paths, \(c.operations) named operations" + (c.unavailablePaths > 0 ? ", \(c.unavailablePaths) paths unavailable" : ""))249 io.out("")250 io.out("Tool search directories (in order; PATH is never searched)")251 for directory in output.searchDirectories {252 io.out(" \(directory.path) [\(directory.origin)\(directory.exists ? "" : ", absent")]")253 }254 io.out("")255 io.out("Bundled tools")256 Shared.table(["TOOL", "VERSION", "SIZE", "FROM", "PATH"], output.tools.map {257 [$0.name, $0.version ?? ($0.path == nil ? "—" : "n/a"), $0.sizeBytes.map(Shared.bytes) ?? "—", $0.origin ?? "—", $0.path ?? "not present (used by: \($0.usedBy))"]258 }).forEach { io.out(" " + $0) }259 io.out("")260 io.out("Linked libraries")261 Shared.table(["LIBRARY", "VERSION", "LINKAGE", "USED BY"], output.libraries.map { [$0.name, $0.version ?? "not reported", $0.linkage, $0.usedBy] }).forEach { io.out(" " + $0) }262 io.out("")263 io.out("Images loaded from outside /System and /usr/lib")264 for image in output.loadedImagesOutsideSystem { io.out(" \(image.path)" + (image.inside ? "" : " <-- OUTSIDE")) }265 io.out("")266 if output.problems.isEmpty {267 io.out("No problem found: every engine is available and every tool and library comes from the bundle or the repository.")268 return .success269 }270 io.out("Problems (\(output.problems.count))")271 output.problems.forEach { io.out(" - \($0)") }272 return .checkFailed273 }274}275