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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use std::path::PathBuf;
use crate::errors::RvResult;
/// Provides a builder pattern for flexibly copying files
///
/// Use the Vfs functions `copy_b` to create a new instance followed by one or more options and
/// complete the operation by calling `exec`.
///
/// ```
/// use rivia::prelude::*;
///
/// let vfs = Memfs::new();
/// let file1 = vfs.root().mash("file1");
/// let file2 = vfs.root().mash("file2");
/// assert_vfs_write_all!(vfs, &file1, "this is a test");
/// assert!(vfs.copy_b(&file1, &file2).unwrap().exec().is_ok());
/// assert_eq!(vfs.read_all(&file2).unwrap(), "this is a test");
/// ```
pub struct Copier
{
pub(crate) opts: CopyOpts,
pub(crate) exec: Box<dyn Fn(CopyOpts) -> RvResult<()>>, // provider callback
}
// Internal type used to encapsulate just the options. This separates the provider implementation
// from the options allowing for sharing options between different vfs providers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CopyOpts
{
pub(crate) src: PathBuf, // source file
pub(crate) dst: PathBuf, // destination path
pub(crate) mode: Option<u32>, // mode to use
pub(crate) cdirs: bool, // chmod only dirs when true
pub(crate) cfiles: bool, // chmod only files when true
pub(crate) follow: bool, // follow links when copying files
}
impl Copier
{
/// Apply chmod to all files and directories
///
/// ### Examples
/// ```
/// use rivia::prelude::*;
///
/// let vfs = Memfs::new();
/// let file1 = vfs.root().mash("file1");
/// let file2 = vfs.root().mash("file2");
/// let dir1 = vfs.root().mash("dir1");
/// let dir2 = vfs.root().mash("dir2");
/// assert!(vfs.mkfile_m(&file1, 0o600).is_ok());
/// assert!(vfs.mkdir_m(&dir1, 0o777).is_ok());
/// assert!(vfs.copy_b(&file1, &file2).unwrap().chmod_all(0o655).exec().is_ok());
/// assert_eq!(vfs.mode(&file2).unwrap(), 0o100655);
/// assert!(vfs.copy_b(&dir1, &dir2).unwrap().chmod_all(0o755).exec().is_ok());
/// assert_eq!(vfs.mode(&dir2).unwrap(), 0o40755);
/// ```
pub fn chmod_all(mut self, mode: u32) -> Self
{
self.opts.cdirs = false;
self.opts.cfiles = false;
self.opts.mode = Some(mode);
self
}
/// Apply chmod to all directories only
///
/// ### Examples
/// ```
/// use rivia::prelude::*;
///
/// let vfs = Memfs::new();
/// let file1 = vfs.root().mash("file1");
/// let file2 = vfs.root().mash("file2");
/// let dir1 = vfs.root().mash("dir1");
/// let dir2 = vfs.root().mash("dir2");
/// assert!(vfs.mkfile_m(&file1, 0o600).is_ok());
/// assert!(vfs.mkdir_m(&dir1, 0o777).is_ok());
/// assert_eq!(vfs.mode(&dir1).unwrap(), 0o40777);
/// assert!(vfs.copy_b(&file1, &file2).unwrap().chmod_dirs(0o655).exec().is_ok());
/// assert_eq!(vfs.mode(&file2).unwrap(), 0o100600);
/// assert!(vfs.copy_b(&dir1, &dir2).unwrap().chmod_dirs(0o755).exec().is_ok());
/// assert_eq!(vfs.mode(&dir2).unwrap(), 0o40755);
/// ```
pub fn chmod_dirs(mut self, mode: u32) -> Self
{
self.opts.cdirs = true;
self.opts.cfiles = false;
self.opts.mode = Some(mode);
self
}
/// Apply chmod to only files
///
/// ### Examples
/// ```
/// use rivia::prelude::*;
///
/// let vfs = Memfs::new();
/// let file1 = vfs.root().mash("file1");
/// let file2 = vfs.root().mash("file2");
/// let dir1 = vfs.root().mash("dir1");
/// let dir2 = vfs.root().mash("dir2");
/// assert!(vfs.mkfile_m(&file1, 0o600).is_ok());
/// assert!(vfs.mkdir_m(&dir1, 0o777).is_ok());
/// assert_eq!(vfs.mode(&dir1).unwrap(), 0o40777);
/// assert!(vfs.copy_b(&file1, &file2).unwrap().chmod_files(0o655).exec().is_ok());
/// assert_eq!(vfs.mode(&file2).unwrap(), 0o100655);
/// assert!(vfs.copy_b(&dir1, &dir2).unwrap().chmod_files(0o755).exec().is_ok());
/// assert_eq!(vfs.mode(&dir2).unwrap(), 0o40777);
/// ```
pub fn chmod_files(mut self, mode: u32) -> Self
{
self.opts.cdirs = false;
self.opts.cfiles = true;
self.opts.mode = Some(mode);
self
}
/// Update the `follow` option
///
/// * Default: false
/// * When `true` links are followed i.e. the file pointed to will be copied not the link
///
/// ### Examples
/// ```
/// use rivia::prelude::*;
///
/// let vfs = Memfs::new();
/// let file1 = vfs.root().mash("file1");
/// let link1 = vfs.root().mash("link1");
/// let file2 = vfs.root().mash("file2");
/// assert_vfs_write_all!(vfs, &file1, "file1");
/// assert_vfs_symlink!(vfs, &link1, &file1);
/// assert!(vfs.copy_b(&link1, &file2).unwrap().follow(true).exec().is_ok());
/// assert_vfs_no_symlink!(vfs, &file2);
/// assert_vfs_read_all!(vfs, &file2, "file1");
/// ```
pub fn follow(mut self, yes: bool) -> Self
{
self.opts.follow = yes;
self
}
/// Execute the [`Copier`] builder current options.
///
/// ### Examples
/// ```
/// use rivia::prelude::*;
///
/// let vfs = Memfs::new();
/// let file1 = vfs.root().mash("file1");
/// let file2 = vfs.root().mash("file2");
/// assert_vfs_write_all!(vfs, &file1, "this is a test");
/// assert!(vfs.copy_b(&file1, &file2).unwrap().exec().is_ok());
/// assert_vfs_read_all!(vfs, &file2, "this is a test");
/// ```
pub fn exec(&self) -> RvResult<()>
{
(self.exec)(self.opts.clone())
}
}
// Unit tests
// -------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests
{
use crate::prelude::*;
#[test]
fn test_vfs_copy_errors()
{
test_copy_errors(assert_vfs_setup!(Vfs::memfs()));
test_copy_errors(assert_vfs_setup!(Vfs::stdfs()));
}
fn test_copy_errors((vfs, tmpdir): (Vfs, PathBuf))
{
let file1 = tmpdir.mash("file1");
let file2 = tmpdir.mash("file2");
// source same as destination
assert!(vfs.copy(&file1, &file1).is_ok());
assert_vfs_no_exists!(vfs, &file1);
// source empty
assert_eq!(vfs.copy("", &file1).unwrap_err().downcast_ref::<PathError>(), Some(&PathError::Empty));
assert_vfs_no_exists!(vfs, &file1);
// destination empty
assert_eq!(vfs.copy(&file1, "").unwrap_err().downcast_ref::<PathError>(), Some(&PathError::Empty));
assert_vfs_no_exists!(vfs, &file1);
// source doesn't exist
assert_eq!(
vfs.copy(&file1, &file2).unwrap_err().downcast_ref::<PathError>(),
Some(&PathError::does_not_exist(&file1))
);
assert_vfs_no_exists!(vfs, &file2);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_vfs_copy_file()
{
test_copy_file(assert_vfs_setup!(Vfs::memfs()));
test_copy_file(assert_vfs_setup!(Vfs::stdfs()));
}
fn test_copy_file((vfs, tmpdir): (Vfs, PathBuf))
{
let file1 = tmpdir.mash("file1");
let file2 = tmpdir.mash("file2");
let link1 = tmpdir.mash("link1");
let link2 = tmpdir.mash("link2");
let dir1 = tmpdir.mash("dir1");
let dir1file3 = dir1.mash("file3");
let dir2 = tmpdir.mash("dir2");
let dir2file1 = dir2.mash("file1");
// file copy i.e. copy with diff name
assert!(vfs.mkfile_m(&file1, 0o600).is_ok());
assert!(vfs.copy(&file1, &file2).is_ok());
assert_eq!(vfs.mode(&file2).unwrap(), 0o100600);
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![file1.clone(), file2.clone()]);
// file copy, i.e. copy with diff name, to dir that doesn't exist
assert_vfs_no_exists!(vfs, &dir1);
assert_vfs_no_exists!(vfs, &dir1file3);
assert!(vfs.copy(&file1, &dir1file3).is_ok());
assert_eq!(vfs.mode(&dir1file3).unwrap(), 0o100600);
assert_vfs_exists!(vfs, &dir1file3);
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
dir1file3.clone(),
file1.clone(),
file2.clone(),
]);
// link copy, i.e. copy with diff name
assert_vfs_symlink!(vfs, &link1, &file1);
assert_vfs_no_exists!(vfs, &link2);
assert!(vfs.copy(&link1, &link2).is_ok());
assert_vfs_exists!(vfs, &link2);
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
dir1file3.clone(),
file1.clone(),
file2.clone(),
link1.clone(),
link2.clone(),
]);
// file clone, i.e. keep original name, to dir that doesn't exist
assert_vfs_no_exists!(vfs, &dir2file1);
assert!(vfs.copy(&file1, &dir2file1).is_ok());
assert_eq!(vfs.mode(&dir2file1).unwrap(), 0o100600);
assert_vfs_exists!(vfs, &dir2file1);
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
dir1file3.clone(),
dir2.clone(),
dir2file1.clone(),
file1.clone(),
file2.clone(),
link1.clone(),
link2.clone(),
]);
// file clone, i.e. keep original name, to dir that already exist
assert_vfs_remove_all!(vfs, &dir2);
assert_vfs_mkdir_p!(vfs, &dir2);
assert_vfs_no_exists!(vfs, &dir2file1);
assert!(vfs.copy(&file1, &dir2).is_ok());
assert_eq!(vfs.mode(&dir2file1).unwrap(), 0o100600);
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
dir1file3.clone(),
dir2.clone(),
dir2file1.clone(),
file1.clone(),
file2.clone(),
link1.clone(),
link2.clone(),
]);
// link clone, i.e. keep original name
let link4 = dir2.mash("link1");
assert_vfs_no_exists!(vfs, &link4);
assert!(vfs.copy(&link1, &dir2).is_ok());
assert_eq!(vfs.readlink(&link4).unwrap(), PathBuf::from("..").mash("file1"));
assert_vfs_exists!(vfs, &link4);
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
dir1file3.clone(),
dir2.clone(),
dir2file1.clone(),
link4.clone(),
file1.clone(),
file2.clone(),
link1.clone(),
link2.clone(),
]);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_vfs_copy_chmod()
{
test_copy_chmod(assert_vfs_setup!(Vfs::memfs()));
test_copy_chmod(assert_vfs_setup!(Vfs::stdfs()));
}
fn test_copy_chmod((vfs, tmpdir): (Vfs, PathBuf))
{
let file1 = tmpdir.mash("file1");
let file2 = tmpdir.mash("file2");
let file3 = tmpdir.mash("file3");
let file4 = tmpdir.mash("file4");
let dir1 = tmpdir.mash("dir1");
let dir2 = tmpdir.mash("dir2");
let dir3 = tmpdir.mash("dir3");
let dir4 = tmpdir.mash("dir4");
// Set file mode but not dir mode
assert!(vfs.mkfile_m(&file1, 0o600).is_ok());
assert!(vfs.mkdir_m(&dir1, 0o777).is_ok());
assert_eq!(vfs.mode(&dir1).unwrap(), 0o40777);
assert!(vfs.copy_b(&file1, &file2).unwrap().chmod_files(0o655).exec().is_ok());
assert_eq!(vfs.mode(&file2).unwrap(), 0o100655);
assert!(vfs.copy_b(&dir1, &dir2).unwrap().chmod_files(0o755).exec().is_ok());
assert_eq!(vfs.mode(&dir2).unwrap(), 0o40777);
// Set dir mode but not file mode
assert!(vfs.copy_b(&file1, &file3).unwrap().chmod_dirs(0o655).exec().is_ok());
assert_eq!(vfs.mode(&file3).unwrap(), 0o100600);
assert!(vfs.copy_b(&dir1, &dir3).unwrap().chmod_dirs(0o755).exec().is_ok());
assert_eq!(vfs.mode(&dir3).unwrap(), 0o40755);
// Set dir and file mode
assert!(vfs.copy_b(&file1, &file4).unwrap().chmod_all(0o655).exec().is_ok());
assert_eq!(vfs.mode(&file4).unwrap(), 0o100655);
assert!(vfs.copy_b(&dir1, &dir4).unwrap().chmod_all(0o755).exec().is_ok());
assert_eq!(vfs.mode(&dir4).unwrap(), 0o40755);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_vfs_copy_dir()
{
test_copy_dir(assert_vfs_setup!(Vfs::memfs()));
test_copy_dir(assert_vfs_setup!(Vfs::stdfs()));
}
fn test_copy_dir((vfs, tmpdir): (Vfs, PathBuf))
{
let dir1 = tmpdir.mash("dir1");
let file1 = dir1.mash("file1");
let dir2 = tmpdir.mash("dir2");
let file2 = dir2.mash("file1");
let dir3 = dir2.mash("dir1");
let file3 = dir3.mash("file1");
let link1 = tmpdir.mash("link1");
let link2 = tmpdir.mash("link2");
// clone i.e. copy with diff name
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_mkfile!(vfs, &file1);
assert!(vfs.copy(&dir1, &dir2).is_ok());
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
file1.clone(),
dir2.clone(),
file2.clone(),
]);
// clone i.e. copy to different location same name
assert!(vfs.copy(&dir1, &dir2).is_ok());
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
file1.clone(),
dir2.clone(),
dir3.clone(),
file3.clone(),
file2.clone(),
]);
// copy dir symlink
assert_vfs_symlink!(vfs, &link1, &dir1);
assert!(vfs.copy(&link1, &link2).is_ok());
assert_vfs_readlink_abs!(vfs, &link1, &dir1);
assert_vfs_readlink_abs!(vfs, &link2, &dir1);
// clone link1 into dir2
let link3 = dir2.mash("link1");
assert!(vfs.copy(&link1, &dir2).is_ok());
assert_vfs_readlink!(vfs, &link3, PathBuf::from("../dir1"));
assert_iter_eq(vfs.all_paths(&tmpdir).unwrap(), vec![
dir1.clone(),
file1.clone(),
dir2.clone(),
dir3.clone(),
file3.clone(),
file2.clone(),
link3.clone(),
link1.clone(),
link2.clone(),
]);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_vfs_copy_follow()
{
test_copy_follow(assert_vfs_setup!(Vfs::memfs()));
test_copy_follow(assert_vfs_setup!(Vfs::stdfs()));
}
fn test_copy_follow((vfs, tmpdir): (Vfs, PathBuf))
{
// don't follow file link - copy
let file1 = tmpdir.mash("file1");
let link1 = tmpdir.mash("link1");
let link2 = tmpdir.mash("link2");
assert_vfs_write_all!(vfs, &file1, "file1");
assert_vfs_symlink!(vfs, &link1, &file1);
assert!(vfs.copy_b(&link1, &link2).unwrap().exec().is_ok());
assert_vfs_readlink_abs!(vfs, &link1, &file1);
assert_vfs_readlink!(vfs, &link1, PathBuf::from("file1"));
assert_eq!(vfs.readlink(&link1).unwrap(), vfs.readlink(&link2).unwrap());
assert_eq!(vfs.readlink_abs(&link1).unwrap(), vfs.readlink_abs(&link2).unwrap());
// follow file link - copy
let file2 = tmpdir.mash("file2");
assert!(vfs.copy_b(&link1, &file2).unwrap().follow(true).exec().is_ok());
assert_vfs_no_symlink!(vfs, &file2);
assert_vfs_is_file!(vfs, &file2);
assert_vfs_read_all!(vfs, &file2, "file1");
// don't follow dir link - copy
let dir1 = tmpdir.mash("dir1");
let dir1file = dir1.mash("dir1file");
let dir1link1 = tmpdir.mash("dir1link1");
let dir1link2 = tmpdir.mash("dir1link2");
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_write_all!(vfs, &dir1file, "dir1file");
assert_vfs_symlink!(vfs, &dir1link1, &dir1);
assert!(vfs.copy_b(&dir1link1, &dir1link2).unwrap().exec().is_ok());
assert_vfs_readlink_abs!(vfs, &dir1link1, &dir1);
// follow dir link - copy
let dir2 = tmpdir.mash("dir2");
let dir2file = dir2.mash("dir1file");
assert!(vfs.copy_b(&dir1link1, &dir2).unwrap().follow(true).exec().is_ok());
assert_vfs_read_all!(vfs, &dir2file, "dir1file");
assert_vfs_no_symlink!(vfs, &dir2file);
assert_vfs_remove_all!(vfs, &tmpdir);
}
}