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
/* ***
*
* Harmos
*
* SPDX-License-Identifier: Apache-2.0
*
* ***
*/
// Hash A Bounded File Range Without Retaining The Complete Payload.
/* === Modules === */
/* === Re-Exports === */
/* === Imports === */
// Standard Library Imports
// Third Party Imports
use sha2::{Digest, Sha256};
// Local Imports
use crate::{Result, StorageManager};
use harmos_domain::model::storage::files::FileHash;
use harmos_domain::operations::storage::{HashFile, ReadFile};
/* === Constants === */
/* === Macros === */
/* === Type Aliases === */
/* === Structs === */
/* === Enums === */
/* === Traits === */
/* === Implementations === */
impl<C> StorageManager<C> {
pub async fn hash_file(&self, request: HashFile) -> Result<FileHash> {
let context = crate::execution::Context::current();
let span = context.sync(|| tracing::debug_span!("storage.file.hash_file", outcome = "cancelled"));
context
.result(span, async {
let mut reader = self.read_file(ReadFile { source: request.source, range: request.range }).await?;
let mut hash = Sha256::new();
let local = reader.is_local();
while let Some(batch) = reader.next().await? {
if local {
hash.update(batch.bytes());
}
}
let completion = reader.completion()?;
Ok(reader.stored_hash()?.unwrap_or_else(|| FileHash { bytes_read: completion.bytes, sha256: hash.finalize().into() }))
})
.await
}
}
/* === Public Functions === */
/* === Private Functions === */
/* === Tests === */