Skip to content

Commit

Permalink
Implement unlinkat - Update comments to unlink at and breakup tests
Browse files Browse the repository at this point in the history
Changes made based on comments from pull request #1058
  • Loading branch information
jlb6740 committed May 28, 2019
1 parent 890de05 commit ce88edd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,8 +1154,10 @@ pub enum UnlinkatFlags {
/// Remove a directory entry
///
/// In the case of a relative path, the directory entry to be removed is determined relative to
/// the directory associated with the file descriptor dirfd. If flag is UnlinkatFlags::RemoveDir
/// then remove the directory entry specified by dirfd and path is performed.
/// the directory associated with the file descriptor `dirfd` or the current working directory
/// if `dirfd` is `None`. In the case of an absolute `path` `dirfd` is ignored. If `flag` is
/// `UnlinkatFlags::RemoveDir` then removal of the directory entry specified by `dirfd` and `path`
/// is performed.
///
/// # References
/// See also [unlinkat(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html)
Expand Down
39 changes: 30 additions & 9 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,37 +579,58 @@ fn test_symlinkat() {


#[test]
fn test_unlinkat() {
#[should_panic]
fn test_unlinkat_dir_noremovedir() {
let tempdir = tempfile::tempdir().unwrap();
let filename = "foo.txt";
let dirname = "foo_dir";
let filepath = tempdir.path().join(filename);
let dirpath = tempdir.path().join(dirname);

// Create file
File::create(&filepath).unwrap();

// Create dir
DirBuilder::new().recursive(true).create(&dirpath).unwrap();

// Get file descriptor for base directory
let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap();

// Attempt unlink dir at relative path without proper flag
panic::set_hook(Box::new(|_info| {}));
let result = std::panic::catch_unwind(|| unlinkat(Some(dirfd), dirname, UnlinkatFlags::NoRemoveDir).unwrap());
assert!(result.is_err());
unlinkat(Some(dirfd), dirname, UnlinkatFlags::NoRemoveDir).unwrap();
assert!(dirpath.exists());
}

#[test]
fn test_unlinkat_dir_removedir() {
let tempdir = tempfile::tempdir().unwrap();
let dirname = "foo_dir";
let dirpath = tempdir.path().join(dirname);

// Create dir
DirBuilder::new().recursive(true).create(&dirpath).unwrap();

// Get file descriptor for base directory
let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap();

// Attempt unlink dir at relative path with proper flag
unlinkat(Some(dirfd), dirname, UnlinkatFlags::RemoveDir).unwrap();
assert!(!dirpath.exists());
}

#[test]
fn test_unlinkat_file() {
let tempdir = tempfile::tempdir().unwrap();
let filename = "foo.txt";
let filepath = tempdir.path().join(filename);

// Create file
File::create(&filepath).unwrap();

// Get file descriptor for base directory
let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap();

// Attempt unlink file at relative path
unlinkat(Some(dirfd), filename, UnlinkatFlags::NoRemoveDir).unwrap();
assert!(!filepath.exists());
}


#[test]
fn test_access_not_existing() {
let tempdir = tempfile::tempdir().unwrap();
Expand Down

0 comments on commit ce88edd

Please sign in to comment.