A Swift package that quickly traverses directories and returns a tree of files with their sizes.
It is designed to be as quick as possible, making it good for scanning filesystems with large numbers of files.
It does so by using the getattrlistbulk syscall from the Darwin API instead of stat, which greatly reduces the number of syscalls required. It also uses a Chase-Lev work stealing deque to parallelize the scanning of directories across multiple threads.
let config = ScanConfig(
root: "/",
workerCount: 50,
includeFiles: true
)
do {
let result : ScanResultNode = try SwiftDiskScanner.startScan(config)
} catch {
// ...
}The scan returns a tree of ScanResultNode values representing the filesystem.
- Directories contain child nodes and their
sizeis the recursive total. - Files have no children and their
sizeis the file size.
public enum NodeType {
case file
case directory
}
public final class ScanResultNode {
public let name: String
public let path: String
public let size: UInt64
public let type: NodeType
public let children: [ScanResultNode]
}Controls how the filesystem is traversed.
public enum SizeKind {
case allocated // disk usage (blocks)
case logical // file size
}
public struct ScanConfig {
public let root: String
public let maxDepth: Int
public let followSymlinks: Bool
public let workerCount: Int
public let includeFiles: Bool
public let sizeKind: SizeKind
public let stayOnDevice: Bool
}- root: starting path
- maxDepth: recursion limit
- followSymlinks: follow symbolic links
- workerCount: parallel workers
- includeFiles: include files in output
- sizeKind: allocated vs logical size
- stayOnDevice: restrict to same filesystem