Skip to content
CryoCryo home

file

import std::fs::file; · source

Types

File

type struct File {
    fd: i32;

    static open(p: Path) -> Result<File, IoError>;
    static create(p: Path) -> Result<File, IoError>;
    static open_with(p: Path, options: &OpenOptions) -> Result<File, IoError>;
    seek_set(mut &this, offset: i64) -> Result<u64, IoError>;
    seek_cur(mut &this, offset: i64) -> Result<u64, IoError>;
    seek_end(mut &this, offset: i64) -> Result<u64, IoError>;
}
import std::fs::file;
import std::fs::path;

match (File::open(Path::from(Str::new("config.json")))) {
    Result::Ok(f)  => { /* f is Read + Write + Seek */ }
    Result::Err(e) => { /* NotFound, PermissionDenied, ... */ }
}

open is read-only, create creates or truncates for writing with mode 0644, and open_with takes an OpenOptions for full control. File implements Read, Write, and Seek, plus the inherent seek_set, seek_cur, and seek_end helpers for direct use; stream_position comes with Seek.

A File wraps a raw fd, and the obligations are the POSIX ones: every open pairs with exactly one drop, which closes it. Calling drop twice is a bug the kernel may not catch — the second close could race an unrelated open that reused the descriptor.

Properties

PropertyType
fdi32

Constructors

static open(p: Path) -> Result<File, IoError>;

Open p for reading. Fails with NotFound, PermissionDenied, or a mapped errno-derived kind on failure.

static create(p: Path) -> Result<File, IoError>;

Create (or truncate) p for writing.

static open_with(p: Path, options: &OpenOptions) -> Result<File, IoError>;

Methods

seek_set(mut &this, offset: i64) -> Result<u64, IoError>;

Seek to offset bytes from the start. Returns the new absolute position.

seek_cur(mut &this, offset: i64) -> Result<u64, IoError>;

Seek by offset bytes relative to the current position (offset may be negative). Returns the new absolute position.

seek_end(mut &this, offset: i64) -> Result<u64, IoError>;

Seek to offset bytes relative to the end of the file (offset is usually <= 0). Returns the new absolute position.

OpenOptions

type struct OpenOptions {
    read:       boolean;
    write:      boolean;
    append:     boolean;
    truncate:   boolean;
    create:     boolean;
    create_new: boolean;

    static new() -> OpenOptions;
    read(mut &this, value: boolean) -> OpenOptions;
    write(mut &this, value: boolean) -> OpenOptions;
    append(mut &this, value: boolean) -> OpenOptions;
    truncate(mut &this, value: boolean) -> OpenOptions;
    create(mut &this, value: boolean) -> OpenOptions;
    create_new(mut &this, value: boolean) -> OpenOptions;
    open(&this, p: Path) -> Result<File, IoError>;
    to_flags(&this) -> i32;
}

A builder. Each setter takes a boolean and returns the options, so they chain; open(p) finishes. to_flags is the O_* bitmask the options translate to, for handing to libc::open directly.

mut opts: OpenOptions = OpenOptions::new();
mut log: File = opts.write(true).create(true).append(true).open(p)?;
Properties
PropertyType
readboolean
writeboolean
appendboolean
truncateboolean
createboolean
create_newboolean
Constructors
static new() -> OpenOptions;
Methods
read(mut &this, value: boolean) -> OpenOptions;
write(mut &this, value: boolean) -> OpenOptions;
append(mut &this, value: boolean) -> OpenOptions;
truncate(mut &this, value: boolean) -> OpenOptions;
create(mut &this, value: boolean) -> OpenOptions;
create_new(mut &this, value: boolean) -> OpenOptions;
open(&this, p: Path) -> Result<File, IoError>;
to_flags(&this) -> i32;

Flatten the options into the platform open/_open flags bitmask. The actual flag values are OS-specific (glibc and MSVCRT disagree on O_CREAT/O_TRUNC/O_APPEND/O_EXCL), so the bit assembly lives in the per-OS open_flags_for helpers below; a method body can't be ![target(...)]-gated, but a free function can.

Trait implementations

implement trait Seek for struct File
implement trait Drop for struct File
implement trait Read for struct File
implement trait Write for struct File

flush is a no-op: the file is unbuffered at this layer, and write_some goes straight to write(2).

Free Functions

Whole-file helpers

FunctionNotes
read(p)The whole file as bytes.
read_to_string(p)Bytes taken verbatim, no UTF-8 validation.
write(p, bytes)Creates or truncates.
copy(from, to)Bytes copied.

copy creates to if missing, truncates it if present, then applies the source's Unix permission bits, mirroring cp. The source must be a regular file — copying a directory fails with InvalidInput. On a mid-copy failure the partially written destination is left in place and the error is returned as-is.

function read(p: Path) -> Result<Array<u8>, IoError>;

Read the entire contents of p into a fresh Array<u8>. Panics on allocation failure; caller handles I/O failure via the Result.

function read_to_string(p: Path) -> Result<String, IoError>;

Read the entire contents of p into a fresh String. The bytes are taken verbatim; no UTF-8 validation is performed (the caller is responsible for that if it matters). Panics on allocation failure; I/O failure surfaces through the Result.

function write(p: Path, bytes: Slice<u8>) -> Result<(), IoError>;

Write bytes to p, creating or truncating.

function copy(from: Path, to: Path) -> Result<u64, IoError>;

Copy the contents of from to to, returning the number of bytes copied. to is created if missing and truncated if it already exists; the source's Unix permission bits are then applied to it, mirroring cp (and std::fs::copy). from must be a regular file - copying a directory (or other non-regular entry) fails with InvalidInput. On an I/O failure mid-copy the partially written destination is left in place and the error is returned as-is.

Constants

const DEFAULT_CREATE_MODE: u32 = 420;