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
use std::borrow::Cow;
use std::io::{BufWriter, Write};
use crate::*;
#[derive(Default, Clone, Eq, PartialEq)]
pub struct ShowVersionsOptions {
pub newest_first: bool,
pub tree_size: bool,
pub start_time: bool,
pub backup_duration: bool,
pub utc: bool,
}
pub fn show_versions(
archive: &Archive,
options: &ShowVersionsOptions,
w: &mut dyn Write,
) -> Result<()> {
let mut band_ids = archive.list_band_ids()?;
if options.newest_first {
band_ids.reverse();
}
for band_id in band_ids {
if !(options.tree_size || options.start_time || options.backup_duration) {
writeln!(w, "{}", band_id)?;
continue;
}
let mut l: Vec<String> = Vec::new();
l.push(format!("{:<20}", band_id));
let band = match Band::open(&archive, &band_id) {
Ok(band) => band,
Err(e) => {
ui::problem(&format!("Failed to open band {:?}: {:?}", band_id, e));
continue;
}
};
let info = match band.get_info() {
Ok(info) => info,
Err(e) => {
ui::problem(&format!("Failed to read band tail {:?}: {:?}", band_id, e));
continue;
}
};
if options.start_time {
let start_time = info.start_time;
let start_time_str = if options.utc {
start_time.format(crate::TIMESTAMP_FORMAT)
} else {
start_time
.with_timezone(&chrono::Local)
.format(crate::TIMESTAMP_FORMAT)
};
l.push(format!("{:<10}", start_time_str));
}
if options.backup_duration {
let duration_str: Cow<str> = if info.is_closed {
info.end_time
.and_then(|et| (et - info.start_time).to_std().ok())
.map(crate::ui::duration_to_hms)
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed("unknown"))
} else {
Cow::Borrowed("incomplete")
};
l.push(format!("{:>10}", duration_str));
}
if options.tree_size {
let tree_mb_str = crate::misc::bytes_to_human_mb(
archive
.open_stored_tree(BandSelectionPolicy::Specified(band_id.clone()))?
.size(None)?
.file_bytes,
);
l.push(format!("{:>14}", tree_mb_str,));
}
writeln!(w, "{}", l.join(" "))?;
}
Ok(())
}
pub fn show_index_json(band: &Band, w: &mut dyn Write) -> Result<()> {
let bw = BufWriter::new(w);
let index_entries: Vec<IndexEntry> = band.index().iter_entries().collect();
serde_json::ser::to_writer_pretty(bw, &index_entries)
.map_err(|source| Error::SerializeIndex { source })
}
pub fn show_entry_names<E: Entry, I: Iterator<Item = E>>(it: I, w: &mut dyn Write) -> Result<()> {
let mut bw = BufWriter::new(w);
for entry in it {
writeln!(bw, "{}", entry.apath())?;
}
Ok(())
}
pub fn show_diff<D: Iterator<Item = DiffEntry>>(diff: D, w: &mut dyn Write) -> Result<()> {
let mut bw = BufWriter::new(w);
for de in diff {
writeln!(bw, "{}", de)?;
}
Ok(())
}