From d824bfaaee889080306efe0544519adbc0f24cd6 Mon Sep 17 00:00:00 2001 From: Imbris Date: Tue, 13 Oct 2020 02:29:32 -0400 Subject: [PATCH] Fix system data folder name, panic when outside the project dir and there is no env var set --- common/src/util/userdata_dir.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/common/src/util/userdata_dir.rs b/common/src/util/userdata_dir.rs index 81a9ae90ca..51f08520b9 100644 --- a/common/src/util/userdata_dir.rs +++ b/common/src/util/userdata_dir.rs @@ -24,15 +24,15 @@ pub fn userdata_dir(workspace: bool, strategy: Option<&str>, manifest_dir: &str) // 2. The VELOREN_USERDATA_STRATEGY environment variable .or_else(|| match strategy { // "system" => system specific project data directory - Some(s) if s.eq_ignore_ascii_case("system") => Some(directories_next::ProjectDirs::from("net", "veloren", "userdata") + Some(s) if s.eq_ignore_ascii_case("system") => Some(directories_next::ProjectDirs::from("net", "veloren", "veloren") .expect("System's $HOME directory path not found!") .data_dir() - .to_owned() + .join("userdata") ), // "executable" => /userdata Some(s) if s.eq_ignore_ascii_case("executable") => { let mut path = std::env::current_exe() - .expect("Failed to retrieve executable directory!"); + .expect("Failed to retrieve executable path!"); path.pop(); path.push("userdata"); Some(path) @@ -47,6 +47,14 @@ pub fn userdata_dir(workspace: bool, strategy: Option<&str>, manifest_dir: &str) if workspace { path.pop(); } + let exe_path = std::env::current_exe() + .expect("Failed to retrieve executable path!"); + // Ensure this path exists + // Ensure that the binary path is prefixed by this path + if !path.exists() || !exe_path.starts_with(&path) { + panic!("Recompile with VELOREN_USERDATA_STRATEGY set to \"system\" or \"executable\" to run the binary outside of the project folder"); + } + path.push("userdata"); path })