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
|
use std::collections::VecDeque;
use std::fs;
use std::io;
use std::path;
use std::sync::mpsc;
use crate::io::{FileOperation, FileOperationOptions, FileOperationProgress};
use crate::util::name_resolution::rename_filename_conflict;
#[derive(Clone, Debug)]
pub struct IoWorkerThread {
_kind: FileOperation,
pub options: FileOperationOptions,
pub paths: Vec<path::PathBuf>,
pub dest: path::PathBuf,
}
impl IoWorkerThread {
pub fn new(
_kind: FileOperation,
paths: Vec<path::PathBuf>,
dest: path::PathBuf,
options: FileOperationOptions,
) -> Self {
Self {
_kind,
options,
paths,
dest,
}
}
pub fn kind(&self) -> FileOperation {
self._kind
}
pub fn start(
&self,
tx: mpsc::Sender<FileOperationProgress>,
) -> io::Result<FileOperationProgress> {
match self.kind() {
FileOperation::Cut => self.paste_cut(tx),
FileOperation::Copy => self.paste_copy(tx),
FileOperation::Delete => self.delete(tx),
}
}
fn paste_copy(
&self,
tx: mpsc::Sender<FileOperationProgress>,
) -> io::Result<FileOperationProgress> {
let (total_files, total_bytes) = query_number_of_items(&self.paths)?;
let mut progress = FileOperationProgress::new(self.kind(), 0, total_files, 0, total_bytes);
for path in self.paths.iter() {
let _ = tx.send(progress.clone());
recursive_copy(
&tx,
path.as_path(),
self.dest.as_path(),
self.options,
&mut progress,
)?;
}
Ok(progress)
}
fn paste_cut(
&self,
tx: mpsc::Sender<FileOperationProgress>,
) -> io::Result<FileOperationProgress> {
let (total_files, total_bytes) = query_number_of_items(&self.paths)?;
let mut progress = FileOperationProgress::new(self.kind(), 0, total_files, 0, total_bytes);
for path in self.paths.iter() {
let _ = tx.send(progress.clone());
recursive_cut(
&tx,
path.as_path(),
self.dest.as_path(),
self.options,
&mut progress,
)?;
}
Ok(progress)
}
fn delete(
&self,
_tx: mpsc::Sender<FileOperationProgress>,
) -> io::Result<FileOperationProgress> {
let (total_files, total_bytes) = query_number_of_items(&self.paths)?;
let progress = FileOperationProgress::new(
self.kind(),
total_files,
total_files,
total_bytes,
total_bytes,
);
if self.options.permanently {
remove_files(&self.paths)?;
} else {
trash_files(&self.paths)?;
}
Ok(progress)
}
}
fn query_number_of_items(paths: &[path::PathBuf]) -> io::Result<(usize, u64)> {
let mut total_bytes = 0;
let mut total_files = 0;
let mut dirs: VecDeque<path::PathBuf> = VecDeque::new();
for path in paths.iter() {
let metadata = path.symlink_metadata()?;
if metadata.is_dir() {
dirs.push_back(path.clone());
} else {
let metadata = path.symlink_metadata()?;
total_bytes += metadata.len();
total_files += 1;
}
}
while let Some(dir) = dirs.pop_front() {
for entry in fs::read_dir(dir)? {
let path = entry?.path();
if path.is_dir() {
dirs.push_back(path);
} else {
let metadata = path.symlink_metadata()?;
total_bytes += metadata.len();
total_files += 1;
}
}
}
Ok((total_files, total_bytes))
}
pub fn recursive_copy(
tx: &mpsc::Sender<FileOperationProgress>,
src: &path::Path,
dest: &path::Path,
options: FileOperationOptions,
progress: &mut FileOperationProgress,
) -> io::Result<()> {
let mut dest_buf = dest.to_path_buf();
if let Some(s) = src.file_name() {
dest_buf.push(s);
}
if !options.overwrite {
rename_filename_conflict(&mut dest_buf);
}
let file_type = fs::symlink_metadata(src)?.file_type();
if file_type.is_dir() {
match fs::create_dir(dest_buf.as_path()) {
Ok(_) => {}
e => {
if !options.overwrite {
return e;
}
}
}
for entry in fs::read_dir(src)? {
let entry = entry?;
let entry_path = entry.path();
recursive_copy(
tx,
entry_path.as_path(),
dest_buf.as_path(),
options,
progress,
)?;
let _ = tx.send(progress.clone());
}
Ok(())
} else if file_type.is_file() {
let bytes_processed = progress.bytes_processed() + fs::copy(src, dest_buf)?;
progress.set_bytes_processed(bytes_processed);
progress.set_files_processed(progress.files_processed() + 1);
Ok(())
} else if file_type.is_symlink() {
let link_path = fs::read_link(src)?;
std::os::unix::fs::symlink(link_path, dest_buf)?;
progress.set_files_processed(progress.files_processed() + 1);
Ok(())
} else {
Ok(())
}
}
pub fn recursive_cut(
tx: &mpsc::Sender<FileOperationProgress>,
src: &path::Path,
dest: &path::Path,
options: FileOperationOptions,
progress: &mut FileOperationProgress,
) -> io::Result<()> {
let mut dest_buf = dest.to_path_buf();
if let Some(s) = src.file_name() {
dest_buf.push(s);
}
if !options.overwrite {
rename_filename_conflict(&mut dest_buf);
}
let metadata = fs::symlink_metadata(src)?;
let file_type = metadata.file_type();
match fs::rename(src, dest_buf.as_path()) {
Ok(_) => {
let bytes_processed = progress.bytes_processed() + metadata.len();
progress.set_bytes_processed(bytes_processed);
progress.set_files_processed(progress.files_processed() + 1);
Ok(())
}
Err(_e) => {
if file_type.is_dir() {
fs::create_dir(dest_buf.as_path())?;
for entry in fs::read_dir(src)? {
let entry_path = entry?.path();
recursive_cut(
tx,
entry_path.as_path(),
dest_buf.as_path(),
options,
progress,
)?;
let _ = tx.send(progress.clone());
}
fs::remove_dir(src)?;
} else if file_type.is_symlink() {
let link_path = fs::read_link(src)?;
std::os::unix::fs::symlink(link_path, dest_buf)?;
fs::remove_file(src)?;
let processed = progress.bytes_processed() + metadata.len();
progress.set_bytes_processed(processed);
progress.set_files_processed(progress.files_processed() + 1);
} else {
let processed = progress.bytes_processed() +
|