Skip to main content

Mountain/RPC/CocoonService/FileSystem/
RenameFile.rs

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