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
use log::info;
#[cfg(test)]
use mockall::{automock, predicate::*};
use std::{
cmp::Ordering,
fs::File,
path::{Path, PathBuf},
};
use crate::{
compress::BackupPCReader,
decode_attribut::{AttributeFile, FileAttributes},
pool::find_file_in_backuppc,
util::{hex_string_to_vec, mangle, mangle_filename, vec_to_osstr, Result},
};
#[cfg_attr(test, automock)]
pub trait SearchTrait: Send + Sync {
/// Read the attributes from a file
///
/// If the file is compressed, uncompress it with special `BackupPCReader` and read the attributes.
///
/// # Arguments
///
/// * `file` - The path to the file to read.
/// * `is_compressed` - A boolean to know if the file is compressed.
///
/// # Returns
///
/// A vector of `FileAttributes` containing the list of attributes.
///
/// # Errors
///
/// If the file cannot be read or uncompressed.
fn read_attrib(&self, file: &Path, is_compressed: bool) -> Result<Vec<FileAttributes>>;
/// List the attributes for a complete path
///
/// The method will define the attrib path depending on the share and filename.
/// The attrib file is known as attrib_*
///
/// # Arguments
///
/// * `hostname` - The name of the host to list the attributes.
/// * `backup_number` - The number of the backup to list the attributes.
/// * `share` - The share where the file is stored.
/// * `filename` - The filename to list the attributes.
///
/// # Returns
///
/// A vector of `FileAttributes` containing the list of attributes
///
/// # Errors
///
/// If the file cannot be read or uncompressed.
/// If the file is not found in the pool.
fn list_file_from_dir<'a, 'b>(
&self,
hostname: &[u8],
backup_number: u32,
share: Option<&'a [u8]>,
filename: Option<&'b [u8]>,
) -> Result<Vec<FileAttributes>>;
/// List the attributes for hostname and backup knowning the attrib file
///
/// This method search the hex of the attrib file (in the filename) and read the corresponding file in the pool.
///
/// # Arguments
///
/// * `hostname` - The name of the host to list the attributes.
/// * `backup_number` - The number of the backup to list the attributes.
/// * `attrib_path` - The path to the attributes file.
/// * `attrib_file` - The prefix of the attributes file (starting attrib_).
///
/// # Returns
///
/// A vector of `FileAttributes` containing the list of attributes (readed from
/// the atrrib file stored in the pool)
///
/// # Errors
///
/// If the file cannot be read or uncompressed.
/// If the file is not found in the pool.
///
fn list_attributes(
&self,
hostname: &[u8],
backup_number: u32,
attrib_path: &str,
attrib_file: &str,
) -> Result<Vec<FileAttributes>>;
/// Return the attributes of a file
///
/// # Arguments
///
/// * `hostname` - The name of the host to list the attributes.
/// * `backup_number` - The number of the backup to list the attributes.
/// * `share` - The share where the file is stored.
/// * `filename` - The filename to list the attributes.
///
/// # Returns
///
/// A vector of `FileAttributes` containing the list of attributes
///
/// # Errors
///
/// If the file cannot be read or uncompressed.
/// If the file is not found in the pool.
fn get_file(
&self,
hostname: &[u8],
backup_number: u32,
share: &[u8],
filename: &[u8],
) -> Result<Vec<FileAttributes>>;
}
pub struct Search {
topdir: PathBuf,
}
impl Search {
#[must_use]
pub fn new<P: AsRef<Path>>(topdir: P) -> Self {
Search {
topdir: topdir.as_ref().to_path_buf(),
}
}
fn search_attrib_file<P: AsRef<Path>>(
&self,
backup_dir: P,
attrib_file: &str,
) -> Option<(String, std::path::PathBuf)> {
// Search for a file starting with the filename "attrib_" in the directory
let file = std::fs::read_dir(backup_dir.as_ref())
.ok()?
.filter_map(|entry| match entry {
Ok(entry) => entry
.file_name()
.to_str()
.map(|s| (s.to_string(), entry.path())),
Err(err) => {
eprintln!(
"Error reading directory: {}, {err}",
backup_dir.as_ref().display()
);
None
}
})
.find(|(name, _)| name.starts_with(attrib_file));
file
}
}
impl SearchTrait for Search {
fn read_attrib(&self, file: &Path, is_compressed: bool) -> Result<Vec<FileAttributes>> {
info!(
"Reading attributes from file: {} {}",
file.display(),
is_compressed
);
let input_file = File::open(file)?;
if is_compressed {
let mut reader = BackupPCReader::new(input_file);
let attrs = AttributeFile::read_from(&mut reader)?;
Ok(attrs.attributes)
} else {
let mut reader = std::io::BufReader::new(input_file);
let attrs = AttributeFile::read_from(&mut reader)?;
Ok(attrs.attributes)
}
}
fn list_attributes(
&self,
hostname: &[u8],
backup_number: u32,
attrib_path: &str,
attrib_file: &str,
) -> Result<Vec<FileAttributes>> {
let hostname_str = vec_to_osstr(hostname);
let backup_dir = self
.topdir
.join("pc")
.join(hostname_str)
.join(backup_number.to_string())
.join(attrib_path);
info!("Looking for attributes in {}", backup_dir.display());
let file = self.search_attrib_file(&backup_dir, attrib_file);
if let Some((_, file)) = file {
// Get the hash at the right of the _ symbole
let file = file.to_str().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Invalid file path: {file:?}"),
)
})?;
let file = file.split('_').collect::<Vec<&str>>();
let file = file[file.len() - 1];
if file == "0" {
return Ok(Vec::new());
}
let md5_hash: Vec<u8> = hex_string_to_vec(file);
match find_file_in_backuppc(&self.topdir, &md5_hash, None) {
Ok((file_path, is_compressed)) => {
let attributes = self.read_attrib(&file_path, is_compressed)?;
return Ok(attributes);
}
Err(message) => {
return Err(
std::io::Error::new(std::io::ErrorKind::InvalidData, message).into(),
)
}
}
}
Ok(Vec::new())
}
fn list_file_from_dir(
&self,
hostname: &[u8],
backup_number: u32,
share: Option<&[u8]>,
filename: Option<&[u8]>,
) -> Result<Vec<FileAttributes>> {
let share = share.map(mangle_filename);
let filename = filename.map(mangle);
let attrib_path = [share, filename]
.iter()
.filter_map(|f| f.as_deref())
.collect::<Vec<_>>()
.join("/");
self.list_attributes(hostname, backup_number, &attrib_path, "attrib_")
}
fn get_file(
&self,
hostname: &[u8],
backup_number: u32,
share: &[u8],
filename: &[u8],
) -> Result<Vec<FileAttributes>> {
info!(
"Looking for file {filename:?} in {}/pc/{hostname:?}/{backup_number}/{}",
self.topdir.display(),
mangle_filename(share),
);
let backup_dir_parts: Vec<&[u8]> = filename.split(|&byte| byte == b'/').collect();
let filename = backup_dir_parts.last().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Invalid path {filename:?}"),
)
})?;
let path = backup_dir_parts[..backup_dir_parts.len() - 1].join(&b'/');
match self.list_file_from_dir(hostname, backup_number, Some(share), Some(&path)) {
Ok(attributes) => Ok(attributes
.into_iter()
.filter(|attr| {
let filename = filename.to_vec();
attr.name.cmp(&filename) == Ordering::Equal
})
.collect()),
Err(e) => Err(e),
}
}
}