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
//
// 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/>.

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use thiserror::Error;
use uuid::Uuid;

use crate::catlib_service::error::CatlibError;
use crate::{CoreXError, Storage};

/// Represents result of a possible path within a Storage. Storages field represents all alternative
/// locations of the path.
///
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum ResolvedPath {
    PathWithStorages {
        /// path within storage
        path_within_storage: PathBuf,
        /// Primary storage that includes the path
        storage: Storage,
    },
    /// Represents virtual node path that are not supposed to be looked up for in any backend.
    /// Contains absolute path in forest namespace.
    VirtualPath(PathBuf),
}

#[derive(Debug, Error, PartialEq, Eq, Clone)]
#[repr(C)]
pub enum PathResolutionError {
    #[error(transparent)]
    CoreXError(#[from] CoreXError),
    #[error(transparent)]
    CatlibError(#[from] CatlibError),
    #[error("Resolution error: {0}")]
    Generic(String),
}

#[mockall::automock]
pub trait PathResolver: Send + Sync {
    /// Returns Storages of containers claiming paths that match the provided argument along with
    /// the part of a path that is inside the container.
    ///
    /// **Example**: if a container claims path `/a/b/` and [`PathResolver`] receives request to resolve
    /// path `/a/b/c/d` then [`PathResolver`] should return path `/c/d` with Storage of that
    /// container.
    ///
    /// Storages from different containers are represented by different elements in a resulting sequence
    /// because the matching paths inside containers may be different. Additionally, method returns full
    /// paths of containers that starts with the provided one as an argument.
    ///
    /// E.g. if container C1 claims path `/a/` and container C2 claims path `/a/b/` and container C3 claims
    /// path `/a/b/c/d` when PathResolver is asked about path `/a/b/c`, then three-element vector is returned:
    /// `[
    ///     PathWithStorages { path: "/b/c/", storage: storage of C1},
    ///     PathWithStorages { path: "/c/", storage: storage of C2},
    ///     VirtualPath ( "/a/b/c/d" ),
    /// ]`
    ///
    ///
    fn resolve(&self, path: &Path) -> Result<HashSet<ResolvedPath>, PathResolutionError>;

    /// Returns optional path of a container with given uuid.
    fn get_mount_path(&self, identifier: Uuid) -> Result<Option<PathBuf>, PathResolutionError>;
}