[−][src]Struct tempfile::NamedTempFile
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]
impl NamedTempFile
pub fn new() -> Result<NamedTempFile>
[src]
pub fn new() -> Result<NamedTempFile>
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:
-
The file has a short lifetime and your temporary file cleaner is sane (doesn't delete recently accessed files).
-
You trust every user on your system (i.e. you are the only user).
-
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:
-
You'll fix it later. No you won't.
-
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.")?;
pub fn new_in<P: AsRef<Path>>(dir: P) -> Result<NamedTempFile>
[src]
pub fn new_in<P: AsRef<Path>>(dir: P) -> Result<NamedTempFile>
Create a new named temporary file in the specified directory.
See NamedTempFile::new()
for details.
pub fn path(&self) -> &Path
[src]
pub fn path(&self) -> &Path
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());
pub fn close(self) -> Result<()>
[src]
pub fn close(self) -> Result<()>
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()?;
pub fn persist<P: AsRef<Path>>(self, new_path: P) -> Result<File, PersistError>
[src]
pub fn persist<P: AsRef<Path>>(self, new_path: P) -> Result<File, PersistError>
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.")?;
pub fn persist_noclobber<P: AsRef<Path>>(
self,
new_path: P
) -> Result<File, PersistError>
[src]
pub fn persist_noclobber<P: AsRef<Path>>(
self,
new_path: P
) -> Result<File, PersistError>
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.")?;
pub fn reopen(&self) -> Result<File>
[src]
pub fn reopen(&self) -> Result<File>
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 File
s returned by this function; the File
s 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()?;
pub fn as_file(&self) -> &File
[src]
pub fn as_file(&self) -> &File
Get a reference to the underlying file.
pub fn as_file_mut(&mut self) -> &mut File
[src]
pub fn as_file_mut(&mut self) -> &mut File
Get a mutable reference to the underlying file.
pub fn into_file(self) -> File
[src]
pub fn into_file(self) -> File
Convert the temporary file into a std::fs::File
.
The inner file will be deleted.
pub fn into_temp_path(self) -> TempPath
[src]
pub fn into_temp_path(self) -> TempPath
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 From<PersistError> for NamedTempFile
[src]
impl From<PersistError> for NamedTempFile
ⓘImportant traits for NamedTempFilefn from(error: PersistError) -> NamedTempFile
[src]
fn from(error: PersistError) -> NamedTempFile
Performs the conversion.
impl AsRef<Path> for NamedTempFile
[src]
impl AsRef<Path> for NamedTempFile
impl Debug for NamedTempFile
[src]
impl Debug for NamedTempFile
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl Read for NamedTempFile
[src]
impl Read for NamedTempFile
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
unsafe fn initializer(&self) -> Initializer
[src]
unsafe fn initializer(&self) -> Initializer
read_initializer
)Determines if this Read
er can work with buffers of uninitialized memory. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0[src]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn bytes(self) -> Bytes<Self>
Transforms this Read
instance to an [Iterator
] over its bytes. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit
bytes from it. Read more
impl<'a> Read for &'a NamedTempFile
[src]
impl<'a> Read for &'a NamedTempFile
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
unsafe fn initializer(&self) -> Initializer
[src]
unsafe fn initializer(&self) -> Initializer
read_initializer
)Determines if this Read
er can work with buffers of uninitialized memory. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0[src]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn bytes(self) -> Bytes<Self>
Transforms this Read
instance to an [Iterator
] over its bytes. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit
bytes from it. Read more
impl Seek for NamedTempFile
[src]
impl Seek for NamedTempFile
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
[src]
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to an offset, in bytes, in a stream. Read more
impl<'a> Seek for &'a NamedTempFile
[src]
impl<'a> Seek for &'a NamedTempFile
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
[src]
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to an offset, in bytes, in a stream. Read more
impl Write for NamedTempFile
[src]
impl Write for NamedTempFile
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<()>
[src]
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0[src]
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Write
. Read more
impl<'a> Write for &'a NamedTempFile
[src]
impl<'a> Write for &'a NamedTempFile
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<()>
[src]
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0[src]
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Write
. Read more
impl AsRawFd for NamedTempFile
[src]
impl AsRawFd for NamedTempFile
Auto Trait Implementations
impl Send for NamedTempFile
impl Send for NamedTempFile
impl Sync for NamedTempFile
impl Sync for NamedTempFile
Blanket Implementations
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,