From 68daa10abfe3015beca966825d32cf67c9f5d5d7 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 18 Jun 2026 20:56:05 +0800 Subject: feat(bucket): implement bucket initialization and logging infrastructure Add bucket init logic with directory structure creation and log macros for tracing --- rola-bucket/Cargo.toml | 1 + rola-bucket/res/bucket.toml | 1 + rola-bucket/src/bucket/init.rs | 50 +++++++++++++++++++++++++++++++++++++++++ rola-bucket/src/bucket/space.rs | 22 +++++++++--------- 4 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 rola-bucket/res/bucket.toml (limited to 'rola-bucket') diff --git a/rola-bucket/Cargo.toml b/rola-bucket/Cargo.toml index 07c555e..61d7940 100644 --- a/rola-bucket/Cargo.toml +++ b/rola-bucket/Cargo.toml @@ -14,3 +14,4 @@ space-system.workspace = true thiserror.workspace = true tokio.workspace = true +log.workspace = true diff --git a/rola-bucket/res/bucket.toml b/rola-bucket/res/bucket.toml new file mode 100644 index 0000000..01abd07 --- /dev/null +++ b/rola-bucket/res/bucket.toml @@ -0,0 +1 @@ +[bucket] diff --git a/rola-bucket/src/bucket/init.rs b/rola-bucket/src/bucket/init.rs index 8b13789..2733a37 100644 --- a/rola-bucket/src/bucket/init.rs +++ b/rola-bucket/src/bucket/init.rs @@ -1 +1,51 @@ +use std::path::{Path, PathBuf}; +use shared_constants::{ + bucket::{ + DIR_BUCKET_COMPRESSED_OBJ, DIR_BUCKET_DELTA, DIR_BUCKET_ID_REVS, DIR_BUCKET_ID_TAGS, + DIR_BUCKET_OBJ, + }, + common::FILE_BUCKET_ROOT_CONFIG, +}; +use space_system::SpaceError; +use tokio::fs; + +pub(crate) async fn init_bucket_at(path: PathBuf) -> Result<(), SpaceError> { + let bucket_config_file = path.join(FILE_BUCKET_ROOT_CONFIG); + + // Check if directory is empty + let mut dir_entries = std::fs::read_dir(&path).map_err(SpaceError::Io)?; + if dir_entries.next().is_some() { + return Err(SpaceError::RequireEmptyDirectory); + } + + write_config(&bucket_config_file).await?; + create_dirs(&bucket_config_file).await?; + + Ok(()) +} + +async fn write_config(bucket_config_file: &Path) -> Result<(), SpaceError> { + fs::write(bucket_config_file, include_str!("../../res/bucket.toml")) + .await + .map_err(SpaceError::Io) +} + +async fn create_dirs(bucket_config_file: &Path) -> Result<(), SpaceError> { + let dirs = [ + DIR_BUCKET_OBJ, + DIR_BUCKET_COMPRESSED_OBJ, + DIR_BUCKET_DELTA, + DIR_BUCKET_ID_REVS, + DIR_BUCKET_ID_TAGS, + ]; + + for dir in dirs { + let full_path = bucket_config_file.join(dir); + fs::create_dir_all(&full_path) + .await + .map_err(SpaceError::Io)?; + } + + Ok(()) +} diff --git a/rola-bucket/src/bucket/space.rs b/rola-bucket/src/bucket/space.rs index 9559b1d..ed1311c 100644 --- a/rola-bucket/src/bucket/space.rs +++ b/rola-bucket/src/bucket/space.rs @@ -1,22 +1,20 @@ -use shared_constants::common::DRAFT_META_DIR; -use space_system::{SpaceError, SpaceRoot, SpaceRootFindPattern}; -use tokio::fs::create_dir_all; +// use log::trace; +use shared_constants::common::FILE_BUCKET_ROOT_CONFIG; +use shared_functions::trace; +use space_system::{SpaceRoot, SpaceRootFindPattern}; -use crate::{AsyncBucketTransferProtocol, Bucket}; +use crate::{AsyncBucketTransferProtocol, Bucket, bucket::init::init_bucket_at}; impl SpaceRoot for Bucket { fn get_pattern() -> SpaceRootFindPattern { - SpaceRootFindPattern::IncludeDotDir(DRAFT_META_DIR.into()) + SpaceRootFindPattern::IncludeFile(FILE_BUCKET_ROOT_CONFIG.into()) } async fn create_space(path: &std::path::Path) -> Result<(), space_system::SpaceError> { - let draft_meta_dir = path.join(DRAFT_META_DIR); - - // Create workspace directory - create_dir_all(&draft_meta_dir) - .await - .map_err(SpaceError::from)?; - + let path_str = path.display().to_string(); + trace!("Creating bucket at: {}", &path_str); + init_bucket_at(path.into()).await?; + trace!("Bucket created at: {}", &path_str); Ok(()) } } -- cgit