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