zoi-install 1.25.2

Advanced Package Manager & Environment Orchestrator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
use std::fmt::Write as _;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::atomic::{AtomicU32, Ordering};

use anyhow::{Result, anyhow};
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use zoi_core::{cache, types, utils};
use zoi_db as db;

use crate::resolver::InstallNode;

/// The number of times to retry a download before giving up.
static DOWNLOAD_RETRY_ATTEMPTS: AtomicU32 = AtomicU32::new(3);

/// Sets the number of download retry attempts.
pub fn set_download_retry_attempts(attempts: u32) {
    let normalized = attempts.max(1);
    DOWNLOAD_RETRY_ATTEMPTS.store(normalized, Ordering::Relaxed);
}

/// Gets the number of download retry attempts.
fn get_download_retry_attempts() -> u32 {
    DOWNLOAD_RETRY_ATTEMPTS.load(Ordering::Relaxed).max(1)
}

/// Sends telemetry for a package installation event.
pub fn send_telemetry(
    event: &str,
    pkg: &types::Package,
    registry_handle: &str,
    install_type: Option<&str>
) {
    match zoi_telemetry::posthog_capture_event(
        event,
        pkg,
        env!("CARGO_PKG_VERSION"),
        registry_handle,
        install_type
    ) {
        Ok(true) => println!("{} telemetry sent", "Info:".green()),
        Ok(false) => (),
        Err(e) => eprintln!("{} telemetry failed: {}", "Warning:".yellow(), e)
    }
}

/// Displays important updates from the package metadata to the user.
///
/// # Errors
///
/// Returns an error if the user aborts the operation after being warned about
/// updates.
pub fn display_updates(pkg: &types::Package, yes: bool) -> Result<bool> {
    if let Some(updates) = &pkg.updates {
        if updates.is_empty() {
            return Ok(true);
        }
        println!("\n{}", "Important Updates:".bold().yellow());
        for update in updates {
            let type_str = match update.update_type {
                types::UpdateType::Change => "Change".blue(),
                types::UpdateType::Vulnerability => {
                    "Vulnerability".red().bold()
                }
                types::UpdateType::Update => "Update".green()
            };
            println!("  - [{}] {}", type_str, update.message);
        }

        if !utils::ask_for_confirmation("\nDo you want to continue?", yes) {
            return Ok(false);
        }
    }
    Ok(true)
}

/// Extracts the filename from a URL.
pub fn get_filename_from_url(url: &str) -> &str {
    url.split('/').next_back().unwrap_or_default()
}

/// Fetches the text content from a list of candidate URLs.
///
/// # Errors
///
/// Returns an error if the text content cannot be fetched from any of the
/// candidate URLs.
pub fn get_text_from_candidate_urls(
    urls: &[String],
    resource_name: &str
) -> Result<String> {
    let client = zoi_core::utils::get_http_client()?;
    let mut last_error = None;

    for candidate_url in urls {
        match client.get(candidate_url).send() {
            Ok(response) => match response.text() {
                Ok(text) => return Ok(text),
                Err(e) => last_error = Some(format!("{candidate_url} ({e})"))
            },
            Err(e) => last_error = Some(format!("{candidate_url} ({e})"))
        }
    }

    Err(anyhow!(
        "Failed to fetch {} from any configured source: {}",
        resource_name,
        last_error
            .unwrap_or_else(|| "no candidate URLs were attempted".to_string())
    ))
}

