feat(fs): add .path(path).set_permissions(perms)
This commit is contained in:
parent
0db3bd40fe
commit
10a745899d
3 changed files with 47 additions and 1 deletions
|
@ -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.
|
||||
//!
|
||||
|
|
|
@ -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<PathHandle<'base, 'path, PathMarker>> for PathBuf {
|
||||
fn from(path: PathReal<PathMarker>) -> Self {
|
||||
|
|
25
tests/fs.rs
25
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::*;
|
||||
|
||||
|
|
Loading…
Reference in a new issue