|
| 1 | +use std::fs; |
1 | 2 | use std::path::{Path, PathBuf}; |
| 3 | +use tempfile::TempDir; |
| 4 | + |
| 5 | +use crate::constants; |
| 6 | + |
| 7 | +/// Builds the fallback temp directory path from the constant. |
| 8 | +/// The FALLBACK_TEMP_DIR constant contains the full relative path (e.g., ".local/state/protonup-rs/tmp") |
| 9 | +/// This function splits it by '/' and joins each component to the base directory. |
| 10 | +fn build_fallback_dir(base_dir: &Path) -> PathBuf { |
| 11 | + constants::FALLBACK_TEMP_DIR |
| 12 | + .split('/') |
| 13 | + .fold(base_dir.to_path_buf(), |acc, component| acc.join(component)) |
| 14 | +} |
| 15 | + |
| 16 | +/// Checks available disk space and returns an appropriate temp directory. |
| 17 | +/// If /tmp has less than 1GB available, returns a fallback directory under |
| 18 | +/// .local/state/protonup-rs/tmp. Creates the fallback directory if it doesn't exist. |
| 19 | +pub fn get_temp_dir() -> std::io::Result<PathBuf> { |
| 20 | + // Check available space on tmp dir |
| 21 | + let tmp_path = TempDir::with_prefix("protonup-rs-").map(|dir| dir.keep()); |
| 22 | + |
| 23 | + if let Ok(temp_dir) = tmp_path { |
| 24 | + let available_space = fs4::available_space(&temp_dir).unwrap_or(0); |
| 25 | + |
| 26 | + if available_space >= constants::MIN_TEMP_SPACE_BYTES { |
| 27 | + // Enough space, use standard tempdir |
| 28 | + return Ok(temp_dir); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Not enough space, or failed creating the temp dir, use fallback directory |
| 33 | + let fallback_dir = match dirs::state_dir() { |
| 34 | + Some(state_dir) => build_fallback_dir(&state_dir), |
| 35 | + None => { |
| 36 | + // Fallback to home directory if state_dir is not available |
| 37 | + let home_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); |
| 38 | + build_fallback_dir(&home_dir) |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + // Create the fallback directory if it doesn't exist |
| 43 | + fs::create_dir_all(&fallback_dir)?; |
| 44 | + |
| 45 | + Ok(fallback_dir) |
| 46 | +} |
| 47 | + |
| 48 | +/// Creates a temporary download directory for a specific version. |
| 49 | +/// Handles disk space checking and creates the necessary directory structure. |
| 50 | +/// Returns the full path where the download file should be saved. |
| 51 | +pub fn create_download_temp_dir(version: &str, download_url: &str) -> std::io::Result<PathBuf> { |
| 52 | + let temp_dir = get_temp_dir()?; |
| 53 | + |
| 54 | + let download_dir = temp_dir.join(format!("{}.{}", version, "download")); |
| 55 | + |
| 56 | + // Create the version-specific subdirectory |
| 57 | + fs::create_dir_all(&download_dir)?; |
| 58 | + |
| 59 | + // Determine the file extension from the download URL |
| 60 | + let ext = crate::files::check_supported_extension(download_url) |
| 61 | + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e.to_string()))?; |
| 62 | + |
| 63 | + let download_path = download_dir.join(format!("{}.{}", version, ext)); |
| 64 | + |
| 65 | + Ok(download_path) |
| 66 | +} |
| 67 | + |
| 68 | +/// Cleans up the fallback temp directory contents. |
| 69 | +/// This should be called on application exit to remove temporary files. |
| 70 | +pub fn cleanup_fallback_temp_dir() -> std::io::Result<()> { |
| 71 | + let fallback_dir = match dirs::state_dir() { |
| 72 | + Some(state_dir) => build_fallback_dir(&state_dir), |
| 73 | + None => { |
| 74 | + let home_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")); |
| 75 | + build_fallback_dir(&home_dir) |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + if fallback_dir.exists() { |
| 80 | + // Remove all contents of the fallback directory |
| 81 | + for entry in fs::read_dir(&fallback_dir)? { |
| 82 | + let entry = entry?; |
| 83 | + let path = entry.path(); |
| 84 | + if path.is_dir() { |
| 85 | + fs::remove_dir_all(&path)?; |
| 86 | + } else { |
| 87 | + fs::remove_file(&path)?; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Ok(()) |
| 93 | +} |
2 | 94 |
|
3 | 95 | pub fn expand_tilde<P: AsRef<Path>>(path_user_input: P) -> Option<PathBuf> { |
4 | 96 | let p = path_user_input.as_ref(); |
|
0 commit comments