diff --git a/src/fs/mod.rs b/src/fs/mod.rs index 2ffc10f..1332eb4 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -52,7 +52,7 @@ //! - [x] `std::fs::remove_dir_all` - `fs.dir(path).remove_all()` - Removes a directory at this path, after removing all its contents. Use carefully! //! - [x] `std::fs::remove_file` - `fs.file(path).remove()` - Removes a file from the filesystem. //! - [x] `std::fs::rename` - `fs.path(path).rename()` - Rename a file or directory to a new name, replacing the original file if to already exists. -//! - [ ] `std::fs::set_permissions` - `fs.path(path).set_permissions()` - Changes the permissions found on a file or a directory. +//! - [x] `std::fs::set_permissions` - `fs.path(path).set_permissions(perms)` - Changes the permissions found on a file or a directory. //! - [x] `std::fs::symlink_metadata` - `fs.path(path).symlink_metadata()` - Query the metadata about a file without following symlinks. //! - [x] `std::fs::write` - `fs.file(path).write()` - Write a slice as the entire contents of a file. //! diff --git a/src/fs/path.rs b/src/fs/path.rs index d284cc0..101e69a 100644 --- a/src/fs/path.rs +++ b/src/fs/path.rs @@ -320,6 +320,27 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> { self.check_error()?; std::fs::symlink_metadata(self.as_pathbuf()).map_err(Error::Io) } + + /// Sets the permissions of a file or directory. + /// + /// Wrapper for [std::fs::set_permissions] + /// + /// ``` + /// # use kxio::fs::Result; + /// # use std::os::unix::fs::PermissionsExt; + /// # fn main() -> Result<()> { + /// let fs = kxio::fs::temp()?; + /// let path = fs.base().join("foo"); + /// let file = fs.file(&path); + /// # file.write("bar")?; + /// file.set_permissions(std::fs::Permissions::from_mode(0o755))?; + /// # Ok(()) + /// # } + /// ``` + pub fn set_permissions(&self, perms: std::fs::Permissions) -> Result<()> { + self.check_error()?; + std::fs::set_permissions(self.as_pathbuf(), perms).map_err(Error::Io) + } } impl<'base, 'path> From> for PathBuf { fn from(path: PathReal) -> Self { diff --git a/tests/fs.rs b/tests/fs.rs index c47b662..4195859 100644 --- a/tests/fs.rs +++ b/tests/fs.rs @@ -7,6 +7,31 @@ type TestResult = Result<(), fs::Error>; mod path { use super::*; + mod set_permissions { + use super::*; + + #[test] + fn should_set_permissions() -> TestResult { + let fs = fs::temp().expect("temp fs"); + let path = fs.base().join("foo"); + let file = fs.file(&path); + file.write("bar").expect("write"); + let md = file.metadata().expect("metadata"); + assert!(md.is_file()); + + let mut perms = md.permissions(); + perms.set_readonly(true); + + let path = fs.path(&path); + path.set_permissions(perms).expect("set_permissions"); + + let md = file.metadata().expect("metadata"); + assert!(md.is_file()); + assert!(md.permissions().readonly()); + + Ok(()) + } + } mod metadata { use super::*;