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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use super::{seekable::SeekableDataSource, DataSource, DataSourceReader, PartSize, SourceKey, UnseekableDataSource};
use digest::Digest;
use once_cell::sync::OnceCell;
use os_str_bytes::OsStrBytes;
use sha1::Sha1;
use std::{
fmt::{self, Debug},
fs::File,
io::Result as IoResult,
path::PathBuf,
sync::Arc,
};
#[cfg(feature = "async")]
use {
super::{first_part_number, AsyncDataSourceReader, AsyncSeekableSource, AsyncUnseekableDataSource},
async_once_cell::OnceCell as AsyncOnceCell,
async_std::{fs::File as AsyncFile, path::PathBuf as AsyncPathBuf},
futures::{future::BoxFuture, lock::Mutex as AsyncMutex, AsyncSeekExt},
std::num::NonZeroUsize,
};
enum Source<A: Digest> {
Seekable(SeekableDataSource),
Unseekable(UnseekableDataSource<File, A>),
}
impl<A: Digest> Debug for Source<A> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Seekable(source) => f.debug_struct("Seekable").field("source", source).finish(),
Self::Unseekable(source) => f.debug_tuple("Unseekable").field(source).finish(),
}
}
}
pub struct FileDataSource<A: Digest = Sha1> {
path: PathBuf,
canonicalized_path: Arc<OnceCell<PathBuf>>,
source: Arc<OnceCell<Source<A>>>,
}
impl<A: Digest> FileDataSource<A> {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
canonicalized_path: Default::default(),
source: Default::default(),
}
}
fn get_seekable_source(&self) -> IoResult<&Source<A>> {
self.source.get_or_try_init(|| {
let file = File::open(&self.path)?;
let file_size = file.metadata()?.len();
SeekableDataSource::new(file, file_size)
.map(Source::Seekable)
.or_else(|_| {
File::open(&self.path)
.map(UnseekableDataSource::new)
.map(Source::Unseekable)
})
})
}
fn get_path(&self) -> IoResult<&PathBuf> {
self.canonicalized_path.get_or_try_init(|| self.path.canonicalize())
}
}
impl<D: Digest + Send> DataSource<D> for FileDataSource<D> {
fn slice(&self, size: PartSize) -> IoResult<Option<DataSourceReader>> {
match self.get_seekable_source()? {
Source::Seekable(source) => DataSource::<D>::slice(source, size),
Source::Unseekable(source) => source.slice(size),
}
}
fn reset(&self) -> IoResult<()> {
match self.get_seekable_source()? {
Source::Seekable(source) => DataSource::<D>::reset(source),
Source::Unseekable(source) => source.reset(),
}
}
fn source_key(&self) -> IoResult<Option<SourceKey<D>>> {
match self.get_seekable_source()? {
Source::Seekable { .. } => {
let mut hasher = D::new();
hasher.update(b"file://");
hasher.update(&self.get_path()?.to_raw_bytes());
Ok(Some(hasher.finalize().into()))
}
Source::Unseekable(source) => source.source_key(),
}
}
fn total_size(&self) -> IoResult<Option<u64>> {
match self.get_seekable_source()? {
Source::Seekable(source) => DataSource::<D>::total_size(source),
Source::Unseekable(source) => source.total_size(),
}
}
}
impl<A: Digest> Debug for FileDataSource<A> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FileDataSource")
.field("path", &self.path)
.field("canonicalized_path", &self.canonicalized_path)
.field("source", &self.source)
.finish()
}
}
impl<A: Digest> Clone for FileDataSource<A> {
#[inline]
fn clone(&self) -> Self {
Self {
path: self.path.clone(),
canonicalized_path: self.canonicalized_path.clone(),
source: self.source.clone(),
}
}
}
#[cfg(feature = "async")]
mod async_reader {
use super::{super::AsyncDataSource, *};
pub struct AsyncFileDataSource<A: Digest> {
path: PathBuf,
canonicalized_path: Arc<AsyncOnceCell<AsyncPathBuf>>,
source: Arc<AsyncOnceCell<AsyncSource<A>>>,
}
impl<A: Digest> AsyncFileDataSource<A> {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
canonicalized_path: Arc::new(AsyncOnceCell::new()),
source: Arc::new(AsyncOnceCell::new()),
}
}
async fn get_async_seekable_source(&self) -> IoResult<&AsyncSource<A>> {
self.source
.get_or_try_init(async {
let mut file = AsyncFile::open(&self.path).await?;
if let Ok(offset) = file.stream_position().await {
Ok(AsyncSource::Seekable {
original_offset: offset,
file_size: file.metadata().await?.len(),
source: AsyncSeekableSource::new(file, 0, 0),
current: AsyncMutex::new(SourceOffset {
offset,
part_number: first_part_number(),
}),
})
} else {
Ok(AsyncSource::Unseekable(AsyncUnseekableDataSource::new(file)))
}
})
.await
}
async fn get_async_path(&self) -> IoResult<&AsyncPathBuf> {
self.canonicalized_path
.get_or_try_init(async { AsyncPathBuf::from(&self.path).canonicalize().await })
.await
}
}
impl<A: Digest + Send> AsyncDataSource<A> for AsyncFileDataSource<A> {
fn slice(&self, size: PartSize) -> BoxFuture<IoResult<Option<AsyncDataSourceReader>>> {
Box::pin(async move {
match self.get_async_seekable_source().await? {
AsyncSource::Seekable {
source,
current,
file_size,
..
} => {
let mut cur = current.lock().await;
if cur.offset < *file_size {
let size = size.as_u64();
let source_reader = AsyncDataSourceReader::seekable(
cur.part_number,
source.clone_with_new_offset_and_length(cur.offset, size),
);
cur.offset += size;
cur.part_number =
NonZeroUsize::new(cur.part_number.get() + 1).expect("Page number is too big");
Ok(Some(source_reader))
} else {
Ok(None)
}
}
AsyncSource::Unseekable(source) => source.slice(size).await,
}
})
}
fn reset(&self) -> BoxFuture<IoResult<()>> {
Box::pin(async move {
match self.get_async_seekable_source().await? {
AsyncSource::Seekable {
current,
original_offset,
..
} => {
let mut cur = current.lock().await;
cur.offset = *original_offset;
cur.part_number = first_part_number();
Ok(())
}
AsyncSource::Unseekable(source) => source.reset().await,
}
})
}
fn source_key(&self) -> BoxFuture<IoResult<Option<SourceKey<A>>>> {
Box::pin(async move {
match self.get_async_seekable_source().await? {
AsyncSource::Seekable { .. } => {
let mut hasher = A::new();
hasher.update(b"file://");
hasher.update(&self.get_async_path().await?.as_os_str().to_raw_bytes());
Ok(Some(hasher.finalize().into()))
}
AsyncSource::Unseekable(source) => source.source_key().await,
}
})
}
fn total_size(&self) -> BoxFuture<IoResult<Option<u64>>> {
Box::pin(async move {
match self.get_async_seekable_source().await? {
AsyncSource::Seekable { file_size, .. } => Ok(Some(*file_size)),
AsyncSource::Unseekable(source) => source.total_size().await,
}
})
}
}
impl<A: Digest> Debug for AsyncFileDataSource<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AsyncFileDataSource")
.field("path", &self.path)
.field("canonicalized_path", &self.canonicalized_path)
.field("source", &self.source)
.finish()
}
}
impl<A: Digest> Clone for AsyncFileDataSource<A> {
#[inline]
fn clone(&self) -> Self {
Self {
path: self.path.clone(),
canonicalized_path: self.canonicalized_path.clone(),
source: self.source.clone(),
}
}
}
#[derive(Debug)]
struct SourceOffset {
offset: u64,
part_number: NonZeroUsize,
}
enum AsyncSource<A: Digest> {
Seekable {
source: AsyncSeekableSource,
current: AsyncMutex<SourceOffset>,
file_size: u64,
original_offset: u64,
},
Unseekable(AsyncUnseekableDataSource<AsyncFile, A>),
}
impl<A: Digest> Debug for AsyncSource<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Seekable {
source,
current,
file_size,
original_offset,
} => f
.debug_struct("Seekable")
.field("source", source)
.field("current", current)
.field("file_size", file_size)
.field("original_offset", original_offset)
.finish(),
Self::Unseekable(file) => f.debug_tuple("Unseekable").field(file).finish(),
}
}
}
}
#[cfg(feature = "async")]
pub use async_reader::*;
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
use std::{
io::{Read, Write},
thread::{spawn as thread_spawn, JoinHandle as ThreadJoinHandle},
};
use tempfile::Builder as TempfileBuilder;
#[test]
fn test_sync_seekable_file_data_source() -> Result<()> {
let temp_file_path = {
let mut temp_file = TempfileBuilder::new().tempfile()?;
for i in 0..255u8 {
let buf = vec![i; 1 << 20];
temp_file.write_all(&buf)?;
}
temp_file.into_temp_path()
};
let data_source = FileDataSource::<Sha1>::new(&temp_file_path);
let mut source_readers = Vec::with_capacity(256);
for _ in 0..255u8 {
source_readers.push(data_source.slice(PartSize::new(1 << 20).unwrap())?.unwrap());
}
assert!(data_source.slice(PartSize::new(1 << 20).unwrap())?.is_none());
let mut threads: Vec<ThreadJoinHandle<IoResult<()>>> = Vec::with_capacity(256);
for (i, mut source_reader) in source_readers.into_iter().enumerate() {
threads.push(thread_spawn(move || {
let mut buf = Vec::new();
let have_read = source_reader.read_to_end(&mut buf)?;
assert_eq!(have_read, 1 << 20);
assert_eq!(buf, vec![i as u8; 1 << 20]);
Ok(())
}));
}
for thread in threads {
thread.join().unwrap()?;
}
Ok(())
}
#[test]
#[cfg(target_os = "unix")]
fn test_sync_unseekable_file_data_source() -> Result<()> {
use defer_lite::defer;
use ipipe::Pipe;
use std::fs::remove_file;
let mut pipe = Pipe::create()?;
let pipe_path = pipe.path().to_owned();
defer! {
remove_file(&pipe_path).ok();
}
let producer_thread: ThreadJoinHandle<IoResult<()>> = {
thread_spawn(move || {
for i in 0..255u8 {
let buf = vec![i; 1 << 20];
pipe.write_all(&buf)?;
}
pipe.close()?;
Ok(())
})
};
let data_source = FileDataSource::new(&pipe_path);
let mut source_readers = Vec::with_capacity(256);
for _ in 0..255u8 {
source_readers.push(data_source.slice(1 << 20)?.unwrap());
}
assert!(data_source.slice(1 << 20)?.is_none());
let mut threads: Vec<ThreadJoinHandle<IoResult<()>>> = Vec::with_capacity(257);
for (i, mut source_reader) in source_readers.into_iter().enumerate() {
threads.push(thread_spawn(move || {
let mut buf = Vec::new();
let have_read = source_reader.read_to_end(&mut buf)?;
assert_eq!(have_read, 1 << 20);
assert_eq!(buf, vec![i as u8; 1 << 20]);
Ok(())
}));
}
threads.push(producer_thread);
for thread in threads {
thread.join().unwrap()?;
}
Ok(())
}
}