forked from rustic-rs/rustic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.rs
222 lines (195 loc) · 6.06 KB
/
ls.rs
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
//! `ls` subcommand
use std::path::Path;
use crate::{commands::open_repository_indexed, status_err, Application, RUSTIC_APP};
use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
use rustic_core::{
repofile::{Node, NodeType},
LsOptions,
};
mod constants {
// constants from man page inode(7)
pub(super) const S_IRUSR: u32 = 0o400; // owner has read permission
pub(super) const S_IWUSR: u32 = 0o200; // owner has write permission
pub(super) const S_IXUSR: u32 = 0o100; // owner has execute permission
pub(super) const S_IRGRP: u32 = 0o040; // group has read permission
pub(super) const S_IWGRP: u32 = 0o020; // group has write permission
pub(super) const S_IXGRP: u32 = 0o010; // group has execute permission
pub(super) const S_IROTH: u32 = 0o004; // others have read permission
pub(super) const S_IWOTH: u32 = 0o002; // others have write permission
pub(super) const S_IXOTH: u32 = 0o001; // others have execute permission
}
use constants::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR};
/// `ls` subcommand
#[derive(clap::Parser, Command, Debug)]
pub(crate) struct LsCmd {
/// Snapshot/path to list
#[clap(value_name = "SNAPSHOT[:PATH]")]
snap: String,
/// show summary
#[clap(long, short = 's')]
summary: bool,
/// show long listing
#[clap(long, short = 'l')]
long: bool,
/// show uid/gid instead of user/group
#[clap(long, long("numeric-uid-gid"))]
numeric_id: bool,
/// Listing options
#[clap(flatten)]
ls_opts: LsOptions,
}
impl Runnable for LsCmd {
fn run(&self) {
if let Err(err) = self.inner_run() {
status_err!("{}", err);
RUSTIC_APP.shutdown(Shutdown::Crash);
};
}
}
/// Sumary of a ls command
///
/// This struct is used to print a summary of the ls command.
#[derive(Default)]
pub struct Summary {
pub files: usize,
pub size: u64,
pub dirs: usize,
}
impl Summary {
/// Update the summary with the node
///
/// # Arguments
///
/// * `node` - the node to update the summary with
pub fn update(&mut self, node: &Node) {
if node.is_dir() {
self.dirs += 1;
}
if node.is_file() {
self.files += 1;
self.size += node.meta.size;
}
}
}
pub trait NodeLs {
fn mode_str(&self) -> String;
fn link_str(&self) -> String;
}
impl NodeLs for Node {
fn mode_str(&self) -> String {
format!(
"{:>1}{:>9}",
match self.node_type {
NodeType::Dir => 'd',
NodeType::Symlink { .. } => 'l',
NodeType::Chardev { .. } => 'c',
NodeType::Dev { .. } => 'b',
NodeType::Fifo { .. } => 'p',
NodeType::Socket => 's',
_ => '-',
},
self.meta
.mode
.map(parse_permissions)
.unwrap_or_else(|| "?????????".to_string())
)
}
fn link_str(&self) -> String {
if let NodeType::Symlink { .. } = &self.node_type {
["->", &self.node_type.to_link().to_string_lossy()].join(" ")
} else {
String::new()
}
}
}
impl LsCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();
let repo = open_repository_indexed(&config.repository)?;
let node =
repo.node_from_snapshot_path(&self.snap, |sn| config.snapshot_filter.matches(sn))?;
// recursive if standard if we specify a snapshot without dirs. In other cases, use the parameter `recursive`
let mut ls_opts = self.ls_opts.clone();
ls_opts.recursive = !self.snap.contains(':') || ls_opts.recursive;
let mut summary = Summary::default();
for item in repo.ls(&node, &ls_opts)? {
let (path, node) = item?;
summary.update(&node);
if self.long {
print_node(&node, &path, self.numeric_id);
} else {
println!("{}", path.display());
}
}
if self.summary {
println!(
"total: {} dirs, {} files, {} bytes",
summary.dirs, summary.files, summary.size
);
}
Ok(())
}
}
/// Print node in format similar to unix `ls`
///
/// # Arguments
///
/// * `node` - the node to print
/// * `path` - the path of the node
pub fn print_node(node: &Node, path: &Path, numeric_uid_gid: bool) {
println!(
"{:>10} {:>8} {:>8} {:>9} {:>17} {path:?} {}",
node.mode_str(),
if numeric_uid_gid {
node.meta.uid.map(|uid| uid.to_string())
} else {
node.meta.user.clone()
}
.unwrap_or_else(|| "?".to_string()),
if numeric_uid_gid {
node.meta.gid.map(|uid| uid.to_string())
} else {
node.meta.group.clone()
}
.unwrap_or_else(|| "?".to_string()),
node.meta.size,
node.meta
.mtime
.map(|t| t.format("%_d %b %Y %H:%M").to_string())
.unwrap_or_else(|| "?".to_string()),
node.link_str(),
);
}
/// Convert permissions into readable format
fn parse_permissions(mode: u32) -> String {
let user = triplet(mode, S_IRUSR, S_IWUSR, S_IXUSR);
let group = triplet(mode, S_IRGRP, S_IWGRP, S_IXGRP);
let other = triplet(mode, S_IROTH, S_IWOTH, S_IXOTH);
[user, group, other].join("")
}
/// Create a triplet of permissions
///
/// # Arguments
///
/// * `mode` - the mode to convert
/// * `read` - the read bit
/// * `write` - the write bit
/// * `execute` - the execute bit
///
/// # Returns
///
/// The triplet of permissions as a string
fn triplet(mode: u32, read: u32, write: u32, execute: u32) -> String {
match (mode & read, mode & write, mode & execute) {
(0, 0, 0) => "---",
(_, 0, 0) => "r--",
(0, _, 0) => "-w-",
(0, 0, _) => "--x",
(_, 0, _) => "r-x",
(_, _, 0) => "rw-",
(0, _, _) => "-wx",
(_, _, _) => "rwx",
}
.to_string()
}