Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RPC/CocoonService/FileSystem/
Stat.rs

1//! Inspect a path: type, size, mtime in ms-since-epoch.
2
3use std::time::UNIX_EPOCH;
4
5use tonic::{Response, Status};
6
7use crate::{
8	RPC::CocoonService::CocoonServiceImpl,
9	Vine::Generated::{StatRequest, StatResponse},
10	dev_log,
11};
12
13pub async fn Fn(_Service:&CocoonServiceImpl, Request:StatRequest) -> Result<Response<StatResponse>, Status> {
14	let Path = CocoonServiceImpl::UriToPath(Request.uri.as_ref())
15		.ok_or_else(|| Status::invalid_argument("stat: missing or empty URI"))?;
16
17	dev_log!("cocoon", "[CocoonService] Stat: {:?}", Path);
18
19	let Metadata = tokio::fs::metadata(&Path).await.map_err(|Error| {
20		dev_log!("cocoon", "warn: [CocoonService] stat failed for {:?}: {}", Path, Error);
21		Status::not_found(format!("stat: {}: {}", Path.display(), Error))
22	})?;
23
24	let MTime = Metadata
25		.modified()
26		.ok()
27		.and_then(|T| T.duration_since(UNIX_EPOCH).ok())
28		.map(|D| D.as_millis() as u64)
29		.unwrap_or(0);
30
31	Ok(Response::new(StatResponse {
32		is_file:Metadata.is_file(),
33		is_directory:Metadata.is_dir(),
34		size:Metadata.len(),
35		mtime:MTime,
36	}))
37}