|
| 1 | +//! Directory Stream functions |
| 2 | +//! |
| 3 | +//! [Further reading and details on the C API](http://man7.org/linux/man-pages/man3/opendir.3.html) |
| 4 | +
|
| 5 | +use {Result, Error, Errno, NixPath}; |
| 6 | +use errno; |
| 7 | +use libc::{self, DIR, c_long}; |
| 8 | +use std::convert::{AsRef, Into}; |
| 9 | +use std::ffi::CStr; |
| 10 | +use std::mem; |
| 11 | + |
| 12 | +#[cfg(any(target_os = "linux", target_os = "android"))] |
| 13 | +use libc::ino64_t; |
| 14 | + |
| 15 | +#[cfg(any(target_os = "linux", target_os = "android"))] |
| 16 | +use libc::{dirent64, readdir64}; |
| 17 | + |
| 18 | +#[cfg(not(any(target_os = "linux", target_os = "android")))] |
| 19 | +use libc::{dirent as dirent64, ino_t as ino64_t, readdir as readdir64}; |
| 20 | + |
| 21 | +#[cfg(not(any(target_os = "ios", target_os = "macos")))] |
| 22 | +use std::os::unix::io::RawFd; |
| 23 | + |
| 24 | +/// Directory Stream object |
| 25 | +pub struct DirectoryStream(*mut DIR); |
| 26 | + |
| 27 | +impl AsRef<DIR> for DirectoryStream { |
| 28 | + fn as_ref(&self) -> &DIR { |
| 29 | + unsafe { &*self.0 } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Consumes directory stream and return underlying directory pointer. |
| 34 | +/// |
| 35 | +/// The pointer must be deallocated manually using `libc::closedir` |
| 36 | +impl Into<*mut DIR> for DirectoryStream { |
| 37 | + fn into(self) -> *mut DIR { |
| 38 | + let dirp = self.0; |
| 39 | + mem::forget(self); |
| 40 | + dirp |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl Drop for DirectoryStream { |
| 45 | + fn drop(&mut self) { |
| 46 | + unsafe { libc::closedir(self.0) }; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// A directory entry |
| 51 | +pub struct DirectoryEntry<'a>(&'a dirent64); |
| 52 | + |
| 53 | +impl<'a> DirectoryEntry<'a> { |
| 54 | + /// File name |
| 55 | + pub fn name(&self) -> &CStr { |
| 56 | + unsafe{ |
| 57 | + CStr::from_ptr(self.0.d_name.as_ptr()) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Inode number |
| 62 | + pub fn inode(&self) -> ino64_t { |
| 63 | + #[cfg(not(any(target_os = "freebsd", target_os = "netbsd", target_os="dragonfly")))] |
| 64 | + return self.0.d_ino; |
| 65 | + #[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os="dragonfly"))] |
| 66 | + return self.0.d_fileno; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<'a> AsRef<dirent64> for DirectoryEntry<'a> { |
| 71 | + fn as_ref(&self) -> &dirent64 { |
| 72 | + self.0 |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Opens a directory stream corresponding to the directory name. |
| 77 | +/// |
| 78 | +/// The stream is positioned at the first entry in the directory. |
| 79 | +pub fn opendir<P: ?Sized + NixPath>(name: &P) -> Result<DirectoryStream> { |
| 80 | + let dirp = try!(name.with_nix_path(|cstr| unsafe { libc::opendir(cstr.as_ptr()) })); |
| 81 | + if dirp.is_null() { |
| 82 | + Err(Error::last().into()) |
| 83 | + } else { |
| 84 | + Ok(DirectoryStream(dirp)) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// Returns directory stream corresponding to the open file descriptor `fd` |
| 89 | +/// |
| 90 | +/// After a successful call to this function, `fd` is used internally by |
| 91 | +/// the implementation, and should not otherwise be used by the application |
| 92 | +#[cfg(not(any(target_os = "ios", target_os = "macos")))] |
| 93 | +pub fn fdopendir(fd: RawFd) -> Result<DirectoryStream> { |
| 94 | + let dirp = unsafe { libc::fdopendir(fd) }; |
| 95 | + if dirp.is_null() { |
| 96 | + Err(Error::last().into()) |
| 97 | + } else { |
| 98 | + Ok(DirectoryStream(dirp)) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Returns the next directory entry in the directory stream. |
| 103 | +/// |
| 104 | +/// It returns `Some(None)` on reaching the end of the directory stream. |
| 105 | +pub fn readdir<'a>(dir: &'a mut DirectoryStream) -> Result<Option<DirectoryEntry>> { |
| 106 | + let dirent = unsafe { |
| 107 | + Errno::clear(); |
| 108 | + readdir64(dir.0) |
| 109 | + }; |
| 110 | + if dirent.is_null() { |
| 111 | + match Errno::last() { |
| 112 | + errno::UnknownErrno => Ok(None), |
| 113 | + _ => Err(Error::last().into()), |
| 114 | + } |
| 115 | + } else { |
| 116 | + Ok(Some(DirectoryEntry(unsafe { &*dirent }))) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/// Sets the location in the directory stream from which the next `readdir` call will start. |
| 121 | +/// |
| 122 | +/// The `loc` argument should be a value returned by a previous call to `telldir` |
| 123 | +#[cfg(not(any(target_os = "android")))] |
| 124 | +pub fn seekdir<'a>(dir: &'a mut DirectoryStream, loc: c_long) { |
| 125 | + unsafe { libc::seekdir(dir.0, loc) }; |
| 126 | +} |
| 127 | + |
| 128 | +/// Returns the current location associated with the directory stream. |
| 129 | +#[cfg(not(any(target_os = "android")))] |
| 130 | +pub fn telldir<'a>(dir: &'a mut DirectoryStream) -> c_long { |
| 131 | + unsafe { libc::telldir(dir.0) } |
| 132 | +} |
0 commit comments