Struct tempfile::NamedTempFile

source ·
pub struct NamedTempFile { /* private fields */ }
Expand description

A named temporary file.

The default constructor, NamedTempFile::new(), creates files in the location returned by std::env::temp_dir(), but NamedTempFile can be configured to manage a temporary file in any location by constructing with NamedTempFile::new_in().

Security

This variant is NOT secure/reliable in the presence of a pathological temporary file cleaner.

Resource Leaking

If the program exits before the NamedTempFile destructor is run, such as via std::process::exit(), by segfaulting, or by receiving a signal like SIGINT, then the temporary file will not be deleted.

Use the tempfile() function unless you absolutely need a named file.

Implementations

Create a new named temporary file.

See Builder for more configuration.

Security

This will create a temporary file in the default temporary file directory (platform dependent). These directories are often patrolled by temporary file cleaners so only use this method if you’re positive that the temporary file cleaner won’t delete your file.

Reasons to use this method:

  1. The file has a short lifetime and your temporary file cleaner is sane (doesn’t delete recently accessed files).

  2. You trust every user on your system (i.e. you are the only user).

  3. You have disabled your system’s temporary file cleaner or verified that your system doesn’t have a temporary file cleaner.

Reasons not to use this method:

  1. You’ll fix it later. No you won’t.

  2. You don’t care about the security of the temporary file. If none of the “reasons to use this method” apply, referring to a temporary file by name may allow an attacker to create/overwrite your non-temporary files. There are exceptions but if you don’t already know them, don’t use this method.

Errors

If the file can not be created, Err is returned.

Examples

Create a named temporary file and write some data to it:

use tempfile::NamedTempFile;

let mut file = NamedTempFile::new()?;

writeln!(file, "Brian was here. Briefly.")?;

Create a new named temporary file in the specified directory.

See NamedTempFile::new() for details.

Get the temporary file’s path.

Security

Only use this method if you’re positive that a temporary file cleaner won’t have deleted your file. Otherwise, the path returned by this method may refer to an attacker controlled file.

Examples
use tempfile::NamedTempFile;

let file = NamedTempFile::new()?;

println!("{:?}", file.path());

Close and remove the temporary file.

Use this if you want to detect errors in deleting the file.

Errors

If the file cannot be deleted, Err is returned.

Examples
use tempfile::NamedTempFile;

let file = NamedTempFile::new()?;

// By closing the `NamedTempFile` explicitly, we can check that it has
// been deleted successfully. If we don't close it explicitly,
// the file will still be deleted when `file` goes out
// of scope, but we won't know whether deleting the file
// succeeded.
file.close()?;

Persist the temporary file at the target path.

If a file exists at the target path, persist will atomically replace it. If this method fails, it will return self in the resulting PersistError.

Note: Temporary files cannot be persisted across filesystems.

Security

Only use this method if you’re positive that a temporary file cleaner won’t have deleted your file. Otherwise, you might end up persisting an attacker controlled file.

Errors

If the file cannot be moved to the new location, Err is returned.

Examples
use tempfile::NamedTempFile;

let file = NamedTempFile::new()?;

let mut persisted_file = file.persist("./saved_file.txt")?;
writeln!(persisted_file, "Brian was here. Briefly.")?;

Persist the temporary file at the target path iff no file exists there.

If a file exists at the target path, fail. If this method fails, it will return self in the resulting PersistError.

Note: Temporary files cannot be persisted across filesystems. Also Note: This method is not atomic. It can leave the original link to the temporary file behind.

Security

Only use this method if you’re positive that a temporary file cleaner won’t have deleted your file. Otherwise, you might end up persisting an attacker controlled file.

Errors

If the file cannot be moved to the new location or a file already exists there, Err is returned.

Examples
use tempfile::NamedTempFile;

let file = NamedTempFile::new()?;

let mut persisted_file = file.persist_noclobber("./saved_file.txt")?;
writeln!(persisted_file, "Brian was here. Briefly.")?;

Reopen the temporary file.

This function is useful when you need multiple independent handles to the same file. It’s perfectly fine to drop the original NamedTempFile while holding on to Files returned by this function; the Files will remain usable. However, they may not be nameable.

Errors

If the file cannot be reopened, Err is returned.

Examples
use tempfile::NamedTempFile;

let file = NamedTempFile::new()?;

let another_handle = file.reopen()?;

Get a reference to the underlying file.

Get a mutable reference to the underlying file.

Convert the temporary file into a std::fs::File.

The inner file will be deleted.

Closes the file, leaving only the temporary file path.

This is useful when another process must be able to open the temporary file.

Trait Implementations

Extracts the raw file descriptor. Read more
Converts this type into a shared reference of the (usually inferred) input type.
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Like read, except that it reads into a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
Read all bytes until EOF in this source, placing them into buf. Read more
Read all bytes until EOF in this source, appending them to buf. Read more
Read the exact number of bytes required to fill buf. Read more
🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
Creates a “by reference” adaptor for this instance of Read. Read more
Transforms this Read instance to an Iterator over its bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Like read, except that it reads into a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
Read all bytes until EOF in this source, placing them into buf. Read more
Read all bytes until EOF in this source, appending them to buf. Read more
Read the exact number of bytes required to fill buf. Read more
🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
Creates a “by reference” adaptor for this instance of Read. Read more
Transforms this Read instance to an Iterator over its bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Seek to an offset, in bytes, in a stream. Read more
Rewind to the beginning of a stream. Read more
🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
Returns the current seek position from the start of the stream. Read more
Seek to an offset, in bytes, in a stream. Read more
Rewind to the beginning of a stream. Read more
🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
Returns the current seek position from the start of the stream. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Like write, except that it writes from a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Attempts to write an entire buffer into this writer. Read more
🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Creates a “by reference” adapter for this instance of Write. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Like write, except that it writes from a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Attempts to write an entire buffer into this writer. Read more
🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.