Mountain/IPC/WindServiceHandlers/FileSystem/Native/
FileReaddirNative.rs1use serde_json::{Value, json};
7
8use crate::{IPC::WindServiceHandlers::Utilities::PathExtraction::Fn as extract_path_from_arg, dev_log};
9
10pub async fn Fn(Arguments:Vec<Value>) -> Result<Value, String> {
11 let Path = extract_path_from_arg(Arguments.get(0).ok_or("Missing directory path")?)?;
12
13 dev_log!("vfs", "readdir: {}", Path);
19
20 let mut Entries = tokio::fs::read_dir(&Path).await.map_err(|E| {
21 if E.kind() == std::io::ErrorKind::NotFound {
22 format!("readdir: {} resource not found ({})", Path, E)
23 } else {
24 format!("Failed to readdir: {} ({})", Path, E)
25 }
26 })?;
27
28 let mut Result = Vec::new();
29
30 while let Some(Entry) = Entries.next_entry().await.map_err(|E| E.to_string())? {
31 let Name = Entry.file_name().to_string_lossy().to_string();
32
33 let FileType = Entry.file_type().await.map_err(|E| E.to_string())?;
34
35 let TypeValue = if FileType.is_symlink() {
36 64
37 } else if FileType.is_dir() {
38 2
39 } else {
40 1
41 };
42
43 Result.push(json!([Name, TypeValue]));
44 }
45
46 Ok(json!(Result))
47}