Skip to main content

lfsx_server/storage/
verify.rs

1use futures_util::StreamExt;
2use serde::Serialize;
3use sha2::{Digest, Sha256};
4
5use super::LocalStore;
6use crate::error::Error;
7use crate::namespace::Namespace;
8
9#[derive(Debug, Default, Serialize, PartialEq, Eq)]
10pub struct VerifyReport {
11    pub checked: u64,
12    pub bytes: u64,
13    pub corrupt: Vec<String>,
14    pub unreadable: Vec<String>,
15    // An audit that could not see the whole repository must not read like one
16    // that found nothing wrong. Silence is the result here, so anything that
17    // makes the silence partial has to be said out loud.
18    pub incomplete: bool,
19}
20
21impl LocalStore {
22    // Every object is named after the digest of its own contents, so the store
23    // checks itself without a manifest — the property a restore is confirmed
24    // with. Compression at rest is what took it away from `sha256sum`: the file
25    // is no longer the bytes it is named after, so reading it back has to go
26    // through the same path a download does.
27    pub async fn verify(&self, ns: &Namespace) -> Result<VerifyReport, Error> {
28        let walk = self.objects_of(ns).await;
29        let mut report = VerifyReport {
30            incomplete: !walk.complete,
31            ..VerifyReport::default()
32        };
33
34        for found in walk.objects {
35            report.checked += 1;
36
37            match self.digest_of(ns, &found.oid).await {
38                Ok((digest, read)) => {
39                    report.bytes += read;
40
41                    if digest != found.oid {
42                        report.corrupt.push(found.oid);
43                    }
44                }
45                // A file that cannot be read is its own kind of answer, and the
46                // one a failing disk gives first. Reporting it as corrupt would
47                // send an operator looking for the wrong problem.
48                Err(error) => {
49                    tracing::warn!(oid = found.oid, %error, "object could not be read");
50                    report.unreadable.push(found.oid);
51                }
52            }
53        }
54
55        Ok(report)
56    }
57
58    async fn digest_of(&self, ns: &Namespace, oid: &str) -> Result<(String, u64), Error> {
59        let object = self.open(ns, oid).await?;
60        let size = object.size();
61
62        let mut hasher = Sha256::new();
63        let mut read = 0u64;
64        let mut chunks = object.stream(0, size).await?;
65
66        while let Some(chunk) = chunks.next().await {
67            let chunk = chunk?;
68            read += chunk.len() as u64;
69            hasher.update(&chunk);
70        }
71
72        Ok((hex::encode(hasher.finalize()), read))
73    }
74}
75
76#[cfg(test)]
77mod tests;