1 //! List directory contents
2 
3 use crate::errno::Errno;
4 use crate::fcntl::{self, OFlag};
5 use crate::sys;
6 use crate::{Error, NixPath, Result};
7 use cfg_if::cfg_if;
8 use std::ffi;
9 use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
10 use std::ptr;
11 
12 #[cfg(target_os = "linux")]
13 use libc::{dirent64 as dirent, readdir64_r as readdir_r};
14 
15 #[cfg(not(target_os = "linux"))]
16 use libc::{dirent, readdir_r};
17 
18 /// An open directory.
19 ///
20 /// This is a lower-level interface than `std::fs::ReadDir`. Notable differences:
21 ///    * can be opened from a file descriptor (as returned by `openat`, perhaps before knowing
22 ///      if the path represents a file or directory).
23 ///    * implements `AsRawFd`, so it can be passed to `fstat`, `openat`, etc.
24 ///      The file descriptor continues to be owned by the `Dir`, so callers must not keep a `RawFd`
25 ///      after the `Dir` is dropped.
26 ///    * can be iterated through multiple times without closing and reopening the file
27 ///      descriptor. Each iteration rewinds when finished.
28 ///    * returns entries for `.` (current directory) and `..` (parent directory).
29 ///    * returns entries' names as a `CStr` (no allocation or conversion beyond whatever libc
30 ///      does).
31 #[derive(Debug, Eq, Hash, PartialEq)]
32 pub struct Dir(ptr::NonNull<libc::DIR>);
33 
34 impl Dir {
35     /// Opens the given path as with `fcntl::open`.
open<P: ?Sized + NixPath>( path: &P, oflag: OFlag, mode: sys::stat::Mode, ) -> Result<Self>36     pub fn open<P: ?Sized + NixPath>(
37         path: &P,
38         oflag: OFlag,
39         mode: sys::stat::Mode,
40     ) -> Result<Self> {
41         let fd = fcntl::open(path, oflag, mode)?;
42         Dir::from_fd(fd)
43     }
44 
45     /// Opens the given path as with `fcntl::openat`.
openat<P: ?Sized + NixPath>( dirfd: Option<RawFd>, path: &P, oflag: OFlag, mode: sys::stat::Mode, ) -> Result<Self>46     pub fn openat<P: ?Sized + NixPath>(
47         dirfd: Option<RawFd>,
48         path: &P,
49         oflag: OFlag,
50         mode: sys::stat::Mode,
51     ) -> Result<Self> {
52         let fd = fcntl::openat(dirfd, path, oflag, mode)?;
53         Dir::from_fd(fd)
54     }
55 
56     /// Converts from a descriptor-based object, closing the descriptor on success or failure.
57     #[inline]
from<F: IntoRawFd>(fd: F) -> Result<Self>58     pub fn from<F: IntoRawFd>(fd: F) -> Result<Self> {
59         Dir::from_fd(fd.into_raw_fd())
60     }
61 
62     /// Converts from a file descriptor, closing it on success or failure.
63     #[doc(alias("fdopendir"))]
from_fd(fd: RawFd) -> Result<Self>64     pub fn from_fd(fd: RawFd) -> Result<Self> {
65         let d = ptr::NonNull::new(unsafe { libc::fdopendir(fd) }).ok_or_else(
66             || {
67                 let e = Error::last();
68                 unsafe { libc::close(fd) };
69                 e
70             },
71         )?;
72         Ok(Dir(d))
73     }
74 
75     /// Returns an iterator of `Result<Entry>` which rewinds when finished.
iter(&mut self) -> Iter76     pub fn iter(&mut self) -> Iter {
77         Iter(self)
78     }
79 }
80 
81 // `Dir` is not `Sync`. With the current implementation, it could be, but according to
82 // https://www.gnu.org/software/libc/manual/html_node/Reading_002fClosing-Directory.html,
83 // future versions of POSIX are likely to obsolete `readdir_r` and specify that it's unsafe to
84 // call `readdir` simultaneously from multiple threads.
85 //
86 // `Dir` is safe to pass from one thread to another, as it's not reference-counted.
87 unsafe impl Send for Dir {}
88 
89 impl AsRawFd for Dir {
as_raw_fd(&self) -> RawFd90     fn as_raw_fd(&self) -> RawFd {
91         unsafe { libc::dirfd(self.0.as_ptr()) }
92     }
93 }
94 
95 impl Drop for Dir {
drop(&mut self)96     fn drop(&mut self) {
97         let e = Errno::result(unsafe { libc::closedir(self.0.as_ptr()) });
98         if !std::thread::panicking() && e == Err(Errno::EBADF) {
99             panic!("Closing an invalid file descriptor!");
100         };
101     }
102 }
103 
104 // The pass by mut is technically needless only because the inner NonNull is
105 // Copy.  But philosophically we're mutating the Dir, so we pass by mut.
106 #[allow(clippy::needless_pass_by_ref_mut)]
next(dir: &mut Dir) -> Option<Result<Entry>>107 fn next(dir: &mut Dir) -> Option<Result<Entry>> {
108     unsafe {
109         // Note: POSIX specifies that portable applications should dynamically allocate a
110         // buffer with room for a `d_name` field of size `pathconf(..., _PC_NAME_MAX)` plus 1
111         // for the NUL byte. It doesn't look like the std library does this; it just uses
112         // fixed-sized buffers (and libc's dirent seems to be sized so this is appropriate).
113         // Probably fine here too then.
114         let mut ent = std::mem::MaybeUninit::<dirent>::uninit();
115         let mut result = ptr::null_mut();
116         if let Err(e) = Errno::result(readdir_r(
117             dir.0.as_ptr(),
118             ent.as_mut_ptr(),
119             &mut result,
120         )) {
121             return Some(Err(e));
122         }
123         if result.is_null() {
124             return None;
125         }
126         assert_eq!(result, ent.as_mut_ptr());
127         Some(Ok(Entry(ent.assume_init())))
128     }
129 }
130 
131 /// Return type of [`Dir::iter`].
132 #[derive(Debug, Eq, Hash, PartialEq)]
133 pub struct Iter<'d>(&'d mut Dir);
134 
135 impl<'d> Iterator for Iter<'d> {
136     type Item = Result<Entry>;
137 
next(&mut self) -> Option<Self::Item>138     fn next(&mut self) -> Option<Self::Item> {
139         next(self.0)
140     }
141 }
142 
143 impl<'d> Drop for Iter<'d> {
drop(&mut self)144     fn drop(&mut self) {
145         unsafe { libc::rewinddir((self.0).0.as_ptr()) }
146     }
147 }
148 
149 /// The return type of [Dir::into_iter]
150 #[derive(Debug, Eq, Hash, PartialEq)]
151 pub struct OwningIter(Dir);
152 
153 impl Iterator for OwningIter {
154     type Item = Result<Entry>;
155 
next(&mut self) -> Option<Self::Item>156     fn next(&mut self) -> Option<Self::Item> {
157         next(&mut self.0)
158     }
159 }
160 
161 /// The file descriptor continues to be owned by the `OwningIter`,
162 /// so callers must not keep a `RawFd` after the `OwningIter` is dropped.
163 impl AsRawFd for OwningIter {
as_raw_fd(&self) -> RawFd164     fn as_raw_fd(&self) -> RawFd {
165         self.0.as_raw_fd()
166     }
167 }
168 
169 impl IntoIterator for Dir {
170     type Item = Result<Entry>;
171     type IntoIter = OwningIter;
172 
173     /// Creates a owning iterator, that is, one that takes ownership of the
174     /// `Dir`. The `Dir` cannot be used after calling this.  This can be useful
175     /// when you have a function that both creates a `Dir` instance and returns
176     /// an `Iterator`.
177     ///
178     /// Example:
179     ///
180     /// ```
181     /// use nix::{dir::Dir, fcntl::OFlag, sys::stat::Mode};
182     /// use std::{iter::Iterator, string::String};
183     ///
184     /// fn ls_upper(dirname: &str) -> impl Iterator<Item=String> {
185     ///     let d = Dir::open(dirname, OFlag::O_DIRECTORY, Mode::S_IXUSR).unwrap();
186     ///     d.into_iter().map(|x| x.unwrap().file_name().as_ref().to_string_lossy().to_ascii_uppercase())
187     /// }
188     /// ```
into_iter(self) -> Self::IntoIter189     fn into_iter(self) -> Self::IntoIter {
190         OwningIter(self)
191     }
192 }
193 
194 /// A directory entry, similar to `std::fs::DirEntry`.
195 ///
196 /// Note that unlike the std version, this may represent the `.` or `..` entries.
197 #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
198 #[repr(transparent)]
199 pub struct Entry(dirent);
200 
201 /// Type of file referenced by a directory entry
202 #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
203 pub enum Type {
204     /// FIFO (Named pipe)
205     Fifo,
206     /// Character device
207     CharacterDevice,
208     /// Directory
209     Directory,
210     /// Block device
211     BlockDevice,
212     /// Regular file
213     File,
214     /// Symbolic link
215     Symlink,
216     /// Unix-domain socket
217     Socket,
218 }
219 
220 impl Entry {
221     /// Returns the inode number (`d_ino`) of the underlying `dirent`.
222     #[allow(clippy::useless_conversion)] // Not useless on all OSes
223     // The cast is not unnecessary on all platforms.
224     #[allow(clippy::unnecessary_cast)]
ino(&self) -> u64225     pub fn ino(&self) -> u64 {
226         cfg_if! {
227             if #[cfg(any(target_os = "aix",
228                          target_os = "emscripten",
229                          target_os = "fuchsia",
230                          target_os = "haiku",
231                          target_os = "hurd",
232                          solarish,
233                          linux_android,
234                          apple_targets))] {
235                 self.0.d_ino as u64
236             } else {
237                 u64::from(self.0.d_fileno)
238             }
239         }
240     }
241 
242     /// Returns the bare file name of this directory entry without any other leading path component.
file_name(&self) -> &ffi::CStr243     pub fn file_name(&self) -> &ffi::CStr {
244         unsafe { ffi::CStr::from_ptr(self.0.d_name.as_ptr()) }
245     }
246 
247     /// Returns the type of this directory entry, if known.
248     ///
249     /// See platform `readdir(3)` or `dirent(5)` manpage for when the file type is known;
250     /// notably, some Linux filesystems don't implement this. The caller should use `stat` or
251     /// `fstat` if this returns `None`.
file_type(&self) -> Option<Type>252     pub fn file_type(&self) -> Option<Type> {
253         #[cfg(not(any(solarish, target_os = "aix", target_os = "haiku")))]
254         match self.0.d_type {
255             libc::DT_FIFO => Some(Type::Fifo),
256             libc::DT_CHR => Some(Type::CharacterDevice),
257             libc::DT_DIR => Some(Type::Directory),
258             libc::DT_BLK => Some(Type::BlockDevice),
259             libc::DT_REG => Some(Type::File),
260             libc::DT_LNK => Some(Type::Symlink),
261             libc::DT_SOCK => Some(Type::Socket),
262             /* libc::DT_UNKNOWN | */ _ => None,
263         }
264 
265         // illumos, Solaris, and Haiku systems do not have the d_type member at all:
266         #[cfg(any(solarish, target_os = "aix", target_os = "haiku"))]
267         None
268     }
269 }
270