/// Downloads a file from a URL with progress reporting.
///
/// # Errors
///
/// Returns an error if the download fails after all retry attempts,
/// if the destination file cannot be created, or if the progress bar fails.
pub fn download_file_with_progress(
    url: &str,
    dest_path: &Path,
    pb_override: Option<&ProgressBar>,
    expected_size: Option<u64>
) -> Result<()> {
    if url.starts_with("http://") {
        let msg = format!("downloading over insecure HTTP: {url}");
        if pb_override.is_none() {
            println!("{}: {}", "Warning:".yellow(), msg);
        }
    }

    let pb_style = ProgressStyle::default_bar()
        .template(
            "{spinner:.green} {msg:30.cyan.bold} [{bar:40.cyan/blue}] \
             {bytes}/{total_bytes} ({bytes_per_sec}, {elapsed_precise})"
        )?
        .progress_chars("=>-");

    let mut internal_pb = None;
    let pb = if let Some(p) = pb_override {
        p.set_style(pb_style.clone());
        p.set_length(expected_size.unwrap_or(0));
        p.set_message(format!("Downloading {}", get_filename_from_url(url)));
        p
    } else {
        let p = ProgressBar::new(expected_size.unwrap_or(0));
        p.set_style(pb_style);
        p.set_message(format!("Downloading {}", get_filename_from_url(url)));
        internal_pb = Some(p);
        internal_pb.as_ref().ok_or_else(|| {
            anyhow!("internal_pb should be set if not using pb_override")
        })?
    };

    let client = zoi_core::utils::get_http_client()?;
    let mut attempt = 0u32;

    let mut partial_size = 0;
    if dest_path.exists() {
        partial_size = dest_path.metadata()?.len();
    }

    let mut request = client.get(url);
    if partial_size > 0 {
        let msg = format!("Resuming download from byte {partial_size}");
        pb.set_message(msg);
        request = request.header("Range", format!("bytes={partial_size}-"));
    }

    let max_attempts = get_download_retry_attempts();
    let response = loop {
        attempt += 1;
        match request
            .try_clone()
            .ok_or_else(|| anyhow!("Failed to clone request"))?
            .send()
        {
            Ok(resp) => break resp,
            Err(e) => {
                if attempt < max_attempts {
                    let msg = format!("Download failed ({e}). Retrying...");
                    pb.set_message(msg);
                    zoi_core::utils::retry_backoff_sleep(attempt);
                    continue;
                }
                return Err(anyhow!(
                    "Failed to download '{url}' after {attempt} attempts: {e}"
                ));
            }
        }
    };

    let mut is_resumed = false;
    if response.status() == reqwest::StatusCode::PARTIAL_CONTENT {
        is_resumed = true;
    } else if response.status().is_success() {
        partial_size = 0;
    } else {
        return Err(anyhow!(
            "Failed to download (HTTP {}): {}",
            response.status(),
            url
        ));
    }

    let total_size = if let Some(s) = expected_size {
        s
    } else {
        partial_size + response.content_length().unwrap_or(0)
    };

    pb.set_length(total_size);
    pb.set_position(partial_size);
    pb.set_message(format!("Downloading {}", get_filename_from_url(url)));

    let mut dest_file = if is_resumed {
        std::fs::OpenOptions::new().append(true).open(dest_path)?
    } else {
        File::create(dest_path)?
    };

    let mut stream = response;
    let mut buffer = [0; 8192];
    loop {
        let bytes_read = stream.read(&mut buffer)?;
        if bytes_read == 0 {
            break;
        }
        dest_file.write_all(
            buffer
                .get(..bytes_read)
                .ok_or_else(|| anyhow!("buffer slice out of bounds"))?
        )?;
        pb.inc(bytes_read as u64);
    }

    if let Some(p) = internal_pb {
        p.finish_and_clear();
        println!("Downloaded {}", get_filename_from_url(url));
    }
    Ok(())
}

/// Verifies the hash of a file against an expected hash.
///
/// # Errors
///
/// Returns an error if the hash algorithm is unsupported or if the hash
/// calculation fails.
pub fn verify_file_hash(
    file_path: &Path,
    expected_hash: &str,
    pb: Option<&ProgressBar>
) -> Result<bool> {
    let expected_clean = expected_hash.trim().to_lowercase();
    let Some(algo) =
        zoi_core::hash::HashAlgorithm::from_len(expected_clean.len())
    else {
        return Err(anyhow!(
            "Unsupported hash length: {}. Expected 128 (SHA-512) or 64 \
             (SHA-256).",
            expected_clean.len()
        ));
    };

    let actual_hash = zoi_core::hash::calculate_file_hash(file_path, algo)?;
    let actual_clean = actual_hash.trim().to_lowercase();

    let result = actual_clean == expected_clean;
    if result {
        let msg = format!(
            "{} Hash verified: {}",
            "::".bold().blue(),
            expected_clean[..12].dimmed()
        );
        if let Some(p) = pb {
            p.println(msg);
        } else {
            println!("{msg}");
        }
    } else {
        let mut msg = format!("{}\n", "Hash verification failed!".red().bold());
        let _ = writeln!(msg, "  Expected: {}", expected_clean.yellow());
        let _ = writeln!(msg, "  Actual:   {}", actual_clean.cyan());
        let _ = write!(
            msg,
            "  Lengths:  Expected={}, Actual={}",
            expected_clean.len(),
            actual_clean.len()
        );

        if let Some(p) = pb {
            p.println(msg);
        } else {
            println!("{msg}");
        }
    }
    Ok(result)
}

/// Fetches a list of files from a remote registry.
///
/// # Errors
///
/// Returns an error if the list of files cannot be fetched from the remote
/// registry.
pub fn get_remote_file_list(url: &str) -> Result<Vec<String>> {
    if zoi_core::offline::is_offline() {
        return Ok(Vec::new());
    }
    let resp = get_text_from_candidate_urls(
        &cache::mirror_candidate_urls(url),
        "files list"
    )?;

    Ok(resp
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect())
}

