navid/veins / eb4f3d934fd0144c58b4695b6e7abf7fb638b384d96c5e06a1d58231e91071f6 / veinsrs/src/store/layout.rs

layout.rs

← veinsrs/src/store · 3941 bytes · raw · download

use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};

use crate::error::{Error, Result};
use crate::platform;

pub fn veins_dir(root: &Path) -> PathBuf {
    root.join(".veins")
}

pub fn packs_dir(root: &Path) -> PathBuf {
    veins_dir(root).join("store").join("packs")
}

pub fn tmp_dir(root: &Path) -> PathBuf {
    veins_dir(root).join("store").join("tmp")
}

pub fn op_heads_dir(root: &Path) -> PathBuf {
    veins_dir(root).join("op-heads")
}

pub fn git_dir(root: &Path) -> PathBuf {
    veins_dir(root).join("git")
}

pub fn git_map_dir(root: &Path) -> PathBuf {
    veins_dir(root).join("git-map")
}

pub fn working_copy_dir(root: &Path) -> PathBuf {
    veins_dir(root).join("working-copy")
}

pub fn resolutions_dir(root: &Path) -> PathBuf {
    veins_dir(root).join("resolutions")
}

pub fn config_path(root: &Path) -> PathBuf {
    veins_dir(root).join("config")
}

pub fn format_path(root: &Path) -> PathBuf {
    veins_dir(root).join("format")
}

pub fn discover(start: &Path) -> Result<PathBuf> {
    let mut current = fs::canonicalize(start)?;
    loop {
        if format_path(&current).is_file() {
            return Ok(current);
        }
        match current.parent() {
            Some(parent) => current = parent.to_path_buf(),
            None => return Err(Error::new("not a veins repository")),
        }
    }
}

pub fn init(path: &Path) -> Result<PathBuf> {
    let root = if path.as_os_str().is_empty() {
        PathBuf::from(".")
    } else {
        path.to_path_buf()
    };
    fs::create_dir_all(&root)?;
    let format = format_path(&root);
    let expected = b"veins-store 1";
    if format.exists() {
        let data = fs::read(&format)?;
        if data != expected {
            return Err(Error::new("unsupported repository format"));
        }
    }
    ensure_layout(&root)?;
    if !format.exists() {
        fs::write(&format, expected)?;
    }
    if !config_path(&root).exists() {
        fs::write(config_path(&root), [])?;
    }
    Ok(root)
}

fn ensure_layout(root: &Path) -> Result<()> {
    let directories = [
        veins_dir(root),
        veins_dir(root).join("store"),
        packs_dir(root),
        tmp_dir(root),
        op_heads_dir(root),
        git_map_dir(root),
        git_map_dir(root).join("raw"),
        working_copy_dir(root),
        resolutions_dir(root),
    ];
    for directory in directories {
        fs::create_dir_all(directory)?;
    }
    Ok(())
}

pub fn write_atomic(final_path: &Path, data: &[u8]) -> Result<()> {
    if let Some(parent) = final_path.parent() {
        fs::create_dir_all(parent)?;
    }
    let parent = final_path
        .parent()
        .ok_or_else(|| Error::new("path has no parent"))?;
    let name = final_path
        .file_name()
        .ok_or_else(|| Error::new("path has no file name"))?
        .to_string_lossy();
    let temporary = parent.join(format!(".{name}.tmp"));
    {
        let mut file = File::create(&temporary)?;
        file.write_all(data)?;
    }
    sync_file(&temporary)?;
    fs::rename(&temporary, final_path)?;
    sync_directory(parent);
    Ok(())
}

fn sync_file(path: &Path) -> Result<()> {
    Ok(platform::sync_file(path)?)
}

fn sync_directory(path: &Path) {
    platform::sync_directory(path);
}

pub fn read_directory(path: &Path) -> Result<Vec<Vec<u8>>> {
    let mut names = Vec::new();
    for entry in fs::read_dir(path)? {
        let entry = entry?;
        names.push(platform::os_string_to_bytes(entry.file_name()));
    }
    names.sort();
    Ok(names)
}

pub fn os_string(bytes: &[u8]) -> std::ffi::OsString {
    platform::os_string_from_bytes(bytes)
}

pub fn append_file(path: &Path, data: &[u8]) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    let mut file = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)?;
    file.write_all(data)?;
    Ok(())
}