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