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