Skip to main content

Mountain/RPC/CocoonService/FileSystem/
RenameFile.rs

1#![allow(non_snake_case)]
2
3//! Rename a file or directory, creating any missing target parents first.
4
5use tonic::{Response, Status};
6
7use crate::{
8	RPC::CocoonService::CocoonServiceImpl,
9	Vine::Generated::{Empty, RenameFileRequest},
10	dev_log,
11};
12
13pub async fn Fn(_Service:&CocoonServiceImpl, Request:RenameFileRequest) -> Result<Response<Empty>, Status> {
14	let OldPath = CocoonServiceImpl::UriToPath(Request.source.as_ref())
15		.ok_or_else(|| Status::invalid_argument("rename_file: missing source URI"))?;
16
17	let NewPath = CocoonServiceImpl::UriToPath(Request.target.as_ref())
18		.ok_or_else(|| Status::invalid_argument("rename_file: missing target URI"))?;
19
20	dev_log!("cocoon", "[CocoonService] rename_file: {:?} → {:?}", OldPath, NewPath);
21
22	if let Some(Parent) = NewPath.parent() {
23		if !Parent.as_os_str().is_empty() {
24			tokio::fs::create_dir_all(Parent)
25				.await
26				.map_err(|Error| Status::internal(format!("rename_file: create_dir_all failed: {}", Error)))?;
27		}
28	}
29
30	tokio::fs::rename(&OldPath, &NewPath)
31		.await
32		.map_err(|Error| Status::internal(format!("rename_file: {}: {}", OldPath.display(), Error)))?;
33
34	Ok(Response::new(Empty {}))
35}