use anyhow::{Result, ensure};
use std::{
io::ErrorKind,
path::{Path, PathBuf},
};
use tokio::fs;
const SHARED: &str = "shared";
async fn directory(path: &Path) -> Result<bool> {
match fs::symlink_metadata(path).await {
Ok(metadata) => {
ensure!(
metadata.is_dir(),
"update cache directory must not be a link or file: {}",
path.display()
);
Ok(true)
}
Err(error) if error.kind() == ErrorKind::NotFound => Ok(false),
Err(error) => Err(error.into()),
}
}
pub(super) async fn prepare(cache: &Path, installed: &str) -> Result<PathBuf> {
fs::create_dir_all(cache).await?;
let shared = cache.join(SHARED);
if !directory(&shared).await? {
let legacy = cache.join(installed);
if crate::build_info::valid_commit(installed) && directory(&legacy).await? {
fs::rename(legacy, &shared).await?;
} else {
fs::create_dir(&shared).await?;
}
}
invalidate(&shared.join("release")).await?;
let mut entries = fs::read_dir(&shared).await?;
while let Some(entry) = entries.next_entry().await? {
if entry.file_type().await?.is_dir() {
invalidate(&entry.path().join("release")).await?;
}
}
Ok(shared)
}
async fn invalidate(profile: &Path) -> Result<()> {
if !directory(profile).await? {
return Ok(());
}
for name in [".fingerprint", "build"] {
let directory_path = profile.join(name);
if !directory(&directory_path).await? {
continue;
}
let mut entries = fs::read_dir(directory_path).await?;
while let Some(entry) = entries.next_entry().await? {
if entry.file_name().to_string_lossy().starts_with("gent-") {
if entry.file_type().await?.is_dir() {
fs::remove_dir_all(entry.path()).await?;
} else {
fs::remove_file(entry.path()).await?;
}
}
}
}
Ok(())
}