Skip to content

Commit

Permalink
address more review comments
Browse files Browse the repository at this point in the history
* s/descriptor/file descriptor/ in a comment
* allow passing through a mode in Dir::open/openat; at least on Linux,
  it's possible to create a new directory with open/openat rather than
  using mkdir/mkdirat.
  • Loading branch information
scottlamb committed Aug 5, 2018
1 parent 6d49a12 commit 370ec52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/dir.rs
Expand Up @@ -18,7 +18,7 @@ use libc::{dirent, readdir_r};
/// * can be opened from a file descriptor (as returned by `openat`, perhaps before knowing
/// if the path represents a file or directory).
/// * implements `AsRawFd`, so it can be passed to `fstat`, `openat`, etc.
/// The descriptor continues to be owned by the `Dir`, so callers must not keep a `RawFd`
/// The file descriptor continues to be owned by the `Dir`, so callers must not keep a `RawFd`
/// after the `Dir` is dropped.
/// * can be iterated through multiple times without closing and reopening the file
/// descriptor. Each iteration rewinds when finished.
Expand All @@ -32,14 +32,16 @@ pub struct Dir(

impl Dir {
/// Opens the given path as with `fcntl::open`.
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag) -> Result<Self> {
let fd = fcntl::open(path, oflag, sys::stat::Mode::empty())?;
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag,
mode: sys::stat::Mode) -> Result<Self> {
let fd = fcntl::open(path, oflag, mode)?;
Dir::from_fd(fd)
}

/// Opens the given path as with `fcntl::openat`.
pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag) -> Result<Self> {
let fd = fcntl::openat(dirfd, path, oflag, sys::stat::Mode::empty())?;
pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag,
mode: sys::stat::Mode) -> Result<Self> {
let fd = fcntl::openat(dirfd, path, oflag, mode)?;
Dir::from_fd(fd)
}

Expand Down
9 changes: 5 additions & 4 deletions test/test_dir.rs
Expand Up @@ -3,6 +3,7 @@ extern crate tempdir;

use nix::dir::{Dir, Type};
use nix::fcntl::OFlag;
use nix::sys::stat::Mode;
use std::fs::File;
use self::tempdir::TempDir;

Expand All @@ -11,8 +12,8 @@ fn read() {
let tmp = TempDir::new("nix-dir-read").unwrap();
File::create(&tmp.path().join("foo")).unwrap();
::std::os::unix::fs::symlink("foo", tmp.path().join("bar")).unwrap();
let mut dir =
Dir::open(tmp.path(), OFlag::O_DIRECTORY | OFlag::O_RDONLY | OFlag::O_CLOEXEC).unwrap();
let mut dir = Dir::open(tmp.path(), OFlag::O_DIRECTORY | OFlag::O_RDONLY | OFlag::O_CLOEXEC,
Mode::empty()).unwrap();
let mut entries: Vec<_> = dir.iter().map(|e| e.unwrap()).collect();
entries.sort_by(|a, b| a.file_name().cmp(b.file_name()));
let entry_names: Vec<_> = entries
Expand All @@ -32,8 +33,8 @@ fn read() {
#[test]
fn rewind() {
let tmp = TempDir::new("nix-dir-rewind").unwrap();
let mut dir =
Dir::open(tmp.path(), OFlag::O_DIRECTORY | OFlag::O_RDONLY | OFlag::O_CLOEXEC).unwrap();
let mut dir = Dir::open(tmp.path(), OFlag::O_DIRECTORY | OFlag::O_RDONLY | OFlag::O_CLOEXEC,
Mode::empty()).unwrap();
let entries1: Vec<_> = dir.iter().map(|e| e.unwrap().file_name().to_owned()).collect();
let entries2: Vec<_> = dir.iter().map(|e| e.unwrap().file_name().to_owned()).collect();
assert_eq!(entries1, entries2);
Expand Down

0 comments on commit 370ec52

Please sign in to comment.