struct DirectoryProperties
let path: Path
The directory path passed to host enumeration operations.
Methods
func entries() -> [DirectoryEntry]
Enumerates immediate children and classifies directories, files, and symlinks.
Names are appended to path without canonicalizing symlinks. Failure to open the directory returns an empty array; an error during iteration returns entries collected before the error.
View Source
pub func entries() -> [DirectoryEntry] {
#unsafe {
let path_string = self.path.to_string()
let path_buf = _alloc<Byte>(count: path_string.byte_count + 1)
_copy(
from: path_string.storage.base,
to: path_buf,
length: path_string.byte_count
)
let real_count = _io_dir_count(path: path_buf)
if real_count < 0 {
_free(ptr: path_buf)
return []
}
let capacity = real_count
let storage = _storage<DirectoryEntry>(capacity: capacity)
let i = 0
let out = 0
loop i < real_count {
let name_len = _io_dir_entry_len(path: path_buf, index: i)
if name_len < 0 {
break
}
let name_buf = _alloc<Byte>(count: name_len)
let copied = _io_dir_entry_copy(path: path_buf, index: i, buf: name_buf)
if copied < 0 {
_free(ptr: name_buf)
break
}
let name = String(
base: ByteStorage(base: name_buf),
byte_count: copied,
capacity: copied
)
let entry_path = self.path.appending(component: name)
let kind = _io_dir_entry_kind(path: path_buf, index: i)
if kind == _directory_entry_directory {
storage.set(
index: out,
value: DirectoryEntry.directory(Directory(path: entry_path))
)
} else {
if kind == _directory_entry_symlink {
storage.set(index: out, value: DirectoryEntry.symlink(entry_path))
} else {
storage.set(index: out, value: DirectoryEntry.file(FsFile(path: entry_path)))
}
}
i = i + 1
out = out + 1
}
_free(ptr: path_buf)
Array<DirectoryEntry>(storage: storage, count: out, capacity: capacity)
}
}func walk(fn: (DirectoryEntry) -> ())
Performs a depth-first pre-order traversal beneath this directory.
fn sees a child directory before its descendants. Symbolic links are reported but never followed, avoiding link cycles; enumeration errors use the same best-effort behavior as entries.
View Source
pub func walk(fn: (DirectoryEntry) -> ()) {
for entry in self.entries() {
match entry {
.directory(directory) -> {
fn(entry)
directory.walk(fn: fn)
},
other -> fn(other)
}
}
}