Skip to main content

Mountain/RPC/CocoonService/GenericRequest/FileSystem/
Stat.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3use std::time::UNIX_EPOCH;
4
5use serde_json::{Value, json};
6use tonic::Response;
7
8use crate::Vine::Generated::GenericResponse;
9
10pub async fn Fn(RequestId:u64, Params:Value) -> Response<GenericResponse> {
11	let Path = Params
12		.as_str()
13		.or_else(|| Params.get("path").and_then(|V| V.as_str()))
14		.unwrap_or("");
15
16	match tokio::fs::metadata(Path).await {
17		Ok(Meta) => {
18			let Mtime = Meta
19				.modified()
20				.ok()
21				.and_then(|T| T.duration_since(UNIX_EPOCH).ok())
22				.map(|D| D.as_millis() as u64)
23				.unwrap_or(0);
24
25			super::OkResponse(
26				RequestId,
27				&json!({
28					"type": if Meta.is_dir() { 2 } else { 1 },
29					"is_file": Meta.is_file(),
30					"is_directory": Meta.is_dir(),
31					"size": Meta.len(),
32					"mtime": Mtime,
33				}),
34			)
35		},
36
37		Err(Error) => super::ErrResponse(RequestId, -32000, format!("fs.stat: {}", Error)),
38	}
39}