From 414eacad2b89a4177cb851d8994aad7ac2c5618e Mon Sep 17 00:00:00 2001 From: Noor Date: Thu, 6 Feb 2025 21:16:29 +0000 Subject: [PATCH] handle app state file and detee dir --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/data.rs | 7 +++++-- src/global.rs | 10 ++++++++-- src/main.rs | 7 +++++-- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 407871b..6839d35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -273,6 +273,7 @@ dependencies = [ "detee-shared", "env_logger", "flate2", + "home", "log", "prost", "prost-types", @@ -534,6 +535,15 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "home" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "http" version = "1.2.0" diff --git a/Cargo.toml b/Cargo.toml index deb58a3..a7149ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ log = "0.4.25" serde = "1.0.217" serde_json = "1.0.138" serde_yml = "0.0.12" +home = "0.5.11" [build-dependencies] tonic-build = "0.12.3" diff --git a/src/data.rs b/src/data.rs index a1a84fb..00a2442 100644 --- a/src/data.rs +++ b/src/data.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use crate::container::delete_enclave; use crate::container::deploy_enclave; -use crate::global::APP_CONFIG_DIR; +use crate::global::app_config_dir; use crate::global::APP_NAME_PREFIX; use crate::utils::handle_package; use crate::utils::prepare_port_map; @@ -100,7 +100,10 @@ impl App { } fn write_config(&self) -> Result<()> { - let mut file = File::create(APP_CONFIG_DIR.to_string() + &self.uuid + ".yaml")?; + let app_config_dir = app_config_dir(); + std::fs::create_dir_all(&app_config_dir)?; + + let mut file = File::create(app_config_dir + &self.uuid + ".yaml")?; file.write_all(serde_yml::to_string(self)?.as_bytes())?; Ok(()) } diff --git a/src/global.rs b/src/global.rs index 317be11..4b9ad46 100644 --- a/src/global.rs +++ b/src/global.rs @@ -6,5 +6,11 @@ pub const PACKAGE_ARCHIVE_DIR_PATH: &str = "./enclave_archives"; pub const PACKAGE_DIR_PATH: &str = "./enclaves"; pub const APP_NAME_PREFIX: &str = "dtpm"; -// TODO: handle this home directory properly -pub const APP_CONFIG_DIR: &str = "~/.dtpm/app_daemon/"; + +const DETEE_DIR_ENV_NAME: &str = "DETEE_DIR"; + +// TODO: try lazy static later +pub fn app_config_dir() -> String { + let home = home::home_dir().unwrap().to_string_lossy().into_owned(); + std::env::var(DETEE_DIR_ENV_NAME).unwrap_or(format!("{home}/.detee/app_daemon/")) +} diff --git a/src/main.rs b/src/main.rs index 4318068..66b19f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use detee_shared::pb::brain::DaemonMessageApp; use detee_shared::pb::brain::MappedPort; use detee_shared::pb::brain::NewAppRes; use detee_shared::types::brain::AppDeployConfig; -use global::APP_CONFIG_DIR; +use global::app_config_dir; use log::info; use std::time::Duration; use tokio::sync::mpsc::Receiver; @@ -106,10 +106,13 @@ impl AppHandler { } async fn handle_del_app_req(&mut self, uuid: String) -> Result<()> { - let content = std::fs::read_to_string(APP_CONFIG_DIR.to_string() + &uuid + ".yaml")?; + let app_config_dir = app_config_dir(); + let app_handle_file_name = format!("{app_config_dir}{}.yaml", &uuid); + let content = std::fs::read_to_string(&app_handle_file_name)?; let app_instance: App = serde_yml::from_str(&content)?; app_instance.delete_app(&mut self.host_resource).await?; + std::fs::remove_file(&app_handle_file_name)?; if let Err(er) = cleanup_enclave_disk_and_package(uuid).await { log::error!("Failed to cleanup disk:\n{er}");