Skip to content

Commit e42a1fe

Browse files
committed
fix: with_get_uid and with_get_gid constructors
1 parent 4665587 commit e42a1fe

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Changelog
22

33
- [Changelog](#changelog)
4+
- [0.1.1](#011)
45
- [0.1.0](#010)
56

7+
## 0.1.1
8+
9+
Released on 22/10/2024
10+
11+
- `with_get_uid` constructor
12+
- `with_get_gid` constructor
13+
614
## 0.1.0

src/lib.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ pub struct MemoryFs {
8080
tree: FsTree,
8181
wrkdir: PathBuf,
8282
connected: bool,
83+
// Fn to get uid
84+
get_uid: Box<dyn Fn() -> u32>,
85+
// Fn to get gid
86+
get_gid: Box<dyn Fn() -> u32>,
8387
}
8488

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

108112
impl MemoryFs {
113+
/// Create a new instance of the [`MemoryFs`] with the provided [`FsTree`].
109114
pub fn new(tree: FsTree) -> Self {
110115
Self {
111116
tree,
112117
wrkdir: PathBuf::from("/"),
113118
connected: false,
119+
get_uid: Box::new(|| 0),
120+
get_gid: Box::new(|| 0),
114121
}
115122
}
116123

124+
/// Set the function to get the user id (uid).
125+
pub fn with_get_uid<F>(mut self, get_uid: F) -> Self
126+
where
127+
F: Fn() -> u32 + 'static,
128+
{
129+
self.get_uid = Box::new(get_uid);
130+
self
131+
}
132+
133+
/// Set the function to get the group id (gid).
134+
pub fn with_get_gid<F>(mut self, get_gid: F) -> Self
135+
where
136+
F: Fn() -> u32 + 'static,
137+
{
138+
self.get_gid = Box::new(get_gid);
139+
self
140+
}
141+
117142
fn absolutize(&self, path: &Path) -> PathBuf {
118143
if path.is_absolute() {
119144
path.to_path_buf()
@@ -345,7 +370,7 @@ impl RemoteFs for MemoryFs {
345370
.unwrap_or_else(|| Path::new("/"))
346371
.to_path_buf();
347372

348-
let dir = Inode::dir(0, 0, mode);
373+
let dir = Inode::dir((self.get_uid)(), (self.get_gid)(), mode);
349374

350375
let parent = self
351376
.tree
@@ -380,7 +405,12 @@ impl RemoteFs for MemoryFs {
380405
.unwrap_or_else(|| Path::new("/"))
381406
.to_path_buf();
382407

383-
let symlink = Inode::symlink(0, 0, UnixPex::from(0o755), target.to_path_buf());
408+
let symlink = Inode::symlink(
409+
(self.get_uid)(),
410+
(self.get_gid)(),
411+
UnixPex::from(0o755),
412+
target.to_path_buf(),
413+
);
384414

385415
let parent = self
386416
.tree
@@ -497,8 +527,8 @@ impl RemoteFs for MemoryFs {
497527
};
498528

499529
let file = Inode::file(
500-
0,
501-
0,
530+
metadata.uid.unwrap_or((self.get_uid)()),
531+
metadata.gid.unwrap_or((self.get_gid)()),
502532
metadata.mode.unwrap_or_else(|| UnixPex::from(0o755)),
503533
content.clone().unwrap_or_default(),
504534
);
@@ -535,8 +565,8 @@ impl RemoteFs for MemoryFs {
535565
.ok_or_else(|| RemoteError::new(RemoteErrorType::NoSuchFileOrDirectory))?;
536566

537567
let file = Inode::file(
538-
0,
539-
0,
568+
metadata.uid.unwrap_or((self.get_uid)()),
569+
metadata.gid.unwrap_or((self.get_gid)()),
540570
metadata.mode.unwrap_or_else(|| UnixPex::from(0o755)),
541571
vec![],
542572
);

src/test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,21 @@ fn should_not_make_symlink() {
525525
finalize_client(client);
526526
}
527527

528+
#[test]
529+
fn test_should_set_gid_and_uid() {
530+
let mut fs = setup_client().with_get_gid(|| 1000).with_get_uid(|| 100);
531+
532+
// create dir
533+
let dir_path = Path::new("test");
534+
assert!(fs.create_dir(dir_path, UnixPex::from(0o775)).is_ok());
535+
536+
// stat
537+
let entry = fs.stat(dir_path).unwrap();
538+
let stat = entry.metadata();
539+
assert_eq!(stat.gid.unwrap(), 1000);
540+
assert_eq!(stat.uid.unwrap(), 100);
541+
}
542+
528543
fn setup_client() -> MemoryFs {
529544
let tempdir = PathBuf::from("/tmp");
530545
let tree = Tree::new(node!(

0 commit comments

Comments
 (0)