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
extern crate errno;
extern crate libc;
use std;
use std::cell::Cell;
use std::io::Error;
use std::path::Path;
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
use std::mem;
use errors::*;
use LibcString;
macro_rules! try_errno {
($expr:expr) => {{
let rc = $expr;
ensure!(rc >= 0, Error::last_os_error());
rc
}}
}
#[allow(non_camel_case_types)]
type int = libc::c_int;
#[derive(Debug)]
pub struct FdRaw {
pub(crate) fd: libc::c_int,
pub(crate) is_managed: Cell<bool>,
}
impl Drop for FdRaw {
fn drop(&mut self) {
if self.is_managed.get() {
let rc = unsafe { libc::close(self.fd) };
if rc < 0 {
warn!("close({:?}) failed in drop(): {:?}", self,
Error::last_os_error());
}
}
}
}
impl FdRaw {
fn _new(fd: int) -> Self {
Self {
fd: fd,
is_managed: Cell::new(fd >= 0 && fd != libc::AT_FDCWD),
}
}
fn _new_unmanaged(fd: int) -> Self {
Self {
fd: fd,
is_managed: Cell::new(false),
}
}
pub fn into_file(self) -> Result<std::fs::File> {
use std::os::unix::io::FromRawFd;
let res = unsafe { std::fs::File::from_raw_fd(self.fd) };
self.is_managed.set(false);
Ok(res)
}
pub fn open<T: AsRef<Path>>(path: &T, flags: int) -> Result<Self> {
let fd = try_errno!(unsafe {
libc::open(path.as_ref().as_libc().0, flags)
});
Ok(Self::_new(fd))
}
pub fn openat<T: AsRef<Path>>(&self, path: &T, flags: int) -> Result<Self> {
let fd = try_errno!(unsafe {
libc::openat(self.fd, path.as_ref().as_libc().0, flags)
});
Ok(Self::_new(fd))
}
pub fn createat<T: AsRef<Path>>(&self, path: &T, flags: int,
mode: u32) -> Result<Self>
{
let fd = try_errno!(unsafe {
libc::openat(self.fd, path.as_ref().as_libc().0,
flags | libc::O_CREAT, mode)
});
Ok(Self::_new(fd))
}
pub fn mkdirat<T: AsRef<Path>>(&self, path: &T, mode: u32) -> Result<()> {
try_errno!(unsafe {
libc::mkdirat(self.fd, path.as_ref().as_libc().0, mode)
});
Ok(())
}
pub fn symlinkat<D,T>(&self, target: &D, path: &T) -> Result<()>
where
D: AsRef<Path>,
T: AsRef<Path>,
{
try_errno!(unsafe {
libc::symlinkat(target.as_ref().as_libc().0,
self.fd,
path.as_ref().as_libc().0)
});
Ok(())
}
pub unsafe fn new(fd: int) -> Self {
assert!(fd >= 0);
Self::_new(fd)
}
pub fn cwd() -> Self {
Self::_new(libc::AT_FDCWD)
}
pub unsafe fn as_unmanaged(&self) -> Self {
Self::_new_unmanaged(self.fd)
}
pub fn dupfd(&self, cloexec: bool) -> Result<Self> {
let cmd: int = match cloexec {
true => libc::F_DUPFD_CLOEXEC,
false => libc::F_DUPFD,
};
let min_fd: int = 3;
let fd = try_errno!(unsafe { libc::fcntl(self.fd, cmd, min_fd) });
Ok(Self::_new(fd))
}
fn is_file_type(&self, fname: &Path, file_type: u32) -> bool {
let stat = self.fstatat(&fname, false);
match stat {
Err(_) => false,
Ok(s) => (s.st_mode & libc::S_IFMT) == file_type,
}
}
pub fn is_lnkat<T: AsRef<Path>>(&self, fname: &T) -> bool {
self.is_file_type(fname.as_ref(), libc::S_IFLNK)
}
pub fn is_regat<T: AsRef<Path>>(&self, fname: &T) -> bool {
self.is_file_type(fname.as_ref(), libc::S_IFREG)
}
pub fn is_dirat<T: AsRef<Path>>(&self, fname: &T) -> bool {
self.is_file_type(fname.as_ref(), libc::S_IFDIR)
}
pub fn stat<T>(fname: &T, do_follow: bool) -> Result<libc::stat>
where
T: AsRef<Path>
{
let mut stat: libc::stat = unsafe { mem::uninitialized() };
try_errno!(unsafe {
if do_follow {
libc::stat(fname.as_ref().as_libc().0, &mut stat)
} else {
libc::lstat(fname.as_ref().as_libc().0, &mut stat)
}
});
Ok(stat)
}
pub fn fstatat<T>(&self, fname: &T, do_follow: bool) -> Result<libc::stat>
where
T: AsRef<Path>
{
let flags = if do_follow {
0
} else {
libc::AT_SYMLINK_NOFOLLOW
};
let mut stat: libc::stat = unsafe { mem::uninitialized() };
try_errno!(unsafe {
libc::fstatat(self.fd, fname.as_ref().as_libc().0, &mut stat, flags)
});
Ok(stat)
}
pub fn fstat(&self) -> Result<libc::stat> {
let mut stat: libc::stat = unsafe { mem::uninitialized() };
try_errno!(unsafe {
libc::fstat(self.fd, &mut stat)
});
Ok(stat)
}
pub fn readlinkat<T: AsRef<Path>>(&self, fname: &T) -> Result<OsString> {
let mut buf = Vec::with_capacity(256);
loop {
let buf_sz = try_errno!(unsafe {
libc::readlinkat(self.fd, fname.as_ref().as_libc().0,
buf.as_mut_ptr() as *mut _,
buf.capacity())
}) as usize;
assert!(buf_sz <= buf.capacity());
unsafe {
buf.set_len(buf_sz);
}
if buf_sz != buf.capacity() {
return Ok(OsString::from_vec(buf));
}
buf.reserve(256);
}
}
}
#[cfg(not(feature = "atomic-rc"))]
type Rc<T> = std::rc::Rc<T>;
#[cfg(feature = "atomic-rc")]
type Rc<T> = std::sync::Arc<T>;
#[derive(Clone, Debug)]
pub struct Fd(Rc<FdRaw>);
impl Fd {
fn to_self(fd: FdRaw) -> Self {
Fd(Rc::new(fd))
}
pub fn to_fdraw(&self) -> &FdRaw {
&self.0
}
pub fn open<T: AsRef<Path>>(path: &T, flags: int) -> Result<Self> {
FdRaw::open(path, flags).map(Self::to_self)
}
pub fn openat<T: AsRef<Path>>(&self, path: &T, flags: int) -> Result<Self> {
self.0.openat(path, flags).map(Self::to_self)
}
pub fn createat<T: AsRef<Path>>(&self, path: &T, flags:
int, mode: u32) -> Result<Self> {
self.0.createat(path, flags, mode).map(Self::to_self)
}
pub fn cwd() -> Self {
Self::to_self(FdRaw::cwd())
}
pub fn into_rawfd(self) -> std::result::Result<FdRaw, Fd> {
match Rc::try_unwrap(self.0) {
Err(fd) => Err(Fd(fd)),
Ok(fd) => Ok(fd),
}
}
pub unsafe fn into_file(self) -> Result<std::fs::File>
{
self.into_rawfd().unwrap().into_file()
}
}
impl std::ops::Deref for Fd {
type Target = FdRaw;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub fn same_file_by_stat(a: &libc::stat, b: &libc::stat) -> bool {
a.st_dev == b.st_dev && a.st_ino == b.st_ino && a.st_mode == b.st_mode
}