Index: Cargo.toml ================================================================== --- Cargo.toml +++ Cargo.toml @@ -1,8 +1,8 @@ [package] name = "fsblobstore" -version = "0.0.2" +version = "0.0.3" edition = "2021" license = "0BSD" categories = [ "filesystem" ] keywords = [ "blob", "datastore" ] repository = "https://repos.qrnch.tech/pub/fsblobstore" @@ -24,15 +24,15 @@ [dependencies] hex = { version = "0.4.3" } idbag = { version = "0.1.2" } recstrm = { version = "0.0.1", optional = true } sha2 = { version = "0.10.8" } -tmpfile = { version = "0.0.1" } +tmpfile = { version = "0.0.2" } walkdir = { version = "2.4.0", optional = true } [dev-dependencies] rand = { version = "0.8.5" } [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs", "--generate-link-to-definition"] Index: src/lib.rs ================================================================== --- src/lib.rs +++ src/lib.rs @@ -32,11 +32,11 @@ use tmpfile::TmpProc; use sha2::{Digest, Sha256}; pub use ch::ContentHash; -pub use tmpfile::TmpFile; +pub use tmpfile::{self, TmpFile}; pub use err::Error; /// Internal type used by the [`TmpFile`] to hash and move blobs into their @@ -54,34 +54,41 @@ self.inner.update(buf); } fn finalize( &mut self, - tmpfile: &Path - ) -> Result<(Self::Output, PathBuf), std::io::Error> { + tmpfile: Option<&Path> + ) -> Result<(Self::Output, Option), std::io::Error> { let result = self.inner.clone().finalize(); let hash = result.to_vec(); - let Some(basedir) = tmpfile.parent() else { - panic!("foo"); + let fname = if let Some(tmpfile) = tmpfile { + let Some(basedir) = tmpfile.parent() else { + panic!("foo"); + }; + + let hexhash = hex::encode(&hash); + let (subdir1, rest) = hexhash.split_at(2); + let (subdir2, fname) = rest.split_at(2); + let subdirs = basedir.join(subdir1).join(subdir2); + if !subdirs.exists() { + std::fs::create_dir_all(&subdirs)?; + } + Some(subdirs.join(fname)) + } else { + None }; - - let hexhash = hex::encode(&hash); - let (subdir1, rest) = hexhash.split_at(2); - let (subdir2, fname) = rest.split_at(2); - let subdirs = basedir.join(subdir1).join(subdir2); - if !subdirs.exists() { - std::fs::create_dir_all(&subdirs)?; - } - Ok((ContentHash::from(hash), subdirs.join(fname))) + Ok((ContentHash::from(hash), fname)) } } /// An abstraction over a blob storage in a file system directory. pub struct FsBlobStore { basedir: PathBuf, + + minsize: Option, /// Used to allocate unique identifiers for naming temporary files. idbag: IdBag } @@ -116,13 +123,27 @@ /// remains valid throughout the object's lifetime. pub fn new(basedir: impl AsRef) -> Result { let basedir = basedir.as_ref(); Ok(Self { basedir: basedir.to_path_buf(), + minsize: None, + idbag: IdBag::new() + }) + } + + pub fn with_minsize( + basedir: impl AsRef, + minsize: usize + ) -> Result { + let basedir = basedir.as_ref(); + Ok(Self { + basedir: basedir.to_path_buf(), + minsize: Some(minsize), idbag: IdBag::new() }) } + /// Check if content for a hash exists in store. pub fn have(&self, hash: &[u8]) -> Result { let fname = self.abspathname(hash); fname.try_exists() @@ -146,11 +167,15 @@ let tp = Hasher { inner: Sha256::new(), _id: id }; let tmpfname = self.basedir.join(tmpfname); - TmpFile::new(tmpfname, Box::new(tp)) + if let Some(minsize) = self.minsize { + TmpFile::with_minsize(tmpfname, Box::new(tp), minsize) + } else { + TmpFile::new(tmpfname, Box::new(tp)) + } } /// Remove a blob from the blob store. /// /// # Panic Index: www/changelog.md ================================================================== --- www/changelog.md +++ www/changelog.md @@ -4,11 +4,17 @@ [Details](/vdiff?from=fsblobstore-0.0.2&to=trunk) ### Added +- Add factory method `FsBlobStore::with_minsize()` to be able to use the memory + buffer for "small files" support introduced in `tmpfile` 0.0.2. +- Re-export `tmpfile` to allow application to access `tmpfile::Output`. + ### Changed + +- Update to `tmpfile` 0.0.2. ### Removed ---