diff options
Diffstat (limited to 'rola-bucket/src/bucket/init.rs')
| -rw-r--r-- | rola-bucket/src/bucket/init.rs | 50 |
1 files changed, 50 insertions, 0 deletions
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(()) +} |
