tempfile/
lib.rs

1//! This is a library for creating temporary files and directories that are automatically deleted
2//! when no longer referenced (i.e., on drop).
3//!
4//! - Use [`tempfile()`] when you need a real [`std::fs::File`] but don't need to refer to it
5//!   by-path.
6//! - Use [`NamedTempFile::new()`] when you need a _named_ temporary file that can be refered to its
7//!   path.
8//! - Use [`tempdir()`] when you need a temporary directory that will be recursively deleted on drop.
9//! - Use [`spooled_tempfile()`] when you need an in-memory buffer that will ultimately be backed by
10//!   a temporary file if it gets too large.
11//!
12//! # Design
13//!
14//! This crate provides several approaches to creating temporary files and directories.
15//! [`tempfile()`] relies on the OS to remove the temporary file once the last handle is closed.
16//! [`TempDir`] and [`NamedTempFile`] both rely on Rust destructors for cleanup.
17//!
18//! ## Resource Leaking
19//!
20//! `tempfile` will (almost) never fail to cleanup temporary resources. However `TempDir` and
21//! `NamedTempFile` will fail if their destructors don't run. This is because `tempfile` relies on
22//! the OS to cleanup the underlying file, while `TempDir` and `NamedTempFile` rely on rust
23//! destructors to do so. Destructors may fail to run if the process exits through an unhandled
24//! signal interrupt (like `SIGINT`), or if the instance is declared statically (like with
25//! [`lazy_static`]), among other possible reasons.
26//!
27//! ## Unexpected File Deletion
28//!
29//! Most operating systems periodically clean up temporary files that haven't been accessed recently
30//! (often on the order of multiple days). This issue does not affect unnamed temporary files but
31//! can invalidate the paths associated with named temporary files on Unix-like systems because the
32//! temporary file can be unlinked from the filesystem while still open and in-use. See the
33//! [temporary file cleaner](#temporary-file-cleaners) section for more security implications.
34//!
35//! ## Security
36//!
37//! This section discusses security issues relevant to Unix-like operating systems that use shared
38//! temporary directories by default. Importantly, it's not relevant for Windows or macOS as both
39//! operating systems use private per-user temporary directories by default.
40//!
41//! Applications can mitigate the issues described below by using [`env::override_temp_dir`] to
42//! change the default temporary directory but should do so if and only if default the temporary
43//! directory ([`env::temp_dir`]) is unsuitable (is world readable, world writable, managed by a
44//! temporary file cleaner, etc.).
45//!
46//! ### Temporary File Cleaners
47//!
48//! In the presence of pathological temporary file cleaner, relying on file paths is unsafe because
49//! a temporary file cleaner could delete the temporary file which an attacker could then replace.
50//!
51//! This isn't an issue for [`tempfile`] as it doesn't rely on file paths. However, [`NamedTempFile`]
52//! and temporary directories _do_ rely on file paths for _some_ operations. See the security
53//! documentation on the [`NamedTempFile`] and the [`TempDir`] types for more information.
54//!
55//! Mitigation:
56//!
57//! - This is rarely an issue for short-lived files as temporary file cleaners usually only remove
58//!   temporary files that haven't been modified or accessed within many (10-30) days.
59//! - Very long lived temporary files should be placed in directories not managed by temporary file
60//!   cleaners.
61//!
62//! ### Access Permissions
63//!
64//! Temporary _files_ created with this library are private by default on all operating systems.
65//! However, temporary _directories_ are created with the default permissions and will therefore be
66//! world-readable by default unless the user has changed their umask and/or default temporary
67//! directory.
68//!
69//! ### Denial of Service
70//!
71//! If the file-name randomness ([`Builder::rand_bytes`]) is too small and/or this crate is built
72//! without the `getrandom` feature, it may be possible for an attacker to predict the random file
73//! names chosen by this library, preventing temporary file creation by creating temporary files
74//! with these predicted file names. By default, this library mitigates this denial of service
75//! attack by:
76//!
77//! 1. Defaulting to 6 random characters per temporary file forcing an attacker to create billions
78//!    of files before random collisions are expected (at which point you probably have larger
79//!    problems).
80//! 2. Re-seeding the random filename generator from system randomness after 3 failed attempts to
81//!    create temporary a file (when the `getrandom` feature is enabled as it is by default on all
82//!    major platforms).
83//!
84//! ## Early drop pitfall
85//!
86//! Because `TempDir` and `NamedTempFile` rely on their destructors for cleanup, this can lead
87//! to an unexpected early removal of the directory/file, usually when working with APIs which are
88//! generic over `AsRef<Path>`. Consider the following example:
89//!
90//! ```no_run
91//! use tempfile::tempdir;
92//! use std::process::Command;
93//!
94//! // Create a directory inside of `env::temp_dir()`.
95//! let temp_dir = tempdir()?;
96//!
97//! // Spawn the `touch` command inside the temporary directory and collect the exit status
98//! // Note that `temp_dir` is **not** moved into `current_dir`, but passed as a reference
99//! let exit_status = Command::new("touch").arg("tmp").current_dir(&temp_dir).status()?;
100//! assert!(exit_status.success());
101//!
102//! # Ok::<(), std::io::Error>(())
103//! ```
104//!
105//! This works because a reference to `temp_dir` is passed to `current_dir`, resulting in the
106//! destructor of `temp_dir` being run after the `Command` has finished execution. Moving the
107//! `TempDir` into the `current_dir` call would result in the `TempDir` being converted into
108//! an internal representation, with the original value being dropped and the directory thus
109//! being deleted, before the command can be executed.
110//!
111//! The `touch` command would fail with an `No such file or directory` error.
112//!
113//! ## Examples
114//!
115//! Create a temporary file and write some data into it:
116//!
117//! ```
118//! use tempfile::tempfile;
119//! use std::io::Write;
120//!
121//! // Create a file inside of `env::temp_dir()`.
122//! let mut file = tempfile()?;
123//!
124//! writeln!(file, "Brian was here. Briefly.")?;
125//! # Ok::<(), std::io::Error>(())
126//! ```
127//!
128//! Create a named temporary file and open an independent file handle:
129//!
130//! ```
131//! use tempfile::NamedTempFile;
132//! use std::io::{Write, Read};
133//!
134//! let text = "Brian was here. Briefly.";
135//!
136//! // Create a file inside of `env::temp_dir()`.
137//! let mut file1 = NamedTempFile::new()?;
138//!
139//! // Re-open it.
140//! let mut file2 = file1.reopen()?;
141//!
142//! // Write some test data to the first handle.
143//! file1.write_all(text.as_bytes())?;
144//!
145//! // Read the test data using the second handle.
146//! let mut buf = String::new();
147//! file2.read_to_string(&mut buf)?;
148//! assert_eq!(buf, text);
149//! # Ok::<(), std::io::Error>(())
150//! ```
151//!
152//! Create a temporary directory and add a file to it:
153//!
154//! ```
155//! use tempfile::tempdir;
156//! use std::fs::File;
157//! use std::io::Write;
158//!
159//! // Create a directory inside of `env::temp_dir()`.
160//! let dir = tempdir()?;
161//!
162//! let file_path = dir.path().join("my-temporary-note.txt");
163//! let mut file = File::create(file_path)?;
164//! writeln!(file, "Brian was here. Briefly.")?;
165//!
166//! // By closing the `TempDir` explicitly, we can check that it has
167//! // been deleted successfully. If we don't close it explicitly,
168//! // the directory will still be deleted when `dir` goes out
169//! // of scope, but we won't know whether deleting the directory
170//! // succeeded.
171//! drop(file);
172//! dir.close()?;
173//! # Ok::<(), std::io::Error>(())
174//! ```
175//!
176//! [`tempfile()`]: fn.tempfile.html
177//! [`tempdir()`]: fn.tempdir.html
178//! [`TempDir`]: struct.TempDir.html
179//! [`NamedTempFile`]: struct.NamedTempFile.html
180//! [`lazy_static`]: https://github.com/rust-lang-nursery/lazy-static.rs/issues/62
181
182#![doc(
183    html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
184    html_favicon_url = "https://www.rust-lang.org/favicon.ico",
185    html_root_url = "https://docs.rs/tempfile/latest"
186)]
187#![cfg_attr(test, deny(warnings))]
188#![deny(rust_2018_idioms)]
189#![allow(clippy::redundant_field_names)]
190// wasip2 conditionally gates stdlib APIs.
191// https://github.com/rust-lang/rust/issues/130323
192#![cfg_attr(
193    all(feature = "nightly", target_os = "wasi", target_env = "p2"),
194    feature(wasip2)
195)]
196#![cfg_attr(all(feature = "nightly", target_os = "wasi"), feature(wasi_ext))]
197
198#[cfg(doctest)]
199doc_comment::doctest!("../README.md");
200
201const NUM_RETRIES: u32 = 65536;
202const NUM_RAND_CHARS: usize = 6;
203
204use std::ffi::OsStr;
205use std::fs::OpenOptions;
206use std::io;
207use std::path::Path;
208
209mod dir;
210mod error;
211mod file;
212mod spooled;
213mod util;
214
215pub mod env;
216
217pub use crate::dir::{tempdir, tempdir_in, TempDir};
218pub use crate::file::{
219    tempfile, tempfile_in, NamedTempFile, PathPersistError, PersistError, TempPath,
220};
221pub use crate::spooled::{spooled_tempfile, spooled_tempfile_in, SpooledData, SpooledTempFile};
222
223/// Create a new temporary file or directory with custom options.
224#[derive(Debug, Clone, Eq, PartialEq)]
225pub struct Builder<'a, 'b> {
226    random_len: usize,
227    prefix: &'a OsStr,
228    suffix: &'b OsStr,
229    append: bool,
230    permissions: Option<std::fs::Permissions>,
231    disable_cleanup: bool,
232}
233
234impl Default for Builder<'_, '_> {
235    fn default() -> Self {
236        Builder {
237            random_len: crate::NUM_RAND_CHARS,
238            prefix: OsStr::new(".tmp"),
239            suffix: OsStr::new(""),
240            append: false,
241            permissions: None,
242            disable_cleanup: false,
243        }
244    }
245}
246
247impl<'a, 'b> Builder<'a, 'b> {
248    /// Create a new `Builder`.
249    ///
250    /// # Examples
251    ///
252    /// Create a named temporary file and write some data into it:
253    ///
254    /// ```
255    /// use std::ffi::OsStr;
256    /// use tempfile::Builder;
257    ///
258    /// let named_tempfile = Builder::new()
259    ///     .prefix("my-temporary-note")
260    ///     .suffix(".txt")
261    ///     .rand_bytes(5)
262    ///     .tempfile()?;
263    ///
264    /// let name = named_tempfile
265    ///     .path()
266    ///     .file_name().and_then(OsStr::to_str);
267    ///
268    /// if let Some(name) = name {
269    ///     assert!(name.starts_with("my-temporary-note"));
270    ///     assert!(name.ends_with(".txt"));
271    ///     assert_eq!(name.len(), "my-temporary-note.txt".len() + 5);
272    /// }
273    /// # Ok::<(), std::io::Error>(())
274    /// ```
275    ///
276    /// Create a temporary directory and add a file to it:
277    ///
278    /// ```
279    /// use std::io::Write;
280    /// use std::fs::File;
281    /// use std::ffi::OsStr;
282    /// use tempfile::Builder;
283    ///
284    /// let dir = Builder::new()
285    ///     .prefix("my-temporary-dir")
286    ///     .rand_bytes(5)
287    ///     .tempdir()?;
288    ///
289    /// let file_path = dir.path().join("my-temporary-note.txt");
290    /// let mut file = File::create(file_path)?;
291    /// writeln!(file, "Brian was here. Briefly.")?;
292    ///
293    /// // By closing the `TempDir` explicitly, we can check that it has
294    /// // been deleted successfully. If we don't close it explicitly,
295    /// // the directory will still be deleted when `dir` goes out
296    /// // of scope, but we won't know whether deleting the directory
297    /// // succeeded.
298    /// drop(file);
299    /// dir.close()?;
300    /// # Ok::<(), std::io::Error>(())
301    /// ```
302    ///
303    /// Create a temporary directory with a chosen prefix under a chosen folder:
304    ///
305    /// ```no_run
306    /// use tempfile::Builder;
307    ///
308    /// let dir = Builder::new()
309    ///     .prefix("my-temporary-dir")
310    ///     .tempdir_in("folder-with-tempdirs")?;
311    /// # Ok::<(), std::io::Error>(())
312    /// ```
313    #[must_use]
314    pub fn new() -> Self {
315        Self::default()
316    }
317
318    /// Set a custom filename prefix.
319    ///
320    /// Path separators are legal but not advisable.
321    /// Default: `.tmp`.
322    ///
323    /// # Examples
324    ///
325    /// ```
326    /// use tempfile::Builder;
327    ///
328    /// let named_tempfile = Builder::new()
329    ///     .prefix("my-temporary-note")
330    ///     .tempfile()?;
331    /// # Ok::<(), std::io::Error>(())
332    /// ```
333    pub fn prefix<S: AsRef<OsStr> + ?Sized>(&mut self, prefix: &'a S) -> &mut Self {
334        self.prefix = prefix.as_ref();
335        self
336    }
337
338    /// Set a custom filename suffix.
339    ///
340    /// Path separators are legal but not advisable.
341    /// Default: empty.
342    ///
343    /// # Examples
344    ///
345    /// ```
346    /// use tempfile::Builder;
347    ///
348    /// let named_tempfile = Builder::new()
349    ///     .suffix(".txt")
350    ///     .tempfile()?;
351    /// # Ok::<(), std::io::Error>(())
352    /// ```
353    pub fn suffix<S: AsRef<OsStr> + ?Sized>(&mut self, suffix: &'b S) -> &mut Self {
354        self.suffix = suffix.as_ref();
355        self
356    }
357
358    /// Set the number of random bytes.
359    ///
360    /// Default: `6`.
361    ///
362    /// # Examples
363    ///
364    /// ```
365    /// use tempfile::Builder;
366    ///
367    /// let named_tempfile = Builder::new()
368    ///     .rand_bytes(5)
369    ///     .tempfile()?;
370    /// # Ok::<(), std::io::Error>(())
371    /// ```
372    pub fn rand_bytes(&mut self, rand: usize) -> &mut Self {
373        self.random_len = rand;
374        self
375    }
376
377    /// Set the file to be opened in append mode.
378    ///
379    /// Default: `false`.
380    ///
381    /// # Examples
382    ///
383    /// ```
384    /// use tempfile::Builder;
385    ///
386    /// let named_tempfile = Builder::new()
387    ///     .append(true)
388    ///     .tempfile()?;
389    /// # Ok::<(), std::io::Error>(())
390    /// ```
391    pub fn append(&mut self, append: bool) -> &mut Self {
392        self.append = append;
393        self
394    }
395
396    /// The permissions to create the tempfile or [tempdir](Self::tempdir) with.
397    ///
398    /// # Security
399    ///
400    /// By default, the permissions of tempfiles on Unix are set for it to be
401    /// readable and writable by the owner only, yielding the greatest amount
402    /// of security.
403    /// As this method allows to widen the permissions, security would be
404    /// reduced in such cases.
405    ///
406    /// # Platform Notes
407    /// ## Unix
408    ///
409    /// The actual permission bits set on the tempfile or tempdir will be affected by the `umask`
410    /// applied by the underlying syscall. The actual permission bits are calculated via
411    /// `permissions & !umask`.
412    ///
413    /// Permissions default to `0o600` for tempfiles and `0o777` for tempdirs. Note, this doesn't
414    /// include effects of the current `umask`. For example, combined with the standard umask
415    /// `0o022`, the defaults yield `0o600` for tempfiles and `0o755` for tempdirs.
416    ///
417    /// ## Windows and others
418    ///
419    /// This setting is unsupported and trying to set a file or directory read-only
420    /// will return an error.
421    ///
422    /// # Examples
423    ///
424    /// Create a named temporary file that is world-readable.
425    ///
426    /// ```
427    /// # #[cfg(unix)]
428    /// # {
429    /// use tempfile::Builder;
430    /// use std::os::unix::fs::PermissionsExt;
431    ///
432    /// let all_read_write = std::fs::Permissions::from_mode(0o666);
433    /// let tempfile = Builder::new().permissions(all_read_write).tempfile()?;
434    /// let actual_permissions = tempfile.path().metadata()?.permissions();
435    /// assert_ne!(
436    ///     actual_permissions.mode() & !0o170000,
437    ///     0o600,
438    ///     "we get broader permissions than the default despite umask"
439    /// );
440    /// # }
441    /// # Ok::<(), std::io::Error>(())
442    /// ```
443    ///
444    /// Create a named temporary directory that is restricted to the owner.
445    ///
446    /// ```
447    /// # #[cfg(unix)]
448    /// # {
449    /// use tempfile::Builder;
450    /// use std::os::unix::fs::PermissionsExt;
451    ///
452    /// let owner_rwx = std::fs::Permissions::from_mode(0o700);
453    /// let tempdir = Builder::new().permissions(owner_rwx).tempdir()?;
454    /// let actual_permissions = tempdir.path().metadata()?.permissions();
455    /// assert_eq!(
456    ///     actual_permissions.mode() & !0o170000,
457    ///     0o700,
458    ///     "we get the narrow permissions we asked for"
459    /// );
460    /// # }
461    /// # Ok::<(), std::io::Error>(())
462    /// ```
463    pub fn permissions(&mut self, permissions: std::fs::Permissions) -> &mut Self {
464        self.permissions = Some(permissions);
465        self
466    }
467
468    /// Disable cleanup of the file/folder to even when the [`NamedTempFile`]/[`TempDir`] goes out
469    /// of scope. Prefer [`NamedTempFile::keep`] and `[`TempDir::keep`] where possible,
470    /// `disable_cleanup` is provided for testing & debugging.
471    ///
472    /// By default, the file/folder is automatically cleaned up in the destructor of
473    /// [`NamedTempFile`]/[`TempDir`]. When `disable_cleanup` is set to `true`, this behavior is
474    /// suppressed. If you wish to disable cleanup after creating a temporary file/directory, call
475    /// [`NamedTempFile::disable_cleanup`] or [`TempDir::disable_cleanup`].
476    ///
477    /// # Warnings
478    ///
479    /// On some platforms (for now, only Windows), temporary files are marked with a special
480    /// "temporary file" (`FILE_ATTRIBUTE_TEMPORARY`) attribute. Disabling cleanup _will not_ unset
481    /// this attribute while calling [`NamedTempFile::keep`] will.
482    ///
483    /// # Examples
484    ///
485    /// ```
486    /// use tempfile::Builder;
487    ///
488    /// let named_tempfile = Builder::new()
489    ///     .disable_cleanup(true)
490    ///     .tempfile()?;
491    /// # Ok::<(), std::io::Error>(())
492    /// ```
493    pub fn disable_cleanup(&mut self, disable_cleanup: bool) -> &mut Self {
494        self.disable_cleanup = disable_cleanup;
495        self
496    }
497
498    /// Deprecated alias for [`Builder::disable_cleanup`].
499    #[deprecated = "Use Builder::disable_cleanup"]
500    pub fn keep(&mut self, keep: bool) -> &mut Self {
501        self.disable_cleanup(keep)
502    }
503
504    /// Create the named temporary file.
505    ///
506    /// # Security
507    ///
508    /// See [the security][security] docs on `NamedTempFile`.
509    ///
510    /// # Resource leaking
511    ///
512    /// See [the resource leaking][resource-leaking] docs on `NamedTempFile`.
513    ///
514    /// # Errors
515    ///
516    /// If the file cannot be created, `Err` is returned.
517    ///
518    /// # Examples
519    ///
520    /// ```
521    /// use tempfile::Builder;
522    ///
523    /// let tempfile = Builder::new().tempfile()?;
524    /// # Ok::<(), std::io::Error>(())
525    /// ```
526    ///
527    /// [security]: struct.NamedTempFile.html#security
528    /// [resource-leaking]: struct.NamedTempFile.html#resource-leaking
529    pub fn tempfile(&self) -> io::Result<NamedTempFile> {
530        self.tempfile_in(env::temp_dir())
531    }
532
533    /// Create the named temporary file in the specified directory.
534    ///
535    /// # Security
536    ///
537    /// See [the security][security] docs on `NamedTempFile`.
538    ///
539    /// # Resource leaking
540    ///
541    /// See [the resource leaking][resource-leaking] docs on `NamedTempFile`.
542    ///
543    /// # Errors
544    ///
545    /// If the file cannot be created, `Err` is returned.
546    ///
547    /// # Examples
548    ///
549    /// ```
550    /// use tempfile::Builder;
551    ///
552    /// let tempfile = Builder::new().tempfile_in("./")?;
553    /// # Ok::<(), std::io::Error>(())
554    /// ```
555    ///
556    /// [security]: struct.NamedTempFile.html#security
557    /// [resource-leaking]: struct.NamedTempFile.html#resource-leaking
558    pub fn tempfile_in<P: AsRef<Path>>(&self, dir: P) -> io::Result<NamedTempFile> {
559        util::create_helper(
560            dir.as_ref(),
561            self.prefix,
562            self.suffix,
563            self.random_len,
564            |path| {
565                file::create_named(
566                    path,
567                    OpenOptions::new().append(self.append),
568                    self.permissions.as_ref(),
569                    self.disable_cleanup,
570                )
571            },
572        )
573    }
574
575    /// Attempts to make a temporary directory inside of [`env::temp_dir()`] whose
576    /// name will have the prefix, `prefix`. The directory and
577    /// everything inside it will be automatically deleted once the
578    /// returned `TempDir` is destroyed.
579    ///
580    /// # Resource leaking
581    ///
582    /// See [the resource leaking][resource-leaking] docs on `TempDir`.
583    ///
584    /// # Errors
585    ///
586    /// If the directory can not be created, `Err` is returned.
587    ///
588    /// # Examples
589    ///
590    /// ```
591    /// use tempfile::Builder;
592    ///
593    /// let tmp_dir = Builder::new().tempdir()?;
594    /// # Ok::<(), std::io::Error>(())
595    /// ```
596    ///
597    /// [resource-leaking]: struct.TempDir.html#resource-leaking
598    pub fn tempdir(&self) -> io::Result<TempDir> {
599        self.tempdir_in(env::temp_dir())
600    }
601
602    /// Attempts to make a temporary directory inside of `dir`.
603    /// The directory and everything inside it will be automatically
604    /// deleted once the returned `TempDir` is destroyed.
605    ///
606    /// # Resource leaking
607    ///
608    /// See [the resource leaking][resource-leaking] docs on `TempDir`.
609    ///
610    /// # Errors
611    ///
612    /// If the directory can not be created, `Err` is returned.
613    ///
614    /// # Examples
615    ///
616    /// ```
617    /// use tempfile::Builder;
618    ///
619    /// let tmp_dir = Builder::new().tempdir_in("./")?;
620    /// # Ok::<(), std::io::Error>(())
621    /// ```
622    ///
623    /// [resource-leaking]: struct.TempDir.html#resource-leaking
624    pub fn tempdir_in<P: AsRef<Path>>(&self, dir: P) -> io::Result<TempDir> {
625        util::create_helper(
626            dir.as_ref(),
627            self.prefix,
628            self.suffix,
629            self.random_len,
630            |path| dir::create(path, self.permissions.as_ref(), self.disable_cleanup),
631        )
632    }
633
634    /// Attempts to create a temporary file (or file-like object) using the
635    /// provided closure. The closure is passed a temporary file path and
636    /// returns an [`std::io::Result`]. The path provided to the closure will be
637    /// inside of [`env::temp_dir()`]. Use [`Builder::make_in`] to provide
638    /// a custom temporary directory. If the closure returns one of the
639    /// following errors, then another randomized file path is tried:
640    ///  - [`std::io::ErrorKind::AlreadyExists`]
641    ///  - [`std::io::ErrorKind::AddrInUse`]
642    ///
643    /// This can be helpful for taking full control over the file creation, but
644    /// leaving the temporary file path construction up to the library. This
645    /// also enables creating a temporary UNIX domain socket, since it is not
646    /// possible to bind to a socket that already exists.
647    ///
648    /// Note that [`Builder::append`] is ignored when using [`Builder::make`].
649    ///
650    /// # Security
651    ///
652    /// This has the same [security implications][security] as
653    /// [`NamedTempFile`], but with additional caveats. Specifically, it is up
654    /// to the closure to ensure that the file does not exist and that such a
655    /// check is *atomic*. Otherwise, a [time-of-check to time-of-use
656    /// bug][TOCTOU] could be introduced.
657    ///
658    /// For example, the following is **not** secure:
659    ///
660    /// ```
661    /// use std::fs::File;
662    /// use tempfile::Builder;
663    ///
664    /// // This is NOT secure!
665    /// let tempfile = Builder::new().make(|path| {
666    ///     if path.is_file() {
667    ///         return Err(std::io::ErrorKind::AlreadyExists.into());
668    ///     }
669    ///
670    ///     // Between the check above and the usage below, an attacker could
671    ///     // have replaced `path` with another file, which would get truncated
672    ///     // by `File::create`.
673    ///
674    ///     File::create(path)
675    /// })?;
676    /// # Ok::<(), std::io::Error>(())
677    /// ```
678    ///
679    /// Note that simply using [`std::fs::File::create`] alone is not correct
680    /// because it does not fail if the file already exists:
681    ///
682    /// ```
683    /// use tempfile::Builder;
684    /// use std::fs::File;
685    ///
686    /// // This could overwrite an existing file!
687    /// let tempfile = Builder::new().make(|path| File::create(path))?;
688    /// # Ok::<(), std::io::Error>(())
689    /// ```
690    /// For creating regular temporary files, use [`Builder::tempfile`] instead
691    /// to avoid these problems. This function is meant to enable more exotic
692    /// use-cases.
693    ///
694    /// # Resource leaking
695    ///
696    /// See [the resource leaking][resource-leaking] docs on `NamedTempFile`.
697    ///
698    /// # Errors
699    ///
700    /// If the closure returns any error besides
701    /// [`std::io::ErrorKind::AlreadyExists`] or
702    /// [`std::io::ErrorKind::AddrInUse`], then `Err` is returned.
703    ///
704    /// # Examples
705    /// ```
706    /// # #[cfg(unix)]
707    /// # {
708    /// use std::os::unix::net::UnixListener;
709    /// use tempfile::Builder;
710    ///
711    /// let tempsock = Builder::new().make(|path| UnixListener::bind(path))?;
712    /// # }
713    /// # Ok::<(), std::io::Error>(())
714    /// ```
715    ///
716    /// [TOCTOU]: https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use
717    /// [security]: struct.NamedTempFile.html#security
718    /// [resource-leaking]: struct.NamedTempFile.html#resource-leaking
719    pub fn make<F, R>(&self, f: F) -> io::Result<NamedTempFile<R>>
720    where
721        F: FnMut(&Path) -> io::Result<R>,
722    {
723        self.make_in(env::temp_dir(), f)
724    }
725
726    /// This is the same as [`Builder::make`], except `dir` is used as the base
727    /// directory for the temporary file path.
728    ///
729    /// See [`Builder::make`] for more details and security implications.
730    ///
731    /// # Examples
732    /// ```
733    /// # #[cfg(unix)]
734    /// # {
735    /// use tempfile::Builder;
736    /// use std::os::unix::net::UnixListener;
737    ///
738    /// let tempsock = Builder::new().make_in("./", |path| UnixListener::bind(path))?;
739    /// # }
740    /// # Ok::<(), std::io::Error>(())
741    /// ```
742    pub fn make_in<F, R, P>(&self, dir: P, mut f: F) -> io::Result<NamedTempFile<R>>
743    where
744        F: FnMut(&Path) -> io::Result<R>,
745        P: AsRef<Path>,
746    {
747        util::create_helper(
748            dir.as_ref(),
749            self.prefix,
750            self.suffix,
751            self.random_len,
752            move |path| {
753                Ok(NamedTempFile::from_parts(
754                    f(&path)?,
755                    TempPath::new(path, self.disable_cleanup),
756                ))
757            },
758        )
759    }
760}