1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import struct TSCBasic.ByteString
import struct TSCBasic.FileInfo
import enum TSCBasic.FileMode
private enum DirectoryNode: Codable {
case directory(name: String, isSymlink: Bool, children: [DirectoryNode])
case file(name: String, isExecutable: Bool, isSymlink: Bool, contents: Data?)
case root(children: [DirectoryNode])
var children: [DirectoryNode] {
switch self {
case .directory(_, _, let children): return children
case .file: return []
case .root(let children): return children
}
}
var name: String {
switch self {
case .directory(let name, _, _): return name
case .file(let name, _, _, _): return name
case .root: return AbsolutePath.root.pathString
}
}
var fileAttributeType: FileAttributeType {
switch self {
case .directory: return .typeDirectory
case .file(_, _, let isSymlink, _): return isSymlink ? .typeSymbolicLink : .typeRegular
case .root: return .typeDirectory
}
}
var isDirectory: Bool {
switch self {
case .directory: return true
case .file: return false
case .root: return true
}
}
var isFile: Bool {
switch self {
case .directory: return false
case .file: return true
case .root: return false
}
}
var isRoot: Bool {
switch self {
case .directory: return false
case .file: return false
case .root: return true
}
}
var isSymlink: Bool {
switch self {
case .directory(_, let isSymlink, _): return isSymlink
case .file(_, _, let isSymlink, _): return isSymlink
case .root: return false
}
}
}
private enum Errors: Swift.Error, LocalizedError {
case noSuchFileOrDirectory(path: AbsolutePath)
case notAFile(path: AbsolutePath)
case readOnlyFileSystem
case unhandledDirectoryNode(path: AbsolutePath)
public var errorDescription: String? {
switch self {
case .noSuchFileOrDirectory(let path): return "no such file or directory: \(path.pathString)"
case .notAFile(let path): return "not a file: \(path.pathString)"
case .readOnlyFileSystem: return "read-only filesystem"
case .unhandledDirectoryNode(let path): return "unhandled directory node: \(path.pathString)"
}
}
}
extension FileSystem {
fileprivate func getDirectoryNodes(
_ path: AbsolutePath,
includeContents: [AbsolutePath]
) throws -> [DirectoryNode] {
try getDirectoryContents(path).compactMap {
let current = path.appending(component: $0)
let isSymlink = isSymlink(current)
if isFile(current) {
let contents: Data?
if includeContents.contains(current) {
contents = try readFileContents(current)
} else {
contents = nil
}
return .file(
name: $0,
isExecutable: isExecutableFile(current),
isSymlink: isSymlink,
contents: contents
)
} else if isDirectory(current) {
if $0.hasPrefix(".") { return nil } // we ignore hidden files
return .directory(
name: $0,
isSymlink: isSymlink,
children: try getDirectoryNodes(current, includeContents: includeContents)
)
} else {
throw Errors.unhandledDirectoryNode(path: current)
}
}
}
}
/// A JSON-backed, read-only virtual file system.
public class VirtualFileSystem: FileSystem {
private let root: DirectoryNode
public init(path: TSCAbsolutePath, fs: FileSystem) throws {
self.root = try JSONDecoder.makeWithDefaults()
.decode(path: AbsolutePath(path), fileSystem: fs, as: DirectoryNode.self)
assert(self.root.isRoot, "VFS needs to have a root node")
}
/// Write information about the directory tree at `directoryPath` into a JSON file at `vfsPath`. This can later be used to construct a `VirtualFileSystem` object.
public static func serializeDirectoryTree(
_ directoryPath: AbsolutePath,
into vfsPath: AbsolutePath,
fs: FileSystem,
includeContents: [AbsolutePath]
) throws {
let data = try JSONEncoder.makeWithDefaults().encode(
DirectoryNode.root(
children: fs.getDirectoryNodes(
directoryPath,
includeContents: includeContents
)
)
)
try data.write(to: URL(fileURLWithPath: vfsPath.pathString))
}
private func findNode(_ path: TSCAbsolutePath, followSymlink: Bool) -> DirectoryNode? {
var current: DirectoryNode? = self.root
for component in path.components {
if component == AbsolutePath.root.pathString { continue }
guard followSymlink, current?.isSymlink == false else { return nil }
current = current?.children.first(where: { $0.name == component })
}
return current
}
public func exists(_ path: TSCAbsolutePath, followSymlink: Bool) -> Bool {
findNode(path, followSymlink: followSymlink) != nil
}
public func isDirectory(_ path: TSCAbsolutePath) -> Bool {
findNode(path, followSymlink: true)?.isDirectory == true
}
public func isFile(_ path: TSCAbsolutePath) -> Bool {
findNode(path, followSymlink: true)?.isFile == true
}
public func isExecutableFile(_ path: TSCAbsolutePath) -> Bool {
guard let node = findNode(path, followSymlink: true) else { return false }
if case .file(_, let isExecutable, _, _) = node {
return isExecutable
} else {
return false
}
}
public func isSymlink(_ path: TSCAbsolutePath) -> Bool {
findNode(path, followSymlink: true)?.isSymlink == true
}
public func isReadable(_ path: TSCAbsolutePath) -> Bool {
self.exists(path)
}
public func isWritable(_: TSCAbsolutePath) -> Bool {
false
}
public func getDirectoryContents(_ path: TSCAbsolutePath) throws -> [String] {
guard let node = findNode(path, followSymlink: true)
else { throw Errors.noSuchFileOrDirectory(path: AbsolutePath(path)) }
return node.children.map(\.name)
}
public let currentWorkingDirectory: TSCAbsolutePath? = nil
public func changeCurrentWorkingDirectory(to path: TSCAbsolutePath) throws {
throw Errors.readOnlyFileSystem
}
public var homeDirectory = TSCAbsolutePath.root
public var cachesDirectory: TSCAbsolutePath? = nil
public var tempDirectory = TSCAbsolutePath.root
public func createSymbolicLink(
_ path: TSCAbsolutePath,
pointingAt destination: TSCAbsolutePath,
relative: Bool
) throws {
throw Errors.readOnlyFileSystem
}
public func removeFileTree(_: TSCAbsolutePath) throws {
throw Errors.readOnlyFileSystem
}
public func copy(from sourcePath: TSCAbsolutePath, to destinationPath: TSCAbsolutePath) throws {
throw Errors.readOnlyFileSystem
}
public func move(from sourcePath: TSCAbsolutePath, to destinationPath: TSCAbsolutePath) throws {
throw Errors.readOnlyFileSystem
}
public func createDirectory(_ path: TSCAbsolutePath, recursive: Bool) throws {
throw Errors.readOnlyFileSystem
}
public func readFileContents(_ path: TSCAbsolutePath) throws -> ByteString {
guard let node = findNode(path, followSymlink: true)
else { throw Errors.noSuchFileOrDirectory(path: AbsolutePath(path)) }
switch node {
case .directory: throw Errors.notAFile(path: AbsolutePath(path))
case .file(_, _, _, let contents):
if let contents {
return ByteString(contents)
} else {
return ""
}
case .root: throw Errors.notAFile(path: AbsolutePath(path))
}
}
public func writeFileContents(_ path: TSCAbsolutePath, bytes: ByteString) throws {
throw Errors.readOnlyFileSystem
}
public func chmod(_ mode: FileMode, path: TSCAbsolutePath, options: Set<FileMode.Option>) throws {
throw Errors.readOnlyFileSystem
}
public func getFileInfo(_ path: TSCAbsolutePath) throws -> FileInfo {
guard let node = findNode(path, followSymlink: true)
else { throw Errors.noSuchFileOrDirectory(path: AbsolutePath(path)) }
let attrs: [FileAttributeKey: Any] = [
.systemNumber: NSNumber(value: UInt64(0)),
.systemFileNumber: UInt64(0),
.posixPermissions: NSNumber(value: Int16(0)),
.type: node.fileAttributeType,
.size: UInt64(0),
.modificationDate: Date(),
]
return FileInfo(attrs)
}
}
// `VirtualFileSystem` is read-only, so it can be marked as `Sendable`.
extension VirtualFileSystem: @unchecked Sendable {}
|