gs-hf 0.4.6

Fork of hf-hub. This crates aims ease the interaction with huggingface It aims to be compatible with the huggingface_hub python package, but only implements a smaller subset of functions.
Documentation
//! Filesystem helpers shared by both backends. OS-specific primitives
//! (advisory locking, symlink creation) live in the per-OS submodules.

use std::path::{Component, Path, PathBuf};

#[cfg(target_family = "unix")]
mod unix;
#[cfg(target_family = "unix")]
use unix::symlink;
#[cfg(target_family = "unix")]
pub(crate) use unix::{lock, unlock};

#[cfg(target_family = "windows")]
mod windows;
#[cfg(target_family = "windows")]
use windows::symlink;
#[cfg(target_family = "windows")]
pub(crate) use windows::{lock, unlock};

#[cfg(not(any(target_family = "unix", target_family = "windows")))]
mod other {
    use std::path::Path;

    pub(crate) fn lock<F>(_file: &F) -> i32 {
        0
    }
    pub(crate) fn unlock<F>(_file: &F) -> i32 {
        0
    }
    pub(crate) fn symlink(_rel_src: &Path, _src: &Path, _dst: &Path) -> std::io::Result<()> {
        Ok(())
    }
}
#[cfg(not(any(target_family = "unix", target_family = "windows")))]
use other::symlink;
#[cfg(not(any(target_family = "unix", target_family = "windows")))]
pub(crate) use other::{lock, unlock};

pub(crate) fn make_relative(src: &Path, dst: &Path) -> PathBuf {
    let path = src;
    let base = dst;

    assert_eq!(
        path.is_absolute(),
        base.is_absolute(),
        "This function is made to look at absolute paths only"
    );
    let mut ita = path.components();
    let mut itb = base.components();

    loop {
        match (ita.next(), itb.next()) {
            (Some(a), Some(b)) if a == b => (),
            (some_a, _) => {
                // Ignoring b, because 1 component is the filename
                // for which we don't need to go back up for relative
                // filename to work.
                let mut new_path = PathBuf::new();
                for _ in itb {
                    new_path.push(Component::ParentDir);
                }
                if let Some(a) = some_a {
                    new_path.push(a);
                    for comp in ita {
                        new_path.push(comp);
                    }
                }
                return new_path;
            }
        }
    }
}

pub(crate) fn symlink_or_rename(src: &Path, dst: &Path) -> Result<(), std::io::Error> {
    if dst.exists() {
        return Ok(());
    }

    let rel_src = make_relative(src, dst);
    symlink(&rel_src, src, dst)
}