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
|
extern crate clap;
extern crate dirs;
use clap::{App, AppSettings::ArgRequiredElseHelp, Arg};
use csv::{ReaderBuilder, Writer};
use std::fs;
use std::path::{Component, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
fn main() {
let matches = App::new("rsycle")
.author("Tom B. <tom@tombarrett.xyz>")
.version("0.1.0")
.about("Rsycle your files via cli")
.arg(
Arg::with_name("restore")
.long("restore")
.help("Restore INPUT file if given, if not open restore prompt"),
)
.arg(
Arg::with_name("empty")
.long("empty")
.help("Empties the rsyclebin"),
)
.arg(
Arg::with_name("list")
.long("list")
.help("List all files located in the rsyclebin"),
)
.arg(Arg::with_name("INPUT").help("File that will be recycled / restored"))
.setting(ArgRequiredElseHelp)
.get_matches();
let mut rsyclebin = dirs::home_dir().unwrap();
rsyclebin.push(".rsyclebin");
if !rsyclebin.exists() {
fs::create_dir(&rsyclebin).unwrap();
}
if matches.is_present("restore") {
if let Some(filename) = matches.value_of("INPUT") {
let filename = filename.to_owned() + ".";
restore(rsyclebin, &filename);
} else {
restore_cli(rsyclebin);
}
} else if matches.is_present("empty") {
empty(rsyclebin);
} else if matches.is_present("list") {
list(rsyclebin);
} else if let Some(filename) = matches.value_of("INPUT") {
rsycle(rsyclebin, filename);
}
}
fn log(rsyclebin: PathBuf, old_path: PathBuf, new_path: PathBuf) {
let mut rsyclebin_log = rsyclebin.clone();
rsyclebin_log.push(".log");
let file = fs::OpenOptions::new()
.append(true)
.create(true)
.open(rsyclebin_log)
.unwrap();
let mut writer = Writer::from_writer(file);
writer
.write_record(&[
fs::canonicalize(old_path).unwrap().to_str().unwrap(),
new_path.to_str().unwrap(),
])
.unwrap();
writer.flush().unwrap();
}
fn rsycle(rsyclebin: PathBuf, filename: &str) {
let old_path: PathBuf = [Component::CurDir, Component::Normal(filename.as_ref())]
.iter()
.collect();
if !old_path.exists() {
println!("File not found!");
return;
}
let new_filename = filename.to_owned()
+ "."
+ &SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.to_string();
let mut new_path = rsyclebin.clone();
new_path.push(new_filename);
log(rsyclebin, old_path.clone(), new_path.clone());
fs::rename(old_path, new_path).unwrap();
}
fn restore(rsyclebin: PathBuf, filename: &str) {
let paths: Vec<PathBuf> = fs::read_dir(rsyclebin.clone())
.unwrap()
.map(Result::unwrap)
.map(|dir| dir.path())
.filter(|path| {
path.file_name()
.unwrap()
.to_str()
.unwrap()
.starts_with(filename)
})
.collect();
match paths.iter().max_by_key(|path| {
path.file_name()
.unwrap()
.to_str()
.unwrap()
.split('.')
.collect::<Vec<&str>>()
.pop()
.unwrap()
.parse::<u64>()
.unwrap()
}) {
Some(latest_path) => {
let original_path = find_original_path(rsyclebin, latest_path.to_path_buf());
fs::rename(latest_path, original_path).unwrap();
}
None => println!("No file found !"),
}
}
fn list(rsyclebin: PathBuf) {
let mut paths: Vec<PathBuf> = fs::read_dir(rsyclebin.clone())
.unwrap()
.map(Result::unwrap)
.map(|dir| dir.path())
.collect();
for current_path in paths.iter_mut() {
let mut filename: Vec<&str> = current_path
.file_name()
.unwrap()
.to_str()
.unwrap()
.split('.')
.collect();
if let Some(last_point) = filename.pop() {
if let Ok(date) = last_point.parse::<u64>() {
let original_path = find_original_path(rsyclebin.clone(), current_path.clone());
let date = UNIX_EPOCH + Duration::from_secs(date);
println!("{:?}, {:?}, {:?}", original_path, current_path, date);
}
}
}
}
fn find_original_path(rsyclebin: PathBuf, path: PathBuf) -> String {
let mut rsyclebin_log = rsyclebin.clone();
rsyclebin_log.push(".log");
let file = fs::OpenOptions::new()
.read(true)
.open(rsyclebin_log)
.unwrap();
let mut reader = ReaderBuilder::new().has_headers(false).from_reader(file);
let path_str = path.to_str().unwrap();
reader
.records()
.map(Result::unwrap)
.find(|line| line.get(1).unwrap() == path_str)
.map(|line| line.get(0).unwrap().to_string())
.unwrap()
}
fn empty(rsyclebin: PathBuf) {
fs::remove_dir_all(rsyclebin).unwrap()
}
fn restore_cli(_rsyclebin: PathBuf) {
println!("unimplemented!")
}
|