From f04e1bc4d7f10273d1ef2b7b71861bca94ec2de7 Mon Sep 17 00:00:00 2001 From: Danny Angelo Carminati Grein Date: Tue, 24 Jan 2017 22:59:28 -0500 Subject: [PATCH 1/2] Fix broken player if mounted path had a trailing slash. The query on browse_songs wasn't returning any results as in the database they were save without the trailing slash. Add two complementary output messages in case of errors trying to create SharedData and SharedConfig where on Linux XDG defaults to readonly system wide paths based on XDG_CONFIG_DIRS and XDG_DATA_DIRS default values from spec (/etc/xdg and /usr/share for example). And a minor change in the api to use forward slash. --- src/api.rs | 2 +- src/config.rs | 6 ++++++ src/utils.rs | 6 ++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/api.rs b/src/api.rs index 9971c24..2ea07da 100644 --- a/src/api.rs +++ b/src/api.rs @@ -82,7 +82,7 @@ pub fn get_api_handler(collection: Arc) -> Mount { } fn path_from_request(request: &Request) -> Result { - let path_string = request.url.path().join("\\"); + let path_string = request.url.path().join("/"); let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8()?; Ok(PathBuf::from(decoded_path.deref())) } diff --git a/src/config.rs b/src/config.rs index 0f2dc5e..358326a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -193,9 +193,11 @@ impl Config { fn clean_path_string(path_string: &str) -> path::PathBuf { let separator_regex = regex::Regex::new(r"\\|/").unwrap(); + let trailing_regex = regex::Regex::new(r"(/)+$").unwrap(); let mut correct_separator = String::new(); correct_separator.push(path::MAIN_SEPARATOR); let path_string = separator_regex.replace_all(path_string, correct_separator.as_str()); + let path_string = trailing_regex.replace_all(&path_string, ""); path::PathBuf::from(path_string) } @@ -213,10 +215,14 @@ fn test_clean_path_string() { assert_eq!(correct_path, clean_path_string(r#"C:/some/path"#)); assert_eq!(correct_path, clean_path_string(r#"C:\some\path"#)); assert_eq!(correct_path, clean_path_string(r#"C:\some\path\"#)); + assert_eq!(correct_path, clean_path_string(r#"C:\some\path\\\\"#)); + assert_eq!(correct_path, clean_path_string(r#"C:\some/path//"#)); } else { assert_eq!(correct_path, clean_path_string(r#"/usr/some/path"#)); assert_eq!(correct_path, clean_path_string(r#"/usr\some\path"#)); assert_eq!(correct_path, clean_path_string(r#"/usr\some\path\"#)); + assert_eq!(correct_path, clean_path_string(r#"/usr\some\path\\\\"#)); + assert_eq!(correct_path, clean_path_string(r#"/usr\some/path//"#)); } } diff --git a/src/utils.rs b/src/utils.rs index f8846d0..4367f78 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,7 +7,8 @@ use errors::*; pub fn get_config_root() -> Result { if let Ok(mut root) = data_root(AppDataType::SharedConfig) { root.push("Polaris"); - fs::create_dir_all(&root)?; + fs::create_dir_all(&root) + .chain_err(|| format!("opening shared config: {}", root.display()))?; return Ok(root); } bail!("Could not retrieve config directory root"); @@ -16,7 +17,8 @@ pub fn get_config_root() -> Result { pub fn get_cache_root() -> Result { if let Ok(mut root) = data_root(AppDataType::SharedData) { root.push("Polaris"); - fs::create_dir_all(&root)?; + fs::create_dir_all(&root) + .chain_err(|| format!("opening shared data: {}", root.display()))?; return Ok(root); } bail!("Could not retrieve cache directory root"); From b348be4e1045a1ede206c3056a3a1650a95d1218 Mon Sep 17 00:00:00 2001 From: Danny Angelo Carminati Grein Date: Wed, 25 Jan 2017 21:15:35 -0500 Subject: [PATCH 2/2] Fix commented issues. --- src/api.rs | 2 +- src/config.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/api.rs b/src/api.rs index 2ea07da..2de873c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -82,7 +82,7 @@ pub fn get_api_handler(collection: Arc) -> Mount { } fn path_from_request(request: &Request) -> Result { - let path_string = request.url.path().join("/"); + let path_string = request.url.path().join(&::std::path::MAIN_SEPARATOR.to_string()); let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8()?; Ok(PathBuf::from(decoded_path.deref())) } diff --git a/src/config.rs b/src/config.rs index 358326a..ca57650 100644 --- a/src/config.rs +++ b/src/config.rs @@ -193,12 +193,10 @@ impl Config { fn clean_path_string(path_string: &str) -> path::PathBuf { let separator_regex = regex::Regex::new(r"\\|/").unwrap(); - let trailing_regex = regex::Regex::new(r"(/)+$").unwrap(); let mut correct_separator = String::new(); correct_separator.push(path::MAIN_SEPARATOR); let path_string = separator_regex.replace_all(path_string, correct_separator.as_str()); - let path_string = trailing_regex.replace_all(&path_string, ""); - path::PathBuf::from(path_string) + path::Path::new(&path_string).iter().collect() } #[test]