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
|
extern crate clap;
extern crate dirs;
use clap::{App, AppSettings::ArgRequiredElseHelp, Arg};
use csv::{ReaderBuilder, Writer};
use std::fs;
use std::io;
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 let Some(filename) = matches.value_of("INPUT") {
let path = build_path(filename).unwrap();
if matches.is_present("restore") {
restore(rsyclebin, path).unwrap();
} else {
rsycle(rsyclebin, path).unwrap();
}
} else if matches.is_present("empty") {
empty(rsyclebin).unwrap();
} else if matches.is_present("list") {
list(rsyclebin).unwrap();
} else if matches.is_present("restore") {
restore_cli(rsyclebin);
}
}
pub fn build_path(filename: &str) -> Result<PathBuf, io::Error> {
let relative: PathBuf = [Component::CurDir, Component::Normal(filename.as_ref())]
.iter()
.collect();
fs::canonicalize(&relative)
}
pub fn rsycle(rsyclebin: PathBuf, old_path: PathBuf) -> Result<(), io::Error> {
if !old_path.exists() {
Err(io::Error::new(io::ErrorKind::NotFound, "File not found!"))
} else {
let new_filename = old_path.file_name().unwrap().to_str().unwrap().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)
}
}
fn log(rsyclebin: PathBuf, old_path: PathBuf, new_path: PathBuf) -> Result<(), io::Error> {
let mut rsyclebin_log = rsyclebin.clone();
rsyclebin_log.push(".log");
let file = fs::OpenOptions::new()
.append(true)
.create(true)
.open(rsyclebin_log)?;
let mut writer = Writer::from_writer(file);
writer.write_record(&[
fs::canonicalize(old_path)?.to_str().unwrap(),
new_path.to_str().unwrap(),
])?;
writer.flush()
}
pub fn restore(rsyclebin: PathBuf, original_path: PathBuf) -> Result<(), io::Error> {
let current_path = most_recent_current_path(rsyclebin, original_path.clone())?;
if !original_path.exists() {
fs::rename(current_path, original_path)
} else {
Err(io::Error::new(
io::ErrorKind::NotFound,
"There is a file in the way!",
))
}
}
pub fn most_recent_current_path(rsyclebin: PathBuf, path: PathBuf) -> Result<PathBuf, io::Error> {
let mut rsyclebin_log = rsyclebin.clone();
rsyclebin_log.push(".log");
let file = fs::OpenOptions::new().read(true).open(rsyclebin_log)?;
let mut reader = ReaderBuilder::new().has_headers(false).from_reader(file);
let path_str = path.to_str().unwrap();
let original_paths: Vec<PathBuf> = reader
.records()
.map(Result::unwrap)
.filter(|line| line.get(0).unwrap() == path_str)
.map(|line| PathBuf::from(line.get(1).unwrap()))
.collect();
Ok(original_paths
.iter()
.max_by_key(|path| {
path.file_name()
.unwrap()
.to_str()
.unwrap()
.split('.')
.collect::<Vec<&str>>()
.pop()
.unwrap()
.parse::<u64>()
.unwrap()
})
.unwrap()
.to_path_buf())
}
pub fn list(rsyclebin: PathBuf) -> Result<(), io::Error> {
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: {:?}, current: {:?}, date: {:?}",
original_path, current_path, date
);
}
}
}
Ok(())
}
fn find_original_path(rsyclebin: PathBuf, current_path: PathBuf) -> Result<PathBuf, io::Error> {
let mut rsyclebin_log = rsyclebin.clone();
rsyclebin_log.push(".log");
let file = fs::OpenOptions::new().read(true).open(rsyclebin_log)?;
let mut reader = ReaderBuilder::new().has_headers(false).from_reader(file);
let path_str = current_path.to_str().unwrap();
Ok(PathBuf::from(
reader
.records()
.map(Result::unwrap)
.find(|line| line.get(1).unwrap() == path_str)
.map(|line| line.get(0).unwrap().to_string())
.unwrap(),
))
}
pub fn empty(rsyclebin: PathBuf) -> Result<(), io::Error> {
let paths: Vec<PathBuf> = fs::read_dir(rsyclebin.clone())
.unwrap()
.map(Result::unwrap)
.map(|dir| dir.path())
.collect();
for path in paths {
if fs::remove_file(path.clone()).is_err() {
fs::remove_dir_all(path)?
}
}
Ok(())
}
fn restore_cli(_rsyclebin: PathBuf) {
println!("unimplemented!")
}
|