1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Catalog client library
//!
//! This library is used by Wildland Core to allow persistent storage for Wildland manifests that
//! describe Wildland entities such as Containers, Storages, Bridges etc.
//!
//! The library acts as a database client depending on the database backend used. The current
//! version of CatLib stores manifests in a local single-file nosql, unstructured database.
//! Location of the database file depends on the platform where the application runs, these are:
//!
//! - `Linux:   /home/alice/.config/catlib`
//! - `Windows: C:\Users\Alice\AppData\Roaming\com.wildland.Cargo\catlib`
//! - `macOS:   /Users/Alice/Library/Application Support/com.wildland.Cargo/catlib`
//!
//! ## Entities relationship
//!
//! ```none
//! +------------+          +------------+
//! |   Forest   | -------> |   Bridge   |
//! +------------+          +------------+
//!       |
//!       |       +-------------+
//!       +-----> |  Container  |
//!               +-------------+
//!                      |
//!                      |       +-----------+
//!                      +-----> |  Storage  |
//!                              +-----------+
//! ```
//! ## Example usage
//!
//! ```rust
//! # use wildland_catlib::CatLib;
//! # use std::collections::HashSet;
//! # use crate::wildland_catlib::*;
//! # use uuid::Uuid;
//! let forest_owner = Identity([1; 32]);
//! let signer = Identity([2; 32]);
//!
//! let catlib = CatLib::default();
//! let forest = catlib.create_forest(
//!                  forest_owner,
//!                  HashSet::from([signer]),
//!                  vec![],
//!              ).unwrap();
//!
//! let mut container = forest.create_container().unwrap();
//! container.add_path("/foo/bar".to_string());
//! container.add_path("/bar/baz".to_string());
//!
//! let storage_template_id = Uuid::from_u128(1);
//! let storage_data = b"credentials_and_whatnot".to_vec();
//! container.create_storage(Some(storage_template_id), storage_data);
//! ```
//!

#![cfg_attr(test, feature(proc_macro_hygiene))]

pub use bridge::Bridge;
pub use container::Container;
pub use contracts::common::*;
pub use contracts::*;
use db::*;
use directories::ProjectDirs;
pub use error::*;
pub use forest::Forest;
use rustbreak::PathDatabase;
use std::path::PathBuf;
use std::rc::Rc;
pub use storage::Storage;
use uuid::Uuid;

mod bridge;
mod container;
pub mod contracts;
mod db;
mod error;
mod forest;
mod storage;

#[derive(Clone)]
pub struct CatLib {
    db: Rc<StoreDb>,
}

impl CatLib {
    pub fn new(path: PathBuf) -> Self {
        let db = PathDatabase::create_at_path(path.clone(), CatLibData::new());

        if db.is_err() {
            let path_str = path.to_str().unwrap();
            panic!("Could not create CatLib database at {path_str}");
        }

        CatLib {
            db: Rc::new(db.unwrap()),
        }
    }

    /// Create new Forest obect.
    ///
    /// `owner` and `signers` are cryptographical objects that are used by the Core module to
    /// verify the cryptographical integrity of the manifests.
    ///
    /// `data` is an arbitrary data object that can be used to synchronize Forest state between
    /// devices.
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use wildland_catlib::{CatLib, Identity};
    /// # use std::collections::HashSet;
    /// # use crate::wildland_catlib::IForest;
    /// let forest_owner = Identity([1; 32]);
    /// let signer = Identity([2; 32]);
    ///
    /// let catlib = CatLib::default();
    /// let forest = catlib.create_forest(
    ///                  forest_owner,
    ///                  HashSet::from([signer]),
    ///                  vec![],
    ///              ).unwrap();
    pub fn create_forest(
        &self,
        owner: Identity,
        signers: Signers,
        data: Vec<u8>,
    ) -> CatlibResult<Forest> {
        let mut forest = Forest::new(owner, signers, data, self.db.clone());
        forest.save()?;

        Ok(forest)
    }

    /// Return [`Forest`] object by Forest UUID.
    pub fn get_forest(&self, uuid: Uuid) -> CatlibResult<Forest> {
        fetch_forest_by_uuid(self.db.clone(), uuid)
    }

    /// Return [`Forest`] owned by specified `owner`.
    ///
    /// **Note: by design each owner may have only one Forest**
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Forest`] was found.
    /// - Returns [`CatlibError::MalformedDatabaseEntry`] if more than one [`Forest`] was found.
    pub fn find_forest(&self, owner: Identity) -> CatlibResult<Forest> {
        self.db.load()?;
        let data = self.db.read(|db| db.clone()).map_err(CatlibError::from)?;

        let forests: Vec<Forest> = data
            .iter()
            .filter(|(id, _)| (**id).starts_with("forest-"))
            .map(|(_, forest_str)| Forest::try_from((*forest_str).clone()).unwrap())
            .filter(|forest| forest.owner() == owner)
            .collect();

        match forests.len() {
            0 => Err(CatlibError::NoRecordsFound),
            1 => Ok(forests[0].clone()),
            _ => Err(CatlibError::MalformedDatabaseEntry),
        }
    }

    /// Return [`Container`] object by Container UUID.
    pub fn get_container(&self, uuid: Uuid) -> CatlibResult<Container> {
        fetch_container_by_uuid(self.db.clone(), uuid)
    }

    /// Return [`Storage`]s that were created using given `template_id` UUID.
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Forest`] was found.
    /// - Returns [`CatlibError::MalformedDatabaseEntry`] if more than one [`Forest`] was found.
    pub fn find_storages_with_template(&self, template_id: Uuid) -> CatlibResult<Vec<Storage>> {
        self.db.load()?;
        let data = self.db.read(|db| db.clone()).map_err(CatlibError::from)?;

        let storages: Vec<Storage> = data
            .iter()
            .filter(|(id, _)| (**id).starts_with("storage-"))
            .map(|(_, storage_str)| Storage::try_from((*storage_str).clone()).unwrap())
            .filter(|storage| {
                storage.template_uuid().is_some() && storage.template_uuid().unwrap() == template_id
            })
            .collect();

        match storages.len() {
            0 => Err(CatlibError::NoRecordsFound),
            _ => Ok(storages),
        }
    }

    /// Return [`Container`]s that were created using given `template_id` UUID.
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Forest`] was found.
    /// - Returns [`CatlibError::MalformedDatabaseEntry`] if more than one [`Forest`] was found.
    pub fn find_containers_with_template(&self, template_id: Uuid) -> CatlibResult<Vec<Container>> {
        let storages = self.find_storages_with_template(template_id)?;

        storages.iter().map(|storage| storage.container()).collect()
    }
}

impl Default for CatLib {
    fn default() -> Self {
        let project_dirs = ProjectDirs::from("com", "wildland", "Cargo");

        let db_file = if let Some(project_dirs) = project_dirs {
            let db_dir = project_dirs.data_local_dir().join("catlib");

            if !db_dir.exists() {
                std::fs::create_dir_all(&db_dir).unwrap();
            }

            db_dir.join("catlib.database")
        } else {
            tracing::info!("Could not create ProjectDirs. Using working directory.");
            "./catlib.database".into()
        };

        CatLib {
            db: Rc::new(PathDatabase::load_from_path_or_default(db_file).unwrap()),
        }
    }
}

#[cfg_attr(test, mocktopus::macros::mockable)]
fn use_default_database() -> Rc<StoreDb> {
    let db = CatLib::default().db;
    db.load().unwrap(); // Definitely not thread safe
    db
}