/// Fetches the expected hash for a file from a remote source.
///
/// # Errors
///
/// Returns an error if the expected hash cannot be fetched from the remote
/// source.
pub fn get_expected_hash(
    hash_url: &str,
    filename: Option<&str>
) -> Result<String> {
    if zoi_core::offline::is_offline() {
        return Ok(String::new());
    }
    let resp = get_text_from_candidate_urls(
        &cache::mirror_candidate_urls(hash_url),
        "hash file"
    )?;

    let is_valid_hash = |s: &str| {
        let len = s.len();
        (len == 128 || len == 64 || len == 32)
            && s.chars().all(|c| c.is_ascii_hexdigit())
    };

    if let Some(target_file) = filename {
        for line in resp.lines() {
            if line.contains(target_file) {
                let parts: Vec<&str> = line.split_whitespace().collect();
                if let Some(hash) = parts.iter().find(|&&p| is_valid_hash(p)) {
                    return Ok(hash.to_string());
                }
            }
        }
    }

    for word in resp.split_whitespace() {
        if is_valid_hash(word) {
            return Ok(word.to_string());
        }
    }

    Ok(resp
        .split_whitespace()
        .next()
        .unwrap_or_default()
        .to_string())
}

/// Fetches the expected download and installed sizes for a package.
///
/// # Errors
///
/// Returns an error if the expected size cannot be fetched from the remote
/// source.
pub fn get_expected_size(size_url: &str) -> Result<(u64, u64)> {
    if zoi_core::offline::is_offline() {
        return Ok((0, 0));
    }
    let resp = get_text_from_candidate_urls(
        &cache::mirror_candidate_urls(size_url),
        "size file"
    )?;

    let mut download_size = 0;
    let mut installed_size = 0;
    let mut found_fields = false;

    for line in resp.lines() {
        if let Some((key, val)) = line.split_once(':')
            && let Ok(num) = val.trim().parse::<u64>()
        {
            match key.trim() {
                "down" => {
                    download_size = num;
                    found_fields = true;
                }
                "install" => {
                    installed_size = num;
                    found_fields = true;
                }
                _ => {}
            }
        }
    }

    if !found_fields && let Ok(num) = resp.trim().parse::<u64>() {
        download_size = num;
    }

    Ok((download_size, installed_size))
}

/// Resolves placeholders in a URL with actual values.
pub fn resolve_url_placeholders(
    url: &str,
    pkg_name: &str,
    repo: &str,
    version: &str,
    platform: &str
) -> String {
    let (os, arch) = (
        platform.split('-').next().unwrap_or_default(),
        platform.split('-').nth(1).unwrap_or_default()
    );

    let id = if repo.is_empty() {
        pkg_name.to_string()
    } else {
        format!("{}.{}", repo.replace('/', "."), pkg_name)
    };

    url.replace("{os}", os)
        .replace("{arch}", arch)
        .replace("{version}", version)
        .replace("{repo}", repo)
        .replace("{name}", pkg_name)
        .replace("{id}", &id)
        .replace("{platform}", platform)
}

/// Finds prebuilt info for a package in the registry.
///
/// # Errors
///
/// Returns an error if the database root or repository configuration cannot be
/// read.
pub fn find_registry_info_for_package(
    pkg: &types::Package,
    registry_handle: &str,
    version: &str,
    is_source: bool
) -> Result<Option<types::PrebuiltInfo>> {
    let platform = zoi_core::utils::get_platform()?;

    let repo_config =
        if zoi_core::utils::is_mini_mode() && registry_handle == "zoidberg" {
            zoi_resolver::mini_resolve::fetch_registry_config().ok()
        } else {
            let db_path = zoi_resolver::resolve::get_db_root()?;
            let repo_db_path = db_path.join(registry_handle);
            zoi_core::config::read_repo_config(&repo_db_path).ok()
        };

    if let Some(repo_config) = repo_config {
        let main_type = if is_source { "source-main" } else { "main" };
        let mirror_type = if is_source { "source-mirror" } else { "mirror" };
        let extension = if is_source { ".zsa" } else { ".zpa" };

        let mut pkg_links_to_try = Vec::new();
        if let Some(main_pkg) =
            repo_config.pkg.iter().find(|p| p.link_type == main_type)
        {
            pkg_links_to_try.push(main_pkg.clone());
        }
        pkg_links_to_try.extend(
            repo_config
                .pkg
                .iter()
                .filter(|p| p.link_type == mirror_type)
                .cloned()
        );

        if let Some(pkg_link) = pkg_links_to_try.into_iter().next() {
            let final_url_base = resolve_url_placeholders(
                &pkg_link.url,
                &pkg.name,
                &pkg.repo,
                version,
                &platform
            );
            let final_url = if final_url_base.ends_with(extension) {
                final_url_base
            } else {
                let archive_filename = if is_source {
                    format!("{}-{}{}", pkg.name, version, extension)
                } else {
                    format!(
                        "{}-{}-{}{}",
                        pkg.name, version, platform, extension
                    )
                };
                format!(
                    "{}/{}",
                    final_url_base.trim_end_matches('/'),
                    archive_filename
                )
            };

            let pgp_url = Some(pkg_link.pgp.as_ref().map_or_else(
                || format!("{final_url}.sig"),
                |url| {
                    resolve_url_placeholders(
                        url, &pkg.name, &pkg.repo, version, &platform
                    )
                }
            ));
            let hash_url = pkg_link.hash.as_ref().map(|url| {
                resolve_url_placeholders(
                    url, &pkg.name, &pkg.repo, version, &platform
                )
            });
            let size_url = pkg_link.size.as_ref().map(|url| {
                resolve_url_placeholders(
                    url, &pkg.name, &pkg.repo, version, &platform
                )
            });
            let files_url = pkg_link.files.as_ref().map(|url| {
                resolve_url_placeholders(
                    url, &pkg.name, &pkg.repo, version, &platform
                )
            });

            return Ok(Some(types::PrebuiltInfo {
                final_url,
                pgp_url,
                hash_url,
                size_url,
                files_url
            }));
        }
    }

    Ok(None)
}

