diff --git a/src/cas/bucket_meta.rs b/src/cas/bucket_meta.rs index 8c27d51..26c0fcf 100644 --- a/src/cas/bucket_meta.rs +++ b/src/cas/bucket_meta.rs @@ -1,7 +1,9 @@ use super::{errors::FsError, fs::PTR_SIZE}; -use chrono::{SecondsFormat, TimeZone, Utc}; -use s3_server::dto::Bucket; -use std::convert::{TryFrom, TryInto}; +use chrono::Utc; +use std::{ + convert::{TryFrom, TryInto}, + time::{SystemTime, UNIX_EPOCH}, +}; #[derive(Debug)] pub struct BucketMeta { @@ -10,29 +12,23 @@ pub struct BucketMeta { } impl BucketMeta { - pub fn new(ctime: i64, name: String) -> Self { - Self { ctime, name } + pub fn new(name: String) -> Self { + Self { + ctime: Utc::now().timestamp(), + name, + } } - pub fn ctime(&self) -> i64 { - self.ctime + pub fn ctime(&self) -> SystemTime { + UNIX_EPOCH + std::time::Duration::from_secs(self.ctime as u64) } pub fn name(&self) -> &str { &self.name } -} -impl From for Bucket { - fn from(bm: BucketMeta) -> Self { - Bucket { - creation_date: Some( - Utc.timestamp_opt(bm.ctime, 0) - .unwrap() - .to_rfc3339_opts(SecondsFormat::Secs, true), - ), - name: Some(bm.name), - } + pub fn to_vec(&self) -> Vec { + self.into() } } diff --git a/src/cas/fs.rs b/src/cas/fs.rs index 76ce515..c4397ae 100644 --- a/src/cas/fs.rs +++ b/src/cas/fs.rs @@ -9,7 +9,6 @@ use super::{ object::Object, }; -use chrono::Utc; use faster_hex::hex_string; use futures::{ channel::mpsc::unbounded, @@ -18,7 +17,7 @@ use futures::{ stream::{StreamExt, TryStreamExt}, }; use md5::{Digest, Md5}; -use s3_server::dto::{Bucket, ByteStream}; +use s3_server::dto::ByteStream; use sled::{Db, Transactional}; use std::{ convert::{TryFrom, TryInto}, @@ -287,10 +286,7 @@ impl CasFS { pub fn create_bucket(&self, bucket_name: String) -> Result<(), MetaError> { let bucket_meta = self.get_tree(BUCKET_META_TREE)?; - let bm = Vec::from(&BucketMeta::new( - Utc::now().timestamp(), - bucket_name.clone(), - )); + let bm = BucketMeta::new(bucket_name.clone()).to_vec(); match bucket_meta.insert(bucket_name, bm) { Ok(_) => Ok(()), @@ -349,7 +345,7 @@ impl CasFS { } /// Get a list of all buckets in the system. - pub fn get_buckets(&self) -> Result, MetaError> { + pub fn list_buckets(&self) -> Result, MetaError> { let bucket_tree = match self.sled_bucket_meta_tree() { Ok(t) => t, Err(e) => return Err(MetaError::UnknownError(e.to_string())), @@ -364,7 +360,7 @@ impl CasFS { }; // unwrap here is fine as it means the db is corrupt let bucket_meta = BucketMeta::try_from(&*value).expect("Corrupted bucket metadata"); - Some(bucket_meta.into()) + Some(bucket_meta) }) .collect(); Ok(buckets) diff --git a/src/s3fs.rs b/src/s3fs.rs index 46ed16d..2a8afb7 100644 --- a/src/s3fs.rs +++ b/src/s3fs.rs @@ -406,12 +406,12 @@ impl S3 for S3FS { &self, _: S3Request, ) -> S3Result> { - let csfs_buckets = try_!(self.casfs.get_buckets()); + let csfs_buckets = try_!(self.casfs.list_buckets()); let mut buckets = Vec::with_capacity(csfs_buckets.len()); for bucket in csfs_buckets { let bucket = Bucket { - creation_date: None, //creation_date: bucket.creation_date, TODO: fix it - name: bucket.name, + creation_date: Some(Timestamp::from(bucket.ctime())), + name: Some(bucket.name().into()), }; buckets.push(bucket); }