File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed
Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -67,3 +67,28 @@ pub fn open_dir(path: &str, create: bool) -> anyhow::Result<Directory> {
6767 _ => Err ( anyhow:: anyhow!( "vfs: unexpected message: {:?}" , message) ) ,
6868 }
6969}
70+
71+ /// Removes a dir at path, errors if path not found or path is not a directory.
72+ pub fn remove_dir ( path : & str ) -> anyhow:: Result < ( ) > {
73+ let request = VfsRequest {
74+ path : path. to_string ( ) ,
75+ action : VfsAction :: RemoveDir ,
76+ } ;
77+
78+ let message = Request :: new ( )
79+ . target ( ( "our" , "vfs" , "distro" , "sys" ) )
80+ . body ( serde_json:: to_vec ( & request) ?)
81+ . send_and_await_response ( 5 ) ?;
82+
83+ match message {
84+ Ok ( Message :: Response { body, .. } ) => {
85+ let response = serde_json:: from_slice :: < VfsResponse > ( & body) ?;
86+ match response {
87+ VfsResponse :: Ok => Ok ( ( ) ) ,
88+ VfsResponse :: Err ( e) => Err ( e. into ( ) ) ,
89+ _ => Err ( anyhow:: anyhow!( "vfs: unexpected response: {:?}" , response) ) ,
90+ }
91+ }
92+ _ => Err ( anyhow:: anyhow!( "vfs: unexpected message: {:?}" , message) ) ,
93+ }
94+ }
Original file line number Diff line number Diff line change @@ -418,3 +418,28 @@ pub fn create_file(path: &str) -> anyhow::Result<File> {
418418 _ => Err ( anyhow:: anyhow!( "vfs: unexpected message: {:?}" , message) ) ,
419419 }
420420}
421+
422+ /// Removes a file at path, errors if path not found or path is not a file.
423+ pub fn remove_file ( path : & str ) -> anyhow:: Result < ( ) > {
424+ let request = VfsRequest {
425+ path : path. to_string ( ) ,
426+ action : VfsAction :: RemoveFile ,
427+ } ;
428+
429+ let message = Request :: new ( )
430+ . target ( ( "our" , "vfs" , "distro" , "sys" ) )
431+ . body ( serde_json:: to_vec ( & request) ?)
432+ . send_and_await_response ( 5 ) ?;
433+
434+ match message {
435+ Ok ( Message :: Response { body, .. } ) => {
436+ let response = serde_json:: from_slice :: < VfsResponse > ( & body) ?;
437+ match response {
438+ VfsResponse :: Ok => Ok ( ( ) ) ,
439+ VfsResponse :: Err ( e) => Err ( e. into ( ) ) ,
440+ _ => Err ( anyhow:: anyhow!( "vfs: unexpected response: {:?}" , response) ) ,
441+ }
442+ }
443+ _ => Err ( anyhow:: anyhow!( "vfs: unexpected message: {:?}" , message) ) ,
444+ }
445+ }
Original file line number Diff line number Diff line change @@ -149,3 +149,16 @@ pub fn metadata(path: &str) -> anyhow::Result<FileMetadata> {
149149 _ => Err ( anyhow:: anyhow!( "vfs: unexpected message: {:?}" , message) ) ,
150150 }
151151}
152+
153+ /// Removes a path, if it's either a directory or a file.
154+ pub fn remove_path ( path : & str ) -> anyhow:: Result < ( ) > {
155+ let meta = metadata ( path) ?;
156+ match meta. file_type {
157+ FileType :: Directory => remove_dir ( path) ,
158+ FileType :: File => remove_file ( path) ,
159+ _ => Err ( anyhow:: anyhow!(
160+ "vfs: path is not a file or directory: {}" ,
161+ path
162+ ) ) ,
163+ }
164+ }
You can’t perform that action at this time.
0 commit comments