Restore cached tools from the cache directory to the root filesystem
(system_info: &SystemInfo, cache_dir: &Path)
| 115 | |
| 116 | /// Restore cached tools from the cache directory to the root filesystem |
| 117 | fn restore_from_cache(system_info: &SystemInfo, cache_dir: &Path) -> Result<()> { |
| 118 | if !is_system_compatible(system_info) { |
| 119 | info!("Cache restore is not supported on this system, skipping"); |
| 120 | return Ok(()); |
| 121 | } |
| 122 | |
| 123 | if !cache_dir.exists() { |
| 124 | debug!("Cache directory does not exist: {}", cache_dir.display()); |
| 125 | return Ok(()); |
| 126 | } |
| 127 | |
| 128 | // Check if the directory has any contents |
| 129 | let has_contents = std::fs::read_dir(cache_dir) |
| 130 | .map(|mut entries| entries.next().is_some()) |
| 131 | .unwrap_or(false); |
| 132 | |
| 133 | if !has_contents { |
| 134 | debug!("Cache directory is empty: {}", cache_dir.display()); |
| 135 | return Ok(()); |
| 136 | } |
| 137 | |
| 138 | debug!( |
| 139 | "Restoring tools from cache directory: {}", |
| 140 | cache_dir.display() |
| 141 | ); |
| 142 | |
| 143 | // Read and log the metadata file if it exists |
| 144 | let metadata_path = cache_dir.join(METADATA_FILENAME); |
| 145 | if metadata_path.exists() { |
| 146 | match std::fs::read_to_string(&metadata_path) { |
| 147 | Ok(content) => { |
| 148 | info!( |
| 149 | "Packages restored from cache: {}", |
| 150 | content.lines().join(", ") |
| 151 | ); |
| 152 | } |
| 153 | Err(e) => { |
| 154 | warn!("Failed to read metadata file: {e}"); |
| 155 | } |
| 156 | } |
| 157 | } else { |
| 158 | debug!("No metadata file found in cache directory"); |
| 159 | } |
| 160 | |
| 161 | // Use bash to properly handle glob expansion |
| 162 | let cache_dir_str = cache_dir |
| 163 | .to_str() |
| 164 | .ok_or_else(|| anyhow!("Invalid cache directory path"))?; |
| 165 | |
| 166 | // IMPORTANT: We have to use 'bash' here to ensure that glob patterns are expanded correctly |
| 167 | let copy_cmd = format!("cp -r {cache_dir_str}/* /"); |
| 168 | run_with_sudo("bash", ["-c", ©_cmd])?; |
| 169 | |
| 170 | debug!("Cache restored successfully"); |
| 171 | Ok(()) |
| 172 | } |
| 173 | |
| 174 | /// Save installed packages to the cache directory |
no test coverage detected