From c526f09c0cff5f8cbfd0be6a21ba827c293a0557 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Mon, 27 May 2024 12:14:50 +0200 Subject: [PATCH] replace custom home_dir --- Cargo.lock | 1 + ffplayout-api/src/utils/files.rs | 2 +- lib/Cargo.toml | 1 + lib/src/utils/config.rs | 4 +-- lib/src/utils/mod.rs | 19 -------------- lib/src/utils/windows.rs | 43 -------------------------------- 6 files changed, 5 insertions(+), 65 deletions(-) delete mode 100644 lib/src/utils/windows.rs diff --git a/Cargo.lock b/Cargo.lock index 551b8426..f20e0e4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1365,6 +1365,7 @@ dependencies = [ "derive_more", "ffprobe", "file-rotate", + "home", "lazy_static", "lettre", "lexical-sort", diff --git a/ffplayout-api/src/utils/files.rs b/ffplayout-api/src/utils/files.rs index 827362b3..4dca58e3 100644 --- a/ffplayout-api/src/utils/files.rs +++ b/ffplayout-api/src/utils/files.rs @@ -112,7 +112,7 @@ pub fn norm_abs_path( let path = &root_path.join(&source_relative); if !FOLDER_WHITELIST.iter().any(|f| path.starts_with(f)) - && !path.starts_with(&HOME_DIR.to_string()) + && !path.starts_with(HOME_DIR.to_string()) { return Err(ServiceError::Forbidden( "Access forbidden: Folder cannot be opened.".to_string(), diff --git a/lib/Cargo.toml b/lib/Cargo.toml index e862fd82..48770847 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -14,6 +14,7 @@ crossbeam-channel = "0.5" derive_more = "0.99" ffprobe = "0.4" file-rotate = "0.7" +home = "0.5" lazy_static = "1.4" lettre = { version = "0.11", features = ["builder", "rustls-tls", "smtp-transport"], default-features = false } lexical-sort = "0.3" diff --git a/lib/src/utils/config.rs b/lib/src/utils/config.rs index 1e62a96d..c28aa029 100644 --- a/lib/src/utils/config.rs +++ b/lib/src/utils/config.rs @@ -15,7 +15,7 @@ use shlex::split; use crate::AdvancedConfig; use super::vec_strings; -use crate::utils::{free_tcp_socket, home_dir, time_to_sec, OutputMode::*}; +use crate::utils::{free_tcp_socket, time_to_sec, OutputMode::*}; pub const DUMMY_LEN: f64 = 60.0; pub const IMAGE_FORMAT: [&str; 21] = [ @@ -406,7 +406,7 @@ impl PlayoutConfig { config.general.config_path = config_path.to_string_lossy().to_string(); - config.general.stat_file = home_dir() + config.general.stat_file = home::home_dir() .unwrap_or_else(env::temp_dir) .join(if config.general.stat_file.is_empty() { ".ffp_status" diff --git a/lib/src/utils/mod.rs b/lib/src/utils/mod.rs index 99dfbcb8..cdae30d9 100644 --- a/lib/src/utils/mod.rs +++ b/lib/src/utils/mod.rs @@ -10,9 +10,6 @@ use std::{ sync::{Arc, Mutex}, }; -#[cfg(not(windows))] -use std::env; - use chrono::{prelude::*, TimeDelta}; use ffprobe::{ffprobe, Stream as FFStream}; use rand::prelude::*; @@ -33,9 +30,6 @@ pub mod json_serializer; mod json_validate; mod logging; -#[cfg(windows)] -mod windows; - pub use config::{ self as playout_config, OutputMode::{self, *}, @@ -988,19 +982,6 @@ pub fn custom_format(template: &str, args: &[T]) -> String { filled_template } -pub fn home_dir() -> Option { - home_dir_inner() -} - -#[cfg(windows)] -use windows::home_dir_inner; - -#[cfg(any(unix, target_os = "redox"))] -fn home_dir_inner() -> Option { - #[allow(deprecated)] - env::home_dir() -} - /// Get system time, in non test/debug case. #[cfg(not(any(test, debug_assertions)))] pub fn time_now() -> DateTime { diff --git a/lib/src/utils/windows.rs b/lib/src/utils/windows.rs deleted file mode 100644 index 58aae005..00000000 --- a/lib/src/utils/windows.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::{env, ffi::OsString, os::windows::ffi::OsStringExt, path::PathBuf, ptr}; - -use winapi::shared::minwindef::MAX_PATH; -use winapi::shared::winerror::S_OK; -use winapi::um::shlobj::{SHGetFolderPathW, CSIDL_PROFILE}; - -pub fn home_dir_inner() -> Option { - env::var_os("USERPROFILE") - .filter(|s| !s.is_empty()) - .map(PathBuf::from) - .or_else(home_dir_crt) -} - -#[cfg(not(target_vendor = "uwp"))] -fn home_dir_crt() -> Option { - unsafe { - let mut path: Vec = Vec::with_capacity(MAX_PATH); - match SHGetFolderPathW( - ptr::null_mut(), - CSIDL_PROFILE, - ptr::null_mut(), - 0, - path.as_mut_ptr(), - ) { - S_OK => { - let len = wcslen(path.as_ptr()); - path.set_len(len); - let s = OsString::from_wide(&path); - Some(PathBuf::from(s)) - } - _ => None, - } - } -} - -#[cfg(target_vendor = "uwp")] -fn home_dir_crt() -> Option { - None -} - -extern "C" { - fn wcslen(buf: *const u16) -> usize; -}