Skip to main content

Mountain/RPC/CocoonService/FileSystem/
Readdir.rs

1#![allow(non_snake_case)]
2
3//! Enumerate the entries of a directory by name.
4
5use tonic::{Response, Status};
6
7use crate::{
8	RPC::CocoonService::CocoonServiceImpl,
9	Vine::Generated::{ReaddirRequest, ReaddirResponse},
10	dev_log,
11};
12
13pub async fn Fn(_Service:&CocoonServiceImpl, Request:ReaddirRequest) -> Result<Response<ReaddirResponse>, Status> {
14	let Path = CocoonServiceImpl::UriToPath(Request.uri.as_ref())
15		.ok_or_else(|| Status::invalid_argument("readdir: missing or empty URI"))?;
16
17	dev_log!("cocoon", "[CocoonService] Readdir: {:?}", Path);
18
19	let mut ReadDir = tokio::fs::read_dir(&Path).await.map_err(|Error| {
20		dev_log!("cocoon", "warn: [CocoonService] readdir failed for {:?}: {}", Path, Error);
21		Status::not_found(format!("readdir: {}: {}", Path.display(), Error))
22	})?;
23
24	let mut Entries = Vec::new();
25	while let Ok(Some(Entry)) = ReadDir.next_entry().await {
26		if let Some(Name) = Entry.file_name().to_str() {
27			Entries.push(Name.to_string());
28		}
29	}
30
31	Ok(Response::new(ReaddirResponse { entries:Entries }))
32}