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
//! temp-dir
//! ========
//! [](https://crates.io/crates/temp-dir)
//! [](https://gitlab.com/leonhard-llc/ops/-/raw/main/temp-dir/LICENSE)
//! [](https://github.com/rust-secure-code/safety-dance/)
//! [](https://gitlab.com/leonhard-llc/ops/-/pipelines)
//!
//! Provides a `TempDir` struct.
//!
//! # Features
//! - Makes a directory in a system temporary directory
//! - Recursively deletes the directory and its contents on drop
//! - Deletes symbolic links and does not follow them
//! - Optional name prefix
//! - Depends only on `std`
//! - `forbid(unsafe_code)`
//! - 100% test coverage
//!
//! # Limitations
//! - Not security-hardened.
//! For example, directory and file names are predictable.
//! - This crate uses
//! [`std::fs::remove_dir_all`](https://doc.rust-lang.org/stable/std/fs/fn.remove_dir_all.html)
//! which may be unreliable on Windows.
//! See [rust#29497](https://github.com/rust-lang/rust/issues/29497) and
//! [`remove_dir_all`](https://crates.io/crates/remove_dir_all) crate.
//!
//! # Alternatives
//! - [`tempdir`](https://crates.io/crates/tempdir)
//! - Unmaintained
//! - Popular and mature
//! - Heavy dependencies (rand, winapi)
//! - [`tempfile`](https://crates.io/crates/tempfile)
//! - Popular and mature
//! - Contains `unsafe`, dependencies full of `unsafe`
//! - Heavy dependencies (libc, winapi, rand, etc.)
//! - [`test_dir`](https://crates.io/crates/test_dir)
//! - Has a handy `TestDir` struct
//! - Incomplete documentation
//! - [`temp_testdir`](https://crates.io/crates/temp_testdir)
//! - Incomplete documentation
//! - [`mktemp`](https://crates.io/crates/mktemp)
//! - Sets directory mode 0700 on unix
//! - Contains `unsafe`
//! - No readme or online docs
//!
//! # Related Crates
//! - [`temp-file`](https://crates.io/crates/temp-file)
//!
//! # Example
//! ```rust
//! use temp_dir::TempDir;
//! let d = TempDir::new().unwrap();
//! // Prints "/tmp/t1a9b-0".
//! println!("{:?}", d.path());
//! let f = d.child("file1");
//! // Prints "/tmp/t1a9b-0/file1".
//! println!("{:?}", f);
//! std::fs::write(&f, b"abc").unwrap();
//! assert_eq!(
//! "abc",
//! std::fs::read_to_string(&f).unwrap(),
//! );
//! // Prints "/tmp/t1a9b-1".
//! println!(
//! "{:?}", TempDir::new().unwrap().path());
//! ```
//!
//! # Cargo Geiger Safety Report
//! # Changelog
//! - v0.2.0 - Implement `AsRef<Path>` on `&TempDir` not `TempDir`, to eliminate footgun.
//! - v0.1.16 - `dont_delete_on_drop()`. Thanks to [A L Manning](https://gitlab.com/A-Manning) for [discussion](https://gitlab.com/leonhard-llc/ops/-/merge_requests/5).
//! - v0.1.15 - Remove a dev dependency.
//! - v0.1.14 - `AsRef<Path>`
//! - v0.1.13 - Update docs.
//! - v0.1.12 - Work when the directory already exists.
//! - v0.1.11
//! - Return `std::io::Error` instead of `String`.
//! - Add
//! [`cleanup`](https://docs.rs/temp-file/latest/temp_file/struct.TempFile.html#method.cleanup).
//! - v0.1.10 - Implement `Eq`, `Ord`, `Hash`
//! - v0.1.9 - Increase test coverage
//! - v0.1.8 - Add [`leak`](https://docs.rs/temp-dir/latest/temp_dir/struct.TempDir.html#method.leak).
//! - v0.1.7 - Update docs:
//! Warn about `std::fs::remove_dir_all` being unreliable on Windows.
//! Warn about predictable directory and file names.
//! Thanks to Reddit user
//! [burntsushi](https://www.reddit.com/r/rust/comments/ma6y0x/tempdir_simple_temporary_directory_with_cleanup/gruo5iu/).
//! - v0.1.6 - Add
//! [`TempDir::panic_on_cleanup_error`](https://docs.rs/temp-dir/latest/temp_dir/struct.TempDir.html#method.panic_on_cleanup_error).
//! Thanks to Reddit users
//! [`KhorneLordOfChaos`](https://www.reddit.com/r/rust/comments/ma6y0x/tempdir_simple_temporary_directory_with_cleanup/grsb5s3/)
//! and
//! [`dpc_pw`](https://www.reddit.com/r/rust/comments/ma6y0x/tempdir_simple_temporary_directory_with_cleanup/gru26df/)
//! for their comments.
//! - v0.1.5 - Explain how it handles symbolic links.
//! Thanks to Reddit user Mai4eeze for this
//! [idea](https://www.reddit.com/r/rust/comments/ma6y0x/tempdir_simple_temporary_directory_with_cleanup/grsoz2g/).
//! - v0.1.4 - Update docs
//! - v0.1.3 - Minor code cleanup, update docs
//! - v0.1.2 - Update docs
//! - v0.1.1 - Fix license
//! - v0.1.0 - Initial version
use ;
use ErrorKind;
use ;
use AtomicBool;
pub static INTERNAL_COUNTER: AtomicU32 = new;
pub static INTERNAL_RETRY: AtomicBool = new;
/// The path of an existing writable directory in a system temporary directory.
///
/// Drop the struct to delete the directory and everything under it.
/// Deletes symbolic links and does not follow them.
///
/// Ignores any error while deleting.
/// See [`TempDir::panic_on_cleanup_error`](struct.TempDir.html#method.panic_on_cleanup_error).
///
/// # Example
/// ```rust
/// use temp_dir::TempDir;
/// let d = TempDir::new().unwrap();
/// // Prints "/tmp/t1a9b-0".
/// println!("{:?}", d.path());
/// let f = d.child("file1");
/// // Prints "/tmp/t1a9b-0/file1".
/// println!("{:?}", f);
/// std::fs::write(&f, b"abc").unwrap();
/// assert_eq!(
/// "abc",
/// std::fs::read_to_string(&f).unwrap(),
/// );
/// // Prints "/tmp/t1a9b-1".
/// println!("{:?}", TempDir::new().unwrap().path());
/// ```