From be60be71ac28c76d59fcee6e85a8b246ecf235e5 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Fri, 12 Apr 2024 21:35:10 -0400 Subject: [PATCH] Fix symlink creation again... fix reading file creation->access time --- .../src/api/get_game.rs | 7 ++-- .../src/api/save_setting.rs | 33 ++++++++++++------- backend/community_settings_srv/src/cli.rs | 2 +- .../community_settings_srv/src/file_util.rs | 5 +-- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/backend/community_settings_srv/src/api/get_game.rs b/backend/community_settings_srv/src/api/get_game.rs index cbfb95f..0b8a650 100644 --- a/backend/community_settings_srv/src/api/get_game.rs +++ b/backend/community_settings_srv/src/api/get_game.rs @@ -62,9 +62,10 @@ fn get_some_settings_by_app_id(steam_app_id: u32, cli: &'static Cli) -> std::io: .filter_map(|res| res.ok()) .filter(|f| f.path().extension().map(|ext| ext == file_util::RON_EXTENSION).unwrap_or(false)) .filter_map(|f| f.metadata().ok().map(|meta| (f, meta))) - .filter_map(|(f, meta)| meta.created().ok().map(|time| (f, meta, time))) + .filter_map(|(f, meta)| meta.modified().ok().map(|time| (f, meta, time))) .collect(); files.sort_by(|(_, _, a_created), (_, _, b_created)| a_created.cmp(b_created)); + let files_len = files.len(); let mut results = Vec::with_capacity(MAX_RESULTS); for (_, (f, _, _)) in files.into_iter().enumerate().take_while(|(i, _)| *i < MAX_RESULTS) { @@ -72,12 +73,14 @@ fn get_some_settings_by_app_id(steam_app_id: u32, cli: &'static Cli) -> std::io: let setting = match ron::de::from_reader(reader) { Ok(x) => x, Err(e) => { + log::debug!("Error while reading {}: {}", f.path().display(), e); let e_msg = format!("{}", e); return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e_msg)); } }; results.push(setting); } + log::debug!("Got {} results (from {} files) for {}", results.len(), files_len, app_id_folder.display()); Ok(results) } @@ -89,7 +92,7 @@ pub async fn get_setting_by_app_id_handler( ) -> std::io::Result { let id: u32 = *id; #[cfg(debug_assertions)] - println!("Accept: {}", accept.to_string()); + log::debug!("Accept: {}", accept.to_string()); let preferred = accept.preference(); if super::is_mime_type_ron_capable(&preferred) { // Send RON diff --git a/backend/community_settings_srv/src/api/save_setting.rs b/backend/community_settings_srv/src/api/save_setting.rs index ae41e3e..1b2bdca 100644 --- a/backend/community_settings_srv/src/api/save_setting.rs +++ b/backend/community_settings_srv/src/api/save_setting.rs @@ -62,24 +62,29 @@ pub async fn save_setting_handler( } } + log::debug!("Saved to {}, building symlinks", path_ron.display()); + // create symlinks for other ways of looking up these settings files let filename_ron = file_util::filename(next_id, file_util::RON_EXTENSION); let filename_json = file_util::filename(next_id, file_util::JSON_EXTENSION); // create symlinks to app id folder let app_id_folder = file_util::setting_folder_by_app_id(&cli.folder, parsed_data.steam_app_id); + log::debug!("App id folder {}", app_id_folder.display()); if !app_id_folder.exists() { std::fs::create_dir(&app_id_folder)?; } #[cfg(target_family = "windows")] // NOTE: windows support is untested and unmaintained { - std::os::windows::fs::symlink_file(&path_ron, app_id_folder.join(&filename_ron).canonicalize()?)?; - std::os::windows::fs::symlink_file(&path_json, app_id_folder.join(&filename_json).canonicalize()?)?; + log::debug!("Symlinking {} -> {}", app_id_folder.join(&filename_ron).display(), path_ron.canonicalize()?.display()); + std::os::windows::fs::symlink_file(&path_ron.canonicalize()?, app_id_folder.join(&filename_ron))?; + std::os::windows::fs::symlink_file(&path_json.canonicalize()?, app_id_folder.join(&filename_json))?; } #[cfg(target_family = "unix")] { - std::os::unix::fs::symlink(&path_ron, app_id_folder.join(&filename_ron).canonicalize()?)?; - std::os::unix::fs::symlink(&path_json, app_id_folder.join(&filename_json).canonicalize()?)?; + log::debug!("Symlinking {} -> {}", app_id_folder.join(&filename_ron).display(), path_ron.canonicalize()?.display()); + std::os::unix::fs::symlink(&path_ron.canonicalize()?, app_id_folder.join(&filename_ron))?; + std::os::unix::fs::symlink(&path_json.canonicalize()?, app_id_folder.join(&filename_json))?; } // create symlinks for user id folder @@ -89,13 +94,15 @@ pub async fn save_setting_handler( } #[cfg(target_family = "windows")] // NOTE: windows support is untested and unmaintained { - std::os::windows::fs::symlink_file(&path_ron, user_id_folder.join(&filename_ron).canonicalize()?)?; - std::os::windows::fs::symlink_file(&path_json, user_id_folder.join(&filename_json).canonicalize()?)?; + log::debug!("Symlinking {} -> {}", user_id_folder.join(&filename_ron).display(), path_ron.canonicalize()?.display()); + std::os::windows::fs::symlink_file(&path_ron.canonicalize()?, user_id_folder.join(&filename_ron))?; + std::os::windows::fs::symlink_file(&path_json.canonicalize()?, user_id_folder.join(&filename_json))?; } #[cfg(target_family = "unix")] { - std::os::unix::fs::symlink(&path_ron, user_id_folder.join(&filename_ron).canonicalize()?)?; - std::os::unix::fs::symlink(&path_json, user_id_folder.join(&filename_json).canonicalize()?)?; + log::debug!("Symlinking {} -> {}", user_id_folder.join(&filename_ron).display(), path_ron.canonicalize()?.display()); + std::os::unix::fs::symlink(&path_ron.canonicalize()?, user_id_folder.join(&filename_ron))?; + std::os::unix::fs::symlink(&path_json.canonicalize()?, user_id_folder.join(&filename_json))?; } // create symlinks for each tag @@ -106,13 +113,15 @@ pub async fn save_setting_handler( } #[cfg(target_family = "windows")] // NOTE: windows support is untested and unmaintained { - std::os::windows::fs::symlink_file(&path_ron, tag_folder.join(&filename_ron).canonicalize()?)?; - std::os::windows::fs::symlink_file(&path_json, tag_folder.join(&filename_json).canonicalize()?)?; + log::debug!("Symlinking {} -> {}", tag_folder.join(&filename_ron).display(), path_ron.canonicalize()?.display()); + std::os::windows::fs::symlink_file(&path_ron.canonicalize()?, tag_folder.join(&filename_ron))?; + std::os::windows::fs::symlink_file(&path_json.canonicalize()?, tag_folder.join(&filename_json))?; } #[cfg(target_family = "unix")] { - std::os::unix::fs::symlink(&path_ron, tag_folder.join(&filename_ron).canonicalize()?)?; - std::os::unix::fs::symlink(&path_json, tag_folder.join(&filename_json).canonicalize()?)?; + log::debug!("Symlinking {} -> {}", tag_folder.join(&filename_ron).display(), path_ron.canonicalize()?.display()); + std::os::unix::fs::symlink(&path_ron.canonicalize()?, tag_folder.join(&filename_ron))?; + std::os::unix::fs::symlink(&path_json.canonicalize()?, tag_folder.join(&filename_json))?; } } diff --git a/backend/community_settings_srv/src/cli.rs b/backend/community_settings_srv/src/cli.rs index 5a05e14..b6feb6b 100644 --- a/backend/community_settings_srv/src/cli.rs +++ b/backend/community_settings_srv/src/cli.rs @@ -16,7 +16,7 @@ pub struct Cli { pub log: std::path::PathBuf, /// Perform maintenance tasks - #[arg(short, long)] + #[arg(long)] pub fix: bool, } diff --git a/backend/community_settings_srv/src/file_util.rs b/backend/community_settings_srv/src/file_util.rs index 1272a0c..3050739 100644 --- a/backend/community_settings_srv/src/file_util.rs +++ b/backend/community_settings_srv/src/file_util.rs @@ -138,13 +138,14 @@ pub fn next_setting_id(root: impl AsRef) -> u128 { let mut last_id = *lock; if last_id == 0 { // needs init + last_id = 1; let mut path = setting_path_by_id(root.as_ref(), last_id, RON_EXTENSION); while path.exists() { last_id += 1; path = setting_path_by_id(root.as_ref(), last_id, RON_EXTENSION); } - *lock = last_id; - println!("setting id initialized to {}", last_id); + *lock = last_id - 1; + log::info!("setting id initialized to {}", last_id); } *lock += 1; *lock