From 4f1e31999d1f1a39c559578725f7cb43e23e6288 Mon Sep 17 00:00:00 2001 From: weisd Date: Wed, 25 Sep 2024 15:27:23 +0800 Subject: [PATCH] update stock api --- ecstore/src/disk/error.rs | 2 +- ecstore/src/disk/local.rs | 33 ++++++++++++++++++++++++++++----- ecstore/src/disk/os.rs | 16 ++++++++++++---- ecstore/src/error.rs | 8 ++++++++ ecstore/src/utils/path.rs | 6 ------ 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/ecstore/src/disk/error.rs b/ecstore/src/disk/error.rs index 145b4d195..535273c2f 100644 --- a/ecstore/src/disk/error.rs +++ b/ecstore/src/disk/error.rs @@ -205,7 +205,7 @@ pub fn clone_disk_err(e: &DiskError) -> Error { } } -pub fn ioerr_to_err(e: io::Error) -> Error { +pub fn os_err_to_file_err(e: io::Error) -> Error { match e.kind() { io::ErrorKind::NotFound => Error::new(DiskError::FileNotFound), io::ErrorKind::PermissionDenied => Error::new(DiskError::FileAccessDenied), diff --git a/ecstore/src/disk/local.rs b/ecstore/src/disk/local.rs index 768e3ac75..c1a9900d4 100644 --- a/ecstore/src/disk/local.rs +++ b/ecstore/src/disk/local.rs @@ -4,7 +4,7 @@ use super::{ os, DeleteOptions, DiskAPI, DiskLocation, FileInfoVersions, FileReader, FileWriter, MetaCacheEntry, ReadMultipleReq, ReadMultipleResp, ReadOptions, RenameDataResp, UpdateMetadataOpts, VolumeInfo, WalkDirOptions, }; -use crate::disk::error::{is_sys_err_not_dir, map_err_not_exists}; +use crate::disk::error::{is_sys_err_not_dir, map_err_not_exists, os_err_to_file_err}; use crate::disk::os::check_path_length; use crate::disk::{LocalFileReader, LocalFileWriter, STORAGE_FORMAT_FILE}; use crate::utils::fs::lstat; @@ -25,7 +25,6 @@ use time::OffsetDateTime; use tokio::fs::{self, File}; use tokio::io::ErrorKind; use tokio::sync::Mutex; -use tower::layer::util; use tracing::{debug, warn}; use uuid::Uuid; @@ -679,7 +678,31 @@ impl DiskAPI for LocalDisk { } } - unimplemented!() + if let Err(err) = os::rename_all(&src_file_path, &dst_file_path, &dst_volume_dir).await { + if let Some(e) = err.to_io_err() { + if is_sys_err_not_empty(&e) || is_sys_err_not_dir(&e) { + return Err(Error::new(DiskError::FileAccessDenied)); + } + + return Err(os_err_to_file_err(e)); + } + + return Err(err); + } + + if let Err(err) = self.write_all(&dst_volume, format!("{}.meta", dst_path).as_str(), meta).await { + if let Some(e) = err.to_io_err() { + return Err(os_err_to_file_err(e)); + } + + return Err(err); + } + + if let Some(parent) = src_file_path.parent() { + self.delete_file(&src_volume_dir, &parent.to_path_buf(), false, false).await?; + } + + Ok(()) } async fn rename_file(&self, src_volume: &str, src_path: &str, dst_volume: &str, dst_path: &str) -> Result<()> { let src_volume_path = self.get_bucket_path(src_volume)?; @@ -1009,8 +1032,8 @@ impl DiskAPI for LocalDisk { let p = self.get_bucket_path(volume)?; - if let Err(err) = utils::fs::access(&p).await { - if os_is_not_exist(err) { + if let Err(e) = utils::fs::access(&p).await { + if os_is_not_exist(&e) { os::make_dir_all(&p).await?; } } diff --git a/ecstore/src/disk/os.rs b/ecstore/src/disk/os.rs index e9a100bb6..3bf4d10e4 100644 --- a/ecstore/src/disk/os.rs +++ b/ecstore/src/disk/os.rs @@ -12,7 +12,7 @@ use crate::{ utils, }; -use super::error::{ioerr_to_err, os_is_exist, DiskError}; +use super::error::{os_err_to_file_err, os_is_exist, DiskError}; pub fn check_path_length(path_name: &str) -> Result<()> { // Apple OS X path length is limited to 1016 @@ -55,7 +55,7 @@ pub fn check_path_length(path_name: &str) -> Result<()> { pub async fn make_dir_all(path: impl AsRef) -> Result<()> { check_path_length(path.as_ref().to_string_lossy().to_string().as_str())?; - utils::fs::make_dir_all(path.as_ref()).map_err(ioerr_to_err).await?; + utils::fs::make_dir_all(path.as_ref()).map_err(os_err_to_file_err).await?; Ok(()) } @@ -147,7 +147,7 @@ pub async fn reliable_rename( pub async fn reliable_mkdir_all(path: impl AsRef, base_dir: impl AsRef) -> io::Result<()> { let mut i = 0; - let mut base_dir = base_dir.as_ref().clone(); + let mut base_dir = base_dir.as_ref(); loop { if let Err(e) = os_mkdir_all(path.as_ref(), base_dir).await { if os_is_not_exist(&e) && i == 0 { @@ -180,7 +180,15 @@ pub async fn os_mkdir_all(dir_path: impl AsRef, base_dir: impl AsRef } if let Some(parent) = dir_path.as_ref().parent() { - Box::pin(os_mkdir_all(parent, base_dir)).await?; + // 不支持递归,直接create_dir_all了 + if let Err(e) = fs::create_dir_all(&parent).await { + if os_is_exist(&e) { + return Ok(()); + } + + return Err(e); + } + // Box::pin(os_mkdir_all(&parent, &base_dir)).await?; } if let Err(e) = utils::fs::mkdir(dir_path.as_ref()).await { diff --git a/ecstore/src/error.rs b/ecstore/src/error.rs index aa513fc58..1fc8dfabf 100644 --- a/ecstore/src/error.rs +++ b/ecstore/src/error.rs @@ -65,6 +65,14 @@ impl Error { pub fn downcast_mut(&mut self) -> Option<&mut T> { self.inner.downcast_mut() } + + pub fn to_io_err(&self) -> Option { + if let Some(e) = self.downcast_ref::() { + Some(io::Error::new(e.kind(), e.to_string())) + } else { + None + } + } } impl From for Error { diff --git a/ecstore/src/utils/path.rs b/ecstore/src/utils/path.rs index 49a7dc9e4..79bfc6ced 100644 --- a/ecstore/src/utils/path.rs +++ b/ecstore/src/utils/path.rs @@ -1,5 +1,3 @@ -use std::path::Path; - const GLOBAL_DIR_SUFFIX: &str = "__XLDIR__"; pub const SLASH_SEPARATOR: &str = "/"; @@ -39,7 +37,3 @@ pub fn retain_slash(s: &str) -> String { format!("{}{}", s, SLASH_SEPARATOR) } } - -pub fn join(p1: &str, p2: &str) -> String { - Path::new(p1).join(Path::new(p2)).to_string_lossy().to_string() -}