Skip to main content

zccache_download_client/
artifact.rs

1use std::fs::{self, File, OpenOptions};
2use std::io::{self, Read, Write};
3use std::path::{Component, Path, PathBuf};
4
5use reqwest::header::ACCEPT_ENCODING;
6use serde::{Deserialize, Serialize};
7use sha2::{Digest, Sha256};
8use tokio::io::AsyncWriteExt;
9use zccache_download::{canonical_destination, stable_download_id, DownloadOptions, DownloadPhase};
10
11use crate::DownloadClient;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
14pub enum WaitMode {
15    Block,
16    NoWait,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
20pub enum ArchiveFormat {
21    Auto,
22    None,
23    Zst,
24    Zip,
25    Xz,
26    TarGz,
27    TarXz,
28    TarZst,
29    SevenZip,
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
33pub enum FetchStatus {
34    Downloaded,
35    AlreadyPresent,
36    Expanded,
37    AlreadyExpanded,
38    Ready,
39    Locked,
40    DryRun,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
44pub enum FetchStateKind {
45    Missing,
46    ArtifactReady,
47    ExpandedReady,
48    Invalid,
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
52pub enum DownloadSource {
53    Url(String),
54    MultipartUrls(Vec<String>),
55}
56
57impl DownloadSource {
58    #[must_use]
59    pub fn primary_url(&self) -> &str {
60        match self {
61            Self::Url(url) => url,
62            Self::MultipartUrls(urls) => urls.first().map(String::as_str).unwrap_or(""),
63        }
64    }
65}
66
67impl From<String> for DownloadSource {
68    fn from(value: String) -> Self {
69        Self::Url(value)
70    }
71}
72
73impl From<&str> for DownloadSource {
74    fn from(value: &str) -> Self {
75        Self::Url(value.to_string())
76    }
77}
78
79impl From<Vec<String>> for DownloadSource {
80    fn from(value: Vec<String>) -> Self {
81        Self::MultipartUrls(value)
82    }
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
86pub struct FetchRequest {
87    pub source: DownloadSource,
88    pub destination_path: PathBuf,
89    pub destination_path_expanded: Option<PathBuf>,
90    pub expected_sha256: Option<String>,
91    pub archive_format: ArchiveFormat,
92    pub wait_mode: WaitMode,
93    pub dry_run: bool,
94    pub force: bool,
95    pub download_options: DownloadOptions,
96}
97
98impl FetchRequest {
99    #[must_use]
100    pub fn new(source: impl Into<DownloadSource>, destination_path: impl Into<PathBuf>) -> Self {
101        Self {
102            source: source.into(),
103            destination_path: destination_path.into(),
104            destination_path_expanded: None,
105            expected_sha256: None,
106            archive_format: ArchiveFormat::Auto,
107            wait_mode: WaitMode::Block,
108            dry_run: false,
109            force: false,
110            download_options: DownloadOptions::default(),
111        }
112    }
113}
114
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
116pub struct FetchResult {
117    pub status: FetchStatus,
118    pub cache_path: PathBuf,
119    pub expanded_path: Option<PathBuf>,
120    pub bytes: Option<u64>,
121    pub sha256: String,
122}
123
124#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
125pub struct FetchState {
126    pub kind: FetchStateKind,
127    pub cache_path: PathBuf,
128    pub expanded_path: Option<PathBuf>,
129    pub bytes: Option<u64>,
130    pub sha256: Option<String>,
131    pub reason: Option<String>,
132}
133
134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
135struct ExpandedMarker {
136    source: DownloadSource,
137    cache_path: String,
138    artifact_sha256: String,
139    archive_format: ArchiveFormat,
140}
141
142#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
143struct ArtifactMarker {
144    source: DownloadSource,
145    cache_path: String,
146    sha256: String,
147    bytes: u64,
148}
149
150#[derive(Debug, Clone, PartialEq, Eq)]
151struct ArtifactFingerprint {
152    sha256: String,
153    bytes: u64,
154}
155
156#[derive(Debug, Clone)]
157struct ResolvedFetchRequest {
158    source: DownloadSource,
159    cache_path: PathBuf,
160    expanded_path: Option<PathBuf>,
161    expected_sha256: Option<String>,
162    archive_format: ArchiveFormat,
163    wait_mode: WaitMode,
164    dry_run: bool,
165    force: bool,
166    download_options: DownloadOptions,
167}
168
169impl DownloadClient {
170    pub fn fetch(&self, request: FetchRequest) -> Result<FetchResult, String> {
171        let resolved = resolve_request(&request)?;
172        let initial = exists_resolved(&resolved)?;
173        if resolved.force && initial.kind != FetchStateKind::Missing {
174            return Err(format!(
175                "artifact state already exists at {}; purge it before forcing replacement",
176                resolved.cache_path.display()
177            ));
178        }
179        if !resolved.force {
180            match initial.kind {
181                FetchStateKind::ExpandedReady => {
182                    return Ok(FetchResult {
183                        status: FetchStatus::AlreadyExpanded,
184                        cache_path: resolved.cache_path,
185                        expanded_path: resolved.expanded_path,
186                        bytes: initial.bytes,
187                        sha256: initial
188                            .sha256
189                            .ok_or_else(|| "missing artifact sha256 fingerprint".to_string())?,
190                    });
191                }
192                FetchStateKind::ArtifactReady if resolved.expanded_path.is_none() => {
193                    return Ok(FetchResult {
194                        status: FetchStatus::AlreadyPresent,
195                        cache_path: resolved.cache_path,
196                        expanded_path: None,
197                        bytes: initial.bytes,
198                        sha256: initial
199                            .sha256
200                            .ok_or_else(|| "missing artifact sha256 fingerprint".to_string())?,
201                    });
202                }
203                _ => {}
204            }
205        }
206
207        if resolved.dry_run {
208            return Ok(FetchResult {
209                status: FetchStatus::DryRun,
210                cache_path: resolved.cache_path,
211                expanded_path: resolved.expanded_path,
212                bytes: initial.bytes,
213                sha256: initial.sha256.unwrap_or_default(),
214            });
215        }
216
217        let _lock = match acquire_fetch_lock(&resolved) {
218            Ok(lock) => lock,
219            Err(message) if message == "locked" => {
220                return Ok(FetchResult {
221                    status: FetchStatus::Locked,
222                    cache_path: resolved.cache_path,
223                    expanded_path: resolved.expanded_path,
224                    bytes: initial.bytes,
225                    sha256: initial.sha256.unwrap_or_default(),
226                });
227            }
228            Err(message) => return Err(message),
229        };
230        let current = exists_resolved(&resolved)?;
231        if current.kind == FetchStateKind::Invalid {
232            return Err(format!(
233                "{}; purge the artifact state before retrying",
234                current
235                    .reason
236                    .clone()
237                    .unwrap_or_else(|| "artifact exists but failed validation".to_string())
238            ));
239        }
240        if !resolved.force {
241            match current.kind {
242                FetchStateKind::ExpandedReady => {
243                    return Ok(FetchResult {
244                        status: FetchStatus::AlreadyExpanded,
245                        cache_path: resolved.cache_path,
246                        expanded_path: resolved.expanded_path,
247                        bytes: current.bytes,
248                        sha256: current
249                            .sha256
250                            .ok_or_else(|| "missing artifact sha256 fingerprint".to_string())?,
251                    });
252                }
253                FetchStateKind::ArtifactReady if resolved.expanded_path.is_none() => {
254                    return Ok(FetchResult {
255                        status: FetchStatus::AlreadyPresent,
256                        cache_path: resolved.cache_path,
257                        expanded_path: None,
258                        bytes: current.bytes,
259                        sha256: current
260                            .sha256
261                            .ok_or_else(|| "missing artifact sha256 fingerprint".to_string())?,
262                    });
263                }
264                _ => {}
265            }
266        }
267
268        let mut downloaded_now = false;
269        if resolved.force || current.kind != FetchStateKind::ArtifactReady {
270            match &resolved.source {
271                DownloadSource::Url(url) => {
272                    let mut handle = self.download(
273                        url,
274                        &resolved.cache_path,
275                        resolved.download_options.clone(),
276                    )?;
277                    let status = loop {
278                        let status = handle.wait(None)?;
279                        if crate::is_terminal(&status) {
280                            break status;
281                        }
282                    };
283                    if status.phase != DownloadPhase::Completed {
284                        return Err(status.error.unwrap_or_else(|| {
285                            format!("download finished in unexpected phase {:?}", status.phase)
286                        }));
287                    }
288                    handle.close()?;
289                }
290                DownloadSource::MultipartUrls(urls) => {
291                    download_explicit_parts(urls, &resolved.cache_path)?;
292                }
293            }
294            downloaded_now = true;
295        }
296
297        let fingerprint = match validate_artifact(&resolved) {
298            Ok(fingerprint) => fingerprint,
299            Err(err) => {
300                cleanup_invalid_fetch_state(&resolved);
301                return Err(err);
302            }
303        };
304        write_artifact_marker(&resolved, &fingerprint)?;
305
306        if let Some(expanded_path) = &resolved.expanded_path {
307            let expanded_ready = expanded_marker_matches(&resolved, &fingerprint)?;
308            if !resolved.force && expanded_ready {
309                return Ok(FetchResult {
310                    status: if downloaded_now {
311                        FetchStatus::Ready
312                    } else {
313                        FetchStatus::AlreadyExpanded
314                    },
315                    cache_path: resolved.cache_path.clone(),
316                    expanded_path: Some(expanded_path.clone()),
317                    bytes: Some(fingerprint.bytes),
318                    sha256: fingerprint.sha256.clone(),
319                });
320            }
321
322            if expanded_path.exists() {
323                return Err(format!(
324                    "expanded destination {} already exists but is not validated; purge it before retrying",
325                    expanded_path.display()
326                ));
327            }
328
329            remove_path_if_exists(&expanded_marker_path(expanded_path))?;
330            extract_archive(&resolved, expanded_path)?;
331            write_expanded_marker(&resolved, &fingerprint)?;
332            return Ok(FetchResult {
333                status: FetchStatus::Expanded,
334                cache_path: resolved.cache_path.clone(),
335                expanded_path: Some(expanded_path.clone()),
336                bytes: Some(fingerprint.bytes),
337                sha256: fingerprint.sha256,
338            });
339        }
340
341        Ok(FetchResult {
342            status: if downloaded_now {
343                FetchStatus::Downloaded
344            } else {
345                FetchStatus::AlreadyPresent
346            },
347            cache_path: resolved.cache_path.clone(),
348            expanded_path: None,
349            bytes: Some(fingerprint.bytes),
350            sha256: fingerprint.sha256,
351        })
352    }
353
354    pub fn exists(&self, request: &FetchRequest) -> Result<FetchState, String> {
355        let resolved = resolve_request_no_create(request)?;
356        exists_resolved(&resolved)
357    }
358}
359
360fn resolve_request(request: &FetchRequest) -> Result<ResolvedFetchRequest, String> {
361    Ok(ResolvedFetchRequest {
362        source: normalize_source(request.source.clone())?,
363        cache_path: canonical_destination(&request.destination_path)
364            .map_err(|e| e.to_string())?
365            .into_path_buf(),
366        expanded_path: request
367            .destination_path_expanded
368            .as_ref()
369            .map(|p| normalize_target(p, true))
370            .transpose()?,
371        expected_sha256: request.expected_sha256.clone().map(normalize_sha256),
372        archive_format: request.archive_format,
373        wait_mode: request.wait_mode,
374        dry_run: request.dry_run,
375        force: request.force,
376        download_options: request.download_options.clone(),
377    })
378}
379
380fn resolve_request_no_create(request: &FetchRequest) -> Result<ResolvedFetchRequest, String> {
381    Ok(ResolvedFetchRequest {
382        source: normalize_source(request.source.clone())?,
383        cache_path: normalize_target(&request.destination_path, false)?,
384        expanded_path: request
385            .destination_path_expanded
386            .as_ref()
387            .map(|p| normalize_target(p, false))
388            .transpose()?,
389        expected_sha256: request.expected_sha256.clone().map(normalize_sha256),
390        archive_format: request.archive_format,
391        wait_mode: request.wait_mode,
392        dry_run: request.dry_run,
393        force: request.force,
394        download_options: request.download_options.clone(),
395    })
396}
397
398fn normalize_target(path: &Path, create_parent: bool) -> Result<PathBuf, String> {
399    let absolute = if path.is_absolute() {
400        path.to_path_buf()
401    } else {
402        std::env::current_dir()
403            .map_err(|e| e.to_string())?
404            .join(path)
405    };
406    let file_name = absolute
407        .file_name()
408        .map(ToOwned::to_owned)
409        .ok_or_else(|| "path must include a terminal file or directory name".to_string())?;
410    let parent = absolute.parent().unwrap_or_else(|| Path::new("."));
411    let canonical_parent = if parent.exists() {
412        std::fs::canonicalize(parent).map_err(|e| e.to_string())?
413    } else if create_parent {
414        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
415        std::fs::canonicalize(parent).map_err(|e| e.to_string())?
416    } else {
417        zccache_core::NormalizedPath::new(parent).into_path_buf()
418    };
419    Ok(canonical_parent.join(file_name))
420}
421
422fn normalize_sha256(value: String) -> String {
423    value.trim().to_ascii_lowercase()
424}
425
426fn normalize_source(source: DownloadSource) -> Result<DownloadSource, String> {
427    match source {
428        DownloadSource::Url(url) => {
429            if url.trim().is_empty() {
430                Err("download source URL must not be empty".to_string())
431            } else {
432                Ok(DownloadSource::Url(url))
433            }
434        }
435        DownloadSource::MultipartUrls(urls) => {
436            if urls.is_empty() {
437                return Err("multipart download source must include at least one URL".to_string());
438            }
439            if urls.iter().any(|url| url.trim().is_empty()) {
440                return Err("multipart download source contains an empty URL".to_string());
441            }
442            Ok(DownloadSource::MultipartUrls(urls))
443        }
444    }
445}
446
447fn exists_resolved(request: &ResolvedFetchRequest) -> Result<FetchState, String> {
448    let cache_exists = request.cache_path.exists();
449    let fingerprint = if cache_exists {
450        Some(read_or_compute_artifact_fingerprint(request)?)
451    } else {
452        None
453    };
454    let cache_valid = fingerprint
455        .as_ref()
456        .map(|fingerprint| artifact_matches_request(request, fingerprint))
457        .unwrap_or(false);
458    let bytes = fingerprint.as_ref().map(|fingerprint| fingerprint.bytes);
459    let sha256 = fingerprint
460        .as_ref()
461        .map(|fingerprint| fingerprint.sha256.clone());
462
463    if let Some(expanded_path) = &request.expanded_path {
464        if cache_valid
465            && expanded_marker_matches(
466                request,
467                fingerprint
468                    .as_ref()
469                    .ok_or_else(|| "missing artifact fingerprint".to_string())?,
470            )?
471            && expanded_path.exists()
472        {
473            return Ok(FetchState {
474                kind: FetchStateKind::ExpandedReady,
475                cache_path: request.cache_path.clone(),
476                expanded_path: Some(expanded_path.clone()),
477                bytes,
478                sha256,
479                reason: None,
480            });
481        }
482
483        if cache_valid {
484            return Ok(FetchState {
485                kind: FetchStateKind::ArtifactReady,
486                cache_path: request.cache_path.clone(),
487                expanded_path: Some(expanded_path.clone()),
488                bytes,
489                sha256,
490                reason: Some("expanded destination not ready".to_string()),
491            });
492        }
493    } else if cache_valid {
494        return Ok(FetchState {
495            kind: FetchStateKind::ArtifactReady,
496            cache_path: request.cache_path.clone(),
497            expanded_path: None,
498            bytes,
499            sha256,
500            reason: None,
501        });
502    }
503
504    if cache_exists {
505        return Ok(FetchState {
506            kind: FetchStateKind::Invalid,
507            cache_path: request.cache_path.clone(),
508            expanded_path: request.expanded_path.clone(),
509            bytes,
510            sha256,
511            reason: Some("artifact exists but failed validation".to_string()),
512        });
513    }
514
515    Ok(FetchState {
516        kind: FetchStateKind::Missing,
517        cache_path: request.cache_path.clone(),
518        expanded_path: request.expanded_path.clone(),
519        bytes: None,
520        sha256: None,
521        reason: None,
522    })
523}
524
525fn artifact_matches_request(
526    request: &ResolvedFetchRequest,
527    fingerprint: &ArtifactFingerprint,
528) -> bool {
529    request
530        .expected_sha256
531        .as_ref()
532        .map(|expected_sha256| fingerprint.sha256 == *expected_sha256)
533        .unwrap_or(true)
534}
535
536fn validate_artifact(request: &ResolvedFetchRequest) -> Result<ArtifactFingerprint, String> {
537    if !request.cache_path.exists() {
538        return Err(format!(
539            "downloaded artifact missing at {}",
540            request.cache_path.display()
541        ));
542    }
543    let fingerprint =
544        compute_artifact_fingerprint(&request.cache_path).map_err(|e| e.to_string())?;
545    if let Some(expected_sha256) = &request.expected_sha256 {
546        if fingerprint.sha256 != *expected_sha256 {
547            return Err(format!(
548                "sha256 mismatch for {}: expected {}, got {}",
549                request.cache_path.display(),
550                expected_sha256,
551                fingerprint.sha256
552            ));
553        }
554    }
555    Ok(fingerprint)
556}
557
558fn cleanup_invalid_fetch_state(request: &ResolvedFetchRequest) {
559    let _ = remove_path_if_exists(&request.cache_path);
560    let _ = remove_path_if_exists(&artifact_marker_path(&request.cache_path));
561    if let Some(expanded_path) = &request.expanded_path {
562        let _ = remove_path_if_exists(expanded_path);
563        let _ = remove_path_if_exists(&expanded_marker_path(expanded_path));
564    }
565}
566
567fn download_explicit_parts(part_urls: &[String], destination: &Path) -> Result<(), String> {
568    let temp_path = temp_download_path(destination);
569    let runtime = tokio::runtime::Builder::new_current_thread()
570        .enable_all()
571        .build()
572        .map_err(|e| format!("failed to create tokio runtime: {e}"))?;
573    runtime.block_on(async move {
574        let client = reqwest::Client::builder()
575            .user_agent(format!("zccache-download/{}", zccache_core::VERSION))
576            .build()
577            .map_err(|e| e.to_string())?;
578
579        if let Some(parent) = destination.parent() {
580            tokio::fs::create_dir_all(parent)
581                .await
582                .map_err(|e| e.to_string())?;
583        }
584
585        let _ = tokio::fs::remove_file(&temp_path).await;
586
587        let result = async {
588            let mut output = tokio::fs::File::create(&temp_path)
589                .await
590                .map_err(|e| e.to_string())?;
591            for url in part_urls {
592                let mut response = client
593                    .get(url)
594                    .header(ACCEPT_ENCODING, "identity")
595                    .send()
596                    .await
597                    .map_err(|e| e.to_string())?;
598                if !response.status().is_success() {
599                    return Err(format!("unexpected status {} for {url}", response.status()));
600                }
601                while let Some(chunk) = response.chunk().await.map_err(|e| e.to_string())? {
602                    output.write_all(&chunk).await.map_err(|e| e.to_string())?;
603                }
604            }
605            output.flush().await.map_err(|e| e.to_string())?;
606            drop(output);
607            if destination.exists() {
608                let _ = tokio::fs::remove_file(destination).await;
609            }
610            tokio::fs::rename(&temp_path, destination)
611                .await
612                .map_err(|e| e.to_string())
613        }
614        .await;
615
616        if result.is_err() {
617            let _ = tokio::fs::remove_file(&temp_path).await;
618        }
619        result
620    })
621}
622
623fn sha256_file(path: &Path) -> io::Result<String> {
624    let mut file = File::open(path)?;
625    let mut hasher = Sha256::new();
626    let mut buf = [0u8; 64 * 1024];
627    loop {
628        let n = file.read(&mut buf)?;
629        if n == 0 {
630            break;
631        }
632        hasher.update(&buf[..n]);
633    }
634    Ok(format!("{:x}", hasher.finalize()))
635}
636
637fn compute_artifact_fingerprint(path: &Path) -> io::Result<ArtifactFingerprint> {
638    let sha256 = sha256_file(path)?;
639    let bytes = fs::metadata(path)?.len();
640    Ok(ArtifactFingerprint { sha256, bytes })
641}
642
643fn temp_download_path(destination: &Path) -> PathBuf {
644    destination.with_extension(format!(
645        "{}part",
646        destination
647            .extension()
648            .map(|ext| format!("{}.", ext.to_string_lossy()))
649            .unwrap_or_default()
650    ))
651}
652
653struct FetchLock {
654    _file: File,
655}
656
657fn acquire_fetch_lock(request: &ResolvedFetchRequest) -> Result<FetchLock, String> {
658    let lock_path = fetch_lock_path(request);
659    if let Some(parent) = lock_path.parent() {
660        fs::create_dir_all(parent).map_err(|e| e.to_string())?;
661    }
662    let file = OpenOptions::new()
663        .read(true)
664        .write(true)
665        .create(true)
666        .truncate(false)
667        .open(&lock_path)
668        .map_err(|e| e.to_string())?;
669    match request.wait_mode {
670        WaitMode::Block => fs2::FileExt::lock_exclusive(&file).map_err(|e| e.to_string())?,
671        WaitMode::NoWait => {
672            if fs2::FileExt::try_lock_exclusive(&file).is_err() {
673                return Err("locked".to_string());
674            }
675        }
676    }
677    Ok(FetchLock { _file: file })
678}
679
680fn fetch_lock_path(request: &ResolvedFetchRequest) -> PathBuf {
681    let mut key = zccache_core::normalize_for_key(&request.cache_path);
682    if let Some(expanded_path) = &request.expanded_path {
683        key.push('\n');
684        key.push_str(&zccache_core::normalize_for_key(expanded_path));
685    }
686    let hash = stable_download_id(Path::new(&key));
687    zccache_core::config::default_cache_dir()
688        .join("downloads")
689        .join("locks")
690        .join(format!("{hash}.lock"))
691        .into_path_buf()
692}
693
694fn artifact_marker_path(cache_path: &Path) -> PathBuf {
695    let hash = stable_download_id(cache_path);
696    zccache_core::config::default_cache_dir()
697        .join("downloads")
698        .join("artifact-state")
699        .join(format!("{hash}.json"))
700        .into_path_buf()
701}
702
703fn expanded_marker_path(expanded_path: &Path) -> PathBuf {
704    let hash = stable_download_id(expanded_path);
705    zccache_core::config::default_cache_dir()
706        .join("downloads")
707        .join("expanded-state")
708        .join(format!("{hash}.json"))
709        .into_path_buf()
710}
711
712fn read_or_compute_artifact_fingerprint(
713    request: &ResolvedFetchRequest,
714) -> Result<ArtifactFingerprint, String> {
715    let fingerprint =
716        compute_artifact_fingerprint(&request.cache_path).map_err(|e| e.to_string())?;
717    if let Ok(content) = fs::read_to_string(artifact_marker_path(&request.cache_path)) {
718        let marker: ArtifactMarker = serde_json::from_str(&content).map_err(|e| e.to_string())?;
719        if marker.source != request.source
720            || marker.cache_path != request.cache_path.to_string_lossy()
721            || marker.sha256 != fingerprint.sha256
722            || marker.bytes != fingerprint.bytes
723        {
724            return Err(format!(
725                "artifact marker for {} does not match the on-disk payload",
726                request.cache_path.display()
727            ));
728        }
729    }
730    Ok(fingerprint)
731}
732
733fn write_artifact_marker(
734    request: &ResolvedFetchRequest,
735    fingerprint: &ArtifactFingerprint,
736) -> Result<(), String> {
737    let marker_path = artifact_marker_path(&request.cache_path);
738    if let Some(parent) = marker_path.parent() {
739        fs::create_dir_all(parent).map_err(|e| e.to_string())?;
740    }
741    let marker = ArtifactMarker {
742        source: request.source.clone(),
743        cache_path: request.cache_path.to_string_lossy().into_owned(),
744        sha256: fingerprint.sha256.clone(),
745        bytes: fingerprint.bytes,
746    };
747    let json = serde_json::to_string(&marker).map_err(|e| e.to_string())?;
748    fs::write(marker_path, json).map_err(|e| e.to_string())
749}
750
751fn expanded_marker_matches(
752    request: &ResolvedFetchRequest,
753    fingerprint: &ArtifactFingerprint,
754) -> Result<bool, String> {
755    let Some(expanded_path) = &request.expanded_path else {
756        return Ok(false);
757    };
758    let marker_path = expanded_marker_path(expanded_path);
759    let marker: ExpandedMarker = match fs::read_to_string(&marker_path) {
760        Ok(content) => serde_json::from_str(&content).map_err(|e| e.to_string())?,
761        Err(_) => return Ok(false),
762    };
763    if marker.source != request.source {
764        return Ok(false);
765    }
766    if marker.cache_path != request.cache_path.to_string_lossy() {
767        return Ok(false);
768    }
769    if marker.artifact_sha256 != fingerprint.sha256 {
770        return Ok(false);
771    }
772    if marker.archive_format != detect_archive_format(request)? {
773        return Ok(false);
774    }
775    Ok(expanded_path.exists())
776}
777
778fn write_expanded_marker(
779    request: &ResolvedFetchRequest,
780    fingerprint: &ArtifactFingerprint,
781) -> Result<(), String> {
782    let Some(expanded_path) = &request.expanded_path else {
783        return Ok(());
784    };
785    let marker_path = expanded_marker_path(expanded_path);
786    if let Some(parent) = marker_path.parent() {
787        fs::create_dir_all(parent).map_err(|e| e.to_string())?;
788    }
789    let marker = ExpandedMarker {
790        source: request.source.clone(),
791        cache_path: request.cache_path.to_string_lossy().into_owned(),
792        artifact_sha256: fingerprint.sha256.clone(),
793        archive_format: detect_archive_format(request)?,
794    };
795    let json = serde_json::to_string(&marker).map_err(|e| e.to_string())?;
796    fs::write(marker_path, json).map_err(|e| e.to_string())
797}
798
799fn detect_archive_format(request: &ResolvedFetchRequest) -> Result<ArchiveFormat, String> {
800    match request.archive_format {
801        ArchiveFormat::Auto => auto_archive_format(&request.cache_path),
802        other => Ok(other),
803    }
804}
805
806fn auto_archive_format(path: &Path) -> Result<ArchiveFormat, String> {
807    let name = path
808        .file_name()
809        .map(|n| n.to_string_lossy().to_ascii_lowercase())
810        .unwrap_or_default();
811    if name.ends_with(".tar.gz") {
812        Ok(ArchiveFormat::TarGz)
813    } else if name.ends_with(".tar.xz") {
814        Ok(ArchiveFormat::TarXz)
815    } else if name.ends_with(".tar.zst") || name.ends_with(".tzst") {
816        Ok(ArchiveFormat::TarZst)
817    } else if name.ends_with(".zip") {
818        Ok(ArchiveFormat::Zip)
819    } else if name.ends_with(".zst") {
820        Ok(ArchiveFormat::Zst)
821    } else if name.ends_with(".xz") {
822        Ok(ArchiveFormat::Xz)
823    } else if name.ends_with(".7z") {
824        Ok(ArchiveFormat::SevenZip)
825    } else {
826        Ok(ArchiveFormat::None)
827    }
828}
829
830fn extract_archive(request: &ResolvedFetchRequest, expanded_path: &Path) -> Result<(), String> {
831    match detect_archive_format(request)? {
832        ArchiveFormat::None => {
833            copy_file(&request.cache_path, expanded_path).map_err(|e| e.to_string())
834        }
835        ArchiveFormat::Zst => {
836            let input = File::open(&request.cache_path).map_err(|e| e.to_string())?;
837            let mut decoder = ruzstd::StreamingDecoder::new(input).map_err(|e| e.to_string())?;
838            write_decoded_to_file(&mut decoder, expanded_path).map_err(|e| e.to_string())
839        }
840        ArchiveFormat::Xz => {
841            let input = File::open(&request.cache_path).map_err(|e| e.to_string())?;
842            if let Some(parent) = expanded_path.parent() {
843                fs::create_dir_all(parent).map_err(|e| e.to_string())?;
844            }
845            let mut output = File::create(expanded_path).map_err(|e| e.to_string())?;
846            let mut input = io::BufReader::new(input);
847            lzma_rs::xz_decompress(&mut input, &mut output).map_err(|e| e.to_string())
848        }
849        ArchiveFormat::Zip => extract_zip(&request.cache_path, expanded_path),
850        ArchiveFormat::TarGz => {
851            let input = File::open(&request.cache_path).map_err(|e| e.to_string())?;
852            let decoder = flate2::read::GzDecoder::new(input);
853            extract_tar(decoder, expanded_path)
854        }
855        ArchiveFormat::TarXz => {
856            let input = File::open(&request.cache_path).map_err(|e| e.to_string())?;
857            let mut decoded = Vec::new();
858            let mut input = io::BufReader::new(input);
859            lzma_rs::xz_decompress(&mut input, &mut decoded).map_err(|e| e.to_string())?;
860            extract_tar(io::Cursor::new(decoded), expanded_path)
861        }
862        ArchiveFormat::TarZst => {
863            let input = File::open(&request.cache_path).map_err(|e| e.to_string())?;
864            let decoder = ruzstd::StreamingDecoder::new(input).map_err(|e| e.to_string())?;
865            extract_tar(decoder, expanded_path)
866        }
867        ArchiveFormat::SevenZip => extract_7z(&request.cache_path, expanded_path),
868        ArchiveFormat::Auto => Err("archive format auto-detection failed".to_string()),
869    }
870}
871
872fn extract_7z(archive_path: &Path, destination: &Path) -> Result<(), String> {
873    fs::create_dir_all(destination).map_err(|e| e.to_string())?;
874    let base = destination.to_path_buf();
875    sevenz_rust::decompress_file_with_extract_fn(
876        archive_path,
877        destination,
878        move |entry, reader, _default_dest| {
879            let relative = Path::new(entry.name());
880            let out_path = safe_join(&base, relative).map_err(std::io::Error::other)?;
881            if entry.is_directory() {
882                fs::create_dir_all(&out_path)?;
883                return Ok(true);
884            }
885            if let Some(parent) = out_path.parent() {
886                fs::create_dir_all(parent)?;
887            }
888            let mut output = File::create(&out_path)?;
889            io::copy(reader, &mut output)?;
890            output.flush()?;
891            Ok(true)
892        },
893    )
894    .map_err(|e| e.to_string())
895}
896
897fn write_decoded_to_file(reader: &mut dyn Read, destination: &Path) -> io::Result<()> {
898    if let Some(parent) = destination.parent() {
899        fs::create_dir_all(parent)?;
900    }
901    let mut output = File::create(destination)?;
902    io::copy(reader, &mut output)?;
903    output.flush()?;
904    Ok(())
905}
906
907fn copy_file(source: &Path, destination: &Path) -> io::Result<()> {
908    if let Some(parent) = destination.parent() {
909        fs::create_dir_all(parent)?;
910    }
911    fs::copy(source, destination)?;
912    Ok(())
913}
914
915fn extract_zip(archive_path: &Path, destination: &Path) -> Result<(), String> {
916    fs::create_dir_all(destination).map_err(|e| e.to_string())?;
917    let file = File::open(archive_path).map_err(|e| e.to_string())?;
918    let mut zip = zip::ZipArchive::new(file).map_err(|e| e.to_string())?;
919    for i in 0..zip.len() {
920        let mut entry = zip.by_index(i).map_err(|e| e.to_string())?;
921        let name = entry
922            .enclosed_name()
923            .map(|p| p.to_path_buf())
924            .ok_or_else(|| format!("unsafe zip entry: {}", entry.name()))?;
925        let out_path = safe_join(destination, &name)?;
926        if entry.is_dir() {
927            fs::create_dir_all(&out_path).map_err(|e| e.to_string())?;
928            continue;
929        }
930        if let Some(mode) = entry.unix_mode() {
931            if (mode & 0o170000) == 0o120000 {
932                return Err(format!(
933                    "zip symlink entries are not allowed: {}",
934                    entry.name()
935                ));
936            }
937        }
938        if let Some(parent) = out_path.parent() {
939            fs::create_dir_all(parent).map_err(|e| e.to_string())?;
940        }
941        let mut out = File::create(&out_path).map_err(|e| e.to_string())?;
942        io::copy(&mut entry, &mut out).map_err(|e| e.to_string())?;
943    }
944    Ok(())
945}
946
947fn extract_tar<R: Read>(reader: R, destination: &Path) -> Result<(), String> {
948    fs::create_dir_all(destination).map_err(|e| e.to_string())?;
949    let mut archive = tar::Archive::new(reader);
950    let entries = archive.entries().map_err(|e| e.to_string())?;
951    for item in entries {
952        let mut entry = item.map_err(|e| e.to_string())?;
953        let path = entry.path().map_err(|e| e.to_string())?;
954        let out_path = safe_join(destination, &path)?;
955        let entry_type = entry.header().entry_type();
956        if entry_type.is_symlink() || entry_type.is_hard_link() {
957            return Err(format!(
958                "tar link entries are not allowed: {}",
959                path.display()
960            ));
961        }
962        if entry_type.is_dir() {
963            fs::create_dir_all(&out_path).map_err(|e| e.to_string())?;
964            continue;
965        }
966        if let Some(parent) = out_path.parent() {
967            fs::create_dir_all(parent).map_err(|e| e.to_string())?;
968        }
969        let mut out = File::create(&out_path).map_err(|e| e.to_string())?;
970        io::copy(&mut entry, &mut out).map_err(|e| e.to_string())?;
971    }
972    Ok(())
973}
974
975fn safe_join(base: &Path, entry: &Path) -> Result<PathBuf, String> {
976    if entry.is_absolute() {
977        return Err(format!(
978            "absolute archive entry is not allowed: {}",
979            entry.display()
980        ));
981    }
982    let mut clean = PathBuf::new();
983    for component in entry.components() {
984        match component {
985            Component::Normal(part) => clean.push(part),
986            Component::CurDir => {}
987            _ => return Err(format!("unsafe archive entry: {}", entry.display())),
988        }
989    }
990    Ok(base.join(clean))
991}
992
993fn remove_path_if_exists(path: &Path) -> Result<(), String> {
994    if !path.exists() {
995        return Ok(());
996    }
997    if path.is_dir() {
998        fs::remove_dir_all(path).map_err(|e| e.to_string())
999    } else {
1000        fs::remove_file(path).map_err(|e| e.to_string())
1001    }
1002}
1003
1004#[cfg(test)]
1005mod tests {
1006    use super::*;
1007
1008    #[path = "../../../../zccache-download-daemon/src/lib.rs"]
1009    mod download_daemon_impl;
1010
1011    use std::net::{TcpListener, TcpStream};
1012    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1013    use std::sync::Arc;
1014    use std::thread;
1015    use std::time::{Duration, Instant};
1016
1017    use download_daemon_impl::DownloadDaemon;
1018    use flate2::write::GzEncoder;
1019    use flate2::Compression;
1020
1021    #[derive(Clone)]
1022    struct TestHttpConfig {
1023        body: Arc<Vec<u8>>,
1024        accept_ranges: bool,
1025        send_content_length: bool,
1026        chunk_size: usize,
1027        chunk_delay: Duration,
1028        path: String,
1029    }
1030
1031    struct TestHttpServer {
1032        url: String,
1033        request_count: Arc<AtomicUsize>,
1034        range_request_count: Arc<AtomicUsize>,
1035        shutdown: Arc<AtomicBool>,
1036        thread: Option<thread::JoinHandle<()>>,
1037    }
1038
1039    impl TestHttpServer {
1040        fn start(config: TestHttpConfig) -> Self {
1041            let listener = TcpListener::bind("127.0.0.1:0").unwrap();
1042            let addr = listener.local_addr().unwrap();
1043            listener.set_nonblocking(true).unwrap();
1044            let url = format!("http://{addr}/{}", config.path);
1045            let request_count = Arc::new(AtomicUsize::new(0));
1046            let range_request_count = Arc::new(AtomicUsize::new(0));
1047            let shutdown = Arc::new(AtomicBool::new(false));
1048            let request_count_clone = Arc::clone(&request_count);
1049            let range_request_count_clone = Arc::clone(&range_request_count);
1050            let shutdown_clone = Arc::clone(&shutdown);
1051            let config_for_thread = config.clone();
1052            let thread = thread::spawn(move || {
1053                while !shutdown_clone.load(Ordering::Relaxed) {
1054                    match listener.accept() {
1055                        Ok((stream, _)) => {
1056                            let config = config_for_thread.clone();
1057                            let request_count = Arc::clone(&request_count_clone);
1058                            let range_request_count = Arc::clone(&range_request_count_clone);
1059                            thread::spawn(move || {
1060                                let _ = handle_test_http_connection(
1061                                    stream,
1062                                    config,
1063                                    request_count,
1064                                    range_request_count,
1065                                );
1066                            });
1067                        }
1068                        Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
1069                            thread::sleep(Duration::from_millis(10));
1070                        }
1071                        Err(_) => break,
1072                    }
1073                }
1074            });
1075            Self {
1076                url,
1077                request_count,
1078                range_request_count,
1079                shutdown,
1080                thread: Some(thread),
1081            }
1082        }
1083
1084        fn request_count(&self) -> usize {
1085            self.request_count.load(Ordering::Relaxed)
1086        }
1087
1088        fn range_request_count(&self) -> usize {
1089            self.range_request_count.load(Ordering::Relaxed)
1090        }
1091    }
1092
1093    impl Drop for TestHttpServer {
1094        fn drop(&mut self) {
1095            self.shutdown.store(true, Ordering::Relaxed);
1096            let _ = TcpStream::connect(
1097                self.url
1098                    .trim_start_matches("http://")
1099                    .split('/')
1100                    .next()
1101                    .unwrap_or_default(),
1102            );
1103            if let Some(thread) = self.thread.take() {
1104                let _ = thread.join();
1105            }
1106        }
1107    }
1108
1109    fn handle_test_http_connection(
1110        mut stream: TcpStream,
1111        config: TestHttpConfig,
1112        request_count: Arc<AtomicUsize>,
1113        range_request_count: Arc<AtomicUsize>,
1114    ) -> io::Result<()> {
1115        let mut request = Vec::new();
1116        let mut buf = [0u8; 4096];
1117        loop {
1118            let n = stream.read(&mut buf)?;
1119            if n == 0 {
1120                return Ok(());
1121            }
1122            request.extend_from_slice(&buf[..n]);
1123            if request.windows(4).any(|window| window == b"\r\n\r\n") {
1124                break;
1125            }
1126        }
1127        request_count.fetch_add(1, Ordering::Relaxed);
1128        let request_text = String::from_utf8_lossy(&request);
1129        let mut lines = request_text.lines();
1130        let request_line = lines.next().unwrap_or_default();
1131        let mut parts = request_line.split_whitespace();
1132        let method = parts.next().unwrap_or_default();
1133        let range_header = request_text.lines().find_map(|line| {
1134            let (name, value) = line.split_once(':')?;
1135            if name.eq_ignore_ascii_case("range") {
1136                Some(value.trim().to_string())
1137            } else {
1138                None
1139            }
1140        });
1141
1142        let mut body = (*config.body).clone();
1143        let mut status_line = "HTTP/1.1 200 OK\r\n".to_string();
1144        let mut content_range = None;
1145        if let Some(range) = range_header {
1146            if config.accept_ranges {
1147                if let Some((start, end)) = parse_range(&range, body.len() as u64) {
1148                    range_request_count.fetch_add(1, Ordering::Relaxed);
1149                    status_line = "HTTP/1.1 206 Partial Content\r\n".to_string();
1150                    content_range = Some(format!("bytes {start}-{end}/{}", body.len()));
1151                    body = body[start as usize..=end as usize].to_vec();
1152                }
1153            }
1154        }
1155
1156        let mut headers = String::new();
1157        headers.push_str("Connection: close\r\n");
1158        headers.push_str("Content-Type: application/octet-stream\r\n");
1159        if config.accept_ranges {
1160            headers.push_str("Accept-Ranges: bytes\r\n");
1161        }
1162        if config.send_content_length {
1163            headers.push_str(&format!("Content-Length: {}\r\n", body.len()));
1164        }
1165        if let Some(content_range) = content_range {
1166            headers.push_str(&format!("Content-Range: {content_range}\r\n"));
1167        }
1168
1169        stream.write_all(status_line.as_bytes())?;
1170        stream.write_all(headers.as_bytes())?;
1171        stream.write_all(b"\r\n")?;
1172
1173        if method.eq_ignore_ascii_case("HEAD") {
1174            stream.flush()?;
1175            return Ok(());
1176        }
1177
1178        if config.chunk_size == 0 {
1179            stream.write_all(&body)?;
1180        } else {
1181            for chunk in body.chunks(config.chunk_size) {
1182                stream.write_all(chunk)?;
1183                stream.flush()?;
1184                if !config.chunk_delay.is_zero() {
1185                    thread::sleep(config.chunk_delay);
1186                }
1187            }
1188        }
1189        stream.flush()?;
1190        Ok(())
1191    }
1192
1193    fn parse_range(header: &str, total_len: u64) -> Option<(u64, u64)> {
1194        let range = header.strip_prefix("bytes=")?;
1195        let (start, end) = range.split_once('-')?;
1196        let start = start.parse::<u64>().ok()?;
1197        let end = if end.is_empty() {
1198            total_len.checked_sub(1)?
1199        } else {
1200            end.parse::<u64>().ok()?
1201        };
1202        if start > end || end >= total_len {
1203            return None;
1204        }
1205        Some((start, end))
1206    }
1207
1208    struct TestDaemon {
1209        endpoint: String,
1210        shutdown: Arc<tokio::sync::Notify>,
1211        thread: Option<thread::JoinHandle<()>>,
1212    }
1213
1214    impl TestDaemon {
1215        fn start() -> Self {
1216            let endpoint = unique_test_endpoint();
1217            let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel(1);
1218            let endpoint_for_thread = endpoint.clone();
1219            let thread = thread::spawn(move || {
1220                let runtime = tokio::runtime::Builder::new_current_thread()
1221                    .enable_all()
1222                    .build()
1223                    .unwrap();
1224                runtime.block_on(async move {
1225                    let mut daemon = DownloadDaemon::bind(&endpoint_for_thread).unwrap();
1226                    ready_tx.send(daemon.shutdown_handle()).unwrap();
1227                    daemon.run().await.unwrap();
1228                });
1229            });
1230            let shutdown = ready_rx
1231                .recv_timeout(Duration::from_secs(5))
1232                .expect("download daemon failed to bind");
1233            let client = DownloadClient::new(Some(endpoint.clone()));
1234            let deadline = Instant::now() + Duration::from_secs(5);
1235            while Instant::now() < deadline {
1236                if client.daemon_status().is_ok() {
1237                    return Self {
1238                        endpoint,
1239                        shutdown,
1240                        thread: Some(thread),
1241                    };
1242                }
1243                thread::sleep(Duration::from_millis(50));
1244            }
1245            panic!("download daemon did not start in time");
1246        }
1247    }
1248
1249    impl Drop for TestDaemon {
1250        fn drop(&mut self) {
1251            self.shutdown.notify_one();
1252            if let Some(thread) = self.thread.take() {
1253                let _ = thread.join();
1254            }
1255        }
1256    }
1257
1258    fn unique_test_endpoint() -> String {
1259        static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
1260        let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
1261        #[cfg(windows)]
1262        {
1263            format!(
1264                r"\\.\pipe\zccache-download-test-{}-{id}",
1265                std::process::id()
1266            )
1267        }
1268        #[cfg(unix)]
1269        {
1270            std::env::temp_dir()
1271                .join(format!(
1272                    "zccache-download-test-{}-{id}.sock",
1273                    std::process::id()
1274                ))
1275                .display()
1276                .to_string()
1277        }
1278    }
1279
1280    fn sha256_hex(data: &[u8]) -> String {
1281        let mut hasher = Sha256::new();
1282        hasher.update(data);
1283        format!("{:x}", hasher.finalize())
1284    }
1285
1286    #[test]
1287    fn auto_detect_archive_formats() {
1288        assert_eq!(
1289            auto_archive_format(Path::new("toolchain.tar.gz")).unwrap(),
1290            ArchiveFormat::TarGz
1291        );
1292        assert_eq!(
1293            auto_archive_format(Path::new("toolchain.tar.xz")).unwrap(),
1294            ArchiveFormat::TarXz
1295        );
1296        assert_eq!(
1297            auto_archive_format(Path::new("toolchain.tar.zst")).unwrap(),
1298            ArchiveFormat::TarZst
1299        );
1300        assert_eq!(
1301            auto_archive_format(Path::new("toolchain.zip")).unwrap(),
1302            ArchiveFormat::Zip
1303        );
1304        assert_eq!(
1305            auto_archive_format(Path::new("toolchain.7z")).unwrap(),
1306            ArchiveFormat::SevenZip
1307        );
1308    }
1309
1310    #[test]
1311    fn safe_join_rejects_parent_traversal() {
1312        let err = safe_join(Path::new("out"), Path::new("../evil")).unwrap_err();
1313        assert!(err.contains("unsafe"));
1314    }
1315
1316    #[test]
1317    fn zip_extraction_rejects_path_traversal() {
1318        let dir = tempfile::tempdir().unwrap();
1319        let archive = dir.path().join("bad.zip");
1320        {
1321            let file = File::create(&archive).unwrap();
1322            let mut zip = zip::ZipWriter::new(file);
1323            let options = zip::write::SimpleFileOptions::default();
1324            zip.start_file("../evil.txt", options).unwrap();
1325            zip.write_all(b"bad").unwrap();
1326            zip.finish().unwrap();
1327        }
1328        let out = dir.path().join("extract");
1329        let err = extract_zip(&archive, &out).unwrap_err();
1330        assert!(err.contains("unsafe zip entry"));
1331    }
1332
1333    #[test]
1334    fn tar_gz_extracts_regular_files() {
1335        let dir = tempfile::tempdir().unwrap();
1336        let archive = dir.path().join("ok.tar.gz");
1337        {
1338            let file = File::create(&archive).unwrap();
1339            let encoder = GzEncoder::new(file, Compression::default());
1340            let mut builder = tar::Builder::new(encoder);
1341            let data = b"hello";
1342            let mut header = tar::Header::new_gnu();
1343            header.set_size(data.len() as u64);
1344            header.set_mode(0o644);
1345            header.set_cksum();
1346            builder
1347                .append_data(&mut header, "bin/tool.txt", &data[..])
1348                .unwrap();
1349            builder.finish().unwrap();
1350        }
1351        let out = dir.path().join("extract");
1352        let file = File::open(&archive).unwrap();
1353        let decoder = flate2::read::GzDecoder::new(file);
1354        extract_tar(decoder, &out).unwrap();
1355        assert_eq!(
1356            fs::read(out.join("bin").join("tool.txt")).unwrap(),
1357            b"hello"
1358        );
1359    }
1360
1361    #[test]
1362    fn fetch_cache_miss_then_hit_and_exists_stay_local() {
1363        let daemon = TestDaemon::start();
1364        let body = b"artifact payload".to_vec();
1365        let server = TestHttpServer::start(TestHttpConfig {
1366            body: Arc::new(body.clone()),
1367            accept_ranges: false,
1368            send_content_length: true,
1369            chunk_size: 0,
1370            chunk_delay: Duration::ZERO,
1371            path: "artifact.bin".to_string(),
1372        });
1373        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1374        let dir = tempfile::tempdir().unwrap();
1375        let mut request = FetchRequest::new(server.url.clone(), dir.path().join("artifact.bin"));
1376        request.expected_sha256 = Some(sha256_hex(&body));
1377
1378        let first = client.fetch(request.clone()).unwrap();
1379        assert_eq!(first.status, FetchStatus::Downloaded);
1380        assert_eq!(first.sha256, sha256_hex(&body));
1381        let requests_after_first = server.request_count();
1382        assert!(requests_after_first > 0);
1383
1384        let second = client.fetch(request.clone()).unwrap();
1385        assert_eq!(second.status, FetchStatus::AlreadyPresent);
1386        assert_eq!(server.request_count(), requests_after_first);
1387
1388        let state = client.exists(&request).unwrap();
1389        assert_eq!(state.kind, FetchStateKind::ArtifactReady);
1390        assert_eq!(state.sha256.as_deref(), Some(first.sha256.as_str()));
1391        assert_eq!(server.request_count(), requests_after_first);
1392    }
1393
1394    #[test]
1395    fn fetch_checksum_mismatch_cleans_up_invalid_artifact() {
1396        let daemon = TestDaemon::start();
1397        let body = b"wrong checksum body".to_vec();
1398        let server = TestHttpServer::start(TestHttpConfig {
1399            body: Arc::new(body),
1400            accept_ranges: false,
1401            send_content_length: true,
1402            chunk_size: 0,
1403            chunk_delay: Duration::ZERO,
1404            path: "bad.bin".to_string(),
1405        });
1406        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1407        let dir = tempfile::tempdir().unwrap();
1408        let destination = dir.path().join("bad.bin");
1409        let mut request = FetchRequest::new(server.url.clone(), &destination);
1410        request.expected_sha256 = Some("00".repeat(32));
1411
1412        let err = client.fetch(request.clone()).unwrap_err();
1413        assert!(err.contains("sha256 mismatch"));
1414        assert!(!destination.exists());
1415
1416        let state = client.exists(&request).unwrap();
1417        assert_eq!(state.kind, FetchStateKind::Missing);
1418    }
1419
1420    #[test]
1421    fn fetch_single_url_max_connections_uses_range_requests() {
1422        let daemon = TestDaemon::start();
1423        let body: Vec<u8> = (0..128 * 1024).map(|i| (i % 251) as u8).collect();
1424        let server = TestHttpServer::start(TestHttpConfig {
1425            body: Arc::new(body.clone()),
1426            accept_ranges: true,
1427            send_content_length: true,
1428            chunk_size: 4096,
1429            chunk_delay: Duration::ZERO,
1430            path: "multipart.bin".to_string(),
1431        });
1432        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1433        let dir = tempfile::tempdir().unwrap();
1434        let mut request = FetchRequest::new(server.url.clone(), dir.path().join("multipart.bin"));
1435        request.download_options.max_connections = Some(4);
1436        request.download_options.min_segment_size = Some(1024);
1437        request.expected_sha256 = Some(sha256_hex(&body));
1438
1439        let result = client.fetch(request).unwrap();
1440        assert_eq!(result.status, FetchStatus::Downloaded);
1441        assert_eq!(result.sha256, sha256_hex(&body));
1442        assert!(server.range_request_count() >= 2);
1443    }
1444
1445    #[test]
1446    fn fetch_explicit_multipart_urls_concatenates_and_stays_local() {
1447        let daemon = TestDaemon::start();
1448        let part_a = b"hello ".to_vec();
1449        let part_b = b"multipart ".to_vec();
1450        let part_c = b"world".to_vec();
1451        let mut full = Vec::new();
1452        full.extend_from_slice(&part_a);
1453        full.extend_from_slice(&part_b);
1454        full.extend_from_slice(&part_c);
1455
1456        let server_a = TestHttpServer::start(TestHttpConfig {
1457            body: Arc::new(part_a),
1458            accept_ranges: false,
1459            send_content_length: true,
1460            chunk_size: 0,
1461            chunk_delay: Duration::ZERO,
1462            path: "artifact.part-aa".to_string(),
1463        });
1464        let server_b = TestHttpServer::start(TestHttpConfig {
1465            body: Arc::new(part_b),
1466            accept_ranges: false,
1467            send_content_length: true,
1468            chunk_size: 0,
1469            chunk_delay: Duration::ZERO,
1470            path: "artifact.part-ab".to_string(),
1471        });
1472        let server_c = TestHttpServer::start(TestHttpConfig {
1473            body: Arc::new(part_c),
1474            accept_ranges: false,
1475            send_content_length: true,
1476            chunk_size: 0,
1477            chunk_delay: Duration::ZERO,
1478            path: "artifact.part-ac".to_string(),
1479        });
1480
1481        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1482        let dir = tempfile::tempdir().unwrap();
1483        let destination = dir.path().join("artifact.bin");
1484        let mut request = FetchRequest::new(
1485            vec![
1486                server_a.url.clone(),
1487                server_b.url.clone(),
1488                server_c.url.clone(),
1489            ],
1490            &destination,
1491        );
1492        request.expected_sha256 = Some(sha256_hex(&full));
1493
1494        let first = client.fetch(request.clone()).unwrap();
1495        assert_eq!(first.status, FetchStatus::Downloaded);
1496        assert_eq!(first.sha256, sha256_hex(&full));
1497        assert_eq!(fs::read(&destination).unwrap(), full);
1498        let request_counts = (
1499            server_a.request_count(),
1500            server_b.request_count(),
1501            server_c.request_count(),
1502        );
1503
1504        let second = client.fetch(request.clone()).unwrap();
1505        assert_eq!(second.status, FetchStatus::AlreadyPresent);
1506        assert_eq!(
1507            (
1508                server_a.request_count(),
1509                server_b.request_count(),
1510                server_c.request_count()
1511            ),
1512            request_counts
1513        );
1514
1515        let state = client.exists(&request).unwrap();
1516        assert_eq!(state.kind, FetchStateKind::ArtifactReady);
1517        assert_eq!(state.sha256.as_deref(), Some(first.sha256.as_str()));
1518    }
1519
1520    #[test]
1521    fn fetch_no_wait_returns_locked_while_other_client_is_downloading() {
1522        let daemon = TestDaemon::start();
1523        let body: Vec<u8> = (0..512 * 1024).map(|i| (i % 251) as u8).collect();
1524        let server = TestHttpServer::start(TestHttpConfig {
1525            body: Arc::new(body),
1526            accept_ranges: false,
1527            send_content_length: true,
1528            chunk_size: 4096,
1529            chunk_delay: Duration::from_millis(2),
1530            path: "slow.bin".to_string(),
1531        });
1532        let dest_dir = tempfile::tempdir().unwrap();
1533        let destination = dest_dir.path().join("slow.bin");
1534
1535        let endpoint = daemon.endpoint.clone();
1536        let url = server.url.clone();
1537        let destination_for_thread = destination.clone();
1538        let download_thread = thread::spawn(move || {
1539            let client = DownloadClient::new(Some(endpoint));
1540            let request = FetchRequest::new(url, &destination_for_thread);
1541            client.fetch(request).unwrap()
1542        });
1543
1544        let deadline = Instant::now() + Duration::from_secs(5);
1545        while Instant::now() < deadline {
1546            if server.request_count() > 0 {
1547                break;
1548            }
1549            thread::sleep(Duration::from_millis(20));
1550        }
1551
1552        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1553        let mut no_wait = FetchRequest::new(server.url.clone(), &destination);
1554        no_wait.wait_mode = WaitMode::NoWait;
1555        let locked = client.fetch(no_wait).unwrap();
1556        assert_eq!(locked.status, FetchStatus::Locked);
1557
1558        let completed = download_thread.join().unwrap();
1559        assert_eq!(completed.status, FetchStatus::Downloaded);
1560    }
1561
1562    #[test]
1563    fn fetch_multipart_no_wait_returns_locked_while_other_client_is_downloading() {
1564        let daemon = TestDaemon::start();
1565        let slow_server = TestHttpServer::start(TestHttpConfig {
1566            body: Arc::new((0..512 * 1024).map(|i| (i % 251) as u8).collect()),
1567            accept_ranges: false,
1568            send_content_length: true,
1569            chunk_size: 4096,
1570            chunk_delay: Duration::from_millis(2),
1571            path: "slow.part-aa".to_string(),
1572        });
1573        let fast_server = TestHttpServer::start(TestHttpConfig {
1574            body: Arc::new(b"tail".to_vec()),
1575            accept_ranges: false,
1576            send_content_length: true,
1577            chunk_size: 0,
1578            chunk_delay: Duration::ZERO,
1579            path: "slow.part-ab".to_string(),
1580        });
1581        let dest_dir = tempfile::tempdir().unwrap();
1582        let destination = dest_dir.path().join("slow.bin");
1583
1584        let endpoint = daemon.endpoint.clone();
1585        let source = vec![slow_server.url.clone(), fast_server.url.clone()];
1586        let destination_for_thread = destination.clone();
1587        let download_thread = thread::spawn(move || {
1588            let client = DownloadClient::new(Some(endpoint));
1589            let request = FetchRequest::new(source, &destination_for_thread);
1590            client.fetch(request).unwrap()
1591        });
1592
1593        let deadline = Instant::now() + Duration::from_secs(5);
1594        while Instant::now() < deadline {
1595            if slow_server.request_count() > 0 {
1596                break;
1597            }
1598            thread::sleep(Duration::from_millis(20));
1599        }
1600
1601        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1602        let mut no_wait = FetchRequest::new(
1603            vec![slow_server.url.clone(), fast_server.url.clone()],
1604            &destination,
1605        );
1606        no_wait.wait_mode = WaitMode::NoWait;
1607        let locked = client.fetch(no_wait).unwrap();
1608        assert_eq!(locked.status, FetchStatus::Locked);
1609
1610        let completed = download_thread.join().unwrap();
1611        assert_eq!(completed.status, FetchStatus::Downloaded);
1612    }
1613
1614    #[test]
1615    fn fetch_dry_run_avoids_network_and_filesystem_mutation() {
1616        let daemon = TestDaemon::start();
1617        let server = TestHttpServer::start(TestHttpConfig {
1618            body: Arc::new(b"dry-run".to_vec()),
1619            accept_ranges: false,
1620            send_content_length: true,
1621            chunk_size: 0,
1622            chunk_delay: Duration::ZERO,
1623            path: "dry.bin".to_string(),
1624        });
1625        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1626        let dir = tempfile::tempdir().unwrap();
1627        let destination = dir.path().join("dry.bin");
1628        let mut request = FetchRequest::new(server.url.clone(), &destination);
1629        request.dry_run = true;
1630
1631        let result = client.fetch(request).unwrap();
1632        assert_eq!(result.status, FetchStatus::DryRun);
1633        assert_eq!(server.request_count(), 0);
1634        assert!(!destination.exists());
1635    }
1636
1637    #[test]
1638    fn fetch_expands_7z_and_exists_reports_expanded_ready() {
1639        let daemon = TestDaemon::start();
1640        let dir = tempfile::tempdir().unwrap();
1641        let source_dir = dir.path().join("source");
1642        fs::create_dir_all(source_dir.join("bin")).unwrap();
1643        fs::write(source_dir.join("bin").join("tool.txt"), b"tool data").unwrap();
1644        let archive_path = dir.path().join("toolchain.7z");
1645        sevenz_rust::compress_to_path(&source_dir, &archive_path).unwrap();
1646        let archive_bytes = fs::read(&archive_path).unwrap();
1647
1648        let server = TestHttpServer::start(TestHttpConfig {
1649            body: Arc::new(archive_bytes.clone()),
1650            accept_ranges: false,
1651            send_content_length: true,
1652            chunk_size: 0,
1653            chunk_delay: Duration::ZERO,
1654            path: "toolchain.7z".to_string(),
1655        });
1656        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1657        let cache_path = dir.path().join("cache").join("toolchain.7z");
1658        let expanded_path = dir.path().join("expanded");
1659        let mut request = FetchRequest::new(server.url.clone(), &cache_path);
1660        request.destination_path_expanded = Some(expanded_path.clone());
1661        request.expected_sha256 = Some(sha256_hex(&archive_bytes));
1662
1663        let first = client.fetch(request.clone()).unwrap();
1664        assert_eq!(first.status, FetchStatus::Expanded);
1665        assert_eq!(first.sha256, sha256_hex(&archive_bytes));
1666        let extracted = [
1667            expanded_path.join("source").join("bin").join("tool.txt"),
1668            expanded_path.join("bin").join("tool.txt"),
1669            expanded_path.join("tool.txt"),
1670        ]
1671        .into_iter()
1672        .find(|path| path.exists())
1673        .expect("expected extracted file in expanded directory");
1674        assert_eq!(fs::read(extracted).unwrap(), b"tool data");
1675
1676        let state = client.exists(&request).unwrap();
1677        assert_eq!(state.kind, FetchStateKind::ExpandedReady);
1678        assert_eq!(state.sha256.as_deref(), Some(first.sha256.as_str()));
1679
1680        let second = client.fetch(request).unwrap();
1681        assert_eq!(second.status, FetchStatus::AlreadyExpanded);
1682        assert_eq!(second.sha256, first.sha256);
1683    }
1684
1685    #[test]
1686    fn fetch_without_expected_sha_then_validate_later_uses_stored_fingerprint() {
1687        let daemon = TestDaemon::start();
1688        let body = b"artifact with delayed hash".to_vec();
1689        let server = TestHttpServer::start(TestHttpConfig {
1690            body: Arc::new(body.clone()),
1691            accept_ranges: false,
1692            send_content_length: true,
1693            chunk_size: 0,
1694            chunk_delay: Duration::ZERO,
1695            path: "delayed.bin".to_string(),
1696        });
1697        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1698        let dir = tempfile::tempdir().unwrap();
1699        let destination = dir.path().join("delayed.bin");
1700
1701        let first = client
1702            .fetch(FetchRequest::new(server.url.clone(), &destination))
1703            .unwrap();
1704        assert_eq!(first.status, FetchStatus::Downloaded);
1705        assert_eq!(first.sha256, sha256_hex(&body));
1706
1707        let mut later = FetchRequest::new(server.url.clone(), &destination);
1708        later.expected_sha256 = Some(first.sha256.clone());
1709        let second = client.fetch(later.clone()).unwrap();
1710        assert_eq!(second.status, FetchStatus::AlreadyPresent);
1711        assert_eq!(second.sha256, first.sha256);
1712
1713        let state = client.exists(&later).unwrap();
1714        assert_eq!(state.kind, FetchStateKind::ArtifactReady);
1715        assert_eq!(state.sha256.as_deref(), Some(second.sha256.as_str()));
1716    }
1717
1718    #[test]
1719    fn expanded_state_remains_valid_when_expected_sha_is_added_later() {
1720        let daemon = TestDaemon::start();
1721        let dir = tempfile::tempdir().unwrap();
1722        let archive_path = dir.path().join("bundle.zip");
1723        {
1724            let file = File::create(&archive_path).unwrap();
1725            let mut zip = zip::ZipWriter::new(file);
1726            let options = zip::write::SimpleFileOptions::default();
1727            zip.start_file("hello.txt", options).unwrap();
1728            zip.write_all(b"hello").unwrap();
1729            zip.finish().unwrap();
1730        }
1731        let archive_bytes = fs::read(&archive_path).unwrap();
1732        let server = TestHttpServer::start(TestHttpConfig {
1733            body: Arc::new(archive_bytes.clone()),
1734            accept_ranges: false,
1735            send_content_length: true,
1736            chunk_size: 0,
1737            chunk_delay: Duration::ZERO,
1738            path: "bundle.zip".to_string(),
1739        });
1740        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1741        let cache_path = dir.path().join("cache").join("bundle.zip");
1742        let expanded_path = dir.path().join("expanded");
1743
1744        let mut initial = FetchRequest::new(server.url.clone(), &cache_path);
1745        initial.destination_path_expanded = Some(expanded_path.clone());
1746        let first = client.fetch(initial).unwrap();
1747        assert_eq!(first.status, FetchStatus::Expanded);
1748
1749        let mut later = FetchRequest::new(server.url.clone(), &cache_path);
1750        later.destination_path_expanded = Some(expanded_path.clone());
1751        later.expected_sha256 = Some(first.sha256.clone());
1752        let second = client.fetch(later.clone()).unwrap();
1753        assert_eq!(second.status, FetchStatus::AlreadyExpanded);
1754        assert_eq!(second.sha256, first.sha256);
1755
1756        let state = client.exists(&later).unwrap();
1757        assert_eq!(state.kind, FetchStateKind::ExpandedReady);
1758        assert_eq!(state.sha256.as_deref(), Some(second.sha256.as_str()));
1759    }
1760
1761    #[test]
1762    fn force_is_rejected_for_existing_artifact_state() {
1763        let daemon = TestDaemon::start();
1764        let body = b"immutable".to_vec();
1765        let server = TestHttpServer::start(TestHttpConfig {
1766            body: Arc::new(body),
1767            accept_ranges: false,
1768            send_content_length: true,
1769            chunk_size: 0,
1770            chunk_delay: Duration::ZERO,
1771            path: "immutable.bin".to_string(),
1772        });
1773        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1774        let dir = tempfile::tempdir().unwrap();
1775        let destination = dir.path().join("immutable.bin");
1776
1777        let _ = client
1778            .fetch(FetchRequest::new(server.url.clone(), &destination))
1779            .unwrap();
1780
1781        let mut force = FetchRequest::new(server.url.clone(), &destination);
1782        force.force = true;
1783        let err = client.fetch(force).unwrap_err();
1784        assert!(err.contains("purge"));
1785    }
1786
1787    #[test]
1788    fn fetch_rejects_unsafe_zip_entries_end_to_end() {
1789        let daemon = TestDaemon::start();
1790        let dir = tempfile::tempdir().unwrap();
1791        let archive_path = dir.path().join("unsafe.zip");
1792        {
1793            let file = File::create(&archive_path).unwrap();
1794            let mut zip = zip::ZipWriter::new(file);
1795            let options = zip::write::SimpleFileOptions::default();
1796            zip.start_file("../evil.txt", options).unwrap();
1797            zip.write_all(b"bad").unwrap();
1798            zip.finish().unwrap();
1799        }
1800        let archive_bytes = fs::read(&archive_path).unwrap();
1801        let server = TestHttpServer::start(TestHttpConfig {
1802            body: Arc::new(archive_bytes),
1803            accept_ranges: false,
1804            send_content_length: true,
1805            chunk_size: 0,
1806            chunk_delay: Duration::ZERO,
1807            path: "unsafe.zip".to_string(),
1808        });
1809        let client = DownloadClient::new(Some(daemon.endpoint.clone()));
1810        let cache_path = dir.path().join("cache").join("unsafe.zip");
1811        let expanded_path = dir.path().join("expanded");
1812        let mut request = FetchRequest::new(server.url.clone(), &cache_path);
1813        request.destination_path_expanded = Some(expanded_path.clone());
1814
1815        let err = client.fetch(request).unwrap_err();
1816        assert!(err.contains("unsafe zip entry"));
1817        assert!(!dir.path().join("evil.txt").exists());
1818    }
1819}