-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathfile_util.rs
740 lines (670 loc) · 20.1 KB
/
file_util.rs
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use std::io;
use std::io::{Read, Write, Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::fs;
use std::fs::{Metadata, rename};
use std::ffi::CString;
use std::time::{Instant, Duration};
use std::os::unix::fs::{PermissionsExt, MetadataExt, symlink};
use std::os::unix::io::AsRawFd;
use std::os::unix::ffi::OsStrExt;
use nix;
use libc;
use libc::{uid_t, gid_t, utime, utimbuf};
use nix::fcntl::{flock, FlockArg};
use digest_traits::Digest;
use sha2::Sha256;
use crate::digest::hex;
use crate::path_util::{tmp_file_name, ToCString};
pub fn read_visible_entries(dir: &Path) -> Result<Vec<PathBuf>, Error> {
let mut res = vec!();
for entry_ in fs::read_dir(dir)? {
let entry = entry_?;
if !entry.file_name()[..].to_str().map(|x| x.starts_with("."))
.unwrap_or(false)
{
res.push(entry.path().to_path_buf());
}
}
Ok(res)
}
pub struct Dir<'a> {
path: &'a Path,
recursive: bool,
mode: Option<u32>,
uid: Option<uid_t>,
gid: Option<gid_t>,
}
impl<'a> Dir<'a> {
pub fn new<P: ?Sized>(path: &'a P) -> Dir<'a>
where P: AsRef<Path>
{
Dir {
path: path.as_ref(),
recursive: false,
mode: None,
uid: None,
gid: None,
}
}
pub fn recursive(&mut self, recursive: bool) -> &mut Dir<'a> {
self.recursive = recursive;
self
}
pub fn mode(&mut self, mode: u32) -> &mut Dir<'a> {
self.mode = Some(mode);
self
}
pub fn uid(&mut self, uid: uid_t) -> &mut Dir<'a> {
self.uid = Some(uid);
self
}
pub fn gid(&mut self, gid: gid_t) -> &mut Dir<'a> {
self.gid = Some(gid);
self
}
pub fn create(&self) -> Result<(), Error> {
self._create(self.path, true)
}
fn _create(&self, path: &Path, is_last: bool)
-> Result<(), Error>
{
if path.is_dir() {
return Ok(())
}
if self.recursive {
match path.parent() {
Some(p) if p != path => {
self._create(p, false)?;
}
_ => {}
}
}
fs::create_dir(path)?;
let mode = if is_last { self.mode } else { None };
fs::set_permissions(path,
fs::Permissions::from_mode(mode.unwrap_or(0o755)))?;
if is_last {
if self.uid.is_some() || self.gid.is_some() {
let uid = if let Some(uid) = self.uid {
uid
} else {
path.symlink_metadata()?.uid()
};
let gid = if let Some(gid) = self.gid {
gid
} else {
path.symlink_metadata()?.gid()
};
set_owner_group(path, uid, gid)?;
}
}
Ok(())
}
}
pub fn safe_ensure_dir(dir: &Path) -> Result<(), String> {
match fs::symlink_metadata(dir) {
Ok(ref stat) if stat.file_type().is_symlink() => {
return Err(format!(concat!("The `{0:?}` dir can't be a symlink. ",
"Please run `unlink {0:?}`"), dir));
}
Ok(ref stat) if stat.file_type().is_dir() => {
// ok
}
Ok(_) => {
return Err(format!(concat!("The {0:?} must be a directory. ",
"Please run `unlink {0:?}`"), dir));
}
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
try_msg!(Dir::new(dir).create(),
"Can't create {dir:?}: {err}", dir=dir);
}
Err(ref e) => {
return Err(format!("Can't stat {:?}: {}", dir, e));
}
}
return Ok(());
}
pub fn ensure_symlink(target: &Path, linkpath: &Path) -> Result<(), io::Error>
{
match symlink(target, linkpath) {
Ok(()) => Ok(()),
Err(e) => {
if e.kind() == io::ErrorKind::AlreadyExists {
match fs::read_link(linkpath) {
Ok(ref path) if Path::new(path) == target => Ok(()),
Ok(_) => Err(e),
Err(e) => Err(e),
}
} else {
Err(e)
}
}
}
}
#[derive(Clone, Copy)]
enum CopyPolicy<T: Copy> {
None,
Preserve,
Set(T),
}
trait ResolveCopyPolicy<T: Copy>: Into<CopyPolicy<T>> + Copy {
fn metadata_value(&self, stat: &Metadata) -> T;
fn resolve(
&self, src: &Path, src_stat: Option<&Metadata>
) -> io::Result<(Option<T>, Option<Metadata>)> {
match (*self).into() {
CopyPolicy::None => Ok((None, None)),
CopyPolicy::Preserve => {
match src_stat {
Some(s) => Ok((Some(self.metadata_value(&s)), None)),
None => {
let stat = src.symlink_metadata()?;
Ok((Some(self.metadata_value(&stat)), Some(stat)))
},
}
}
CopyPolicy::Set(v) => Ok((Some(v), None)),
}
}
}
#[derive(Clone, Copy)]
pub enum CopyModePolicy {
None,
Set(u32),
}
impl ResolveCopyPolicy<u32> for CopyModePolicy {
fn metadata_value(&self, stat: &Metadata) -> u32 {
stat.mode()
}
}
impl From<CopyModePolicy> for CopyPolicy<u32> {
fn from(p: CopyModePolicy) -> Self {
use CopyModePolicy::*;
match p {
None => Self::None,
Set(mode) => Self::Set(mode),
}
}
}
impl From<Option<u32>> for CopyModePolicy {
fn from(v: Option<u32>) -> Self {
match v {
Some(v) => CopyModePolicy::Set(v),
None => CopyModePolicy::None,
}
}
}
#[derive(Clone, Copy)]
pub enum CopyOwnerPolicy {
None,
Set(u32, u32),
}
impl From<Option<(u32, u32)>> for CopyOwnerPolicy {
fn from(v: Option<(u32, u32)>) -> Self {
match v {
Some((uid, gid)) => Self::Set(uid, gid),
None => Self::None,
}
}
}
impl ResolveCopyPolicy<(u32, u32)> for CopyOwnerPolicy {
fn metadata_value(&self, stat: &Metadata) -> (u32, u32) {
(stat.uid(), stat.gid())
}
}
impl From<CopyOwnerPolicy> for CopyPolicy<(u32, u32)> {
fn from(p: CopyOwnerPolicy) -> Self {
use CopyOwnerPolicy::*;
match p {
None => Self::None,
Set(uid, gid) => Self::Set((uid, gid)),
}
}
}
#[derive(Clone, Copy)]
pub enum CopyTimePolicy {
None,
Preserve,
Set { atime: i64, mtime: i64 }
}
impl ResolveCopyPolicy<(i64, i64)> for CopyTimePolicy {
fn metadata_value(&self, stat: &Metadata) -> (i64, i64) {
(stat.atime(), stat.mtime())
}
}
impl From<CopyTimePolicy> for CopyPolicy<(i64, i64)> {
fn from(p: CopyTimePolicy) -> Self {
use CopyTimePolicy::*;
match p {
None => Self::None,
Preserve => Self::Preserve,
Set { atime, mtime } => Self::Set((atime, mtime)),
}
}
}
pub struct FileCopy<'s, 'd> {
src: &'s Path,
dst: &'d Path,
src_stat: Option<&'s Metadata>,
mode: CopyModePolicy,
owner: CopyOwnerPolicy,
time: CopyTimePolicy,
atomic: bool,
}
impl<'s, 'd> FileCopy<'s, 'd> {
pub fn new(src: &'s Path, dst: &'d Path) -> Self {
Self {
src,
dst,
src_stat: None,
mode: CopyModePolicy::None,
owner: CopyOwnerPolicy::None,
time: CopyTimePolicy::None,
atomic: false,
}
}
pub fn src_stat(&mut self, src_stat: &'s Metadata) -> &mut Self {
self.src_stat = Some(src_stat);
self
}
pub fn time<T: Into<CopyTimePolicy>>(&mut self, time: T) -> &mut Self {
self.time = time.into();
self
}
pub fn owner<T: Into<CopyOwnerPolicy>>(&mut self, owner: T) -> &mut Self {
self.owner = owner.into();
self
}
pub fn mode<T: Into<CopyModePolicy>>(&mut self, mode: T) -> &mut Self {
self.mode = mode.into();
self
}
pub fn atomic(&mut self, atomic: bool) -> &mut Self {
self.atomic = atomic;
self
}
pub fn copy(&self) -> io::Result<()> {
let _dst;
let dst = if self.atomic {
let name = match self.dst.file_name() {
Some(name) => name,
None => return Err(io::Error::new(
io::ErrorKind::Other,
format!("Cannot detect file name of {:?}", &self.dst)
)),
};
let dst_dir = match self.dst.parent() {
Some(dir) => dir,
None => return Err(io::Error::new(
io::ErrorKind::Other,
format!("Cannot detect destination directory of {:?}", &self.dst)
)),
};
_dst = dst_dir.join(tmp_file_name(name));
_dst.as_path()
} else {
self.dst
};
let (mode, src_stat) = self.mode.resolve(
self.src, self.src_stat
)?;
_copy(&self.src, dst, mode)?;
let (owner, src_stat) = self.owner.resolve(
self.src, src_stat.as_ref().or(self.src_stat)
)?;
if let Some((uid, gid)) = owner {
set_owner_group(dst, uid, gid)?;
}
let (time, _) = self.time.resolve(
self.src, src_stat.as_ref().or(self.src_stat)
)?;
if let Some((atime, mtime)) = time {
set_times(dst, atime, mtime)?;
}
if self.atomic {
rename(dst, self.dst)?;
}
Ok(())
}
}
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
{
_copy(from.as_ref(), to.as_ref(), None)
}
fn _copy(from: &Path, to: &Path, mode: Option<u32>) -> io::Result<()> {
if !from.is_file() {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"the source path is not an existing regular file"))
}
let mut reader = fs::File::open(from)?;
let mut writer = fs::File::create(to)?;
copy_stream(&mut reader, &mut writer)?;
let perm = match mode {
Some(mode) => {
fs::Permissions::from_mode(mode)
},
None => {
reader.metadata()?.permissions()
}
};
fs::set_permissions(to, perm)?;
Ok(())
}
pub fn copy_stream(reader: &mut dyn Read, writer: &mut dyn Write)
-> io::Result<()>
{
// Smaller buffer on the stack
// Because rust musl has very small stack (80k)
// Allocating buffer on heap for each copy is too slow
let mut buf = [0; 32768];
loop {
let len = match reader.read(&mut buf) {
Ok(0) => break,
Ok(len) => len,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(&buf[..len])?;
}
Ok(())
}
pub struct Lock {
file: fs::File,
path: PathBuf,
cleanup: bool,
}
impl Lock {
fn lock(path: &Path, arg: FlockArg) -> Result<fs::File, Error> {
loop {
let file = fs::File::create(path)?;
let stat1 = file.metadata()?;
flock(file.as_raw_fd(), arg)
.map_err(|e| match e {
nix::Error::Sys(code) => Error::from_raw_os_error(code as i32),
nix::Error::InvalidUtf8 => unreachable!(),
nix::Error::InvalidPath => unreachable!(),
nix::Error::UnsupportedOperation => panic!("Can't flock"),
})?;
// Ensure lock file was not deleted or recreated by another process
// between create and flock calls
let stat2 = match path.metadata() {
Ok(s) => s,
Err(e) if e.kind() == ErrorKind::NotFound => continue,
Err(e) => return Err(e),
};
if stat1.ino() != stat2.ino() {
continue;
}
return Ok(file);
}
}
pub fn exclusive<P: AsRef<Path>>(path: P) -> Result<Lock, Error> {
let path = path.as_ref();
let file = Self::lock(path, FlockArg::LockExclusiveNonblock)?;
Ok(Lock {
file,
path: path.to_path_buf(),
cleanup: false,
})
}
pub fn exclusive_wait<P: AsRef<Path>>(
path: P, cleanup: bool, message: &str
) -> Result<Lock, Error>
{
let path = path.as_ref();
let file = match Self::lock(path, FlockArg::LockExclusiveNonblock) {
Ok(file) => file,
Err(e) if e.kind() == ErrorKind::WouldBlock => {
warn!("{}", message);
let lock_start = Instant::now();
let file = Self::lock(path, FlockArg::LockExclusive)?;
let elapsed = lock_start.elapsed();
if elapsed > Duration::new(5, 0) {
warn!("Lock was held {} seconds. Proceeding...",
elapsed.as_secs());
}
file
}
Err(e) => return Err(e),
};
Ok(Lock {
file,
path: path.to_path_buf(),
cleanup,
})
}
}
impl Drop for Lock {
fn drop(&mut self) {
if self.cleanup {
safe_remove(&self.path)
.map_err(|e| error!("Couldn't delete lock file: {}", e))
.ok();
}
flock(self.file.as_raw_fd(), FlockArg::Unlock)
.map_err(|e| error!("Couldn't unlock file: {:?}", e)).ok();
}
}
pub fn check_stream_hashsum(mut reader: &mut dyn Read, sha256: &String)
-> Result<(), String>
{
let mut hash = Sha256::new();
try_msg!(io::copy(&mut reader, &mut hash),
"Error when calculating hashsum: {err}");
let hash_str = format!("{:x}", hex(&hash));
if !hash_str.starts_with(sha256) {
return Err(format!("Hashsum mismatch: expected {} but was {}",
sha256, hash_str));
}
Ok(())
}
pub fn force_symlink(target: &Path, linkpath: &Path)
-> Result<(), io::Error>
{
let tmpname = linkpath.with_extension(".vagga.tmp.link~");
symlink(target, &tmpname)?;
fs::rename(&tmpname, linkpath)?;
Ok(())
}
pub fn set_owner_group(target: &Path, uid: uid_t, gid: gid_t)
-> Result<(), io::Error>
{
let c_target = target.to_cstring();
let rc = unsafe { libc::lchown( c_target.as_ptr(), uid, gid) };
if rc != 0 {
warn!("Can't chown {:?}: {}", target, io::Error::last_os_error());
Ok(())
// Err(io::Error::last_os_error())
} else {
Ok(())
}
}
/// Shallow copy of file/dir/symlink
///
/// The owner_uid/owner_gid parameters optionally override the owner
///
/// Returns false if path is a directory
pub struct ShallowCopy<'s, 'm, 'd> {
src: &'s Path,
src_stat: Option<&'m Metadata>,
dst: &'d Path,
owner_uid: Option<u32>,
owner_gid: Option<u32>,
mode: Option<u32>,
time_policy: CopyTimePolicy,
}
impl<'s, 'm, 'd> ShallowCopy<'s, 'm, 'd> {
pub fn new(src: &'s Path, dst: &'d Path)
-> ShallowCopy<'s, 'm, 'd>
{
ShallowCopy {
src: src,
src_stat: None,
dst: dst,
owner_uid: None,
owner_gid: None,
mode: None,
time_policy: CopyTimePolicy::None,
}
}
pub fn src_stat(&mut self, stat: &'m Metadata)
-> &mut ShallowCopy<'s, 'm, 'd>
{
self.src_stat = Some(stat);
self
}
pub fn owner_uid<T: Into<Option<u32>>>(&mut self, uid: T)
-> &mut ShallowCopy<'s, 'm, 'd>
{
self.owner_uid = uid.into();
self
}
pub fn owner_gid<T: Into<Option<u32>>>(&mut self, gid: T)
-> &mut ShallowCopy<'s, 'm, 'd>
{
self.owner_gid = gid.into();
self
}
pub fn mode<T: Into<Option<u32>>>(&mut self, mode: T)
-> &mut ShallowCopy<'s, 'm, 'd>
{
self.mode = mode.into();
self
}
pub fn times(&mut self, atime: i64, mtime: i64)
-> &mut ShallowCopy<'s, 'm, 'd>
{
self.time_policy = CopyTimePolicy::Set { atime, mtime };
self
}
pub fn preserve_times(&mut self) -> &mut ShallowCopy<'s, 'm, 'd>
{
self.time_policy = CopyTimePolicy::Preserve;
self
}
pub fn copy(&self) -> io::Result<bool> {
shallow_copy(self.src, self.src_stat, self.dst,
self.owner_uid, self.owner_gid, self.mode, self.time_policy)
}
}
fn shallow_copy(src: &Path, src_stat: Option<&Metadata>, dest: &Path,
owner_uid: Option<uid_t>, owner_gid: Option<gid_t>,
mode: Option<u32>, time_policy: CopyTimePolicy)
-> Result<bool, io::Error>
{
let _src_stat;
let src_stat = if let Some(stat) = src_stat {
stat
} else {
_src_stat = src.symlink_metadata()?;
&_src_stat
};
let src_type = src_stat.file_type();
let mut is_dir = false;
if src_type.is_dir() {
is_dir = true;
let nstat = dest.symlink_metadata();
// We don't change permissions and owner of already created directories
//
// There are couple of reasons:
// 1. We consider such overlaps a "merge" of a directory, so it's
// unclear which one should override the permissions
// 2. Changing permissions of the root directory of the copied system
// is usually counter-intuitive (so we contradict rsync)
// 3. Some directories (/proc, /sys, /dev) are mount points and we
// can't change the permissions
if nstat.is_err() {
Dir::new(dest)
.mode(mode.unwrap_or(src_stat.mode()))
.uid(owner_uid.unwrap_or(src_stat.uid()))
.gid(owner_gid.unwrap_or(src_stat.gid()))
.create()?;
}
} else if src_type.is_symlink() {
let value = fs::read_link(&src)?;
force_symlink(&value, dest)?;
set_owner_group(dest,
owner_uid.unwrap_or(src_stat.uid()),
owner_gid.unwrap_or(src_stat.gid()))?;
} else {
FileCopy::new(src, dest)
.src_stat(src_stat)
.mode(mode)
.time(time_policy)
.owner(CopyOwnerPolicy::Set(
owner_uid.unwrap_or(src_stat.uid()),
owner_gid.unwrap_or(src_stat.gid()),
))
.copy()?;
}
Ok(!is_dir)
}
pub fn set_times<P: AsRef<Path>>(path: P, atime: i64, mtime: i64)
-> io::Result<()>
{
let filename = CString::new(path.as_ref().as_os_str().as_bytes()).unwrap();
let utimes = utimbuf {
actime: atime,
modtime: mtime,
};
let rc = unsafe { utime(filename.as_ptr(), &utimes) };
if rc != 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
pub fn safe_remove<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref();
path.symlink_metadata()
.and_then(|_| fs::remove_file(path))
.or_else(|e| {
if e.kind() == io::ErrorKind::NotFound {
Ok(())
}
else {
Err(e)
}
})
}
pub fn truncate_file<P: AsRef<Path>>(path: P) -> Result<(), String> {
let path = path.as_ref();
if path.symlink_metadata()
.map(|m| m.file_type().is_file())
.or_else(|e| {
if e.kind() == io::ErrorKind::NotFound {
Ok(true)
} else {
Err(format!("Cannot stat {:?} file: {}", path, e))
}
})?
{
fs::File::create(path).map_err(|e| {
if e.kind() == io::ErrorKind::NotFound {
format!(
"Cannot create file {:?}: no such directory {:?}",
path, path.parent())
} else {
format!("Cannot truncate file {:?}: {}", path, e)
}
})?;
}
Ok(())
}
pub fn human_size(size: u64) -> String {
fn format_size(s: f64, p: &str) -> String {
if s < 10.0 && !p.is_empty() {
format!("{:.1}{}B", s, p)
} else {
format!("{:.0}{}B", s, p)
}
}
let mut s = size as f64;
for prefix in &["", "K", "M", "G", "T"][..] {
if s < 1000.0 {
return format_size(s, prefix);
}
s /= 1000.0;
}
return format_size(s, "P");
}