Module

std::fs::dir

Directory Iteration

Owned iterator over directory entries with automatic cleanup.

Fs::ReadDir is an owned directory iterator that wraps DIR* and automatically calls closedir when dropped. Each call to next() returns an owned DirEntry with copied strings.

Example

import Fs from "std::fs";
import Io from "std::io";

function main(): i32 {
  let result: Result<Fs::ReadDir, Io::IoError> = Fs::ReadDir::open("/tmp");
  if (result.isError()) {
    return 1;
  }

  let mut iter: Fs::ReadDir = result.unwrap();

  loop {
    match (iter.next()) {
      Result::OK(Option::SOME(entry)) -> {
        if (entry.isDir()) {
          Io::println(entry.name);
        }
      },
      Result::OK(Option::NONE) -> {
        break;
      },
      Result::ERROR(_) -> {
        return 1;
      },
    }
  }

  return 0;
}
namespace Fs

Owned directory entry.

Records

record DirEntry

Directory entry produced by ReadDir and filesystem walking helpers.

Members
name: String

File name (owned).

path: PathBuf

Full path: baseDir + “/” + name (owned).

fileType: FileType

Classified file type.

ino: u64

Inode number.

isDir(&self): boolean

Returns true if this entry is a directory.

isFile(&self): boolean

Returns true if this entry is a regular file.

isSymlink(&self): boolean

Returns true if this entry is a symbolic link.

record ReadDir

Owned directory iterator.

Members
dir: *mut void
baseDir: String
static open(path: str): Result<ReadDir, IoError>

Opens a directory for iteration.

close(&mut self): void

Closes the directory stream without consuming the iterator value.

drop(&mut self): void

Closes the directory stream.

next(&mut self): Result<Option<DirEntry>, IoError>

Returns the next directory entry.

Result::OK(Option::NONE) means end of directory.

Functions

function collectWalk(path: str): Result<Vector<String>, IoError>

Returns a depth-first list of paths rooted at path.

function collectWalkChildren(parent: &String, output: &mut Vector<String>): Result<void, IoError>

Recursively collects children of parent without following symlink dirs.

function collectWalkDirectory(path: str, output: &mut Vector<String>): Result<void, IoError>

Collects entries under an existing directory path.

function collectWalkEntry(path: &String, isDirectory: boolean, output: &mut Vector<String>): Result<void, IoError>

Adds path to walk output and descends when it is a directory.

function collectWalkPath(path: str, output: &mut Vector<String>): Result<void, IoError>

Walks a raw C-string path after copying it to owned storage.

function collectWalkPathOwned(path: &String, output: &mut Vector<String>): Result<void, IoError>

Walks an owned path after classifying it with lstat.

function collectWalkSingle(path: str, output: &mut Vector<String>): Result<void, IoError>

Collects a single non-directory path.

function isDotEntry(name: String): boolean

Returns true for owned . and .. entry names.

function isDotEntryName(name: str): boolean

Returns true for raw C-string . and .. entry names.

function isDotEntryPath(path: &PathBuf): boolean

Returns true when a path’s final component is . or ...

function removeDirAllPath(path: str): Result<void, IoError>

Removes a directory tree through the platform system layer.

function walkContains(entries: &Vector<String>, expected: str): boolean

Returns whether a walk result contains expected exactly.

function walkDescribe(entries: &Vector<String>): String

Joins walk result entries with newlines for diagnostics and tests.

function walkEntryFromPath(path: PathBuf): Result<DirEntry, IoError>

Builds a DirEntry for a path using lstat so symlinks stay leaves.