Skip to content

Commit

Permalink
fix: with_get_uid and with_get_gid constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Oct 22, 2024
1 parent 4665587 commit e42a1fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Changelog

- [Changelog](#changelog)
- [0.1.1](#011)
- [0.1.0](#010)

## 0.1.1

Released on 22/10/2024

- `with_get_uid` constructor
- `with_get_gid` constructor

## 0.1.0
42 changes: 36 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub struct MemoryFs {
tree: FsTree,
wrkdir: PathBuf,
connected: bool,
// Fn to get uid
get_uid: Box<dyn Fn() -> u32>,
// Fn to get gid
get_gid: Box<dyn Fn() -> u32>,
}

#[derive(Debug, Clone)]
Expand All @@ -106,14 +110,35 @@ impl Write for WriteHandle {
}

impl MemoryFs {
/// Create a new instance of the [`MemoryFs`] with the provided [`FsTree`].
pub fn new(tree: FsTree) -> Self {
Self {
tree,
wrkdir: PathBuf::from("/"),
connected: false,
get_uid: Box::new(|| 0),
get_gid: Box::new(|| 0),
}
}

/// Set the function to get the user id (uid).
pub fn with_get_uid<F>(mut self, get_uid: F) -> Self
where
F: Fn() -> u32 + 'static,
{
self.get_uid = Box::new(get_uid);
self
}

/// Set the function to get the group id (gid).
pub fn with_get_gid<F>(mut self, get_gid: F) -> Self
where
F: Fn() -> u32 + 'static,
{
self.get_gid = Box::new(get_gid);
self
}

fn absolutize(&self, path: &Path) -> PathBuf {
if path.is_absolute() {
path.to_path_buf()
Expand Down Expand Up @@ -345,7 +370,7 @@ impl RemoteFs for MemoryFs {
.unwrap_or_else(|| Path::new("/"))
.to_path_buf();

let dir = Inode::dir(0, 0, mode);
let dir = Inode::dir((self.get_uid)(), (self.get_gid)(), mode);

let parent = self
.tree
Expand Down Expand Up @@ -380,7 +405,12 @@ impl RemoteFs for MemoryFs {
.unwrap_or_else(|| Path::new("/"))
.to_path_buf();

let symlink = Inode::symlink(0, 0, UnixPex::from(0o755), target.to_path_buf());
let symlink = Inode::symlink(
(self.get_uid)(),
(self.get_gid)(),
UnixPex::from(0o755),
target.to_path_buf(),
);

let parent = self
.tree
Expand Down Expand Up @@ -497,8 +527,8 @@ impl RemoteFs for MemoryFs {
};

let file = Inode::file(
0,
0,
metadata.uid.unwrap_or((self.get_uid)()),
metadata.gid.unwrap_or((self.get_gid)()),
metadata.mode.unwrap_or_else(|| UnixPex::from(0o755)),
content.clone().unwrap_or_default(),
);
Expand Down Expand Up @@ -535,8 +565,8 @@ impl RemoteFs for MemoryFs {
.ok_or_else(|| RemoteError::new(RemoteErrorType::NoSuchFileOrDirectory))?;

let file = Inode::file(
0,
0,
metadata.uid.unwrap_or((self.get_uid)()),
metadata.gid.unwrap_or((self.get_gid)()),
metadata.mode.unwrap_or_else(|| UnixPex::from(0o755)),
vec![],
);
Expand Down
15 changes: 15 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,21 @@ fn should_not_make_symlink() {
finalize_client(client);
}

#[test]
fn test_should_set_gid_and_uid() {
let mut fs = setup_client().with_get_gid(|| 1000).with_get_uid(|| 100);

// create dir
let dir_path = Path::new("test");
assert!(fs.create_dir(dir_path, UnixPex::from(0o775)).is_ok());

// stat
let entry = fs.stat(dir_path).unwrap();
let stat = entry.metadata();
assert_eq!(stat.gid.unwrap(), 1000);
assert_eq!(stat.uid.unwrap(), 100);
}

fn setup_client() -> MemoryFs {
let tempdir = PathBuf::from("/tmp");
let tree = Tree::new(node!(
Expand Down

0 comments on commit e42a1fe

Please sign in to comment.