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
use {Result, Error, Errno, NixPath};
use libc::{self, DIR, c_long};
use std::convert::{AsRef, Into};
use std::ffi::CStr;
use std::mem;
#[cfg(any(target_os = "linux"))]
use libc::ino64_t;
#[cfg(any(target_os = "android"))]
use libc::ino_t as ino64_t;
#[cfg(any(target_os = "linux", target_os = "android"))]
use libc::{dirent64, readdir64};
#[cfg(not(any(target_os = "linux", target_os = "android")))]
use libc::{dirent as dirent64, ino_t as ino64_t, readdir as readdir64};
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
use std::os::unix::io::RawFd;
#[allow(missing_debug_implementations)]
pub struct DirectoryStream(*mut DIR);
impl AsRef<DIR> for DirectoryStream {
fn as_ref(&self) -> &DIR {
unsafe { &*self.0 }
}
}
impl Into<*mut DIR> for DirectoryStream {
fn into(self) -> *mut DIR {
let dirp = self.0;
mem::forget(self);
dirp
}
}
impl Drop for DirectoryStream {
fn drop(&mut self) {
unsafe { libc::closedir(self.0) };
}
}
#[allow(missing_debug_implementations)]
pub struct DirectoryEntry<'a>(&'a dirent64);
impl<'a> DirectoryEntry<'a> {
pub fn name(&self) -> &CStr {
unsafe{
CStr::from_ptr(self.0.d_name.as_ptr())
}
}
pub fn inode(&self) -> ino64_t {
#[cfg(not(any(target_os = "freebsd", target_os = "netbsd", target_os="dragonfly")))]
return self.0.d_ino;
#[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os="dragonfly"))]
return self.0.d_fileno;
}
}
impl<'a> AsRef<dirent64> for DirectoryEntry<'a> {
fn as_ref(&self) -> &dirent64 {
self.0
}
}
pub fn opendir<P: ?Sized + NixPath>(name: &P) -> Result<DirectoryStream> {
let dirp = try!(name.with_nix_path(|cstr| unsafe { libc::opendir(cstr.as_ptr()) }));
if dirp.is_null() {
Err(Error::last().into())
} else {
Ok(DirectoryStream(dirp))
}
}
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
pub fn fdopendir(fd: RawFd) -> Result<DirectoryStream> {
let dirp = unsafe { libc::fdopendir(fd) };
if dirp.is_null() {
Err(Error::last().into())
} else {
Ok(DirectoryStream(dirp))
}
}
pub fn readdir<'a>(dir: &'a mut DirectoryStream) -> Result<Option<DirectoryEntry>> {
let dirent = unsafe {
Errno::clear();
readdir64(dir.0)
};
if dirent.is_null() {
match Errno::last() {
Errno::UnknownErrno => Ok(None),
_ => Err(Error::last().into()),
}
} else {
Ok(Some(DirectoryEntry(unsafe { &*dirent })))
}
}
#[cfg(not(any(target_os = "android")))]
pub fn seekdir<'a>(dir: &'a mut DirectoryStream, loc: c_long) {
unsafe { libc::seekdir(dir.0, loc) };
}
#[cfg(not(any(target_os = "android")))]
pub fn telldir<'a>(dir: &'a mut DirectoryStream) -> c_long {
unsafe { libc::telldir(dir.0) }
}
pub fn dirfd<'a>(dir: &'a mut DirectoryStream) -> Result<RawFd> {
let res = unsafe { libc::dirfd(dir.0) };
Errno::result(res)
}