diff options
author | Alex Auvolat <alex@adnab.me> | 2020-04-09 23:45:07 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-04-09 23:45:07 +0200 |
commit | d66c0d6833ddbeb61e34ee222dde92a5363bda1f (patch) | |
tree | 0e2da23fb32a6bf62a205fdf5f90d986ac14ad0c /src/block.rs | |
parent | a3eb88e6013e70238e7ddd66b4644f138b3d1b93 (diff) | |
download | garage-d66c0d6833ddbeb61e34ee222dde92a5363bda1f.tar.gz garage-d66c0d6833ddbeb61e34ee222dde92a5363bda1f.zip |
Why is it not Sync??
Diffstat (limited to 'src/block.rs')
-rw-r--r-- | src/block.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/block.rs b/src/block.rs new file mode 100644 index 00000000..b9e7eee8 --- /dev/null +++ b/src/block.rs @@ -0,0 +1,49 @@ +use std::sync::Arc; +use std::path::PathBuf; + +use tokio::fs; +use tokio::prelude::*; + +use crate::error::Error; +use crate::server::Garage; +use crate::proto::*; +use crate::data::*; + +fn block_dir(garage: &Garage, hash: &Hash) -> PathBuf { + let mut path = garage.system.config.data_dir.clone(); + path.push(hex::encode(&hash.as_slice()[0..1])); + path.push(hex::encode(&hash.as_slice()[1..2])); + path +} + +pub async fn write_block(garage: Arc<Garage>, hash: &Hash, data: &[u8]) -> Result<Message, Error> { + garage.fs_lock.lock().await; + + let mut path = block_dir(&garage, hash); + fs::create_dir_all(&path).await?; + + path.push(hex::encode(hash)); + if fs::metadata(&path).await.is_ok() { + return Ok(Message::Ok) + } + + let mut f = fs::File::create(path).await?; + f.write_all(data).await?; + drop(f); + + Ok(Message::Ok) +} + +pub async fn read_block(garage: Arc<Garage>, hash: &Hash) -> Result<Message, Error> { + let mut path = block_dir(&garage, hash); + path.push(hex::encode(hash)); + + let mut f = fs::File::open(path).await?; + let mut data = vec![]; + f.read_to_end(&mut data).await?; + + Ok(Message::PutBlock(PutBlockMessage{ + hash: hash.clone(), + data, + })) +} |