/// Finds prebuilt info for a prebuilt package.
///
/// # Errors
///
/// Returns an error if the registry information cannot be retrieved.
pub fn find_prebuilt_info_for_package(
    pkg: &types::Package,
    registry_handle: &str,
    version: &str
) -> Result<Option<types::PrebuiltInfo>> {
    find_registry_info_for_package(pkg, registry_handle, version, false)
}

/// Finds info for a source bundle package.
///
/// # Errors
///
/// Returns an error if the registry information cannot be retrieved.
pub fn find_source_bundle_info_for_package(
    pkg: &types::Package,
    registry_handle: &str,
    version: &str
) -> Result<Option<types::PrebuiltInfo>> {
    find_registry_info_for_package(pkg, registry_handle, version, true)
}

/// Finds prebuilt info for an install node.
///
/// # Errors
///
/// Returns an error if the registry information cannot be retrieved.
pub fn find_prebuilt_info(
    node: &InstallNode
) -> Result<Option<types::PrebuiltInfo>> {
    find_prebuilt_info_for_package(
        &node.pkg,
        &node.registry_handle,
        &node.version
    )
}

/// Finds source bundle info for an install node.
///
/// # Errors
///
/// Returns an error if the registry information cannot be retrieved.
pub fn find_source_bundle_info(
    node: &InstallNode
) -> Result<Option<types::PrebuiltInfo>> {
    find_source_bundle_info_for_package(
        &node.pkg,
        &node.registry_handle,
        &node.version
    )
}

/// Gets the expected download and installed sizes for a package.
pub fn get_package_sizes(
    pkg: &types::Package,
    registry_handle: &str,
    version: &str
) -> (u64, u64) {
    let download_size = pkg.archive_size.unwrap_or(0);
    let installed_size = pkg.installed_size.unwrap_or(0);

    if download_size > 0 && installed_size > 0 {
        return (download_size, installed_size);
    }

    if let Ok(Some((db_down, db_inst))) = db::get_package_sizes_from_db(
        registry_handle,
        &pkg.name,
        pkg.sub_package.as_deref()
    ) {
        return (db_down, db_inst);
    }

    match find_prebuilt_info_for_package(pkg, registry_handle, version) {
        Ok(Some(info)) => {
            if let Some(size_url) = &info.size_url {
                if zoi_core::offline::is_offline() {
                    (download_size, installed_size)
                } else {
                    get_expected_size(size_url).unwrap_or_else(|e| {
                        eprintln!(
                            "Warning: could not fetch size for {}: {}. \
                             Falling back to metadata.",
                            pkg.name, e
                        );
                        (download_size, installed_size)
                    })
                }
            } else {
                (download_size, installed_size)
            }
        }
        _ => (download_size, installed_size)
    }
}

#[cfg(test)]
mod tests {
    use super::{get_download_retry_attempts, set_download_retry_attempts};

    #[test]
    fn download_retry_attempts_are_clamped_to_minimum_one() {
        let previous = get_download_retry_attempts();
        set_download_retry_attempts(0);
        assert_eq!(get_download_retry_attempts(), 1);
        set_download_retry_attempts(previous);
    }

    #[test]
    fn download_retry_attempts_accept_positive_values() {
        let previous = get_download_retry_attempts();
        set_download_retry_attempts(7);
        assert_eq!(get_download_retry_attempts(), 7);
        set_download_retry_attempts(previous);
    }
}