From de46ff57c13e5a22c19216996089695a115d1ab2 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 3 Nov 2024 20:31:11 +0000 Subject: [PATCH] feat(fs): add .path(path).set_permissions(perms) --- src/fs/mod.rs | 2 +- src/fs/path.rs | 21 +++++++++++++++++++++ tests/fs.rs | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) 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 93e2b2f..99f7ca5 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::*;