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
use std::cmp::min;
use std::collections::HashMap;
use std::io::{self, Seek, SeekFrom, Read};
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::{RwLock, Arc};
use std::fs::{File};
use failure::Backtrace;
use futures::{Future, Async};
use futures_cpupool::{CpuPool, CpuFuture};
use dir_signature::v1::{self, ParseError};
use virtual_path::VPath;
pub use block_id::{BlockHash};
pub trait GetBlock {
type Data: AsRef<[u8]>;
type Error: fmt::Display;
type Future: Future<Item=Self::Data, Error=Self::Error> + 'static;
fn read_block(&self, hash: BlockHash, hint: BlockHint) -> Self::Future;
}
#[derive(Debug)]
pub struct BlockHint {
hint: Option<(VPath, PathBuf, u64)>,
}
trait Assert: Send + Sync + 'static {}
impl Assert for BlockHint {}
impl Assert for BlockHash {}
fn _object_safe() {
use futures::future::FutureResult;
let _: Option<&GetBlock<Data=Vec<u8>, Error=String,
Future=FutureResult<Vec<u8>, String>>> = None;
}
#[derive(Debug, Clone)]
struct BlockPointer {
path: Arc<PathBuf>,
offset: u64,
size: usize,
}
#[derive(Debug, Clone)]
pub struct ThreadedBlockReader {
pool: CpuPool,
blocks: Arc<RwLock<HashMap<BlockHash, BlockPointer>>>,
}
#[derive(Debug)]
pub struct FutureBlock(CpuFuture<Vec<u8>, ReadError>);
#[derive(Debug, Fail)]
pub enum ReadError {
#[fail(display="error reading file {:?}: {}", _0, _1)]
Fs(PathBuf, io::Error, Backtrace),
#[fail(display="block {} not found", _0)]
NotFound(BlockHash),
#[doc(hidden)]
#[fail(display="block lock was poisoned")]
LockError(Backtrace),
#[doc(hidden)]
#[fail(display="non-existent-error")]
__Nonexhaustive,
}
#[derive(Debug, Fail)]
pub enum DirError {
#[fail(display="error parsing index: {}", _0)]
ParseError(ParseError),
#[doc(hidden)]
#[fail(display="blocks lock was poisoned")]
LockError(Backtrace),
#[doc(hidden)]
#[fail(display="hash size is unsupported")]
HashSize(Backtrace),
#[doc(hidden)]
#[fail(display="non-existent-error")]
__Nonexhaustive,
}
impl ThreadedBlockReader {
pub fn new() -> ThreadedBlockReader {
ThreadedBlockReader {
pool: CpuPool::new(40),
blocks: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn new_num_threads(num: usize) -> ThreadedBlockReader {
ThreadedBlockReader {
pool: CpuPool::new(num),
blocks: Arc::new(RwLock::new(HashMap::new())),
}
}
pub fn register_dir<P: AsRef<Path>>(&self, dir: P, virtual_path: &VPath,
index_data: &[u8])
-> Result<(), DirError>
{
self._register_dir(dir.as_ref(), virtual_path, index_data)
}
fn _register_dir(&self, dir: &Path, _vpath: &VPath, index_data: &[u8])
-> Result<(), DirError>
{
let ref mut cur = io::Cursor::new(&index_data);
let mut parser = v1::Parser::new(cur).map_err(DirError::ParseError)?;
let header = parser.get_header();
let block_size = header.get_block_size();
let mut blocks = self.blocks.write()
.map_err(|_| DirError::LockError(Backtrace::new()))?;
for entry in parser.iter() {
match entry.expect("just created index is valid") {
v1::Entry::File { ref path, ref hashes, size, .. } => {
let path = Arc::new(dir.join(
path.strip_prefix("/").expect("paths are absolute")
));
let mut left = size;
for (idx, hash) in hashes.iter().enumerate() {
let id = BlockHash::from_bytes(hash)
.ok_or_else(||
DirError::HashSize(Backtrace::new()))?;
blocks.insert(id, BlockPointer {
path: path.clone(),
offset: idx as u64 * block_size,
size: min(left, block_size) as usize,
});
left = left.saturating_sub(block_size);
}
}
_ => {}
}
}
Ok(())
}
}
impl GetBlock for ThreadedBlockReader {
type Data = Vec<u8>;
type Error = ReadError;
type Future = FutureBlock;
fn read_block(&self, hash: BlockHash, _hint: BlockHint) -> FutureBlock {
let blocks = self.blocks.clone();
FutureBlock(self.pool.spawn_fn(move || {
let blocks = blocks.read()
.map_err(|_| ReadError::LockError(Backtrace::new()))?;
let pointer = blocks.get(&hash)
.ok_or_else(|| ReadError::NotFound(hash))?;
let mut result = vec![0u8; pointer.size];
let mut file = File::open(&*pointer.path)
.map_err(|e| ReadError::Fs(pointer.path.to_path_buf(), e,
Backtrace::new()))?;
file.seek(SeekFrom::Start(pointer.offset))
.map_err(|e| ReadError::Fs(pointer.path.to_path_buf(), e,
Backtrace::new()))?;
let bytes = file.read(&mut result[..])
.map_err(|e| ReadError::Fs(pointer.path.to_path_buf(), e,
Backtrace::new()))?;
result.truncate(bytes);
assert_eq!(result.len(), pointer.size);
Ok(result)
}))
}
}
impl Future for FutureBlock {
type Item = Vec<u8>;
type Error = ReadError;
fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
self.0.poll()
}
}
impl BlockHint {
pub fn empty() -> BlockHint {
BlockHint {
hint: None,
}
}
}