test:put_object

This commit is contained in:
weisd
2024-07-05 17:29:31 +08:00
parent 91c3fc2fe1
commit 98df9b91ba
3 changed files with 60 additions and 5 deletions
+56 -5
View File
@@ -184,6 +184,43 @@ impl LocalDisk {
clean_tmp: true,
})
}
pub async fn rename_all(&self, src_data_path: &PathBuf, dst_data_path: &PathBuf, skip: &PathBuf) -> Result<()> {
if !skip.starts_with(&src_data_path) {
fs::create_dir_all(dst_data_path.parent().unwrap_or(Path::new("/"))).await?;
}
debug!(
"rename_all from \n {:?} \n to \n {:?} \n skip:{:?}",
&src_data_path, &dst_data_path, &skip
);
fs::rename(&src_data_path, &dst_data_path).await?;
Ok(())
}
pub async fn delete_file(&self, base_path: &PathBuf, delete_path: &PathBuf) -> Result<()> {
if is_root_path(base_path) || is_root_path(delete_path) {
return Ok(());
}
if !delete_path.starts_with(base_path) || base_path == delete_path {
return Ok(());
}
if delete_path.is_dir() {
fs::remove_dir(delete_path).await?;
} else {
fs::remove_file(delete_path).await?;
}
Ok(())
}
}
fn is_root_path(path: &PathBuf) -> bool {
path.components().count() == 1 && path.has_root()
}
// 过滤 std::io::ErrorKind::NotFound
@@ -337,9 +374,9 @@ impl DiskAPI for LocalDisk {
}
async fn rename_data(&self, src_volume: &str, src_path: &str, fi: &FileInfo, dst_volume: &str, dst_path: &str) -> Result<()> {
let src_volume_path = self.get_bucket_path(&src_volume)?;
if !skip_access_checks(&src_volume) {
let vol_path = self.get_bucket_path(&src_volume)?;
check_volume_exists(&vol_path).await?;
check_volume_exists(&src_volume_path).await?;
}
let dst_volume_path = self.get_bucket_path(&dst_volume)?;
@@ -369,15 +406,15 @@ impl DiskAPI for LocalDisk {
}
};
let curreng_data_path = self.get_object_path(&dst_volume, &dst_path);
// let curreng_data_path = self.get_object_path(&dst_volume, &dst_path);
let mut meta = FileMeta::new();
let (dst_buf, _) = read_file_exists(&dst_file_path).await?;
let mut skipParent = dst_volume_path.as_path();
let mut skipParent = dst_volume_path;
if !&dst_buf.is_empty() {
skipParent = skipParent.parent().unwrap_or(Path::new("/"));
skipParent = PathBuf::from(&dst_file_path.parent().unwrap_or(Path::new("/")));
}
if !dst_buf.is_empty() {
@@ -395,6 +432,20 @@ impl DiskAPI for LocalDisk {
fs::write(&src_file_path, fm_data).await?;
let no_inline = src_data_path.has_root() && fi.data.is_empty() && fi.size > 0;
if no_inline {
self.rename_all(&src_data_path, &dst_data_path, &skipParent).await?;
}
self.rename_all(&src_file_path, &dst_file_path, &skipParent).await?;
if src_volume != RUSTFS_META_MULTIPART_BUCKET {
fs::remove_dir(&src_file_path.parent().unwrap()).await?;
} else {
self.delete_file(&src_volume_path, &PathBuf::from(src_file_path.parent().unwrap()))
.await?;
}
Ok(())
}
+2
View File
@@ -16,6 +16,8 @@ use crate::{
utils::hash,
};
const DEFAULT_INLINE_BLOCKS: usize = 128 * 1024;
#[derive(Debug)]
pub struct Sets {
pub id: Uuid,
+2
View File
@@ -20,6 +20,7 @@ pub struct FileInfo {
pub data_dir: Uuid,
pub mod_time: OffsetDateTime,
pub size: usize,
pub data: Vec<u8>,
}
impl FileInfo {
@@ -38,6 +39,7 @@ impl Default for FileInfo {
data_dir: Uuid::nil(),
mod_time: OffsetDateTime::UNIX_EPOCH,
size: Default::default(),
data: Default::default(),
}
}
}