sharing/
fs.rs

1use std::env;
2use std::path::PathBuf;
3use crate::{proc, string};
4use crate::paths::bin_dir;
5
6pub fn tmp_dir() -> PathBuf {
7    #[cfg(target_os = "windows")]
8    let temp_dir = if let Ok(user_info) = crate::proc::get_gran_father_info(){
9        PathBuf::from(user_info.tmp_path)
10    } else {
11        std::env::temp_dir()
12    };
13
14    #[cfg(not(target_os = "windows"))]
15    let temp_dir = std::env::temp_dir();
16
17    let folder_name = format!("pispas-{}", string::generate_random_string(10));
18    let temp_folder_path = temp_dir.join(&folder_name);
19
20    if let Err(err) = std::fs::create_dir(&temp_folder_path) {
21        tracing::error!("Error creating {} folder: {}", temp_folder_path.display(), err);
22    }
23    tracing::info!("Created temp folder: {}", temp_folder_path.display());
24    return temp_folder_path;
25}
26
27pub fn copy_dir(source: &PathBuf, target: &PathBuf) -> crate::PisPasResult<()> {
28    // copy all files from source folder
29    for entry in std::fs::read_dir(source)? {
30        let entry = entry?;
31        let path = entry.path();
32        let file_name = path.file_name().unwrap();
33        let target_path = target.join(file_name);
34        if path.is_dir() {
35            std::fs::create_dir_all(&target_path)?;
36            copy_dir(&path, &target_path)?;
37        } else if let Err(err1) = std::fs::copy(&path, &target_path){
38            tracing::warn!("Error copying file {path:?} try to kill process => {err1:?}");
39            proc::kill_process_by_name(file_name.to_str().unwrap());
40            if let Err(error) = std::fs::copy(&path, &target_path){
41                tracing::error!("Error copying file {path:?} => error => {error:?}");
42                continue;
43            };
44        }
45    }
46    Ok(())
47}
48
49pub fn current_executable_folder() -> PathBuf {
50    match env::current_exe() {
51        Ok(path) => match path.parent() {
52            Some(parent) => {
53                tracing::info!("Current executable directory: {}", parent.display());
54                parent.to_path_buf()
55            }
56            None => {
57                tracing::warn!("No parent directory found for executable path: {}", path.display());
58                fallback_safe_uninstall_dir()
59            }
60        },
61        Err(e) => {
62            tracing::warn!("Could not get current executable path: {e:?}");
63            fallback_safe_uninstall_dir()
64        }
65    }
66}
67
68fn fallback_safe_uninstall_dir() -> PathBuf {
69    if let Some(base) = dirs::data_dir() {
70        let fallback = base.join("Unpispas").join("UninstallFallback");
71        tracing::info!("Using fallback uninstall directory: {}", fallback.display());
72        return fallback;
73    }
74
75    // Ăšltimo recurso si incluso `data_dir()` falla
76    let default = PathBuf::from(r"C:\ProgramData\Unpispas\UninstallFallback");
77    tracing::error!("Using hardcoded fallback uninstall directory: {}", default.display());
78    default
79}
80pub fn remove_files(folder: &PathBuf) -> crate::PisPasResult<()> {
81    if !folder.exists() {
82        tracing::warn!("Noting to remove in dir {}", folder.display());
83    }
84    match std::fs::remove_dir_all(folder) {
85        Ok(_) => {
86            tracing::info!("Removed files in dir {}", folder.display());
87            Ok(())
88        }
89        Err(error) => {
90            tracing::error!("Error removing {} dir files => {error:?}", folder.display());
91            Err(error)?
92        }
93    }
94}
95
96
97pub fn is_subdirectory(parent: &PathBuf, child: &PathBuf) -> bool {
98    child.starts_with(parent.parent().unwrap())
99}
100
101pub fn is_bin_dir(path: &PathBuf) -> bool {
102    let temp_dir = bin_dir();
103    is_subdirectory(&temp_dir, path)
104}