From 118380f81c276ed45faba5ce219d8a2a34ca75cf Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 2 Mar 2023 19:30:43 +0100 Subject: [PATCH] [WIP] REPO: Pass the config to the repo to build it correctly... --- src/rust/Cargo.lock | 22 +++++------ src/rust/commons/src/config.rs | 3 +- src/rust/lektor_repo/src/repo.rs | 29 +++++++++++--- src/rust/lektor_unsafe/src/config.rs | 57 ++++------------------------ src/rust/lektor_unsafe/src/repo.rs | 12 +++++- 5 files changed, 56 insertions(+), 67 deletions(-) diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index 742eb357..1005dd4a 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -57,7 +57,7 @@ dependencies = [ [[package]] name = "amadeus" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "amalib", "clap", @@ -71,7 +71,7 @@ dependencies = [ [[package]] name = "amalib" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "async-trait", "commons", @@ -423,7 +423,7 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] name = "commons" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "error-stack", "lazy_static", @@ -1700,7 +1700,7 @@ dependencies = [ [[package]] name = "kurisu_api" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "commons", "hashbrown 0.13.2", @@ -1722,7 +1722,7 @@ checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" [[package]] name = "lektor_c_compat" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "lazy_static", "libc", @@ -1731,7 +1731,7 @@ dependencies = [ [[package]] name = "lektor_config" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "serde", "thiserror", @@ -1739,7 +1739,7 @@ dependencies = [ [[package]] name = "lektor_db" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "commons", "diesel", @@ -1754,7 +1754,7 @@ dependencies = [ [[package]] name = "lektor_repo" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "commons", "futures", @@ -1772,7 +1772,7 @@ dependencies = [ [[package]] name = "lektor_unsafe" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "commons", "lazy_static", @@ -1824,7 +1824,7 @@ checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" [[package]] name = "lkt" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "amalib", "clap", @@ -1973,7 +1973,7 @@ dependencies = [ [[package]] name = "mpris" -version = "0.1.0" +version = "0.0.0-git" dependencies = [ "commons", "getset", diff --git a/src/rust/commons/src/config.rs b/src/rust/commons/src/config.rs index 15198f4b..8cd08c80 100644 --- a/src/rust/commons/src/config.rs +++ b/src/rust/commons/src/config.rs @@ -6,7 +6,7 @@ pub fn get_or_write_default_config<Config>(appli: &'static str) -> Result<Config where Config: Default + Serialize + for<'de> Deserialize<'de>, { - let path = crate::user_config_directory("amadeus").join(format!("{appli}.toml")); + let path = crate::user_config_directory("lektor").join(format!("{appli}.toml")); match std::fs::read_to_string(&path) { Ok(config) => { let config = Box::leak(String::into_boxed_str(config)); @@ -28,3 +28,4 @@ where } } } + diff --git a/src/rust/lektor_repo/src/repo.rs b/src/rust/lektor_repo/src/repo.rs index f187cbac..1baadbac 100644 --- a/src/rust/lektor_repo/src/repo.rs +++ b/src/rust/lektor_repo/src/repo.rs @@ -1,5 +1,5 @@ use crate::*; -use std::num::NonZeroUsize; +use std::{num::NonZeroUsize, path::PathBuf}; /// The structure responsible to download karas from kurisu. We use a a pointer /// instead of an Arc because we will call manually drop on instances of the @@ -8,6 +8,10 @@ use std::num::NonZeroUsize; pub struct LktModuleRepoRs { queue: LktCQueue, handlers: Vec<JoinHandle<()>>, + kara_prefix: PathBuf, + folder_permission: u64, + file_permission: u64, + concurrent_count: u64, hosts: Vec<RepoConfig>, db: LktLockDbPtr, rt: Runtime, @@ -19,19 +23,34 @@ fn concurrent() -> NonZeroUsize { } impl LktModuleRepoRs { - pub fn new(queue: LktCQueue, hosts: Vec<RepoConfig>, db: LktLockDbPtr) -> Self { + pub fn new( + queue: LktCQueue, + db_config: &LektorDatabaseConfig, + repo_config: &LektorRepoConfig, + db: LktLockDbPtr, + ) -> Self { + let workers = u64::try_from(repo_config.workers) + .expect("passed workers count from config file is too big"); let par = std::thread::available_parallelism() .expect("failed to get an estimate of the default amount of parallelism"); - log::info!(target: "REPO", "detected parallelism {par} for repo module, min value is always 2"); + log::info!(target: "REPO", "detected parallelism {par}, got {workers} from repo module config, min value is always 2"); Self { db, queue, - hosts, + concurrent_count: workers, handlers: Default::default(), + hosts: repo_config.server.clone(), rt: tokio::runtime::Builder::new_multi_thread() - .worker_threads(std::cmp::min(2_usize, par.into())) + .worker_threads(std::cmp::min(2, repo_config.workers)) .build() .expect("failed to build the tokio runtime"), + kara_prefix: db_config + .kara_dir + .clone() + .canonicalize() + .expect("failed to canocnicalize the kara prefix folder"), + folder_permission: db_config.dir_permission, + file_permission: db_config.dir_permission & !0o111, } } diff --git a/src/rust/lektor_unsafe/src/config.rs b/src/rust/lektor_unsafe/src/config.rs index ebf380ce..ad449ef5 100644 --- a/src/rust/lektor_unsafe/src/config.rs +++ b/src/rust/lektor_unsafe/src/config.rs @@ -7,57 +7,16 @@ use crate::*; use lektor_config::*; -/// Write the default config file to the location, returns true uppon success. -/// ### Safety -/// The file argument must be a valid C string. -#[no_mangle] -pub unsafe extern "C" fn lkt_config_write_default_to_file(file: *const u8) -> bool { - let mut len = 0; - while *file.offset(len) != 0 { - len += 1 - } - let file = ManuallyDrop::new(String::from_raw_parts( - file as *mut _, - len as usize, - len as usize, - )); - - let conf = match toml::to_string_pretty(&LektorConfig::default()) { - Ok(conf) => conf, - Err(err) => { - log::error!(target: "CONF", "failed to serialize default config: {err}"); - return false; - } - }; - - match std::fs::write(PathBuf::from(&file[..]), conf) { - Ok(()) => true, - Err(err) => { - log::error!(target: "CONF", "failed to write default config to {}: {err}", &file[..]); - false - } - } -} - -/// Get the config file or return and write the default config. +/// Write the default config file if it didn't exists. /// ### Safety /// Should be safe to call, it's juste a wrapper to call from C code. #[no_mangle] pub unsafe extern "C" fn lkt_config_write_default_to_default_file() -> bool { - let path = commons::user_config_directory("lektor").join("lektor.toml"); - let conf = match toml::to_string_pretty(&LektorConfig::default()) { - Ok(conf) => conf, - Err(err) => { - log::error!(target: "CONF", "failed to serialize default config: {err}"); - return false; - } - }; - - match std::fs::write(&path, conf) { - Ok(()) => true, - Err(err) => { - log::error!(target: "CONF", "failed to write default config to {}: {err}", path.to_string_lossy()); - false - } - } + commons::config::get_or_write_default_config::<LektorConfig>("lektord") + .map_err(|err| log::error!(target: "CONF", "failed to write the default config file for lektord: {err}")) + .is_ok() } + +// TODO: Load the config file in a lazy static thing and add a function to access it like a +// dictionary... + diff --git a/src/rust/lektor_unsafe/src/repo.rs b/src/rust/lektor_unsafe/src/repo.rs index 2ba63ff4..1737c9df 100644 --- a/src/rust/lektor_unsafe/src/repo.rs +++ b/src/rust/lektor_unsafe/src/repo.rs @@ -18,9 +18,19 @@ extern "C" fn lkt_module_repo_rs_init( queue: LktQueuePtr, db: LktLockDbPtr, ) -> i32 { + let config = match commons::config::get_or_write_default_config::<lektor_config::LektorConfig>( + "lektord", + ) { + Ok(config) => config, + Err(err) => { + log::error!(target: "REPO", "failed to read the default config file for lektord: {err}"); + return 1; + } + }; let repo = ManuallyDrop::new(LktModuleRepoRs::new( LktCQueue::from(queue), - Default::default(), + &config.database, + &config.repo, unsafe { Arc::increment_strong_count(db); Arc::from_raw(db) -- GitLab