diff options
| author | qvkap <qvkapp@gmail.com> | 2026-09-05 08:29:46 +0300 |
|---|---|---|
| committer | qvkap <qvkapp@gmail.com> | 2026-09-05 08:29:46 +0300 |
| commit | 74c76bb5cea8ff70153a8f9ebe02df6ffb0205f7 (patch) | |
| tree | 7dd16b620d79f579a3ddf4da29e7a55fb585af2f /src/parallel.rs | |
| parent | 22f9abec46c28f50df2c19b134da88848942c168 (diff) | |
| download | ror-74c76bb5cea8ff70153a8f9ebe02df6ffb0205f7.tar.gz ror-74c76bb5cea8ff70153a8f9ebe02df6ffb0205f7.zip | |
fix: dependency resolution, update file removal and sync TLS validation
Diffstat (limited to 'src/parallel.rs')
| -rw-r--r-- | src/parallel.rs | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/src/parallel.rs b/src/parallel.rs index ed4671f..a3a722b 100644 --- a/src/parallel.rs +++ b/src/parallel.rs @@ -1,8 +1,3 @@ -// parallel.rs -#![allow(dead_code)] -#![allow(unused_imports)] -#![allow(unused_variables)] - use std::collections::{HashMap, HashSet}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -11,7 +6,6 @@ use std::time::Duration; use std::io::Write; use colored::Colorize; -use futures::StreamExt; use sha2::{Digest, Sha256}; use tokio::sync::oneshot; use tokio::task; @@ -60,12 +54,12 @@ impl InstalledDB { } struct AtomicInstaller { - shadow: tempfile::TempDir, + _shadow: tempfile::TempDir, } impl AtomicInstaller { fn new(shadow: tempfile::TempDir) -> Self { - Self { shadow } + Self { _shadow: shadow } } fn commit( @@ -90,8 +84,21 @@ impl AtomicInstaller { std::fs::create_dir_all(parent) .map_err(|e| format!("Create parent {}: {}", parent.display(), e))?; - let bak_path = dst.with_extension("ror-old"); if dst.exists() && dst.is_file() { + let mut bak_num = 0u32; + let bak_path = loop { + let mut p = dst.as_os_str().to_os_string(); + if bak_num == 0 { + p.push(".ror-old"); + } else { + p.push(format!(".ror-old.{}", bak_num)); + } + let p = PathBuf::from(p); + if !p.exists() { + break p; + } + bak_num += 1; + }; if let Err(e) = std::fs::rename(&dst, &bak_path) { return Err(format!("Cannot backup {}: {}", dst.display(), e)); } @@ -100,19 +107,14 @@ impl AtomicInstaller { let rename_err = std::fs::rename(&src, &dst).err(); if let Some(e) = rename_err { - use std::io::ErrorKind; if e.raw_os_error() == Some(18) { if let Err(ce) = std::fs::copy(&src, &dst) { - if let Some((last_bak, orig)) = backup_paths.pop() { - let _ = std::fs::rename(&last_bak, &orig); - } + Self::rollback(&backup_paths); return Err(format!("Failed to install (copy) {}: {}", dst.display(), ce)); } let _ = std::fs::remove_file(&src); } else { - if let Some((last_bak, orig)) = backup_paths.pop() { - let _ = std::fs::rename(&last_bak, &orig); - } + Self::rollback(&backup_paths); return Err(format!("Failed to install {}: {}", dst.display(), e)); } } @@ -128,9 +130,21 @@ impl AtomicInstaller { Ok(installed) } + + fn rollback(backup_paths: &[(PathBuf, PathBuf)]) { + for (bak, orig) in backup_paths { + if bak.exists() { + let _ = std::fs::rename(bak, orig); + } + } + } } async fn download_one(url: &str, expected_sha: &str, _cfg: &Config) -> Result<PathBuf, String> { + if url.starts_with("http://") { + return Err(format!("Insecure HTTP URL rejected: {}. Only HTTPS allowed.", url)); + } + let result = timeout(Duration::from_secs(60), async { let client = reqwest::Client::builder() .timeout(Duration::from_secs(30)) @@ -147,24 +161,17 @@ async fn download_one(url: &str, expected_sha: &str, _cfg: &Config) -> Result<Pa return Err(format!("HTTP {} for {}", res.status(), url)); } - let tmp_dir = Path::new("/tmp/ror-download"); - std::fs::create_dir_all(tmp_dir).map_err(|e| e.to_string())?; - let file_name = url.rsplit('/').next().unwrap_or("archive.tmp"); - let file_path = tmp_dir.join(file_name); - - let mut file = std::fs::File::create(&file_path).map_err(|e| e.to_string())?; + let mut tmp_file = NamedTempFile::new().map_err(|e| e.to_string())?; + let file_path = tmp_file.path().to_path_buf(); let mut hasher = Sha256::new(); let body = res.bytes().await.map_err(|e| e.to_string())?; hasher.update(&body); - file.write_all(&body).map_err(|e| e.to_string())?; - - drop(file); + tmp_file.write_all(&body).map_err(|e| e.to_string())?; if !expected_sha.is_empty() { let hash = format!("{:x}", hasher.finalize()); if hash != expected_sha { - let _ = std::fs::remove_file(&file_path); return Err(format!( "SHA256 mismatch: expected {}, got {}", expected_sha, hash @@ -172,6 +179,7 @@ async fn download_one(url: &str, expected_sha: &str, _cfg: &Config) -> Result<Pa } } + drop(tmp_file); Ok(file_path) }) .await |