import Darwin import Foundation enum PackagingPaths { static func metadata(_ path: URL) throws -> stat { var value = stat() guard lstat(path.path, &value) == 0 else { throw DevError("\(path.path): \(String(cString: strerror(errno)))") } return value } static func resolved(_ path: URL) throws -> URL { guard let pointer = realpath(path.path, nil) else { throw DevError("\(path.path): \(String(cString: strerror(errno)))") } defer { free(pointer) } return URL(fileURLWithPath: String(cString: pointer)) } static func resolveAllowMissing(_ path: URL) throws -> URL { var pending = path.path.components(separatedBy: "/") var parts: [String] = [] var links = 0 while !pending.isEmpty { let component = pending.removeFirst() if component.isEmpty || component == "." { continue } if component == ".." { if !parts.isEmpty { parts.removeLast() } continue } let candidate = URL(fileURLWithPath: "/" + (parts + [component]).joined(separator: "/")) var value = stat() if lstat(candidate.path, &value) != 0 { guard errno == ENOENT else { throw DevError("\(candidate.path): \(String(cString: strerror(errno)))") } parts.append(component) } else if value.st_mode & S_IFMT == S_IFLNK { links += 1 guard links <= 40 else { throw DevError("Too many levels of symbolic links") } let destination = try FileManager.default.destinationOfSymbolicLink(atPath: candidate.path) if destination.hasPrefix("/") { parts = [] } pending = destination.components(separatedBy: "/") + pending } else { parts.append(component) } } return URL(fileURLWithPath: "/" + parts.joined(separator: "/")) } static func preflight(_ app: URL) throws { let root = try resolved(app) guard try metadata(root).st_mode & S_IFMT == S_IFDIR else { throw DevError("expected an app directory.") } var failures: [String] = [] func reject(_ path: URL, _ reason: String) { let relative = String(path.path.dropFirst(root.path.count + 1)) failures.append("\(String(reflecting: relative)): \(reason)") } func visit(_ directory: URL) throws { for path in try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) .sorted(by: { $0.lastPathComponent < $1.lastPathComponent }) { switch try metadata(path).st_mode & S_IFMT { case S_IFLNK: let destination = try FileManager.default.destinationOfSymbolicLink(atPath: path.path) if destination.hasPrefix("/") { reject(path, "absolute symlink") continue } do { let target = try resolveAllowMissing(path) if target.path != root.path && !target.path.hasPrefix(root.path + "/") { reject(path, "symlink escapes the bundle") } else { _ = try resolved(path) } } catch { reject(path, "unresolvable symlink (\(error))") } case S_IFDIR: try visit(path) case S_IFREG: break default: reject(path, "not a regular file, directory, or relative symlink") } } } try visit(root) guard failures.isEmpty else { throw DevError("Bundle preflight refused:\n" + failures.joined(separator: "\n")) } } static func install(source: URL, target: URL) throws { func exists(_ path: URL) throws -> Bool { var value = stat() if lstat(path.path, &value) != 0 { guard errno == ENOENT else { throw DevError("\(path.path): \(String(cString: strerror(errno)))") } return false } guard value.st_mode & S_IFMT == S_IFDIR else { throw DevError("Expected a bundle directory, not a symlink or file: \(path.path)") } return true } guard try exists(source) else { throw DevError("Staged bundle not found: \(source.path)") } let replacing = try exists(target) // Swapping keeps the previous bundle available until the replacement is published. let flags = replacing ? UInt32(RENAME_SWAP) : UInt32(RENAME_EXCL) guard renamex_np(source.path, target.path, flags) == 0 else { throw DevError("\(target.path): \(String(cString: strerror(errno)))") } } }