quichash_core/database/
mod.rs1use std::collections::HashMap;
5use std::io::Write;
6use std::path::{Path, PathBuf};
7
8use crate::error::HashUtilityError;
9use crate::hash::Algorithm;
10use crate::manifest::Manifest;
11use crate::operation::FailurePolicy;
12
13pub(crate) mod checksum;
14pub(crate) mod compression;
15pub(crate) mod hashdeep;
16pub(crate) mod manifest_io;
17pub(crate) mod quichash;
18
19pub(crate) use hashdeep::HashdeepRecord;
20
21#[derive(Debug, Clone)]
23pub struct DatabaseEntry {
24 pub hash: String,
26 pub algorithm: String,
28 pub fast_mode: bool,
30}
31
32#[derive(Debug, Clone, Copy, PartialEq)]
34pub enum DatabaseFormat {
35 Quichash,
37 Hashdeep,
39}
40
41#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
43pub struct DatabaseIssue {
44 pub line: usize,
46 pub message: String,
48}
49
50#[derive(Clone, Debug, serde::Serialize)]
52pub struct ManifestRead {
53 pub manifest: Manifest,
55 pub issues: Vec<DatabaseIssue>,
57}
58
59pub struct DatabaseHandler;
61
62impl DatabaseHandler {
63 pub fn checksum_algorithm_from_path(path: &Path) -> Option<Algorithm> {
65 checksum::checksum_algorithm_from_path(path)
66 }
67
68 pub fn verification_checksum_algorithm(
70 path: &Path,
71 ) -> Result<Option<Algorithm>, HashUtilityError> {
72 checksum::verification_checksum_algorithm(path)
73 }
74
75 pub fn read_checksum_manifest(path: &Path) -> Result<Manifest, HashUtilityError> {
77 checksum::read_checksum_manifest(path)
78 }
79
80 pub fn canonical_output_path(
82 requested_path: &Path,
83 format: DatabaseFormat,
84 compressed: bool,
85 ) -> Result<PathBuf, HashUtilityError> {
86 compression::canonical_output_path(requested_path, format, compressed)
87 }
88
89 pub fn write_manifest_file(
91 requested_path: &Path,
92 manifest: &Manifest,
93 format: DatabaseFormat,
94 compressed: bool,
95 ) -> Result<PathBuf, HashUtilityError> {
96 manifest_io::write_manifest_file(requested_path, manifest, format, compressed)
97 }
98
99 pub fn is_compressed(path: &Path) -> bool {
101 compression::is_compressed(path)
102 }
103
104 pub fn compress_database(input_path: &Path) -> Result<PathBuf, HashUtilityError> {
106 compression::compress_database(input_path)
107 }
108
109 pub fn detect_format(path: &Path) -> Result<DatabaseFormat, HashUtilityError> {
111 manifest_io::detect_format(path)
112 }
113
114 pub fn write_entry(
116 writer: &mut impl Write,
117 hash: &str,
118 algorithm: &str,
119 fast_mode: bool,
120 path: &Path,
121 ) -> std::io::Result<()> {
122 quichash::write_entry(writer, hash, algorithm, fast_mode, path)
123 }
124
125 pub fn write_hashdeep_header(
127 writer: &mut impl Write,
128 algorithms: &[String],
129 ) -> std::io::Result<()> {
130 hashdeep::write_hashdeep_header(writer, algorithms)
131 }
132
133 pub fn write_hashdeep_entry(
135 writer: &mut impl Write,
136 size: u64,
137 hashes: &[String],
138 path: &Path,
139 ) -> std::io::Result<()> {
140 hashdeep::write_hashdeep_entry(writer, size, hashes, path)
141 }
142
143 pub fn read_database(path: &Path) -> Result<HashMap<PathBuf, DatabaseEntry>, HashUtilityError> {
145 manifest_io::read_database(path)
146 }
147
148 pub fn read_manifest(path: &Path) -> Result<Manifest, HashUtilityError> {
150 manifest_io::read_manifest(path)
151 }
152
153 pub fn read_manifest_with_policy(
155 path: &Path,
156 failure_policy: FailurePolicy,
157 ) -> Result<ManifestRead, HashUtilityError> {
158 manifest_io::read_manifest_with_policy(path, failure_policy)
159 }
160
161 pub fn write_manifest(
163 writer: &mut impl Write,
164 manifest: &Manifest,
165 format: DatabaseFormat,
166 ) -> std::io::Result<()> {
167 manifest_io::write_manifest(writer, manifest, format)
168 }
169
170 pub fn parse_line(line: &str) -> Option<(String, String, bool, PathBuf)> {
172 quichash::parse_line(line)
173 }
174
175 pub fn parse_hashdeep_line(
177 line: &str,
178 algorithms: &[String],
179 ) -> Option<Vec<(PathBuf, DatabaseEntry)>> {
180 hashdeep::parse_hashdeep_line(line, algorithms)
181 }
182
183 pub(crate) fn parse_hashdeep_record(
184 line: &str,
185 expected_hash_count: Option<usize>,
186 ) -> Option<HashdeepRecord> {
187 hashdeep::parse_hashdeep_record(line, expected_hash_count)
188 }
189}