From 5363f350887b1e5b5dd21a86f88c8af9d7fea6da Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:25 +0200 Subject: Merging upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/tempfile/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'vendor/tempfile/src/lib.rs') diff --git a/vendor/tempfile/src/lib.rs b/vendor/tempfile/src/lib.rs index 51c2da09a..c38ca7b87 100644 --- a/vendor/tempfile/src/lib.rs +++ b/vendor/tempfile/src/lib.rs @@ -27,6 +27,42 @@ //! rely on file paths for _some_ operations. See the security documentation on //! the `NamedTempFile` type for more information. //! +//! ## Early drop pitfall +//! +//! Because `TempDir` and `NamedTempFile` rely on their destructors for cleanup, this can lead +//! to an unexpected early removal of the directory/file, usually when working with APIs which are +//! generic over `AsRef`. Consider the following example: +//! +//! ```no_run +//! # use tempfile::tempdir; +//! # use std::io; +//! # use std::process::Command; +//! # fn main() { +//! # if let Err(_) = run() { +//! # ::std::process::exit(1); +//! # } +//! # } +//! # fn run() -> Result<(), io::Error> { +//! // Create a directory inside of `std::env::temp_dir()`. +//! let temp_dir = tempdir()?; +//! +//! // Spawn the `touch` command inside the temporary directory and collect the exit status +//! // Note that `temp_dir` is **not** moved into `current_dir`, but passed as a reference +//! let exit_status = Command::new("touch").arg("tmp").current_dir(&temp_dir).status()?; +//! assert!(exit_status.success()); +//! +//! # Ok(()) +//! # } +//! ``` +//! +//! This works because a reference to `temp_dir` is passed to `current_dir`, resulting in the +//! destructor of `temp_dir` being run after the `Command` has finished execution. Moving the +//! `TempDir` into the `current_dir` call would result in the `TempDir` being converted into +//! an internal representation, with the original value being dropped and the directory thus +//! being deleted, before the command can be executed. +//! +//! The `touch` command would fail with an `No such file or directory` error. +//! //! ## Examples //! //! Create a temporary file and write some data into it: @@ -125,9 +161,10 @@ #![cfg_attr(test, deny(warnings))] #![deny(rust_2018_idioms)] #![allow(clippy::redundant_field_names)] +#![cfg_attr(feature = "nightly", feature(wasi_ext))] -#[macro_use] -extern crate cfg_if; +#[cfg(doctest)] +doc_comment::doctest!("../README.md"); const NUM_RETRIES: u32 = 1 << 31; const NUM_RAND_CHARS: usize = 6; -- cgit v1.2.3