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