@@ -18,6 +18,8 @@ pub enum SysError {
18
18
ReadFileMetadata ( io:: Error ) ,
19
19
#[ error( "Unable to write file. {0}" ) ]
20
20
WriteFile ( io:: Error ) ,
21
+ #[ error( "Unable to remove file. {0}" ) ]
22
+ RemoveFile ( io:: Error ) ,
21
23
#[ error( "Error while trying to find the path for the command '{0}'" ) ]
22
24
FindCommandPath ( String ) ,
23
25
#[ error( "Command '{0}' not found." ) ]
@@ -95,10 +97,19 @@ async fn fwrite(data: &str, file: &Path) -> Result<usize, FnError> {
95
97
96
98
#[ nasl_function]
97
99
async fn file_stat ( path : & Path ) -> Result < u64 , FnError > {
98
- let metadata = std:: fs:: metadata ( path) . map_err ( |e| SysError :: ReadFileMetadata ( e) ) ?;
100
+ let metadata = tokio:: fs:: metadata ( path)
101
+ . await
102
+ . map_err ( |e| SysError :: ReadFileMetadata ( e) ) ?;
99
103
Ok ( metadata. len ( ) )
100
104
}
101
105
106
+ #[ nasl_function]
107
+ async fn unlink ( path : & Path ) -> Result < ( ) , FnError > {
108
+ tokio:: fs:: remove_file ( path)
109
+ . await
110
+ . map_err ( |e| SysError :: RemoveFile ( e) . into ( ) )
111
+ }
112
+
102
113
#[ nasl_function]
103
114
async fn get_tmp_dir ( ) -> PathBuf {
104
115
env:: temp_dir ( )
@@ -108,18 +119,19 @@ function_set! {
108
119
Sys ,
109
120
async_stateless,
110
121
(
111
- ( pread, "pread" ) ,
112
- ( fread, "fread" ) ,
113
- ( file_stat, "file_stat" ) ,
114
- ( find_in_path, "find_in_path" ) ,
115
- ( fwrite, "fwrite" ) ,
116
- ( get_tmp_dir, "get_tmp_dir" ) ,
122
+ pread,
123
+ fread,
124
+ file_stat,
125
+ find_in_path,
126
+ fwrite,
127
+ get_tmp_dir,
128
+ unlink,
117
129
)
118
130
}
119
131
120
132
#[ cfg( test) ]
121
133
mod tests {
122
- use crate :: nasl:: test_prelude:: * ;
134
+ use crate :: nasl:: { builtin :: sys :: SysError , test_prelude:: * } ;
123
135
124
136
#[ tokio:: test]
125
137
async fn pread ( ) {
@@ -146,6 +158,8 @@ mod tests {
146
158
t. ok ( r#"fwrite(file: file, data: "foo");"# , 3 ) ;
147
159
t. ok ( r#"fread(file);"# , "foo" ) ;
148
160
t. ok ( r#"file_stat(file);"# , 3 ) ;
161
+ t. run ( r#"unlink(file);"# ) ;
162
+ check_err_matches ! ( t, r#"file_stat(file);"# , SysError :: ReadFileMetadata ( _) ) ;
149
163
t. async_verify ( ) . await ;
150
164
}
151
165
}
0 commit comments