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
#![allow(unused_variables)]
use crate::repository::backend::common::files::*;
use crate::repository::backend::*;
use crate::repository::{ChunkSettings, Key};
use super::Result;
use async_trait::async_trait;
use rmp_serde as rmps;
use std::fs::File;
use std::path::{Path, PathBuf};
pub mod index;
pub mod manifest;
pub mod segment;
#[derive(Debug, Clone)]
pub struct MultiFile {
index_handle: index::Index,
manifest_handle: manifest::Manifest,
segment_handle: segment::SegmentHandler,
path: PathBuf,
}
impl MultiFile {
pub async fn open_defaults(
path: impl AsRef<Path>,
chunk_settings: Option<ChunkSettings>,
key: &Key,
) -> Result<MultiFile> {
let size_limit = 2_000_000_000;
let segments_per_directory = 100;
let index_handle = index::Index::open(&path)?;
let mut manifest_handle = manifest::Manifest::open(&path, chunk_settings, key)?;
let chunk_settings = if let Some(chunk_settings) = chunk_settings {
chunk_settings
} else {
manifest_handle.chunk_settings().await
};
let segment_handle = segment::SegmentHandler::open(
&path,
size_limit,
segments_per_directory,
chunk_settings,
key.clone(),
)?;
let path = path.as_ref().to_path_buf();
Ok(MultiFile {
index_handle,
manifest_handle,
segment_handle,
path,
})
}
pub fn read_key(path: impl AsRef<Path>) -> Result<EncryptedKey> {
let key_path = path.as_ref().join("key");
let file = File::open(&key_path)?;
Ok(rmps::decode::from_read(&file)?)
}
}
#[async_trait]
impl Backend for MultiFile {
type Manifest = manifest::Manifest;
type Index = index::Index;
fn get_index(&self) -> Self::Index {
self.index_handle.clone()
}
fn get_manifest(&self) -> Self::Manifest {
self.manifest_handle.clone()
}
async fn write_key(&self, key: &EncryptedKey) -> Result<()> {
let key_path = self.path.join("key");
let mut file =
LockedFile::open_read_write(&key_path)?.ok_or(BackendError::FileLockError)?;
Ok(rmps::encode::write(&mut file, key)?)
}
async fn read_key(&self) -> Result<EncryptedKey> {
let key_path = self.path.join("key");
let file = File::open(&key_path)?;
Ok(rmps::decode::from_read(&file)?)
}
async fn read_chunk(&mut self, location: SegmentDescriptor) -> Result<Chunk> {
self.segment_handle.read_chunk(location).await
}
async fn write_chunk(&mut self, chunk: Chunk) -> Result<SegmentDescriptor> {
self.segment_handle.write_chunk(chunk).await
}
async fn close(&mut self) {
self.index_handle.close().await;
self.manifest_handle.close().await;
self.segment_handle.close().await;
}
fn get_object_handle(&self) -> BackendObject {
backend_to_object(self.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::repository::Encryption;
use tempfile::{tempdir, TempDir};
async fn setup(key: &Key) -> (TempDir, MultiFile) {
let tempdir = tempdir().unwrap();
let path = tempdir.path().to_path_buf();
let mf = MultiFile::open_defaults(path, Some(ChunkSettings::lightweight()), key)
.await
.unwrap();
(tempdir, mf)
}
#[tokio::test]
async fn key_store_load() {
let key = Key::random(32);
let (tempdir, mut mf) = setup(&key).await;
let enc_key = EncryptedKey::encrypt(&key, 512, 1, Encryption::new_aes256ctr(), b"");
mf.write_key(&enc_key).await.expect("Unable to write key");
let enc_key = mf
.read_key()
.await
.expect("Unable to read key (before drop)");
let new_key = enc_key
.decrypt(b"")
.expect("Unable to decrypt key (before drop)");
assert_eq!(key, new_key);
mf.close().await;
let enc_key = MultiFile::read_key(tempdir.path()).expect("Unable to read key (after drop)");
let new_key = enc_key
.decrypt(b"")
.expect("Unable to decrypt key (after drop)");
assert_eq!(key, new_key);
}
}