@@ -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
108112impl 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 ) ;
0 commit comments