node: Add `Storage::open`

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2022-08-29 16:32:58 +02:00
parent 2c519f5b5f
commit a5a7af57b5
No known key found for this signature in database
3 changed files with 13 additions and 5 deletions

View File

@ -1,5 +1,5 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{fmt, fs}; use std::{fmt, fs, io};
use git_ref_format::refspec; use git_ref_format::refspec;
use git_url::Url; use git_url::Url;
@ -59,10 +59,16 @@ impl WriteStorage for Storage {
} }
impl Storage { impl Storage {
pub fn new<P: AsRef<Path>>(path: P) -> Self { pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
let path = path.as_ref().to_path_buf(); let path = path.as_ref().to_path_buf();
Self { path } match fs::create_dir_all(&path) {
Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {}
Err(err) => return Err(err),
Ok(()) => {}
}
Ok(Self { path })
} }
pub fn path(&self) -> &Path { pub fn path(&self) -> &Path {
@ -231,7 +237,7 @@ mod tests {
fn test_fetch() { fn test_fetch() {
let tmp = tempfile::tempdir().unwrap(); let tmp = tempfile::tempdir().unwrap();
let alice = fixtures::storage(tmp.path().join("alice")); let alice = fixtures::storage(tmp.path().join("alice"));
let bob = Storage::new(tmp.path().join("bob")); let bob = Storage::open(tmp.path().join("bob")).unwrap();
let inventory = alice.inventory().unwrap(); let inventory = alice.inventory().unwrap();
let proj = inventory.first().unwrap(); let proj = inventory.first().unwrap();
let remotes = alice.repository(proj).unwrap().remotes().unwrap(); let remotes = alice.repository(proj).unwrap().remotes().unwrap();

View File

@ -7,7 +7,7 @@ use crate::test::arbitrary;
pub fn storage<P: AsRef<Path>>(path: P) -> Storage { pub fn storage<P: AsRef<Path>>(path: P) -> Storage {
let path = path.as_ref(); let path = path.as_ref();
let storage = Storage::new(path); let storage = Storage::open(path).unwrap();
let proj_ids = arbitrary::set::<ProjId>(3..5); let proj_ids = arbitrary::set::<ProjId>(3..5);
let user_ids = arbitrary::set::<UserId>(1..3); let user_ids = arbitrary::set::<UserId>(1..3);

View File

@ -8,7 +8,9 @@ use nakamoto_net::Protocol as _;
use crate::collections::{HashMap, HashSet}; use crate::collections::{HashMap, HashSet};
use crate::protocol::*; use crate::protocol::*;
use crate::storage::git::Storage;
use crate::storage::ReadStorage; use crate::storage::ReadStorage;
use crate::test::fixtures;
#[allow(unused)] #[allow(unused)]
use crate::test::logger; use crate::test::logger;
use crate::test::peer::Peer; use crate::test::peer::Peer;