Struct tempfile::NamedTempFile[][src]

pub struct NamedTempFile { /* fields omitted */ }

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.

Methods

impl NamedTempFile
[src]

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()?;

Important traits for &'a File

Get a reference to the underlying file.

Important traits for &'a File

Get a mutable reference to the underlying file.

Important traits for &'a 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

impl Debug for NamedTempFile
[src]

Formats the value using the given formatter. Read more

impl AsRef<Path> for NamedTempFile
[src]

Performs the conversion.

impl From<PersistError> for NamedTempFile
[src]

Important traits for NamedTempFile

Performs the conversion.

impl Read for NamedTempFile
[src]

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader can work with buffers of uninitialized memory. 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

Important traits for &'a mut R

Creates a "by reference" adaptor for this instance of Read. Read more

Important traits for Bytes<R>

Transforms this Read instance to an [Iterator] over its bytes. Read more

Important traits for Chars<R>

Deprecated since 1.27.0

: Use str::from_utf8 instead: https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples

🔬 This is a nightly-only experimental API. (io)

the semantics of a partial read/write of where errors happen is currently unclear and may change

Transforms this Read instance to an [Iterator] over [char]s. Read more

Important traits for Chain<T, U>

Creates an adaptor which will chain this stream with another. Read more

Important traits for Take<T>

Creates an adaptor which will read at most limit bytes from it. Read more

impl<'a> Read for &'a NamedTempFile
[src]

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader can work with buffers of uninitialized memory. 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

Important traits for &'a mut R

Creates a "by reference" adaptor for this instance of Read. Read more

Important traits for Bytes<R>

Transforms this Read instance to an [Iterator] over its bytes. Read more

Important traits for Chars<R>

Deprecated since 1.27.0

: Use str::from_utf8 instead: https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples

🔬 This is a nightly-only experimental API. (io)

the semantics of a partial read/write of where errors happen is currently unclear and may change

Transforms this Read instance to an [Iterator] over [char]s. Read more

Important traits for Chain<T, U>

Creates an adaptor which will chain this stream with another. Read more

Important traits for Take<T>

Creates an adaptor which will read at most limit bytes from it. Read more

impl Write for NamedTempFile
[src]

Write a buffer into this object, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Attempts to write an entire buffer into this write. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Important traits for &'a mut R

Creates a "by reference" adaptor for this instance of Write. Read more

impl<'a> Write for &'a NamedTempFile
[src]

Write a buffer into this object, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Attempts to write an entire buffer into this write. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Important traits for &'a mut R

Creates a "by reference" adaptor for this instance of Write. Read more

impl Seek for NamedTempFile
[src]

Seek to an offset, in bytes, in a stream. Read more

impl<'a> Seek for &'a NamedTempFile
[src]

Seek to an offset, in bytes, in a stream. Read more

impl AsRawFd for NamedTempFile
[src]

Extracts the raw file descriptor. Read more

Auto Trait Implementations