TemporaryFile structv0.1.0

Source
A temporary file that owns both its contents and its name.
pub struct TemporaryFile {
    pub file: File;
    // private fields
}

Move-only. Close deletes the file and reports what the deletion said; destruction does the same with the outcome discarded. Keep gives up ownership of the name instead, for the caller who has decided the file should outlive this value — which is what a rename into place is built on.

Fields

NameTypeDescription
fileFileThe open file.

Associated functions

Create

pub func Create(
    allocator: Allocator,
    directory: Path,
    prefix: char8[..]
) -> TemporaryFile ! IoError

Creates a new temporary file in directory, named from prefix and sixteen hex digits of entropy. The file is opened for reading and writing and is created, never opened: a name that already exists is a collision, and the next name is tried.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
directoryPaththe directory to create it in
prefixchar8[..]the text the generated name starts with

Returns: the created file, which is removed when closed unless kept

Source

Methods

AsPath

pub func AsPath(self: &TemporaryFile) -> Path

The file's path, for handing to something that needs a name.

Returns: the path it was created at

Source

Close

pub func Close(
    self: &var TemporaryFile,
    allocator: Allocator
) -> ! IoError

Closes the file and deletes it, reporting the first thing that went wrong.

NameTypeDescription
allocatorAllocatorwhere the storage comes from

Returns: success when it was removed

Source

Keep

pub func Keep(self: &var TemporaryFile) -> ! IoError

Closes the file and leaves it where it is, giving up the name.

Returns: success when it will be left in place rather than removed

Source

Destructor

func ~TemporaryFile(self: &var TemporaryFile)

Closes and deletes with the outcome discarded: the safety net, not the contract. A destructor has nowhere to report a failed deletion, so a caller who needs to know calls Close. What this guarantees is that a temporary file is never left behind by an early return or a failure path, which is the reason the type exists.

Source

TemporaryNameAttempts

pub const TemporaryNameAttempts: uint = 4;

How many names are tried before a temporary file gives up. With 128 bits of name, one collision is already a surprise and three in a row is not chance; failing is the honest response to an entropy source that has stopped being one.

Source

TemporaryDirectory

pub func TemporaryDirectory(allocator: Allocator) -> PathBuffer ! IoError

The directory the system sets aside for temporary files. On Windows this is what the platform reports, which consults TMP, TEMP and USERPROFILE in turn. On the Unix systems it is /tmp: reading TMPDIR needs environment access this package does not have yet, so a caller who must honor it passes the directory to the functions below instead.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhere the storage comes from

Returns: the system's directory for temporary files