Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library

    std.fs

    Read and write text or bytes and list directory entries.

    APIs in this module

    5 APIs

    NameModule
    std.fs.liststd.fs
    std.fs.readBytesstd.fs
    std.fs.readTextstd.fs
    std.fs.writeBytesstd.fs
    std.fs.writeTextstd.fs

    Module

    Link

    Read complete UTF-8 text or raw bytes, replace complete file contents, and list immediate directory entries in exact name order. Each operation reports recoverable access and host failures through Result.

    Why

    Link

    Whole file operations preserve bytes and expose failures without requiring the caller to manage an open resource.

    Availability

    Link

    Import std.fs in language mode 5.21. The current compiler selects std/fs.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Exported types

    Link

    export type Bytes = Bytes

    export type Path = Path

    Type members keep the compiler-owned type name shown in their declarations. Importing a module does not create a new type identity.

    Public API

    Link

    std.fs.readText

    std.fs.writeText

    std.fs.readBytes

    std.fs.writeBytes

    std.fs.list

    Pending contracts

    Link

    Read and replace whole files and list directories

    Link
    Ruletopaz.stdlib.contract.filesystem.rule

    Read and replace whole files and list directories

    Each function accepts a string path or a Path. Strings use the host path syntax. A Path supplies its normalized relative text. Relative paths use the working directory in an ordinary installed CLI invocation. A package host applies its configured filesystem base and capability policy.

    Path and payload arguments are evaluated once in source order before the operation starts. The operation acquires and releases its own resources before returning. It does not change a separately opened File cursor.

    Empty paths and paths containing NUL return Err containing a string. Missing read targets, incorrect path kinds, permission denial, package capability denial, and host failures use the same error channel. Errors are values. They do not terminate the program unless the caller chooses to propagate or assert them.

    An error string identifies the public operation and the supplied path. It also describes the failure condition. Platform wording, operating system error numbers, and localized explanations are not stable API values.

    readText returns the entire regular file as text. UTF-8 validity and text preservation follow UTF-8 decoding. Empty files return an empty string. Invalid UTF-8 returns Err without a decoded text value.

    readBytes returns every byte in order, including zero bytes and bytes that are not valid UTF-8. Empty files return empty Bytes. Reading does not create or modify the file.

    writeText encodes its string as UTF-8. writeBytes writes the supplied bytes unchanged. Both create a missing regular file or replace the full contents of an existing regular file. An empty payload creates or truncates the file to zero bytes. No newline or byte order mark is added.

    Writes do not create missing parent directories. They follow the host permission and creation policy. Replacing a file with shorter content removes its previous suffix. Success returns Ok(()) after all payload bytes have been accepted.

    A write failure may occur after creation, truncation, or a prefix write. Err does not promise rollback, atomic replacement, or persistence across a system crash. Earlier argument effects remain visible.

    list returns immediate children sorted by their exact names in ascending Unicode scalar order. It does not return dot or parent pseudoentries, traverse child directories, normalize Unicode names, or fold case. The name field contains the child name without the parent path.

    Each entry kind is exactly file, directory, symlink, or other. Classification uses the entry itself and does not follow a symbolic link. For a regular file, sizeBytes is Some of its byte length when that length fits in int. Other kinds and an unrepresentable size use None().

    A name that cannot be represented as Unicode, a failure while reading an entry, or a failure obtaining its metadata returns Err for the whole list. No partial array is returned. Separate operations do not provide a filesystem snapshot, so other processes may change a path between calls.

    Host access follows symbolic links where the operation addresses their target, subject to host capability checks. A normalized Path is not an authorization token. Use File when operations must retain one open resource and cursor, and std.fs when the operation concerns complete contents or directory entries.

    Why

    Exact bytes and strict text decoding keep data conversion explicit. Whole file replacement has a clear effect, and Result makes permission and host failures recoverable. Sorting names makes directory traversal repeatable for a fixed directory state without assuming a host enumeration order.

    Source evidence

    Replace and create text files with exact content

    Link
    main.tpzReplace and create text files with exact contentOpen in Playground
    1. import std.fs
    2. import std.path { Path }
    3. import std.test
    4. import std.io
    5. let path = test.assertOk(Path.from("chapter.txt"))
    6. test.assertOk(fs.writeText(path, "short\n"))
    7. io.writeLine(test.assertOk(fs.readText("chapter.txt")))
    8. test.assertOk(fs.writeText("created.txt", "한글"))
    9. io.writeLine(test.assertOk(fs.readText("created.txt")))
    10. test.assertOk(fs.writeText("created.txt", ""))
    11. let text = test.assertOk(fs.readText("created.txt"))
    12. let empty = text == ""
    13. io.writeLine("empty {empty}")
    Expected outputexit 0
    short
    
    한글
    empty true
    
    $ topaz run main.tpz · target product 5.21.3
    chapter.txtReplace and create text files with exact contentOpen in Playground
    1. a much longer old value
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. short
    2. 한글
    3. empty true
    Source evidence

    Preserve a byte order mark and physical line endings

    Link
    main.tpzPreserve a byte order mark and physical line endingsOpen in Playground
    1. import std.fs
    2. import std.test
    3. import std.io
    4. io.writeLine(test.assertOk(fs.readText("source.txt")))
    Expected outputexit 0
    alpha
    한글
    
    
    $ topaz run main.tpz · target product 5.21.3
    source.txtPreserve a byte order mark and physical line endingsOpen in Playground
    1. alpha
    2. 한글
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. alpha
    2. 한글
    Source evidence

    Preserve raw bytes and reject invalid UTF-8 text

    Link
    main.tpzPreserve raw bytes and reject invalid UTF-8 textOpen in Playground
    1. import std.fs
    2. import std.bytes { Bytes }
    3. import std.test
    4. import std.io
    5. let bytes = test.assertOk(Bytes.fromArray([0, 65, 255]))
    6. test.assertOk(fs.writeBytes("data.bin", bytes))
    7. io.writeLine(test.assertOk(fs.readBytes("data.bin")).toHex())
    8. test.assertErr(fs.readText("data.bin"))
    9. io.writeLine("invalid text rejected")
    10. test.assertOk(fs.writeBytes("data.bin", Bytes.empty()))
    11. io.writeLine(test.assertOk(fs.readBytes("data.bin")).toHex())
    Expected outputexit 0
    0041ff
    invalid text rejected
    
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 0041ff
    2. invalid text rejected
    Source evidence

    List immediate entries in exact name order

    Link
    main.tpzList immediate entries in exact name orderOpen in Playground
    1. import std.fs
    2. import std.test
    3. import std.io
    4. let entries = test.assertOk(fs.list("entries"))
    5. for entry in (entries) {
    6. let size = match (entry.sizeBytes) {
    7. case Some(value) => "{value}"
    8. case None() => "none"
    9. }
    10. io.writeLine("{entry.name} {entry.kind} {size}")
    11. }
    Expected outputexit 0
    a.txt file 1
    nested directory none
    z.txt file 2
    
    $ topaz run main.tpz · target product 5.21.3
    entries/z.txtList immediate entries in exact name orderOpen in Playground
    1. zz
    entries/a.txtList immediate entries in exact name orderOpen in Playground
    1. a
    entries/nested/inside.txtList immediate entries in exact name orderOpen in Playground
    1. inside
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. a.txt file 1
    2. nested directory none
    3. z.txt file 2
    Source evidence

    Return recoverable errors for the wrong path kind

    Link
    main.tpzReturn recoverable errors for the wrong path kindOpen in Playground
    1. import std.fs
    2. import std.test
    3. import std.io
    4. test.assertErr(fs.readText("directory"))
    5. test.assertErr(fs.readBytes("directory"))
    6. test.assertErr(fs.writeText("directory", "text"))
    7. test.assertErr(fs.list("plain.txt"))
    8. io.writeLine("wrong kinds rejected")
    Expected outputexit 0
    wrong kinds rejected
    
    $ topaz run main.tpz · target product 5.21.3
    directory/inside.txtReturn recoverable errors for the wrong path kindOpen in Playground
    1. inside
    plain.txtReturn recoverable errors for the wrong path kindOpen in Playground
    1. plain
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. wrong kinds rejected
    Source evidence

    Evaluate payloads before replacing a file

    Link
    main.tpzEvaluate payloads before replacing a fileOpen in Playground
    1. import std.fs
    2. import std.test
    3. import std.io
    4. function target() -> string {
    5. io.writeLine("path")
    6. return "source.txt"
    7. }
    8. function payload() -> string {
    9. io.writeLine("text")
    10. io.writeLine(test.assertOk(fs.readText("source.txt")))
    11. return "new"
    12. }
    13. test.assertOk(fs.writeText(target(), payload()))
    14. io.writeLine(test.assertOk(fs.readText("source.txt")))
    Expected outputexit 0
    path
    text
    old
    new
    
    $ topaz run main.tpz · target product 5.21.3
    source.txtEvaluate payloads before replacing a fileOpen in Playground
    1. old
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. path
    2. text
    3. old
    4. new
    Source evidence

    Identify the operation and path in a readText error

    Link
    main.tpzIdentify the operation and path in a readText errorOpen in Playground
    1. import std.fs
    2. import std.bytes { Bytes }
    3. import std.test
    4. import std.io
    5. let error = test.assertErr(fs.readText("missing/item.txt"))
    6. let operation = error.contains("readText")
    7. let path = error.contains("missing/item.txt")
    8. io.writeLine("{operation} {path}")
    Expected outputexit 0
    true true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true true
    Source evidence

    Identify the operation and path in a readBytes error

    Link
    main.tpzIdentify the operation and path in a readBytes errorOpen in Playground
    1. import std.fs
    2. import std.bytes { Bytes }
    3. import std.test
    4. import std.io
    5. let error = test.assertErr(fs.readBytes("missing/item.txt"))
    6. let operation = error.contains("readBytes")
    7. let path = error.contains("missing/item.txt")
    8. io.writeLine("{operation} {path}")
    Expected outputexit 0
    true true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true true
    Source evidence

    Identify the operation and path in a writeText error

    Link
    main.tpzIdentify the operation and path in a writeText errorOpen in Playground
    1. import std.fs
    2. import std.bytes { Bytes }
    3. import std.test
    4. import std.io
    5. let error = test.assertErr(fs.writeText("missing/item.txt", "text"))
    6. let operation = error.contains("writeText")
    7. let path = error.contains("missing/item.txt")
    8. io.writeLine("{operation} {path}")
    Expected outputexit 0
    true true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true true
    Source evidence

    Identify the operation and path in a writeBytes error

    Link
    main.tpzIdentify the operation and path in a writeBytes errorOpen in Playground
    1. import std.fs
    2. import std.bytes { Bytes }
    3. import std.test
    4. import std.io
    5. let error = test.assertErr(fs.writeBytes("missing/item.txt", Bytes.empty()))
    6. let operation = error.contains("writeBytes")
    7. let path = error.contains("missing/item.txt")
    8. io.writeLine("{operation} {path}")
    Expected outputexit 0
    true true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true true
    Source evidence

    Identify the operation and path in a list error

    Link
    main.tpzIdentify the operation and path in a list errorOpen in Playground
    1. import std.fs
    2. import std.bytes { Bytes }
    3. import std.test
    4. import std.io
    5. let error = test.assertErr(fs.list("missing/item.txt"))
    6. let operation = error.contains("list")
    7. let path = error.contains("missing/item.txt")
    8. io.writeLine("{operation} {path}")
    Expected outputexit 0
    true true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true true
    Source evidence

    Menu

    Download