feat(fs): add .path(path).set_permissions(perms)
All checks were successful
Rust / build (map[name:nightly]) (push) Successful in 3m56s
Rust / build (map[name:stable]) (push) Successful in 5m42s
Release Please / Release-plz (push) Successful in 45s

This commit is contained in:
Paul Campbell 2024-11-03 20:31:11 +00:00
parent abd854f749
commit de46ff57c1
3 changed files with 47 additions and 1 deletions

View file

@ -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_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::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. //! - [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::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. //! - [x] `std::fs::write` - `fs.file(path).write()` - Write a slice as the entire contents of a file.
//! //!

View file

@ -320,6 +320,27 @@ impl<'base, 'path, T: PathType> PathReal<'base, 'path, T> {
self.check_error()?; self.check_error()?;
std::fs::symlink_metadata(self.as_pathbuf()).map_err(Error::Io) 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<PathHandle<'base, 'path, PathMarker>> for PathBuf { impl<'base, 'path> From<PathHandle<'base, 'path, PathMarker>> for PathBuf {
fn from(path: PathReal<PathMarker>) -> Self { fn from(path: PathReal<PathMarker>) -> Self {

View file

@ -7,6 +7,31 @@ type TestResult = Result<(), fs::Error>;
mod path { mod path {
use super::*; 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 { mod metadata {
use super::*; use super::*;