async_tempfile/tempdir.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
#[cfg(not(feature = "uuid"))]
use crate::RandomName;
use crate::{Error, Ownership};
use std::borrow::Borrow;
use std::fmt::{Debug, Formatter};
use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::Arc;
#[cfg(feature = "uuid")]
use uuid::Uuid;
const DIR_PREFIX: &str = "atmpd_";
/// A named temporary directory that will be cleaned automatically
/// after the last reference to it is dropped.
pub struct TempDir {
/// A local reference to the directory.
dir: ManuallyDrop<PathBuf>,
/// A shared pointer to the owned (or non-owned) directory.
/// The `Arc` ensures that the enclosed dir is kept alive
/// until all references to it are dropped.
core: ManuallyDrop<Arc<TempDirCore>>,
}
/// The instance that tracks the temporary file.
/// If dropped, the file will be deleted.
struct TempDirCore {
/// The path of the contained file.
path: PathBuf,
/// A hacky approach to allow for "non-owned" files.
/// If set to `Ownership::Owned`, the file specified in `path` will be deleted
/// when this instance is dropped. If set to `Ownership::Borrowed`, the file will be kept.
ownership: Ownership,
}
impl TempDir {
/// Creates a new temporary directory in the default location.
/// When the instance goes out of scope, the directory will be deleted.
///
/// ## Example
///
/// ```
/// # use async_tempfile::{TempDir, Error};
/// # use tokio::fs;
/// # let _ = tokio_test::block_on(async {
/// let dir = TempDir::new().await?;
///
/// // The file exists.
/// let dir_path = dir.dir_path().clone();
/// assert!(fs::metadata(dir_path.clone()).await.is_ok());
///
/// // Deletes the directory.
/// drop(dir);
///
/// // The directory was removed.
/// assert!(fs::metadata(dir_path).await.is_err());
/// # Ok::<(), Error>(())
/// # });
/// ```
pub async fn new() -> Result<Self, Error> {
Self::new_in(Self::default_dir()).await
}
/// Creates a new temporary directory in the default location.
/// When the instance goes out of scope, the directory will be deleted.
///
/// ## Arguments
///
/// * `name` - The name of the directory to create in the default temporary directory root.
///
/// ## Example
///
/// ```
/// # use async_tempfile::{TempDir, Error};
/// # use tokio::fs;
/// # let _ = tokio_test::block_on(async {
/// let dir = TempDir::new_with_name("temporary.dir").await?;
///
/// // The directory exists.
/// let dir_path = dir.dir_path().clone();
/// assert!(fs::metadata(dir_path.clone()).await.is_ok());
///
/// // Deletes the directory.
/// drop(dir);
///
/// // The directory was removed.
/// assert!(fs::metadata(dir_path).await.is_err());
/// # Ok::<(), Error>(())
/// # });
/// ```
pub async fn new_with_name<N: AsRef<str>>(name: N) -> Result<Self, Error> {
Self::new_with_name_in(name, Self::default_dir()).await
}
/// Creates a new temporary directory in the default location.
/// When the instance goes out of scope, the directory will be deleted.
///
/// ## Arguments
///
/// * `uuid` - A UUID to use as a suffix to the directory name.
///
/// ## Example
///
/// ```
/// # use async_tempfile::{TempDir, Error};
/// # use tokio::fs;
/// # let _ = tokio_test::block_on(async {
/// let id = uuid::Uuid::new_v4();
/// let dir = TempDir::new_with_uuid(id).await?;
///
/// // The directory exists.
/// let dir_path = dir.dir_path().clone();
/// assert!(fs::metadata(dir_path.clone()).await.is_ok());
///
/// // Deletes the directory.
/// drop(dir);
///
/// // The directory was removed.
/// assert!(fs::metadata(dir_path).await.is_err());
/// # Ok::<(), Error>(())
/// # });
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
#[cfg(feature = "uuid")]
pub async fn new_with_uuid(uuid: Uuid) -> Result<Self, Error> {
Self::new_with_uuid_in(uuid, Self::default_dir()).await
}
/// Creates a new temporary directory in the specified location.
/// When the instance goes out of scope, the directory will be deleted.
///
/// ## Crate Features
///
/// * `uuid` - When the `uuid` crate feature is enabled, a random UUIDv4 is used to
/// generate the temporary directory name.
///
/// ## Arguments
///
/// * `dir` - The directory to create the directory in.
///
/// ## Example
///
/// ```
/// # use async_tempfile::{TempDir, Error};
/// # use tokio::fs;
/// # let _ = tokio_test::block_on(async {
/// let path = std::env::temp_dir();
/// let dir = TempDir::new_in(path).await?;
///
/// // The directory exists.
/// let dir_path = dir.dir_path().clone();
/// assert!(fs::metadata(dir_path.clone()).await.is_ok());
///
/// // Deletes the directory.
/// drop(dir);
///
/// // The directory was removed.
/// assert!(fs::metadata(dir_path).await.is_err());
/// # Ok::<(), Error>(())
/// # });
pub async fn new_in<P: Borrow<Path>>(root_dir: P) -> Result<Self, Error> {
#[cfg(feature = "uuid")]
{
let id = Uuid::new_v4();
Self::new_with_uuid_in(id, root_dir).await
}
#[cfg(not(feature = "uuid"))]
{
let name = RandomName::new(DIR_PREFIX);
Self::new_with_name_in(name, root_dir).await
}
}
/// Creates a new temporary directory in the specified location.
/// When the instance goes out of scope, the directory will be deleted.
///
/// ## Arguments
///
/// * `dir` - The root directory to create the directory in.
/// * `name` - The directory name to use.
///
/// ## Example
///
/// ```
/// # use async_tempfile::{TempDir, Error};
/// # use tokio::fs;
/// # let _ = tokio_test::block_on(async {
/// let path = std::env::temp_dir();
/// let dir = TempDir::new_with_name_in("temporary.dir", path).await?;
///
/// // The directory exists.
/// let dir_path = dir.dir_path().clone();
/// assert!(fs::metadata(dir_path.clone()).await.is_ok());
///
/// // Deletes the directory.
/// drop(dir);
///
/// // The directory was removed.
/// assert!(fs::metadata(dir_path).await.is_err());
/// # Ok::<(), Error>(())
/// # });
/// ```
pub async fn new_with_name_in<N: AsRef<str>, P: Borrow<Path>>(
name: N,
root_dir: P,
) -> Result<Self, Error> {
let dir = root_dir.borrow();
if !dir.is_dir() {
return Err(Error::InvalidDirectory);
}
let file_name = name.as_ref();
let mut path = PathBuf::from(dir);
path.push(file_name);
Self::new_internal(path, Ownership::Owned).await
}
/// Creates a new directory file in the specified location.
/// When the instance goes out of scope, the directory will be deleted.
///
/// ## Arguments
///
/// * `dir` - The root directory to create the directory in.
/// * `uuid` - A UUID to use as a suffix to the directory name.
///
/// ## Example
///
/// ```
/// # use async_tempfile::{TempDir, Error};
/// # use tokio::fs;
/// # let _ = tokio_test::block_on(async {
/// let path = std::env::temp_dir();
/// let id = uuid::Uuid::new_v4();
/// let dir = TempDir::new_with_uuid_in(id, path).await?;
///
/// // The directory exists.
/// let dir_path = dir.dir_path().clone();
/// assert!(fs::metadata(dir_path.clone()).await.is_ok());
///
/// // Deletes the directory.
/// drop(dir);
///
/// // The directory was removed.
/// assert!(fs::metadata(dir_path).await.is_err());
/// # Ok::<(), Error>(())
/// # });
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
#[cfg(feature = "uuid")]
pub async fn new_with_uuid_in<P: Borrow<Path>>(uuid: Uuid, root_dir: P) -> Result<Self, Error> {
let file_name = format!("{}{}", DIR_PREFIX, uuid);
Self::new_with_name_in(file_name, root_dir).await
}
/// Wraps a new instance of this type around an existing directory.
/// If `ownership` is set to [`Ownership::Borrowed`], this method does not take ownership of
/// the file, i.e. the directory will not be deleted when the instance is dropped.
///
/// ## Arguments
///
/// * `path` - The path of the directory to wrap.
/// * `ownership` - The ownership of the directory.
pub async fn from_existing(path: PathBuf, ownership: Ownership) -> Result<Self, Error> {
if !path.is_dir() {
return Err(Error::InvalidDirectory);
}
Self::new_internal(path, ownership).await
}
/// Returns the path of the underlying temporary directory.
pub fn dir_path(&self) -> &PathBuf {
&self.core.path
}
/// Creates a new [`TempDir`] instance that shares the same underlying
/// file handle as the existing [`TempDir`] instance.
/// Reads, writes, and seeks will affect both [`TempDir`] instances simultaneously.
#[allow(dead_code)]
pub async fn try_clone(&self) -> Result<TempDir, Error> {
Ok(TempDir {
core: self.core.clone(),
dir: self.dir.clone(),
})
}
/// Determines the ownership of the temporary directory.
/// ### Example
/// ```
/// # use async_tempfile::{Ownership, TempDir};
/// # let _ = tokio_test::block_on(async {
/// let dir = TempDir::new().await?;
/// assert_eq!(dir.ownership(), Ownership::Owned);
/// # drop(dir);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # });
/// ```
pub fn ownership(&self) -> Ownership {
self.core.ownership
}
async fn new_internal<P: Borrow<Path>>(path: P, ownership: Ownership) -> Result<Self, Error> {
// Create the directory and all its parents.
tokio::fs::create_dir_all(path.borrow()).await?;
let core = TempDirCore {
ownership,
path: PathBuf::from(path.borrow()),
};
Ok(Self {
dir: ManuallyDrop::new(PathBuf::from(path.borrow())),
core: ManuallyDrop::new(Arc::new(core)),
})
}
/// Gets the default temporary file directory.
#[inline(always)]
fn default_dir() -> PathBuf {
std::env::temp_dir()
}
}
/// Ensures the file handles are closed before the core reference is freed.
/// If the core reference would be freed while handles are still open, it is
/// possible that the underlying file cannot be deleted.
impl Drop for TempDir {
fn drop(&mut self) {
// Ensure all directory handles are closed before we attempt to delete the directory itself via core.
drop(unsafe { ManuallyDrop::take(&mut self.dir) });
drop(unsafe { ManuallyDrop::take(&mut self.core) });
}
}
/// Ensures that the underlying directory is deleted if this is an owned instance.
/// If the underlying directory is not owned, this operation does nothing.
impl Drop for TempDirCore {
/// See also [`TempDirCore::close`].
fn drop(&mut self) {
// Ensure we don't drop borrowed directories.
if self.ownership != Ownership::Owned {
return;
}
// TODO: Use asynchronous variant if running in an async context.
// Note that if TempDir is used from the executor's handle,
// this may block the executor itself.
// Using remove_dir_all to delete all content recursively.
let _ = std::fs::remove_dir_all(&self.path);
}
}
impl Debug for TempDirCore {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.path)
}
}
impl Debug for TempDir {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.core)
}
}
/// Allows implicit treatment of TempDir as a Path.
impl Deref for TempDir {
type Target = Path;
fn deref(&self) -> &Self::Target {
&self.dir
}
}
impl Borrow<Path> for TempDir {
fn borrow(&self) -> &Path {
&self.dir
}
}
impl Borrow<Path> for &TempDir {
fn borrow(&self) -> &Path {
&self.dir
}
}
impl AsRef<Path> for TempDir {
fn as_ref(&self) -> &Path {
&self.dir
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TempFile;
#[tokio::test]
async fn test_new() -> Result<(), Error> {
let dir = TempDir::new().await?;
// The directory exists.
let dir_path = dir.dir_path().clone();
assert!(tokio::fs::metadata(dir_path.clone()).await.is_ok());
// Deletes the directory.
drop(dir);
assert!(tokio::fs::metadata(dir_path).await.is_err());
Ok(())
}
#[tokio::test]
#[cfg(not(target_os = "windows"))]
async fn test_files_in_dir() -> Result<(), Error> {
let dir = TempDir::new().await?;
let file = TempFile::new_in(&dir).await?;
let file2 = TempFile::new_in(&dir).await?;
// The directory exists.
let dir_path = dir.dir_path().clone();
assert!(tokio::fs::metadata(dir_path.clone()).await.is_ok());
// The files exist.
let file_path = file.file_path().clone();
let file_path2 = file2.file_path().clone();
assert!(tokio::fs::metadata(file_path.clone()).await.is_ok());
assert!(tokio::fs::metadata(file_path2.clone()).await.is_ok());
// Deletes the directory.
drop(dir);
// The files are gone (even though they are still open).
// TODO: This may cause trouble on Windows as Windows locks files when open.
assert!(tokio::fs::metadata(file_path).await.is_err());
assert!(tokio::fs::metadata(file_path2).await.is_err());
// The directory is gone.
assert!(tokio::fs::metadata(dir_path).await.is_err());
Ok(())
}
}