Skip to content

Commit c1ac722

Browse files
authored
Merge pull request #45 from kinode-dao/bp/vfs-removals
vfs: add remove_file, remove_dir, remove_path
2 parents ea5e303 + 1553aa9 commit c1ac722

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/vfs/directory.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

src/vfs/file.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

src/vfs/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)