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

pub mod models;

use std::path::Path;
use std::sync::Arc;

use async_trait::async_trait;
use wildland_corex::dfs::interface::{
    FsStat,
    IStream,
    OStream,
    ProgressReporter,
    SpaceUsage,
    WlPermissions,
};
use wildland_corex::dfs::unix_timestamp::UnixTimestamp;
use wildland_corex::Storage;

use self::models::*;

/// Error represents scenario when data could not be retrieved from the StorageBackend, e.g. some
/// network error. This mean that operation can be called again later of data can still be successfully
/// retrieved from another equivalent backend.
///
/// All logical errors, e.g. trying opening directory, should be reflected in the inner type, like OpenResponse.
/// Those variants are hidden inside Ok value because they should not trigger retrying operation.
#[async_trait]
pub trait StorageBackend: Send + Sync {
    async fn read_dir(&self, path: &Path) -> Result<ReadDirResponse, StorageBackendError>;
    async fn metadata(&self, path: &Path) -> Result<MetadataResponse, StorageBackendError>;
    async fn create_dir(&self, path: &Path) -> Result<CreateDirResponse, StorageBackendError>;
    async fn set_wildland_object_id(
        &self,
        path: &Path,
        wildland_object_id: String,
    ) -> Result<SetWildlandObjectIdResponse, StorageBackendError>;
    async fn remove_dir(
        &self,
        path: &Path,
        is_recursive: bool,
    ) -> Result<RemoveDirResponse, StorageBackendError>;
    async fn path_exists(&self, path: &Path) -> Result<bool, StorageBackendError>;
    async fn remove_file(&self, path: &Path) -> Result<RemoveFileResponse, StorageBackendError>;
    async fn rename(
        &self,
        old_path: &Path,
        new_path: &Path,
    ) -> Result<RenameResponse, StorageBackendError>;
    async fn set_permissions(
        &self,
        path: &Path,
        permissions: WlPermissions,
    ) -> Result<SetPermissionsResponse, StorageBackendError>;
    async fn stat_fs(&self) -> Result<FsStat, StorageBackendError>;
    async fn mount(&self) -> Result<(), StorageBackendError>;

    /// Returns amount of (used, total) bytes
    async fn get_space_usage(&self) -> Result<SpaceUsage, StorageBackendError>;

    async fn download(
        &self,
        path: &Path,
        output: Box<dyn OStream>,
        progress_reporter: Box<dyn ProgressReporter>,
    ) -> Result<DownloadResponse, StorageBackendError>;
    async fn upload(
        &self,
        path: &Path,
        input: Box<dyn IStream>,
        progress_reporter: Box<dyn ProgressReporter>,
        creation_time: Option<UnixTimestamp>,
    ) -> Result<UploadResponse, StorageBackendError>;

    async fn get_path_by_uuid(&self, uuid: String) -> Result<GetInfoResponse, StorageBackendError>;
}

pub trait StorageBackendFactory: Send + Sync {
    fn init_backend(&self, storage: Storage) -> anyhow::Result<Arc<dyn StorageBackend>>;
}

pub struct MutexAdaptor<T>(tokio::sync::Mutex<T>)
where
    T: StorageBackend;

impl<T> MutexAdaptor<T>
where
    T: StorageBackend,
{
    pub fn new(inner: T) -> Self {
        Self(tokio::sync::Mutex::new(inner))
    }
}

#[async_trait]
impl<T> StorageBackend for MutexAdaptor<T>
where
    T: StorageBackend,
{
    async fn read_dir(&self, path: &Path) -> Result<ReadDirResponse, StorageBackendError> {
        self.0.lock().await.read_dir(path).await
    }

    async fn metadata(&self, path: &Path) -> Result<MetadataResponse, StorageBackendError> {
        self.0.lock().await.metadata(path).await
    }

    async fn create_dir(&self, path: &Path) -> Result<CreateDirResponse, StorageBackendError> {
        self.0.lock().await.create_dir(path).await
    }

    async fn set_wildland_object_id(
        &self,
        path: &Path,
        wildland_object_uuid: String,
    ) -> Result<SetWildlandObjectIdResponse, StorageBackendError> {
        self.0
            .lock()
            .await
            .set_wildland_object_id(path, wildland_object_uuid)
            .await
    }

    async fn remove_dir(
        &self,
        path: &Path,
        is_recursive: bool,
    ) -> Result<RemoveDirResponse, StorageBackendError> {
        self.0.lock().await.remove_dir(path, is_recursive).await
    }

    async fn path_exists(&self, path: &Path) -> Result<bool, StorageBackendError> {
        self.0.lock().await.path_exists(path).await
    }

    async fn remove_file(&self, path: &Path) -> Result<RemoveFileResponse, StorageBackendError> {
        self.0.lock().await.remove_file(path).await
    }

    async fn rename(
        &self,
        old_path: &Path,
        new_path: &Path,
    ) -> Result<RenameResponse, StorageBackendError> {
        self.0.lock().await.rename(old_path, new_path).await
    }

    async fn set_permissions(
        &self,
        path: &Path,
        permissions: WlPermissions,
    ) -> Result<SetPermissionsResponse, StorageBackendError> {
        self.0.lock().await.set_permissions(path, permissions).await
    }

    async fn stat_fs(&self) -> Result<FsStat, StorageBackendError> {
        self.0.lock().await.stat_fs().await
    }

    async fn mount(&self) -> Result<(), StorageBackendError> {
        self.0.lock().await.mount().await
    }

    async fn get_space_usage(&self) -> Result<SpaceUsage, StorageBackendError> {
        self.0.lock().await.get_space_usage().await
    }

    async fn get_path_by_uuid(&self, uuid: String) -> Result<GetInfoResponse, StorageBackendError> {
        self.0.lock().await.get_path_by_uuid(uuid).await
    }

    async fn download(
        &self,
        path: &Path,
        output: Box<dyn OStream>,
        progress_reporter: Box<dyn ProgressReporter>,
    ) -> Result<DownloadResponse, StorageBackendError> {
        self.0
            .lock()
            .await
            .download(path, output, progress_reporter)
            .await
    }

    async fn upload(
        &self,
        path: &Path,
        input: Box<dyn IStream>,
        progress_reporter: Box<dyn ProgressReporter>,
        creation_time: Option<UnixTimestamp>,
    ) -> Result<UploadResponse, StorageBackendError> {
        self.0
            .lock()
            .await
            .upload(path, input, progress_reporter, creation_time)
            .await
    }
}