Skip to content

jokerhutt/swift-disk-scanner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift Disk Scanner

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.


Usage

let config = ScanConfig(
    root: "/",
    workerCount: 50,
    includeFiles: true
)

do {
    let result : ScanResultNode = try SwiftDiskScanner.startScan(config)
} catch {
    // ...
}

Data Model

The scan returns a tree of ScanResultNode values representing the filesystem.

  • Directories contain child nodes and their size is the recursive total.
  • Files have no children and their size is 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]
}

Configuration

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

About

This is a Swift package that quickly scans a file system and returns an in-memory tree with file sizes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages