fix: use different algorithm for finding cas dir

This commit is contained in:
daimond113 2024-11-01 18:31:11 +01:00
parent 5232abc1d5
commit 397ea11ef5
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
2 changed files with 34 additions and 38 deletions

View file

@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- Use a different algorithm for finding a CAS directory to avoid issues with mounted drives by @daimond113
## [0.5.0-rc.7] - 2024-10-30 ## [0.5.0-rc.7] - 2024-10-30
### Added ### Added
- New website by @lukadev-0 - New website by @lukadev-0

View file

@ -11,10 +11,11 @@ use indicatif_log_bridge::LogWrapper;
use pesde::{AuthConfig, Project, MANIFEST_FILE_NAME}; use pesde::{AuthConfig, Project, MANIFEST_FILE_NAME};
use std::{ use std::{
collections::HashSet, collections::HashSet,
fs::create_dir_all, fs::{create_dir_all, hard_link},
path::{Path, PathBuf}, path::{Path, PathBuf},
thread::spawn, thread::spawn,
}; };
use tempfile::NamedTempFile;
mod cli; mod cli;
pub mod util; pub mod util;
@ -34,37 +35,27 @@ struct Cli {
subcommand: cli::commands::Subcommand, subcommand: cli::commands::Subcommand,
} }
#[cfg(windows)] fn get_linkable_dir(path: &Path) -> PathBuf {
fn get_root(path: &std::path::Path) -> PathBuf { let mut curr_path = PathBuf::new();
match path.components().next().unwrap() { let file_to_try = NamedTempFile::new_in(&curr_path).expect("failed to create temporary file");
std::path::Component::Prefix(prefix) => {
let mut string = prefix.as_os_str().to_string_lossy().to_string();
if string.ends_with(':') {
string.push(std::path::MAIN_SEPARATOR);
}
std::path::PathBuf::from(&string) for component in path.components() {
curr_path.push(component);
if hard_link(
file_to_try.path(),
curr_path.join(file_to_try.path().file_name().unwrap()),
)
.is_ok()
{
return curr_path;
} }
_ => unreachable!(),
}
}
#[cfg(unix)]
fn get_root(path: &std::path::Path) -> PathBuf {
use std::os::unix::fs::MetadataExt;
let path = std::fs::canonicalize(path).unwrap();
let mut current = path.as_path();
while let Some(parent) = current.parent() {
if std::fs::metadata(parent).unwrap().dev() != std::fs::metadata(current).unwrap().dev() {
break;
}
current = parent;
} }
current.to_path_buf() panic!(
"couldn't find a linkable directory for any point in {}",
curr_path.display()
);
} }
fn run() -> anyhow::Result<()> { fn run() -> anyhow::Result<()> {
@ -182,19 +173,20 @@ fn run() -> anyhow::Result<()> {
multi multi
}; };
let data_dir = home_dir()?.join("data"); let home_dir = home_dir()?;
let data_dir = home_dir.join("data");
create_dir_all(&data_dir).expect("failed to create data directory"); create_dir_all(&data_dir).expect("failed to create data directory");
let home_cas_dir = data_dir.join("cas"); let cas_dir = get_linkable_dir(&project_root_dir).join(HOME_DIR);
create_dir_all(&home_cas_dir).expect("failed to create cas directory");
let project_root = get_root(&project_root_dir); let cas_dir = if cas_dir == home_dir {
let cas_dir = if get_root(&home_cas_dir) == project_root { &data_dir
log::debug!("using home cas dir");
home_cas_dir
} else { } else {
log::debug!("using cas dir in {}", project_root.display()); &cas_dir
project_root.join(HOME_DIR).join("cas") }
}; .join("cas");
log::debug!("using cas dir in {}", cas_dir.display());
let project = Project::new( let project = Project::new(
project_root_dir, project_root_dir,