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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use std::{
fmt::Debug,
fs::{self, File, Metadata},
io::{Read, Seek, SeekFrom, Write},
path::{Path, PathBuf},
process::Command,
};
use aho_corasick::AhoCorasick;
use bytes::Bytes;
use log::{debug, error, trace, warn};
use walkdir::WalkDir;
use rustic_core::{
ALL_FILE_TYPES, CommandInput, ErrorKind, FileType, Id, ReadBackend, RusticError, RusticResult,
WriteBackend,
};
/// A local backend.
#[derive(Clone, Debug)]
pub struct LocalBackend {
/// The base path of the backend.
path: PathBuf,
/// The command to call after a file was created.
post_create_command: Option<String>,
/// The command to call after a file was deleted.
post_delete_command: Option<String>,
}
impl LocalBackend {
/// Create a new [`LocalBackend`]
///
/// # Arguments
///
/// * `path` - The base path of the backend
/// * `options` - Additional options for the backend
///
/// # Errors
///
/// * If the directory could not be created.
///
/// # Options
///
/// * `post-create-command` - The command to call after a file was created.
/// * `post-delete-command` - The command to call after a file was deleted.
pub fn new(
path: impl AsRef<str>,
options: impl IntoIterator<Item = (String, String)>,
) -> RusticResult<Self> {
let path = path.as_ref().into();
let mut post_create_command = None;
let mut post_delete_command = None;
for (option, value) in options {
match option.as_str() {
"post-create-command" => {
post_create_command = Some(value);
}
"post-delete-command" => {
post_delete_command = Some(value);
}
opt => {
warn!("Option {opt} is not supported! Ignoring it.");
}
}
}
Ok(Self {
path,
post_create_command,
post_delete_command,
})
}
/// Base path of the given file type and id.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
///
/// # Returns
///
/// The base path of the file.
fn base_path(&self, tpe: FileType, id: &Id) -> PathBuf {
let hex_id = id.to_hex();
match tpe {
FileType::Config => self.path.clone(),
FileType::Pack => self.path.join("data").join(&hex_id[0..2]),
_ => self.path.join(tpe.dirname()),
}
}
fn filename(tpe: FileType, id: &Id) -> String {
match tpe {
FileType::Config => "config".to_string(),
_ => id.to_hex().to_string(),
}
}
/// Path to the given file type and id.
///
/// If the file type is `FileType::Pack`, the id will be used to determine the subdirectory.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
///
/// # Returns
///
/// The path to the file.
fn path(&self, tpe: FileType, id: &Id) -> PathBuf {
self.base_path(tpe, id).join(Self::filename(tpe, id))
}
/// Call the given command.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
/// * `filename` - The path to the file.
/// * `command` - The command to call.
///
/// # Errors
///
/// * If the patterns could not be compiled.
/// * If the command could not be parsed.
/// * If the command could not be executed.
/// * If the command was not successful.
///
/// # Notes
///
/// The following placeholders are supported:
/// * `%file` - The path to the file.
/// * `%type` - The type of the file.
/// * `%id` - The id of the file.
fn call_command(tpe: FileType, id: &Id, filename: &Path, command: &str) -> RusticResult<()> {
let id = id.to_hex();
let patterns = &["%file", "%type", "%id"];
let ac = AhoCorasick::new(patterns).map_err(|err| {
RusticError::with_source(
ErrorKind::Internal,
"Experienced an error building AhoCorasick automaton for command replacement.",
err,
)
.ask_report()
})?;
let replace_with = &[filename.to_str().unwrap(), tpe.dirname(), id.as_str()];
let actual_command = ac.replace_all(command, replace_with);
debug!("calling {actual_command}...");
let command: CommandInput = actual_command.parse().map_err(|err| {
RusticError::with_source(
ErrorKind::Internal,
"Failed to parse command input: `{command}` is not a valid command.",
err,
)
.attach_context("command", actual_command)
.attach_context("replacement", replace_with.join(", "))
.ask_report()
})?;
let status = Command::new(command.command())
.args(command.args())
.status()
.map_err(|err| {
RusticError::with_source(
ErrorKind::ExternalCommand,
"Failed to execute `{command}`. Please check the command and try again.",
err,
)
.attach_context("command", command.to_string())
})?;
if !status.success() {
return Err(RusticError::new(
ErrorKind::ExternalCommand,
"Command was not successful: `{command}` failed with status `{status}`.",
)
.attach_context("command", command.to_string())
.attach_context("file_name", replace_with[0])
.attach_context("file_type", replace_with[1])
.attach_context("id", replace_with[2])
.attach_context("status", status.to_string()));
}
Ok(())
}
}
impl ReadBackend for LocalBackend {
/// Returns the location of the backend.
///
/// This is `local:<path>`.
fn location(&self) -> String {
let mut location = "local:".to_string();
location.push_str(&self.path.to_string_lossy());
location
}
/// Lists all files of the given type.
///
/// # Arguments
///
/// * `tpe` - The type of the files to list.
///
/// # Notes
///
/// If the file type is `FileType::Config`, this will return a list with a single default id.
fn list(&self, tpe: FileType) -> RusticResult<Vec<Id>> {
trace!("listing tpe: {tpe:?}");
if tpe == FileType::Config {
return Ok(if self.path.join("config").exists() {
vec![Id::default()]
} else {
Vec::new()
});
}
let walker = WalkDir::new(self.path.join(tpe.dirname()))
.into_iter()
.inspect(|r| {
if let Err(err) = r {
error!("Error while listing files: {err:?}");
}
})
.filter_map(|r| {
let entry = r
.inspect_err(|err| error!("error listing {tpe}: {err}"))
.ok()?;
if !entry.file_type().is_file() {
return None;
}
let name = entry.file_name().to_string_lossy();
Id::parse_some(&name, tpe)
});
Ok(walker.collect())
}
/// Lists all files with their size of the given type.
///
/// # Arguments
///
/// * `tpe` - The type of the files to list.
///
/// # Errors
///
/// * If the metadata of the file could not be queried.
/// * If the length of the file could not be converted to u32.
/// * If the metadata of the file could not be queried.
fn list_with_size(&self, tpe: FileType) -> RusticResult<Vec<(Id, u32)>> {
fn length<E: Debug>(
meta: Result<Metadata, E>,
file_name: &str,
tpe: FileType,
) -> Option<u32> {
let metadata = meta.inspect_err(|err| {
error!(
"Failed to query metadata of the file {file_name} while listing {tpe}: {err:?}"
);
}).ok()?;
let length = metadata.len();
length.try_into().inspect_err(|err| {
error!("Failed to convert file length {length} of {file_name} to u32 while listing {tpe}: {err:?}");
}).ok()
}
trace!("listing tpe: {tpe:?}");
let path = self.path.join(tpe.dirname());
if tpe == FileType::Config {
if path.exists() {
return Ok(vec![(Id::default(), {
length(path.metadata(), "config", tpe).unwrap_or_default()
})]);
}
return Ok(Vec::new());
}
let walker = WalkDir::new(path).into_iter().filter_map(|r| {
let entry = r
.inspect_err(|err| error!("error listing {tpe}: {err}"))
.ok()?;
if !entry.file_type().is_file() {
return None;
}
let name = entry.file_name().to_string_lossy();
let id = Id::parse_some(&name, tpe)?;
let length = length(entry.metadata(), &name, tpe)?;
Some((id, length))
});
Ok(walker.collect())
}
/// Reads full data of the given file.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
///
/// # Errors
///
/// * If the file could not be read.
/// * If the file could not be found.
fn read_full(&self, tpe: FileType, id: &Id) -> RusticResult<Bytes> {
trace!("reading tpe: {tpe:?}, id: {id}");
Ok(fs::read(self.path(tpe, id))
.map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Failed to read the contents of the file. Please check the file and try again.",
err,
)
.attach_context("path", self.path(tpe, id).to_string_lossy())
})?
.into())
}
/// Reads partial data of the given file.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
/// * `cacheable` - Whether the file is cacheable.
/// * `offset` - The offset to read from.
/// * `length` - The length to read.
///
/// # Errors
///
/// * If the file could not be opened.
/// * If the file could not be sought to the given position.
/// * If the length of the file could not be converted to u32.
/// * If the exact length of the file could not be read.
fn read_partial(
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
offset: u32,
length: u32,
) -> RusticResult<Bytes> {
trace!("reading tpe: {tpe:?}, id: {id}, offset: {offset}, length: {length}");
let filename = self.path(tpe, id);
let mut file = File::open(filename.clone()).map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Failed to open the file `{path}`. Please check the file and try again.",
err,
)
.attach_context("path", filename.to_string_lossy())
})?;
_ = file.seek(SeekFrom::Start(offset.into())).map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Failed to seek to the position `{offset}` in the file `{path}`. Please check the file and try again.",
err,
)
.attach_context("path", self.path(tpe, id).to_string_lossy())
.attach_context("offset", offset.to_string())
})?;
let mut vec = vec![
0;
length.try_into().map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Failed to convert length `{length}` to u64.",
err,
)
.attach_context("length", length.to_string())
.ask_report()
})?
];
file.read_exact(&mut vec).map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Failed to read the exact length `{length}` of the file `{path}`. Please check the file and try again.",
err,
)
.attach_context("path", self.path(tpe, id).to_string_lossy())
.attach_context("length", length.to_string())
})?;
Ok(vec.into())
}
fn warmup_path(&self, tpe: FileType, id: &Id) -> String {
// For local backends, we can provide the filesystem path as the warmup path
// though warmup is not typically needed for local storage
self.path(tpe, id).to_string_lossy().to_string()
}
}
impl WriteBackend for LocalBackend {
/// Create a repository on the backend.
///
/// # Errors
///
/// * If the directory could not be created.
fn create(&self) -> RusticResult<()> {
trace!("creating repo at {}", self.path.display());
fs::create_dir_all(&self.path).map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to create the directory `{path}`. Please check the path and try again.",
err,
)
.attach_context("path", self.path.display().to_string())
})?;
for tpe in ALL_FILE_TYPES {
let path = self.path.join(tpe.dirname());
fs::create_dir_all(path.clone()).map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to create the directory `{path}`. Please check the path and try again.",
err,
)
.attach_context("path", path.display().to_string())
})?;
}
for i in 0u8..=255 {
let path = self.path.join("data").join(hex::encode([i]));
fs::create_dir_all(path.clone()).map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to create the directory `{path}`. Please check the path and try again.",
err,
)
.attach_context("path", path.display().to_string())
})?;
}
Ok(())
}
/// Write the given bytes to the given file.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
/// * `cacheable` - Whether the file is cacheable.
/// * `buf` - The bytes to write.
///
/// # Errors
///
/// * If the file could not be opened.
/// * If the length of the bytes could not be converted to u64.
/// * If the length of the file could not be set.
/// * If the bytes could not be written to the file.
/// * If the OS Metadata could not be synced to disk.
/// * If the file does not have a parent directory.
/// * If the parent directory could not be created.
/// * If the file cannot be opened, due to missing permissions.
/// * If the file cannot be written to, due to lack of space on the disk.
fn write_bytes(
&self,
tpe: FileType,
id: &Id,
_cacheable: bool,
buf: Bytes,
) -> RusticResult<()> {
fn write_local_file(filename: &Path, buf: &[u8]) -> RusticResult<()> {
let length = buf.len().try_into().map_err(|err| {
RusticError::with_source(
ErrorKind::Internal,
"Failed to convert length `{length}` to u64.",
err,
)
.attach_context("length", buf.len().to_string())
.ask_report()
})?;
let mut file = fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(filename)
.map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to open the file `{path}`. Please check the file and try again.",
err,
)
.attach_context("path", filename.to_string_lossy())
})?;
file.set_len(length)
.map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to set the length of the file `{path}`. Please check the file and try again.",
err,
)
.attach_context("path", filename.to_string_lossy())
})?;
file.write_all(buf).map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to write to the buffer: `{path}`. Please check the file and try again.",
err,
)
.attach_context("path", filename.to_string_lossy())
})?;
file.sync_all().map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to sync OS Metadata to disk: `{path}`. Please check the file and try again.",
err,
)
.attach_context("path", filename.to_string_lossy())
})?;
Ok(())
}
trace!("writing tpe: {:?}, id: {}", &tpe, &id);
let filename = self.path(tpe, id);
let parent = self.base_path(tpe, id);
// create parent directory if it does not exist
fs::create_dir_all(&parent)
.map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to create directories `{path}`. Does the directory already exist? Please check the file and try again.",
err,
)
.attach_context("path", parent.display().to_string())
.ask_report()
})?;
// Write to temporary file
let filename_tmp = parent.join(Self::filename(tpe, id) + "-tmp-");
match write_local_file(&filename_tmp, &buf) {
Ok(file) => file,
Err(err) => {
// Clean-up in case of error
_ = fs::remove_file(&filename_tmp);
return Err(err);
}
}
// rename temporary file to real file
fs::rename(&filename_tmp, &filename).map_err(|err| {
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to move `{path_tmp}` to `{path}`",
err,
)
.attach_context("path_tmp", filename_tmp.display().to_string())
.attach_context("path", filename.display().to_string())
.ask_report()
})?;
if let Some(command) = &self.post_create_command
&& let Err(err) = Self::call_command(tpe, id, &filename, command)
{
warn!("post-create: {}", err.display_log());
}
Ok(())
}
/// Remove the given file.
///
/// # Arguments
///
/// * `tpe` - The type of the file.
/// * `id` - The id of the file.
/// * `cacheable` - Whether the file is cacheable.
///
/// # Errors
///
/// * If the file could not be removed.
fn remove(&self, tpe: FileType, id: &Id, _cacheable: bool) -> RusticResult<()> {
trace!("removing tpe: {:?}, id: {}", &tpe, &id);
let filename = self.path(tpe, id);
fs::remove_file(&filename).map_err(|err|
RusticError::with_source(
ErrorKind::Backend,
"Failed to remove the file `{path}`. Was the file already removed or is it in use? Please check the file and remove it manually.",
err
)
.attach_context("path", filename.to_string_lossy())
)?;
if let Some(command) = &self.post_delete_command
&& let Err(err) = Self::call_command(tpe, id, &filename, command)
{
warn!("post-delete: {}", err.display_log());
}
Ok(())
}
}