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
use std::io::{self, Read, Write, Seek, SeekFrom};
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::ops::{Deref, DerefMut};
use std::error;
use std::fmt;
use std::env;
use std;
use super::imp;
use super::util;
pub struct NamedTempFile(Option<NamedTempFileInner>);
struct NamedTempFileInner {
file: File,
path: PathBuf,
}
impl fmt::Debug for NamedTempFile {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NamedTempFile({:?})", self.0.as_ref().unwrap().path)
}
}
impl Deref for NamedTempFile {
type Target = File;
#[inline]
fn deref(&self) -> &File {
&self.inner().file
}
}
impl DerefMut for NamedTempFile {
#[inline]
fn deref_mut(&mut self) -> &mut File {
&mut self.inner_mut().file
}
}
#[derive(Debug)]
pub struct PersistError {
pub error: io::Error,
pub file: NamedTempFile,
}
impl From<PersistError> for io::Error {
#[inline]
fn from(error: PersistError) -> io::Error {
error.error
}
}
impl fmt::Display for PersistError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "failed to persist temporary file: {}", self.error)
}
}
impl error::Error for PersistError {
#[inline]
fn description(&self) -> &str {
"failed to persist temporary file"
}
#[inline]
fn cause(&self) -> Option<&error::Error> {
Some(&self.error)
}
}
impl NamedTempFile {
#[inline]
fn inner(&self) -> &NamedTempFileInner {
self.0.as_ref().unwrap()
}
#[inline]
fn inner_mut(&mut self) -> &mut NamedTempFileInner {
self.0.as_mut().unwrap()
}
#[inline]
pub fn new() -> io::Result<NamedTempFile> {
Self::new_in(&env::temp_dir())
}
#[inline]
pub fn new_in<P: AsRef<Path>>(dir: P) -> io::Result<NamedTempFile> {
for _ in 0..::NUM_RETRIES {
let path = dir.as_ref().join(&util::tmpname());
return match imp::create_named(&path) {
Ok(file) => Ok(NamedTempFile(Some(NamedTempFileInner { path: path, file: file, }))),
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
Err(e) => Err(e),
}
}
Err(io::Error::new(io::ErrorKind::AlreadyExists,
"too many temporary directories already exist"))
}
#[inline]
pub fn path(&self) -> &Path {
&self.inner().path
}
#[inline]
pub fn close(mut self) -> io::Result<()> {
let NamedTempFileInner { path, file } = self.0.take().unwrap();
drop(file);
fs::remove_file(path)
}
#[inline]
pub fn persist<P: AsRef<Path>>(mut self, new_path: P) -> Result<File, PersistError> {
match imp::persist(&self.inner().path, new_path.as_ref()) {
Ok(_) => Ok(self.0.take().unwrap().file),
Err(e) => Err(PersistError { file: self, error: e }),
}
}
}
impl Drop for NamedTempFile {
#[inline]
fn drop(&mut self) {
if let Some(NamedTempFileInner { file, path }) = self.0.take() {
drop(file);
let _ = fs::remove_file(path);
}
}
}
impl Read for NamedTempFile {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(**self).read(buf)
}
}
impl Write for NamedTempFile {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(**self).write(buf)
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
(**self).flush()
}
}
impl Seek for NamedTempFile {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(**self).seek(pos)
}
}
#[cfg(unix)]
impl std::os::unix::io::AsRawFd for NamedTempFile {
#[inline]
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
(**self).as_raw_fd()
}
}
#[cfg(windows)]
impl std::os::windows::io::AsRawHandle for NamedTempFile {
#[inline]
fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
(**self).as_raw_handle()
}
}