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
use anyhow::{Context, Result};
use fs2::FileExt;
use std::fs::{self, File, OpenOptions};
use std::io::{Seek, SeekFrom};
use std::iter::Iterator;
use std::path::{Path, PathBuf};

pub fn create_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
    let path = path.as_ref();
    fs::create_dir_all(path).with_context(|| format!("create_dir_all {path:?}"))
}

pub fn read_dir<'a>(parent_path: &'a Path) -> Result<impl 'a + Iterator<Item = Result<PathBuf>>> {
    let iter = fs::read_dir(parent_path).with_context(|| format!("read_dir {parent_path:?}"))?;
    let iter = iter.map(move |res_entry| {
        res_entry
            .with_context(|| format!("read_dir entry {parent_path:?}"))
            .map(|entry| entry.path())
    });
    Ok(iter)
}

pub fn open_file<P: AsRef<Path>>(path: P, oo: &OpenOptions) -> Result<File> {
    let path = path.as_ref();
    oo.open(path).with_context(|| format!("open {path:?}"))
}

pub fn lock_file<P: AsRef<Path>>(path: P) -> Result<File> {
    let path = path.as_ref();
    let file = open_file(path, OpenOptions::new().read(true))?;
    file.try_lock_exclusive()
        .context(format!("try_lock_exclusive {path:?}"))?;
    Ok(file)
}

pub fn seek<P: AsRef<Path>>(
    mut seekable: impl Seek,
    sf: SeekFrom,
    implicit_path: P,
) -> Result<u64> {
    seekable
        .seek(sf)
        .with_context(|| format!("seek {:?}", implicit_path.as_ref()))
}

pub fn rename_file<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()> {
    let from = from.as_ref();
    let to = to.as_ref();
    fs::rename(from, to).with_context(|| format!("rename {from:?} {to:?}"))
}

pub fn hard_link_file<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> Result<()> {
    let original = original.as_ref();
    let link = link.as_ref();
    fs::hard_link(original, link).with_context(|| format!("hard_link {original:?} {link:?}"))
}

pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()> {
    let path = path.as_ref();
    fs::remove_file(path).with_context(|| format!("remove_file {path:?}"))
}

pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
    let path = path.as_ref();
    fs::remove_dir_all(path).with_context(|| format!("remove_dir_all {path:?}"))
}