Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: Add `Storage::open`
Alexis Sellier committed 3 years ago
commit a5a7af57b50c2c9ddaa5b278417f82e61deddc99
parent 2c519f5b5f495a48ab7da8db64fa43c1583a1b6e
3 files changed +13 -5
modified node/src/storage/git.rs
@@ -1,5 +1,5 @@
use std::path::{Path, PathBuf};
-
use std::{fmt, fs};
+
use std::{fmt, fs, io};

use git_ref_format::refspec;
use git_url::Url;
@@ -59,10 +59,16 @@ impl WriteStorage for 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();

-
        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 {
@@ -231,7 +237,7 @@ mod tests {
    fn test_fetch() {
        let tmp = tempfile::tempdir().unwrap();
        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 proj = inventory.first().unwrap();
        let remotes = alice.repository(proj).unwrap().remotes().unwrap();
modified node/src/test/fixtures.rs
@@ -7,7 +7,7 @@ use crate::test::arbitrary;

pub fn storage<P: AsRef<Path>>(path: P) -> Storage {
    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 user_ids = arbitrary::set::<UserId>(1..3);

modified node/src/test/tests.rs
@@ -8,7 +8,9 @@ use nakamoto_net::Protocol as _;

use crate::collections::{HashMap, HashSet};
use crate::protocol::*;
+
use crate::storage::git::Storage;
use crate::storage::ReadStorage;
+
use crate::test::fixtures;
#[allow(unused)]
use crate::test::logger;
use crate::test::peer::Peer;