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
#[cfg(feature = "rand_gen")]
use crate::global_consts::{num_retry, rand_fn_len, valid_chars};
#[cfg(feature = "rand_gen")]
use rand::Rng;
#[cfg(feature = "regex_support")]
use regex::{Error as RErr, Regex};
use std::env;
use std::fs;
#[cfg(unix)]
use std::fs::Permissions;
use std::io;
use std::path::{Path, PathBuf};
use crate::error::TempResult;
use crate::helpers::normalize_path;
use crate::temp_file::TempFile;
// TODO: created_dir like in temp_file.rs
/// A temporary directory that automatically cleans up its contents when dropped.
///
/// Files created through the `TempDir` are tracked and removed upon drop.
pub struct TempDir {
/// The full path to the temporary directory.
path: Option<PathBuf>,
/// Temporary files contained within the directory.
files: Vec<TempFile>,
/// The first created parent directory of the parent directories.
created_parent: Option<PathBuf>,
}
impl TempDir {
/// Creates a new temporary directory at the specified path.
///
/// The directory (and any missing parent directories) will be created.
///
/// # Arguments
///
/// * `path` - The path at which to create the directory. If a relative path is provided, it is resolved relative to the system temporary directory.
///
/// # Errors
///
/// Returns an error if the directory cannot be created.
pub fn new<P: AsRef<Path>>(path: P) -> TempResult<Self> {
let path_ref = normalize_path(path.as_ref());
let path_buf = if path_ref.is_absolute() {
path_ref
} else {
env::temp_dir().join(path_ref)
};
let created = Self::create_with_parent(&path_buf)?;
Ok(Self {
path: Some(path_buf),
files: Vec::new(),
created_parent: created,
})
}
/// Creates a new temporary directory at the specified path.
///
/// The directory (and any missing parent directories) will be created.
///
/// # Arguments
///
/// * `path` - The path at which to create the directory. If a relative path is provided, it is resolved relative to the current directory.
///
/// # Errors
///
/// Returns an error if the directory cannot be created.
pub fn new_here<P: AsRef<Path>>(path: P) -> TempResult<Self> {
let path_ref = normalize_path(path.as_ref());
let path_buf = if path_ref.is_absolute() {
path_ref
} else {
env::current_dir()?.join(path_ref)
};
Self::new(path_buf)
}
#[cfg(feature = "rand_gen")]
/// Creates a new temporary directory with a random name in the given parent directory.
///
/// The directory name will consist of alphanumeric characters only, ensuring compatibility
/// across different filesystems.
///
/// # Arguments
///
/// * `dir` - An optional parent directory in which to create the temporary directory. If a relative directory is provided, it is resolved relative to the system temporary directory.
///
/// # Errors
///
/// Returns an error if a unique directory name cannot be generated or if directory creation fails.
pub fn new_random<P: AsRef<Path>>(dir: Option<P>) -> TempResult<Self> {
let parent_dir = if let Some(d) = dir {
let d_ref = normalize_path(d.as_ref());
if d_ref.is_absolute() {
d_ref
} else {
env::temp_dir().join(d_ref)
}
} else {
env::temp_dir()
};
let mut rng = rand::rng();
for _ in 0..num_retry() {
let name: String = (0..rand_fn_len())
.map(|_| {
let idx = rng.random_range(0..valid_chars().len());
valid_chars()[idx] as char
})
.collect();
let full_path = parent_dir.join(&name);
if !full_path.exists() {
let created = Self::create_with_parent(&full_path)?;
return Ok(Self {
path: Some(full_path),
files: Vec::new(),
created_parent: created,
});
}
}
Err(io::Error::new(
io::ErrorKind::AlreadyExists,
"Could not generate a unique directory name",
)
.into())
}
/// Function to create the directory and its parent directories, then set their permissions to rwx------, returning the first component of the parent's path which does not exist, or None if it all exists except for the child.
fn create_with_parent(path: &PathBuf) -> TempResult<Option<PathBuf>> {
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
let nonexistent = crate::helpers::first_missing_directory_component(path);
fs::create_dir_all(path)?;
#[cfg(unix)]
if let Some(first_missing) = nonexistent.clone() {
let mut current = first_missing;
// Loop until the final directory in the path is reached.
while current != *path {
fs::set_permissions(¤t, Permissions::from_mode(0o700))?;
// Append the next path component.
if let Some(component) = path.strip_prefix(¤t).unwrap().components().next() {
current = current.join(component);
} else {
break;
}
}
// Finally, set permissions on the final directory.
fs::set_permissions(path, Permissions::from_mode(0o700))?;
} else {
// If no directory was missing (only the child directory was created)
fs::set_permissions(path, Permissions::from_mode(0o700))?;
}
Ok(nonexistent)
}
/// Creates a new temporary directory with a random name in the given parent directory.
///
/// The directory name will consist of alphanumeric characters only, ensuring compatibility
/// across different filesystems.
///
/// # Arguments
///
/// * `dir` - An optional parent directory in which to create the temporary directory. If a relative directory is provided, it is resolved relative to the current working directory.
///
/// # Errors
///
/// Returns an error if a unique directory name cannot be generated or if directory creation fails.
#[cfg(feature = "rand_gen")]
pub fn new_random_here<P: AsRef<Path>>(dir: Option<P>) -> TempResult<Self> {
if let Some(dir) = dir {
let d_ref = normalize_path(dir.as_ref());
if d_ref.is_absolute() {
Self::new_random(Some(d_ref))
} else {
Self::new_random(Some(&env::current_dir()?.join(d_ref)))
}
} else {
Self::new_random(Some(&env::current_dir()?))
}
}
/// Creates a new temporary file with the given filename in the directory.
///
/// The created file is tracked and will be automatically deleted on drop.
///
/// # Arguments
///
/// * `filename` - The name of the file to create.
///
/// # Errors
///
/// This function will return an error if the inner path is `None`.
#[allow(clippy::missing_panics_doc)]
pub fn create_file<S: AsRef<str>>(&mut self, filename: S) -> TempResult<&mut TempFile> {
let dir = self.path.as_ref().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "Temporary directory path is not set")
})?;
let file_path = dir.join(filename.as_ref());
self.files.push(TempFile::new(file_path)?);
Ok(self.files.last_mut().unwrap())
}
#[cfg(feature = "rand_gen")]
/// Creates a new temporary file with a random name in the directory.
///
/// The file is tracked and will be automatically deleted on drop.
///
/// # Errors
///
/// Returns an error if a unique filename cannot be generated or if file creation fails.
#[allow(clippy::missing_panics_doc)]
pub fn create_random_file(&mut self) -> TempResult<&mut TempFile> {
let dir = self.path.as_ref().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "Temporary directory path is not set")
})?;
self.files
.push(TempFile::new_random(Some(normalize_path(dir)))?);
Ok(self.files.last_mut().unwrap())
}
/// Removes a file from the directory's management.
///
/// This does not delete the file immediately—it will be removed when the directory is dropped.
///
/// # Arguments
///
/// * `filename` - The name of the file to remove from management.
pub fn remove_file<S: AsRef<str>>(&mut self, filename: S) {
let filename = filename.as_ref();
self.files.retain(|f| {
f.path
.as_ref()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str())
!= Some(filename)
});
}
/// Retrieves a reference to a temporary file by filename.
///
/// # Arguments
///
/// * `filename` - The name of the file to retrieve.
pub fn get_file<S: AsRef<str>>(&self, filename: S) -> Option<&TempFile> {
let filename = filename.as_ref();
self.files.iter().find(|f| {
f.path
.as_ref()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str())
== Some(filename)
})
}
/// Retrieves a mutable reference to a temporary file by filename.
///
/// # Arguments
///
/// * `filename` - The name of the file to retrieve.
pub fn get_file_mut<S: AsRef<str>>(&mut self, filename: S) -> Option<&mut TempFile> {
let filename = filename.as_ref();
self.files.iter_mut().find(|f| {
f.path
.as_ref()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str())
== Some(filename)
})
}
/// Returns the path of the temporary directory.
#[must_use]
pub fn path(&self) -> Option<&Path> {
self.path.as_deref()
}
/// Consumes the `TempDir`, returning its path and preventing cleanup.
#[must_use]
pub fn into_path(mut self) -> Option<PathBuf> {
self.path.take()
}
/// Lists the paths of all files managed by the directory.
#[must_use]
pub fn list_files(&self) -> Vec<&Path> {
self.files
.iter()
.filter_map(|f| f.path.as_deref())
.collect()
}
#[cfg(feature = "rand_gen")]
/// Creates a new temporary directory with a random name within the given parent directory.
///
/// # Arguments
///
/// * `path` - The parent directory in which to create the temporary directory. If a relative path is provided, it is resolved relative to the system temporary directory.
///
/// # Errors
///
/// Returns an error if a unique directory name cannot be generated or if directory creation fails.
pub fn new_in<P: AsRef<Path>>(path: P) -> TempResult<Self> {
Self::new_random(Some(path))
}
}
#[cfg(feature = "regex_support")]
impl TempDir {
/// Finds files with names matching a regex pattern.
///
/// # Arguments
///
/// * `pattern` - A regex pattern to match file names.
///
/// # Errors
///
/// Returns an error if the regex pattern is invalid.
pub fn find_files_by_pattern<S: AsRef<str>>(&self, pattern: S) -> Result<Vec<&TempFile>, RErr> {
let re = Regex::new(pattern.as_ref())?;
Ok(self
.files
.iter()
.filter(|f| {
f.path
.as_ref()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str())
.is_some_and(|name| re.is_match(name))
})
.collect())
}
/// Finds mutable references to files with names matching a regex pattern.
///
/// # Arguments
///
/// * `pattern` - A regex pattern to match file names.
///
/// # Errors
///
/// Returns an error if the regex pattern is invalid.
pub fn find_files_by_pattern_mut<S: AsRef<str>>(
&mut self,
pattern: S,
) -> Result<Vec<&mut TempFile>, RErr> {
let re = Regex::new(pattern.as_ref())?;
Ok(self
.files
.iter_mut()
.filter(|f| {
f.path
.as_ref()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str())
.is_some_and(|name| re.is_match(name))
})
.collect())
}
}
impl Drop for TempDir {
fn drop(&mut self) {
match (self.path.take(), self.created_parent.take()) {
(Some(p), None) => {
self.files.clear();
let _ = fs::remove_dir_all(p);
}
(Some(_), Some(d)) => {
self.files.clear();
let _ = fs::remove_dir_all(d);
}
_ => {}
}
}
}
impl AsRef<Path> for TempDir {
fn as_ref(&self) -> &Path {
self.path.as_ref().expect("TempDir path is None")
}
}