Migrate to SQLite for Internal Launcher Data (#1300)

* initial migration

* barebones profiles

* Finish profiles

* Add back file watcher

* UI support progress

* Finish most of cache

* Fix options page

* Fix forge, finish modrinth auth

* Accounts, process cache

* Run SQLX prepare

* Finish

* Run lint + actions

* Fix version to be compat with windows

* fix lint

* actually fix lint

* actually fix lint again
This commit is contained in:
Geometrically
2024-07-24 11:03:19 -07:00
committed by GitHub
parent 90f74427d9
commit 49a20a303a
156 changed files with 9208 additions and 8547 deletions

View File

@@ -9,9 +9,9 @@ use tokio::{
};
use crate::{
prelude::{Credentials, DirectoryInfo},
prelude::Credentials,
util::io::{self, IOError},
{state::ProfilePathId, State},
State,
};
#[derive(Serialize, Debug)]
@@ -66,7 +66,7 @@ impl Logs {
async fn build(
log_type: LogType,
age: SystemTime,
profile_subpath: &ProfilePathId,
profile_subpath: &str,
filename: String,
clear_contents: Option<bool>,
) -> crate::Result<Self> {
@@ -95,19 +95,20 @@ impl Logs {
#[tracing::instrument]
pub async fn get_logs_from_type(
profile_path: &ProfilePathId,
profile_path: &str,
log_type: LogType,
clear_contents: Option<bool>,
logs: &mut Vec<crate::Result<Logs>>,
) -> crate::Result<()> {
let state = State::get().await?;
let logs_folder = match log_type {
LogType::InfoLog => {
DirectoryInfo::profile_logs_dir(profile_path).await?
}
LogType::InfoLog => state.directories.profile_logs_dir(profile_path),
LogType::CrashReport => {
DirectoryInfo::crash_reports_dir(profile_path).await?
state.directories.crash_reports_dir(profile_path)
}
};
if logs_folder.exists() {
for entry in std::fs::read_dir(&logs_folder)
.map_err(|e| IOError::with_path(e, &logs_folder))?
@@ -142,21 +143,19 @@ pub async fn get_logs_from_type(
#[tracing::instrument]
pub async fn get_logs(
profile_path_id: ProfilePathId,
profile_path_id: &str,
clear_contents: Option<bool>,
) -> crate::Result<Vec<Logs>> {
let profile_path = profile_path_id.profile_path().await?;
let mut logs = Vec::new();
get_logs_from_type(
&profile_path,
profile_path_id,
LogType::InfoLog,
clear_contents,
&mut logs,
)
.await?;
get_logs_from_type(
&profile_path,
profile_path_id,
LogType::CrashReport,
clear_contents,
&mut logs,
@@ -170,54 +169,47 @@ pub async fn get_logs(
#[tracing::instrument]
pub async fn get_logs_by_filename(
profile_path_id: ProfilePathId,
profile_path: &str,
log_type: LogType,
filename: String,
) -> crate::Result<Logs> {
let profile_path = profile_path_id.profile_path().await?;
let state = State::get().await?;
let path = match log_type {
LogType::InfoLog => {
DirectoryInfo::profile_logs_dir(&profile_path).await
}
LogType::InfoLog => state.directories.profile_logs_dir(profile_path),
LogType::CrashReport => {
DirectoryInfo::crash_reports_dir(&profile_path).await
state.directories.crash_reports_dir(profile_path)
}
}?
}
.join(&filename);
let metadata = std::fs::metadata(&path)?;
let age = metadata.created().unwrap_or(SystemTime::UNIX_EPOCH);
Logs::build(log_type, age, &profile_path, filename, Some(true)).await
Logs::build(log_type, age, profile_path, filename, Some(true)).await
}
#[tracing::instrument]
pub async fn get_output_by_filename(
profile_subpath: &ProfilePathId,
profile_subpath: &str,
log_type: LogType,
file_name: &str,
) -> crate::Result<CensoredString> {
let state = State::get().await?;
let logs_folder = match log_type {
LogType::InfoLog => {
DirectoryInfo::profile_logs_dir(profile_subpath).await?
}
LogType::InfoLog => state.directories.profile_logs_dir(profile_subpath),
LogType::CrashReport => {
DirectoryInfo::crash_reports_dir(profile_subpath).await?
state.directories.crash_reports_dir(profile_subpath)
}
};
let path = logs_folder.join(file_name);
let credentials: Vec<Credentials> = state
.users
.read()
.await
.users
.clone()
.into_values()
let credentials = Credentials::get_all(&state.pool)
.await?
.into_iter()
.map(|x| x.1)
.collect();
// Load .gz file into String
@@ -265,10 +257,10 @@ pub async fn get_output_by_filename(
}
#[tracing::instrument]
pub async fn delete_logs(profile_path_id: ProfilePathId) -> crate::Result<()> {
let profile_path = profile_path_id.profile_path().await?;
pub async fn delete_logs(profile_path_id: &str) -> crate::Result<()> {
let state = State::get().await?;
let logs_folder = DirectoryInfo::profile_logs_dir(&profile_path).await?;
let logs_folder = state.directories.profile_logs_dir(profile_path_id);
for entry in std::fs::read_dir(&logs_folder)
.map_err(|e| IOError::with_path(e, &logs_folder))?
{
@@ -283,20 +275,18 @@ pub async fn delete_logs(profile_path_id: ProfilePathId) -> crate::Result<()> {
#[tracing::instrument]
pub async fn delete_logs_by_filename(
profile_path_id: ProfilePathId,
profile_path_id: &str,
log_type: LogType,
filename: &str,
) -> crate::Result<()> {
let profile_path = profile_path_id.profile_path().await?;
let state = State::get().await?;
let logs_folder = match log_type {
LogType::InfoLog => {
DirectoryInfo::profile_logs_dir(&profile_path).await
}
LogType::InfoLog => state.directories.profile_logs_dir(profile_path_id),
LogType::CrashReport => {
DirectoryInfo::crash_reports_dir(&profile_path).await
state.directories.crash_reports_dir(profile_path_id)
}
}?;
};
let path = logs_folder.join(filename);
io::remove_dir_all(&path).await?;
@@ -305,7 +295,7 @@ pub async fn delete_logs_by_filename(
#[tracing::instrument]
pub async fn get_latest_log_cursor(
profile_path: ProfilePathId,
profile_path: &str,
cursor: u64, // 0 to start at beginning of file
) -> crate::Result<LatestLogCursor> {
get_generic_live_log_cursor(profile_path, "latest.log", cursor).await
@@ -313,14 +303,12 @@ pub async fn get_latest_log_cursor(
#[tracing::instrument]
pub async fn get_generic_live_log_cursor(
profile_path_id: ProfilePathId,
profile_path_id: &str,
log_file_name: &str,
mut cursor: u64, // 0 to start at beginning of file
) -> crate::Result<LatestLogCursor> {
let profile_path = profile_path_id.profile_path().await?;
let state = State::get().await?;
let logs_folder = DirectoryInfo::profile_logs_dir(&profile_path).await?;
let logs_folder = state.directories.profile_logs_dir(profile_path_id);
let path = logs_folder.join(log_file_name);
if !path.exists() {
// Allow silent failure if latest.log doesn't exist (as the instance may have been launched, but not yet created the file)
@@ -358,13 +346,10 @@ pub async fn get_generic_live_log_cursor(
let output = String::from_utf8_lossy(&buffer).to_string(); // Convert to String
let cursor = cursor + bytes_read as u64; // Update cursor
let credentials: Vec<Credentials> = state
.users
.read()
.await
.users
.clone()
.into_values()
let credentials = Credentials::get_all(&state.pool)
.await?
.into_iter()
.map(|x| x.1)
.collect();
let output = CensoredString::censor(output, &credentials);
Ok(LatestLogCursor {