Skip to content

Commit d0b8f5f

Browse files
committed
Implement unlink.
1 parent f7ebfdd commit d0b8f5f

File tree

1 file changed

+22
-8
lines changed
  • rust/src/nasl/builtin/sys

1 file changed

+22
-8
lines changed

rust/src/nasl/builtin/sys/mod.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub enum SysError {
1818
ReadFileMetadata(io::Error),
1919
#[error("Unable to write file. {0}")]
2020
WriteFile(io::Error),
21+
#[error("Unable to remove file. {0}")]
22+
RemoveFile(io::Error),
2123
#[error("Error while trying to find the path for the command '{0}'")]
2224
FindCommandPath(String),
2325
#[error("Command '{0}' not found.")]
@@ -95,10 +97,19 @@ async fn fwrite(data: &str, file: &Path) -> Result<usize, FnError> {
9597

9698
#[nasl_function]
9799
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))?;
99103
Ok(metadata.len())
100104
}
101105

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+
102113
#[nasl_function]
103114
async fn get_tmp_dir() -> PathBuf {
104115
env::temp_dir()
@@ -108,18 +119,19 @@ function_set! {
108119
Sys,
109120
async_stateless,
110121
(
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,
117129
)
118130
}
119131

120132
#[cfg(test)]
121133
mod tests {
122-
use crate::nasl::test_prelude::*;
134+
use crate::nasl::{builtin::sys::SysError, test_prelude::*};
123135

124136
#[tokio::test]
125137
async fn pread() {
@@ -146,6 +158,8 @@ mod tests {
146158
t.ok(r#"fwrite(file: file, data: "foo");"#, 3);
147159
t.ok(r#"fread(file);"#, "foo");
148160
t.ok(r#"file_stat(file);"#, 3);
161+
t.run(r#"unlink(file);"#);
162+
check_err_matches!(t, r#"file_stat(file);"#, SysError::ReadFileMetadata(_));
149163
t.async_verify().await;
150164
}
151165
}

0 commit comments

Comments
 (0)