You've already forked AstralRinth
forked from didirus/AstralRinth
0.8.0 beta fixes (#2154)
* initial fixes * 0.8.0 beta fixes * run actions * run fmt * Fix windows build * Add purge cache opt * add must revalidate to project req * lint + clippy * fix processes, open folder * Update migrator to use old launcher cache for perf * fix empty dirs not moving * fix lint + create natives dir if not exist * fix large request batches * finish * Fix deep linking on mac * fix comp err * fix comp err (2) --------- Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
//! Theseus directory information
|
||||
use crate::state::{JavaVersion, Settings};
|
||||
use crate::event::emit::{emit_loading, init_loading};
|
||||
use crate::state::{JavaVersion, Profile, Settings};
|
||||
use crate::util::fetch::IoSemaphore;
|
||||
use crate::LoadingBarType;
|
||||
use dashmap::DashSet;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use tokio::fs;
|
||||
|
||||
pub const CACHES_FOLDER_NAME: &str = "caches";
|
||||
@@ -11,7 +15,7 @@ pub const METADATA_FOLDER_NAME: &str = "meta";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DirectoryInfo {
|
||||
pub settings_dir: PathBuf, // Base settings directory- settings.json and icon cache.
|
||||
pub settings_dir: PathBuf, // Base settings directory- app database
|
||||
pub config_dir: PathBuf, // Base config directory- instances, minecraft downloads, etc. Changeable as a setting.
|
||||
}
|
||||
|
||||
@@ -153,7 +157,7 @@ impl DirectoryInfo {
|
||||
/// Get the cache directory for Theseus
|
||||
#[inline]
|
||||
pub fn caches_dir(&self) -> PathBuf {
|
||||
self.settings_dir.join(CACHES_FOLDER_NAME)
|
||||
self.config_dir.join(CACHES_FOLDER_NAME)
|
||||
}
|
||||
|
||||
/// Get path from environment variable
|
||||
@@ -162,6 +166,7 @@ impl DirectoryInfo {
|
||||
std::env::var_os(name).map(PathBuf::from)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(settings, exec, io_semaphore))]
|
||||
pub async fn move_launcher_directory<'a, E>(
|
||||
settings: &mut Settings,
|
||||
exec: E,
|
||||
@@ -170,13 +175,14 @@ impl DirectoryInfo {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Sqlite> + Copy,
|
||||
{
|
||||
let app_dir = DirectoryInfo::get_initial_settings_dir().ok_or(
|
||||
crate::ErrorKind::FSError(
|
||||
"Could not find valid config dir".to_string(),
|
||||
),
|
||||
)?;
|
||||
|
||||
if let Some(ref prev_custom_dir) = settings.prev_custom_dir {
|
||||
let prev_dir = PathBuf::from(prev_custom_dir);
|
||||
let app_dir = DirectoryInfo::get_initial_settings_dir().ok_or(
|
||||
crate::ErrorKind::FSError(
|
||||
"Could not find valid config dir".to_string(),
|
||||
),
|
||||
)?;
|
||||
|
||||
let move_dir = settings
|
||||
.custom_dir
|
||||
@@ -203,70 +209,267 @@ impl DirectoryInfo {
|
||||
}
|
||||
}
|
||||
|
||||
async fn move_directory(
|
||||
source: &Path,
|
||||
destination: &Path,
|
||||
io_semaphore: &IoSemaphore,
|
||||
) -> crate::Result<()> {
|
||||
if !source.exists() {
|
||||
crate::util::io::create_dir_all(source).await?;
|
||||
}
|
||||
|
||||
if !destination.exists() {
|
||||
crate::util::io::create_dir_all(destination).await?;
|
||||
}
|
||||
|
||||
for entry_path in
|
||||
crate::pack::import::get_all_subfiles(source).await?
|
||||
fn is_same_disk(
|
||||
old_dir: &Path,
|
||||
new_dir: &Path,
|
||||
) -> crate::Result<bool> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let relative_path = entry_path.strip_prefix(source)?;
|
||||
let new_path = destination.join(relative_path);
|
||||
|
||||
crate::util::fetch::copy(
|
||||
&entry_path,
|
||||
&new_path,
|
||||
io_semaphore,
|
||||
)
|
||||
.await?;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
Ok(old_dir.metadata()?.dev() == new_dir.metadata()?.dev())
|
||||
}
|
||||
|
||||
Ok(())
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let old_dir = crate::util::io::canonicalize(old_dir)?;
|
||||
let new_dir = crate::util::io::canonicalize(new_dir)?;
|
||||
|
||||
let old_component = old_dir.components().next();
|
||||
let new_component = new_dir.components().next();
|
||||
|
||||
match (old_component, new_component) {
|
||||
(
|
||||
Some(std::path::Component::Prefix(old)),
|
||||
Some(std::path::Component::Prefix(new)),
|
||||
) => Ok(old.as_os_str() == new.as_os_str()),
|
||||
_ => Ok(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_disk_usage(path: &Path) -> crate::Result<Option<u64>> {
|
||||
let path = crate::util::io::canonicalize(path)?;
|
||||
|
||||
let disks = sysinfo::Disks::new_with_refreshed_list();
|
||||
|
||||
for disk in disks.iter() {
|
||||
if path.starts_with(disk.mount_point()) {
|
||||
return Ok(Some(disk.available_space()));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
let new_dir = move_dir.to_string_lossy().to_string();
|
||||
|
||||
if prev_dir != move_dir {
|
||||
if !is_dir_writeable(&move_dir).await? {
|
||||
settings.custom_dir = Some(prev_custom_dir.clone());
|
||||
let loader_bar_id = init_loading(
|
||||
LoadingBarType::DirectoryMove {
|
||||
old: prev_dir.clone(),
|
||||
new: move_dir.clone(),
|
||||
},
|
||||
100.0,
|
||||
"Moving launcher directory",
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Ok(());
|
||||
if !is_dir_writeable(&move_dir).await? {
|
||||
return Err(crate::ErrorKind::DirectoryMoveError(format!("Cannot move directory to {}: directory is not writeable", move_dir.display())).into());
|
||||
}
|
||||
|
||||
move_directory(
|
||||
&prev_dir.join(CACHES_FOLDER_NAME),
|
||||
&app_dir.join(CACHES_FOLDER_NAME),
|
||||
io_semaphore,
|
||||
)
|
||||
.await?;
|
||||
move_directory(
|
||||
&prev_dir.join(LAUNCHER_LOGS_FOLDER_NAME),
|
||||
&app_dir.join(LAUNCHER_LOGS_FOLDER_NAME),
|
||||
io_semaphore,
|
||||
)
|
||||
.await?;
|
||||
const MOVE_DIRS: &[&str] = &[
|
||||
CACHES_FOLDER_NAME,
|
||||
PROFILES_FOLDER_NAME,
|
||||
METADATA_FOLDER_NAME,
|
||||
];
|
||||
|
||||
move_directory(
|
||||
&prev_dir.join(PROFILES_FOLDER_NAME),
|
||||
&move_dir.join(PROFILES_FOLDER_NAME),
|
||||
io_semaphore,
|
||||
)
|
||||
.await?;
|
||||
move_directory(
|
||||
&prev_dir.join(METADATA_FOLDER_NAME),
|
||||
&move_dir.join(METADATA_FOLDER_NAME),
|
||||
io_semaphore,
|
||||
)
|
||||
.await?;
|
||||
struct MovePath {
|
||||
old: PathBuf,
|
||||
new: PathBuf,
|
||||
size: u64,
|
||||
}
|
||||
|
||||
async fn add_paths(
|
||||
source: &Path,
|
||||
destination: &Path,
|
||||
paths: &mut Vec<MovePath>,
|
||||
total_size: &mut u64,
|
||||
) -> crate::Result<()> {
|
||||
if !source.exists() {
|
||||
crate::util::io::create_dir_all(source).await?;
|
||||
}
|
||||
|
||||
if !destination.exists() {
|
||||
crate::util::io::create_dir_all(destination).await?;
|
||||
}
|
||||
|
||||
for entry_path in
|
||||
crate::pack::import::get_all_subfiles(source, false)
|
||||
.await?
|
||||
{
|
||||
let relative_path = entry_path.strip_prefix(source)?;
|
||||
let new_path = destination.join(relative_path);
|
||||
let path_size =
|
||||
entry_path.metadata().map(|x| x.len()).unwrap_or(0);
|
||||
|
||||
*total_size += path_size;
|
||||
|
||||
paths.push(MovePath {
|
||||
old: entry_path,
|
||||
new: new_path,
|
||||
size: path_size,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
let mut paths: Vec<MovePath> = vec![];
|
||||
let mut total_size = 0;
|
||||
|
||||
for dir in MOVE_DIRS {
|
||||
add_paths(
|
||||
&prev_dir.join(dir),
|
||||
&move_dir.join(dir),
|
||||
&mut paths,
|
||||
&mut total_size,
|
||||
)
|
||||
.await?;
|
||||
emit_loading(
|
||||
&loader_bar_id,
|
||||
10.0 / (MOVE_DIRS.len() as f64),
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let paths_len = paths.len();
|
||||
|
||||
if is_same_disk(&prev_dir, &move_dir).unwrap_or(false) {
|
||||
let success_idxs = Arc::new(DashSet::new());
|
||||
|
||||
let loader_bar_id = Arc::new(&loader_bar_id);
|
||||
let res =
|
||||
futures::future::try_join_all(paths.iter().enumerate().map(|(idx, x)| {
|
||||
let loader_bar_id = loader_bar_id.clone();
|
||||
let success_idxs = success_idxs.clone();
|
||||
|
||||
async move {
|
||||
let _permit = io_semaphore.0.acquire().await?;
|
||||
|
||||
if let Some(parent) = x.new.parent() {
|
||||
crate::util::io::create_dir_all(parent).await.map_err(|e| {
|
||||
crate::Error::from(crate::ErrorKind::DirectoryMoveError(
|
||||
format!(
|
||||
"Failed to create directory {}: {}",
|
||||
parent.display(),
|
||||
e
|
||||
)
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
||||
crate::util::io::rename(
|
||||
&x.old,
|
||||
&x.new,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
crate::Error::from(crate::ErrorKind::DirectoryMoveError(
|
||||
format!(
|
||||
"Failed to move directory from {} to {}: {}",
|
||||
x.old.display(),
|
||||
x.new.display(),
|
||||
e
|
||||
),
|
||||
))
|
||||
})?;
|
||||
|
||||
let _ = emit_loading(
|
||||
&loader_bar_id,
|
||||
90.0 / paths_len as f64,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
success_idxs.insert(idx);
|
||||
|
||||
Ok::<(), crate::Error>(())
|
||||
}
|
||||
}))
|
||||
.await;
|
||||
|
||||
if let Err(e) = res {
|
||||
for idx in success_idxs.iter() {
|
||||
let path = &paths[*idx.key()];
|
||||
|
||||
let res =
|
||||
tokio::fs::rename(&path.new, &path.old).await;
|
||||
|
||||
if let Err(e) = res {
|
||||
tracing::warn!(
|
||||
"Failed to rollback directory {}: {}",
|
||||
path.new.display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Err(e);
|
||||
}
|
||||
} else {
|
||||
if let Some(disk_usage) = get_disk_usage(&move_dir)? {
|
||||
if total_size > disk_usage {
|
||||
return Err(crate::ErrorKind::DirectoryMoveError(format!("Not enough space to move directory to {}: only {} bytes available", app_dir.display(), disk_usage)).into());
|
||||
}
|
||||
}
|
||||
|
||||
let loader_bar_id = Arc::new(&loader_bar_id);
|
||||
futures::future::try_join_all(paths.iter().map(|x| {
|
||||
let loader_bar_id = loader_bar_id.clone();
|
||||
|
||||
async move {
|
||||
crate::util::fetch::copy(
|
||||
&x.old,
|
||||
&x.new,
|
||||
io_semaphore,
|
||||
)
|
||||
.await.map_err(|e| { crate::Error::from(
|
||||
crate::ErrorKind::DirectoryMoveError(format!("Failed to move directory from {} to {}: {}", x.old.display(), x.new.display(), e)))
|
||||
})?;
|
||||
|
||||
let _ = emit_loading(
|
||||
&loader_bar_id,
|
||||
((x.size as f64) / (total_size as f64)) * 60.0,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok::<(), crate::Error>(())
|
||||
}
|
||||
}))
|
||||
.await?;
|
||||
|
||||
futures::future::join_all(paths.iter().map(|x| {
|
||||
let loader_bar_id = loader_bar_id.clone();
|
||||
|
||||
async move {
|
||||
let res = async {
|
||||
let _permit = io_semaphore.0.acquire().await?;
|
||||
crate::util::io::remove_file(&x.old).await?;
|
||||
|
||||
emit_loading(
|
||||
&loader_bar_id,
|
||||
30.0 / paths_len as f64,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok::<(), crate::Error>(())
|
||||
};
|
||||
|
||||
if let Err(e) = res.await {
|
||||
tracing::warn!(
|
||||
"Failed to remove old file {}: {}",
|
||||
x.old.display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}))
|
||||
.await;
|
||||
}
|
||||
|
||||
let java_versions = JavaVersion::get_all(exec).await?;
|
||||
for (_, mut java_version) in java_versions {
|
||||
@@ -274,16 +477,42 @@ impl DirectoryInfo {
|
||||
prev_custom_dir,
|
||||
new_dir.trim_end_matches('/').trim_end_matches('\\'),
|
||||
);
|
||||
java_version.upsert(exec).await?;
|
||||
java_version.upsert(exec).await?
|
||||
}
|
||||
|
||||
let profiles = Profile::get_all(exec).await?;
|
||||
|
||||
for mut profile in profiles {
|
||||
profile.icon_path = profile.icon_path.map(|x| {
|
||||
x.replace(
|
||||
prev_custom_dir,
|
||||
new_dir
|
||||
.trim_end_matches('/')
|
||||
.trim_end_matches('\\'),
|
||||
)
|
||||
});
|
||||
profile.java_path = profile.java_path.map(|x| {
|
||||
x.replace(
|
||||
prev_custom_dir,
|
||||
new_dir
|
||||
.trim_end_matches('/')
|
||||
.trim_end_matches('\\'),
|
||||
)
|
||||
});
|
||||
profile.upsert(exec).await?;
|
||||
}
|
||||
}
|
||||
|
||||
settings.custom_dir = Some(new_dir.clone());
|
||||
settings.prev_custom_dir = Some(new_dir);
|
||||
|
||||
settings.update(exec).await?;
|
||||
settings.custom_dir = Some(new_dir);
|
||||
}
|
||||
|
||||
settings.prev_custom_dir.clone_from(&settings.custom_dir);
|
||||
if settings.custom_dir.is_none() {
|
||||
settings.custom_dir = Some(app_dir.to_string_lossy().to_string());
|
||||
}
|
||||
|
||||
settings.update(exec).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user