bitkv_rs/util/
file.rs

1use std::{
2  fs, io,
3  path::{Path, PathBuf},
4};
5
6// calculate available disk space
7pub fn available_disk_space() -> u64 {
8  match fs2::available_space(PathBuf::from("/")) {
9    Ok(size) => size,
10    _ => 0,
11  }
12}
13
14// calculate the total size of directory in disk
15pub fn dir_disk_size<P: AsRef<Path>>(dir_path: P) -> u64 {
16  match fs_extra::dir::get_size(dir_path) {
17    Ok(size) => size,
18    _ => 0,
19  }
20}
21
22pub fn copy_dir<P: AsRef<Path>>(src: P, dst: P, exclude: &[&str]) -> io::Result<()> {
23  //
24  if !dst.as_ref().exists() {
25    fs::create_dir_all(&dst)?;
26  }
27
28  for dir_entry in fs::read_dir(&src)? {
29    let entry = dir_entry?;
30    let src_path = entry.path();
31
32    if exclude.iter().any(|&x| src_path.ends_with(x)) {
33      continue;
34    }
35
36    let dst_path = dst.as_ref().join(src_path.file_name().unwrap());
37    if entry.file_type()?.is_dir() {
38      copy_dir(src_path, dst_path, exclude)?;
39    } else {
40      fs::copy(&src_path, &dst_path)?;
41    }
42  }
43  Ok(())
44}
45
46#[test]
47fn test_available_disk_space() {
48  let size = available_disk_space();
49  assert!(size > 0);
50}