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
|
use std::{
fmt::Display,
fs::{create_dir_all, File},
io::Read,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
use tracing::info;
use crate::{
backup::{Backup, Id},
config::Config,
error::{Error, Result},
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathInfo {
is_file: bool,
rel_location: String,
location_root: LocationRoot,
last_modified: Option<Id>,
children: Vec<PathInfo>,
}
impl PathInfo {
pub fn from_path(config: &Config, path: &str) -> Result<Self> {
let locations = Self::parse_location(path, config)?;
Self::handle_dir(config, &locations.0, &locations.1)
}
fn handle_dir(
config: &Config,
rel_location: &str,
location_root: &LocationRoot,
) -> Result<Self> {
info!("Handling {rel_location}");
let path = Self::get_abs_path(&location_root.to_string(), rel_location);
Ok(if path.is_dir() {
let mut last_modified = Some(String::new());
let mut last_modified_timestamp = 0;
let mut children: Vec<PathInfo> = Vec::new();
let paths = std::fs::read_dir(path).unwrap();
for path in paths {
let pathstr = path.unwrap().path().to_string_lossy().to_string();
let root = format!("{location_root}/");
let Some(rl) = pathstr.split_once(&root) else {
panic!("HUH");
};
let handle = Self::handle_dir(config, rl.1, location_root)?;
if let Some(lm) = handle.last_modified.clone() {
if last_modified.is_some() {
let ts = Backup::from_index(config, &lm)?.timestamp;
if ts > last_modified_timestamp {
last_modified_timestamp = ts;
last_modified = Some(lm);
};
}
} else {
last_modified = None;
};
children.push(handle);
}
Self {
is_file: false,
rel_location: rel_location.to_string(),
location_root: location_root.clone(),
last_modified,
children,
}
} else {
Self::from_file(config, rel_location, location_root)?
})
}
fn from_file(
config: &Config,
rel_location: &str,
location_root: &LocationRoot,
) -> Result<Self> {
let last_modified = Self::compare_to_last_modified(config, location_root, rel_location)?;
info!("From file {rel_location} ({last_modified:?})");
Ok(Self {
rel_location: rel_location.to_string(),
location_root: location_root.clone(),
last_modified,
is_file: true,
children: Vec::new(),
})
}
pub fn compare_to_last_modified(
config: &Config,
location_root: &LocationRoot,
rel_location: &str,
) -> Result<Option<String>> {
let Some(last_backup) = Backup::get_last(config)? else {
// First Backup
return Ok(None);
};
let files = last_backup.files.clone();
let last_file_opt = Self::find_last_modified(files, rel_location, location_root);
let Some(last_file) = last_file_opt else {
// File didn't exist last Backup
return Ok(None);
};
let modified_backup = if let Some(modified_backup_id) = &last_file.last_modified {
Backup::from_index(config, modified_backup_id)?
} else {
last_backup
};
let old_path = modified_backup.get_absolute_file_location(config, &last_file.rel_location);
let new_path = format!("{location_root}/{rel_location}");
let mut old = File::open(old_path)?; let mut new = File::open(new_path)?;
let old_len = old.metadata()?.len();
let new_len = new.metadata()?.len();
if old_len != new_len {
return Ok(None);
}
let mut old_content = String::new();
old.read_to_string(&mut old_content)?;
let mut new_content = String::new();
new.read_to_string(&mut new_content)?;
if old_content != new_content {
return Ok(None);
}
Ok(Some(modified_backup.id.clone()))
}
pub fn find_last_modified(
files: Vec<Self>,
rel_location: &str,
location_root: &LocationRoot,
) -> Option<PathInfo> {
for path in files {
if path.is_file {
if path.rel_location == rel_location && path.location_root == *location_root {
return Some(path);
};
} else {
let is_modified =
PathInfo::find_last_modified(path.children, rel_location, location_root);
if is_modified.is_some() {
return is_modified;
};
}
}
None
}
pub fn get_absolute_path(&self) -> PathBuf {
Self::get_abs_path(&self.location_root.to_string(), &self.rel_location)
}
pub fn save(&self, backup_root: &str) -> Result<()> {
if self.last_modified.is_some() {
return Ok(());
}
info!("Save File {:?}", self.rel_location);
if self.is_file {
let new_path = format!("{}/{}", backup_root, self.rel_location);
let np = Path::new(&new_path);
if let Some(parent) = np.parent() {
create_dir_all(parent)?;
}
std::fs::copy(self.get_absolute_path(), new_path)?;
} else {
for child in &self.children {
child.save(backup_root)?;
}
};
Ok(())
}
fn get_abs_path(location_root: &str, rel_location: &str) -> PathBuf {
let path = format!("{location_root}/{rel_location}");
PathBuf::from(path)
}
fn parse_location(value: &str, config: &Config) -> Result<(String, LocationRoot)> {
let Some(split) = value.split_once('/') else {
return Err(Error::InvalidDirectory(value.to_string()));
};
if split.0.starts_with('~') {
if config.user.len() != 1 {
return Err(Error::MultiUser);
}
return Ok((
split.1.to_string(),
LocationRoot::User(config.user[0].clone()),
));
};
Ok((
split.1.to_string(),
LocationRoot::from_op_str(split.0, config)?,
))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum LocationRoot {
User(String),
Custom(String),
SystemSettings,
Root,
}
impl Display for LocationRoot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LocationRoot::User(user) => write!(f, "/home/{user}"),
LocationRoot::Custom(loc) => write!(f, "{loc}"),
LocationRoot::SystemSettings => write!(f, "/etc"),
LocationRoot::Root => write!(f, "/"),
}
}
}
impl LocationRoot {
fn from_op_str(value: &str, config: &Config) -> Result<Self> {
let split_str = value.split_once(':');
let Some(split_op) = split_str else {
return Err(Error::NoIndex);
};
match split_op.0 {
"u" => Ok(Self::User(split_op.1.to_string())),
"s" => Ok(Self::SystemSettings),
"r" => Ok(Self::Root),
"c" => Ok(Self::Custom(
config
.custom_directories
.get(split_op.1)
.ok_or_else(|| Error::CustomDirectory(split_op.1.to_string()))?
.to_string(),
)),
_ => Err(Error::InvalidIndex(split_op.0.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use std::{
fs::{create_dir_all, remove_dir_all, File},
io::Write,
};
use crate::{
backup::Backup,
config::Config,
error::{Error, Result},
packages::{pacman::Pacman, PackageManager},
};
use super::LocationRoot;
use super::PathInfo;
#[test]
fn from_op_str() -> Result<()> {
let mut config = Config::default();
config
.custom_directories
.insert("test".to_string(), "/usr/local/test".to_string());
let mut values_ok: Vec<(&str, LocationRoot)> = Vec::new();
values_ok.push(("u:test", LocationRoot::User("test".to_string())));
values_ok.push(("s:", LocationRoot::SystemSettings));
values_ok.push(("r:", LocationRoot::Root));
values_ok.push((
"c:test",
LocationRoot::Custom("/usr/local/test".to_string()),
));
for value in values_ok {
println!("Testing {value:?}");
assert_eq!(LocationRoot::from_op_str(value.0, &config)?, value.1);
println!("\x1B[FTesting {value:?} ✓");
}
let mut values_err: Vec<(&str, String)> = Vec::new();
values_err.push((
"c:rest",
Error::CustomDirectory("rest".to_string()).to_string(),
));
values_err.push(("t:test/", Error::InvalidIndex("t".to_string()).to_string()));
values_err.push((
"test:test/usr",
Error::InvalidIndex("test".to_string()).to_string(),
));
values_err.push(("/usr/local/test", Error::NoIndex.to_string()));
values_err.push(("c/usr/local/test", Error::NoIndex.to_string()));
for value in values_err {
println!("Testing {value:?}");
assert_eq!(
LocationRoot::from_op_str(value.0, &config)
.err()
.unwrap()
.to_string(),
value.1
);
println!("\x1B[FTesting {value:?} ✓");
}
Ok(())
}
#[test]
fn parse_location() -> Result<()> {
let mut config = Config::default();
config.user.push("test".to_string());
config
.custom_directories
.insert("test".to_string(), "/usr/local/test".to_string());
let mut values_ok: Vec<(&str, (String, LocationRoot))> = Vec::new();
values_ok.push((
"~/.config/nvim",
(
".config/nvim".to_string(),
LocationRoot::User("test".to_string()),
),
));
values_ok.push((
"u:test/.config/nvim",
(
".config/nvim".to_string(),
LocationRoot::User("test".to_string()),
),
));
values_ok.push((
"r:/.config/nvim",
(".config/nvim".to_string(), LocationRoot::Root),
));
values_ok.push((
"r:/.config/nvim",
(".config/nvim".to_string(), LocationRoot::Root),
));
values_ok.push((
"s:/.config/nvim",
(".config/nvim".to_string(), LocationRoot::SystemSettings),
));
values_ok.push((
"c:test/.config/nvim",
(
".config/nvim".to_string(),
LocationRoot::Custom("/usr/local/test".to_string()),
),
));
for value in values_ok {
print!("Testing {value:?}");
assert_eq!(PathInfo::parse_location(&value.0, &config)?, value.1);
println!("\x1B[FTesting {value:?} ✓");
}
Ok(())
}
#[test]
fn compare_to_last_modified() -> color_eyre::Result<()> {
let mut config = Config::default();
config.root = "./backup-test".to_string();
config
.directories
.push("u:fx/code/proj/arps/backup-test-dir".to_string());
create_dir_all("./backup-test-dir")?;
let mut f = File::create("./backup-test-dir/size.txt")?;
f.write_all("unmodified".as_bytes())?;
let mut f = File::create("./backup-test-dir/content.txt")?;
f.write_all("unmodified".as_bytes())?;
let mut f = File::create("./backup-test-dir/nothing.txt")?;
f.write_all("unmodified".as_bytes())?;
let pacman = Pacman;
let pkgs = pacman.get_installed()?;
let backup = Backup::create(&config, pkgs)?;
backup.save(&config)?;
let mut f = File::create("./backup-test-dir/size.txt")?;
f.write_all("modified".as_bytes())?;
let mut f = File::create("./backup-test-dir/content.txt")?;
f.write_all("unmodefied".as_bytes())?;
let pi = PathInfo::from_path(&config, "u:fx/code/proj/arps/backup-test-dir")?;
let last_backup = Backup::get_last(&config)?.unwrap();
for file in pi.children {
println!("test rel: {}", file.rel_location);
let res = if file.rel_location == "code/proj/arps/backup-test-dir/nothing.txt" {
Some(last_backup.id.clone())
} else {
None
};
println!("Testing {file:?}");
assert_eq!(
PathInfo::compare_to_last_modified(
&config,
&file.location_root,
&file.rel_location
)?,
res
);
println!("\x1B[FTesting {file:?} ✓");
}
remove_dir_all("./backup-test-dir")?;
remove_dir_all("./backup-test")?;
Ok(())
}
}
|