zv 0.9.2

Ziglang Version Manager and Project Starter
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
use crate::{
    ZvError,
    tools::canonicalize,
    types::{ArchiveExt, Shim},
};
use color_eyre::eyre::eyre;
use indicatif::{ProgressBar, ProgressStyle};
use same_file::Handle;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::time::Duration;

/// Checks if a file is a valid zv shim by comparing it with the current executable
fn is_zv_shim(shim_path: &Path, current_exe_handle: &Handle) -> bool {
    // First check for hard links using same-file crate
    if let Ok(shim_handle) = Handle::from_path(shim_path)
        && shim_handle == *current_exe_handle
    {
        tracing::debug!("Found ZV shim (hard link) at {:?}", shim_path);
        return true;
    }

    // Check for symlinks
    if shim_path.is_symlink()
        && let Ok(target) = std::fs::read_link(shim_path)
    {
        // Handle both absolute and relative symlink targets
        let resolved_target = if target.is_absolute() {
            canonicalize(&target)
        } else {
            // For relative symlinks, resolve relative to the symlink's parent directory
            if let Some(parent) = shim_path.parent() {
                canonicalize(parent.join(&target))
            } else {
                canonicalize(&target)
            }
        };

        if let Ok(resolved_target) = resolved_target {
            // Compare the resolved target with current exe using same-file
            if let Ok(target_handle) = Handle::from_path(&resolved_target)
                && target_handle == *current_exe_handle
            {
                tracing::debug!("Found ZV shim (symlink) at {:?}", shim_path);
                return true;
            }
        }
    }

    false
}

/// Detect and validate ZV shim in the bin directory
/// Returns the canonicalized path if a valid ZV shim is found
pub fn detect_shim(bin_path: &Path, shim: Shim) -> Option<PathBuf> {
    let shim_file = bin_path.join(shim.executable_name());

    // Basic existence and file type check
    if !shim_file.is_file() {
        return None;
    }

    // Get current executable handle for comparison
    let current_exe_path = match std::env::current_exe() {
        Ok(path) => path,
        Err(e) => {
            tracing::warn!("Failed to get current executable path: {}", e);
            return None;
        }
    };

    let current_exe_handle = match Handle::from_path(&current_exe_path) {
        Ok(handle) => handle,
        Err(e) => {
            tracing::warn!("Failed to create handle for current executable: {}", e);
            return None;
        }
    };

    #[cfg(unix)]
    {
        // On Unix, also check if the file is executable
        if let Ok(metadata) = std::fs::metadata(&shim_file) {
            use std::os::unix::fs::PermissionsExt;
            if metadata.permissions().mode() & 0o111 == 0 {
                tracing::debug!(
                    "File {} exists but is not executable",
                    shim.executable_name()
                );
                return None;
            }
        }
    }

    // Check if this is actually a zv shim
    if is_zv_shim(&shim_file, &current_exe_handle) {
        canonicalize(&shim_file).ok()
    } else {
        tracing::debug!(
            "File {} exists but is not a zv shim at {:?}",
            shim.executable_name(),
            shim_file
        );
        None
    }
}

/// Construct the zig tarball name based on HOST arch, os. zig 0.14.1 onwards, the naming convention changed
/// to {arch}-{os}-{version}
pub fn zig_tarball(
    semver_version: &semver::Version,
    extension: Option<ArchiveExt>,
) -> Option<String> {
    use target_lexicon::HOST;
    let arch = match HOST.architecture {
        target_lexicon::Architecture::X86_64 => "x86_64",
        target_lexicon::Architecture::Aarch64(_) => "aarch64",
        target_lexicon::Architecture::X86_32(_) => "x86",
        target_lexicon::Architecture::Arm(_) => "arm",
        target_lexicon::Architecture::Riscv64(_) => "riscv64",
        target_lexicon::Architecture::Powerpc64 => "powerpc64",
        target_lexicon::Architecture::Powerpc64le => "powerpc64le",
        target_lexicon::Architecture::S390x => "s390x",
        target_lexicon::Architecture::LoongArch64 => "loongarch64",
        _ => return None,
    };

    let os = match HOST.operating_system {
        target_lexicon::OperatingSystem::Linux => "linux",
        target_lexicon::OperatingSystem::Darwin(_) => "macos",
        target_lexicon::OperatingSystem::Windows => "windows",
        target_lexicon::OperatingSystem::Freebsd => "freebsd",
        target_lexicon::OperatingSystem::Netbsd => "netbsd",
        _ => return None,
    };
    let ext = if let Some(ext) = extension {
        ext
    } else if HOST.operating_system == target_lexicon::OperatingSystem::Windows {
        ArchiveExt::Zip
    } else {
        ArchiveExt::TarXz
    };
    if semver_version.le(&semver::Version::new(0, 14, 0)) {
        Some(format!("zig-{os}-{arch}-{semver_version}.{ext}"))
    } else {
        Some(format!("zig-{arch}-{os}-{semver_version}.{ext}"))
    }
}

/// Returns the host target string in the format used by Zig releases
pub fn host_target() -> Option<String> {
    use target_lexicon::HOST;

    let arch = match HOST.architecture {
        target_lexicon::Architecture::X86_64 => "x86_64",
        target_lexicon::Architecture::Aarch64(_) => "aarch64",
        target_lexicon::Architecture::X86_32(_) => "x86",
        target_lexicon::Architecture::Arm(_) => "arm",
        target_lexicon::Architecture::Riscv64(_) => "riscv64",
        target_lexicon::Architecture::Powerpc64 => "powerpc64",
        target_lexicon::Architecture::Powerpc64le => "powerpc64le",
        target_lexicon::Architecture::S390x => "s390x",
        target_lexicon::Architecture::LoongArch64 => "loongarch64",
        _ => return None,
    };

    let os = match HOST.operating_system {
        target_lexicon::OperatingSystem::Linux => "linux",
        target_lexicon::OperatingSystem::Darwin(_) => "macos",
        target_lexicon::OperatingSystem::Windows => "windows",
        target_lexicon::OperatingSystem::Freebsd => "freebsd",
        target_lexicon::OperatingSystem::Netbsd => "netbsd",
        _ => return None,
    };

    Some(format!("{arch}-{os}"))
}

/// User-Agent string for network requests
pub const fn zv_agent() -> &'static str {
    concat!("zv-cli/", env!("CARGO_PKG_VERSION"))
}

/// Messages that can be sent to the progress bar actor
#[derive(Debug, Clone)]
pub enum ProgressMessage {
    Start { message: String },
    Update { message: String },
    Finish { message: String },
    FinishWithError { message: String },
    Shutdown,
}

/// Progress bar actor that runs in its own thread
struct ProgressActor {
    rx: tokio::sync::mpsc::Receiver<ProgressMessage>,
}

impl ProgressActor {
    fn run(mut self) {
        let mut spinner: Option<ProgressBar> = None;

        while let Some(msg) = self.rx.blocking_recv() {
            match msg {
                ProgressMessage::Start { message } => {
                    let pb = ProgressBar::new_spinner();
                    pb.set_style(
                        ProgressStyle::default_spinner()
                            .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ ")
                            .template("{spinner:.blue} {msg}")
                            .unwrap(),
                    );
                    pb.set_message(message);
                    pb.enable_steady_tick(Duration::from_millis(120));
                    spinner = Some(pb);
                }
                ProgressMessage::Update { message } => {
                    if let Some(ref pb) = spinner {
                        pb.set_message(message);
                    }
                }
                ProgressMessage::Finish { message } => {
                    if let Some(pb) = spinner.take() {
                        pb.finish_with_message(message);
                    }
                }
                ProgressMessage::FinishWithError { message } => {
                    if let Some(pb) = spinner.take() {
                        pb.finish_with_message(message);
                    }
                }
                ProgressMessage::Shutdown => {
                    if let Some(pb) = spinner.take() {
                        pb.finish_and_clear();
                    }
                    break;
                }
            }
        }
    }
}

/// Handle to a progress bar actor with automatic cleanup
pub struct ProgressHandle {
    tx: tokio::sync::mpsc::Sender<ProgressMessage>,
    handle: Option<std::thread::JoinHandle<()>>,
}

impl ProgressHandle {
    /// Spawn a new progress bar actor in its own thread
    pub fn spawn() -> Self {
        let (tx, rx) = tokio::sync::mpsc::channel(32);

        let handle = std::thread::spawn(move || {
            let actor = ProgressActor { rx };
            actor.run();
        });

        Self {
            tx,
            handle: Some(handle),
        }
    }

    /// Send a message to the progress bar actor
    pub async fn send(
        &self,
        msg: ProgressMessage,
    ) -> Result<(), tokio::sync::mpsc::error::SendError<ProgressMessage>> {
        self.tx.send(msg).await
    }

    /// Start the progress bar with a message
    pub async fn start(
        &self,
        message: impl Into<String>,
    ) -> Result<(), tokio::sync::mpsc::error::SendError<ProgressMessage>> {
        self.send(ProgressMessage::Start {
            message: message.into(),
        })
        .await
    }

    /// Update the progress bar message
    pub async fn update(
        &self,
        message: impl Into<String>,
    ) -> Result<(), tokio::sync::mpsc::error::SendError<ProgressMessage>> {
        self.send(ProgressMessage::Update {
            message: message.into(),
        })
        .await
    }

    /// Finish the progress bar with a success message
    pub async fn finish(
        &self,
        message: impl Into<String>,
    ) -> Result<(), tokio::sync::mpsc::error::SendError<ProgressMessage>> {
        self.send(ProgressMessage::Finish {
            message: message.into(),
        })
        .await
    }

    /// Finish the progress bar with an error message
    pub async fn finish_with_error(
        &self,
        message: impl Into<String>,
    ) -> Result<(), tokio::sync::mpsc::error::SendError<ProgressMessage>> {
        self.send(ProgressMessage::FinishWithError {
            message: message.into(),
        })
        .await
    }

    /// Manually shutdown the progress bar (usually not needed due to Drop)
    #[allow(unused)]
    pub async fn shutdown(mut self) -> Result<(), Box<dyn std::error::Error>> {
        self.tx.send(ProgressMessage::Shutdown).await?;

        if let Some(handle) = self.handle.take() {
            handle
                .join()
                .map_err(|_| "Failed to join progress thread")?;
        }

        Ok(())
    }
}

impl Drop for ProgressHandle {
    fn drop(&mut self) {
        // Send shutdown message (ignore errors as channel might be closed)
        let _ = self.tx.try_send(ProgressMessage::Shutdown);

        if let Some(handle) = self.handle.take() {
            let _ = handle.join();
            tracing::debug!(target: "app::util", "Dropped ProgessHandle");
        }
    }
}

/// Removes all files in the provided slice of paths.
/// Skips files that don't exist and logs any deletion errors
pub async fn remove_files(paths: &[impl AsRef<Path>]) {
    for path in paths {
        let path_ref = path.as_ref();
        const TARGET: &str = "zv::utils::remove_files";
        // Check if file exists before attempting to remove
        match tokio::fs::metadata(path_ref).await {
            Ok(metadata) => {
                // File exists, attempt to remove it
                if metadata.is_file()
                    && let Err(e) = tokio::fs::remove_file(path_ref).await
                {
                    // Only log error if it's not a "file not found" error
                    // (in case file was deleted between metadata check and removal)
                    if e.kind() != std::io::ErrorKind::NotFound {
                        tracing::debug!(
                            target: TARGET,
                            "Failed to remove file '{}': {}",
                            path_ref.display(),
                            e
                        );
                    }
                }
            }
            Err(e) => {
                // If error is "not found", skip this file
                if e.kind() != std::io::ErrorKind::NotFound {
                    // For other metadata errors (permissions, etc.), log the error
                    tracing::debug!(
                        target: TARGET,
                        "Failed to access file '{}': {}",
                        path_ref.display(),
                        e
                    );
                }
                // File doesn't exist, skip it
            }
        }
    }
}

/// Verify SHA-256 checksum of a file
///
/// Reads the file and computes its SHA-256 hash, comparing it with the expected checksum.
/// Returns an error if the checksums don't match or if file reading fails.
/// Enhanced with comprehensive error handling and detailed logging for debugging.
pub(crate) async fn verify_checksum(
    file_path: &Path,
    expected_shasum: &str,
) -> Result<(), ZvError> {
    use tokio::io::AsyncReadExt;
    const TARGET: &str = "zv::utils::verify_checksum";
    tracing::debug!(target: TARGET, "Starting checksum verification for file: {}", file_path.display());
    tracing::debug!(target: TARGET, "Expected SHA-256: {}", expected_shasum);

    // Validate input parameters
    if expected_shasum.is_empty() {
        let error_msg = "Expected checksum is empty - cannot verify file integrity";
        tracing::error!(target: TARGET, "{}", error_msg);
        return Err(ZvError::General(eyre!(error_msg)));
    }

    if expected_shasum.len() != 64 {
        let error_msg = format!(
            "Expected checksum has invalid length {} (should be 64 hex characters for SHA-256): {}",
            expected_shasum.len(),
            expected_shasum
        );
        tracing::error!(target: TARGET, "{}", error_msg);
        return Err(ZvError::General(eyre!(error_msg)));
    }

    // Validate that expected checksum contains only hex characters
    if !expected_shasum.chars().all(|c| c.is_ascii_hexdigit()) {
        let error_msg = format!(
            "Expected checksum contains non-hexadecimal characters: {}",
            expected_shasum
        );
        tracing::error!(target: TARGET, "{}", error_msg);
        return Err(ZvError::General(eyre!(error_msg)));
    }

    // Check if file exists and get metadata
    let file_metadata = match tokio::fs::metadata(file_path).await {
        Ok(metadata) => {
            let file_size = metadata.len();
            tracing::debug!(target: TARGET, "File size: {} bytes ({:.1} MB)", file_size, file_size as f64 / 1_048_576.0);

            if file_size == 0 {
                tracing::warn!(target: TARGET, "File is empty - this may indicate a download failure");
            }

            metadata
        }
        Err(e) => {
            let error_msg = format!(
                "Failed to read file metadata for checksum verification: {}",
                file_path.display()
            );
            tracing::error!(target: TARGET, "{}: {}", error_msg, e);
            return Err(ZvError::General(eyre!("{}: {}", error_msg, e)));
        }
    };

    // Open the file for reading
    let mut file = match tokio::fs::File::open(file_path).await {
        Ok(file) => {
            tracing::debug!(target: TARGET, "Successfully opened file for checksum verification");
            file
        }
        Err(e) => {
            let error_msg = format!(
                "Failed to open file for checksum verification: {}",
                file_path.display()
            );
            tracing::error!(target: TARGET, "{}: {}", error_msg, e);

            // Provide specific error context
            match e.kind() {
                std::io::ErrorKind::NotFound => {
                    tracing::error!(target: TARGET, "File not found - it may have been deleted or moved during verification");
                }
                std::io::ErrorKind::PermissionDenied => {
                    tracing::error!(target: TARGET, "Permission denied - check file read permissions");
                }
                _ => {
                    tracing::error!(target: TARGET, "Unexpected I/O error opening file: {:?}", e.kind());
                }
            }

            return Err(ZvError::Io(e));
        }
    };

    // Create SHA-256 hasher
    let mut hasher = <Sha256 as Digest>::new();
    let mut buffer = [0u8; 8192]; // 8KB buffer for efficient reading
    let mut total_bytes_read = 0u64;
    let file_size = file_metadata.len();

    tracing::debug!(target: TARGET, "Starting SHA-256 computation with 8KB buffer");

    // Read file in chunks and update hasher
    loop {
        let bytes_read = match file.read(&mut buffer).await {
            Ok(bytes) => bytes,
            Err(e) => {
                let error_msg = format!(
                    "Failed to read file during checksum verification: {}",
                    file_path.display()
                );
                tracing::error!(target: TARGET, "{}: {}", error_msg, e);

                // Provide specific error context
                match e.kind() {
                    std::io::ErrorKind::UnexpectedEof => {
                        tracing::error!(target: TARGET, "Unexpected end of file - file may be truncated or corrupted");
                    }
                    std::io::ErrorKind::Interrupted => {
                        tracing::warn!(target: TARGET, "Read operation interrupted - this is usually recoverable");
                        continue; // Retry the read operation
                    }
                    _ => {
                        tracing::error!(target: TARGET, "Unexpected I/O error during file read: {:?}", e.kind());
                    }
                }

                return Err(ZvError::Io(e));
            }
        };

        if bytes_read == 0 {
            tracing::debug!(target: TARGET, "Reached end of file after reading {} bytes", total_bytes_read);
            break; // End of file
        }

        hasher.update(&buffer[..bytes_read]);
        total_bytes_read += bytes_read as u64;
    }

    // Verify we read the expected amount of data
    if total_bytes_read != file_size {
        let error_msg = format!(
            "File size mismatch during checksum verification: expected {} bytes, read {} bytes",
            file_size, total_bytes_read
        );
        tracing::error!(target: TARGET, "{}", error_msg);
        return Err(ZvError::General(eyre!(error_msg)));
    }

    // Finalize hash and convert to hex string
    let computed_hash = hasher.finalize();
    let computed_hex = format!("{:x}", computed_hash);

    tracing::debug!(target: TARGET, "Computed SHA-256: {}", computed_hex);
    tracing::debug!(target: TARGET, "Checksum computation completed for {} bytes", total_bytes_read);

    // Compare with expected checksum (case-insensitive)
    if computed_hex.eq_ignore_ascii_case(expected_shasum) {
        tracing::trace!(target: TARGET, "Checksum verification successful for file: {} ({:.1} MB)", 
                      file_path.display(), total_bytes_read as f64 / 1_048_576.0);
        Ok(())
    } else {
        let error_msg = format!(
            "Checksum verification failed for file: {}\nFile size: {} bytes ({:.1} MB)\nExpected SHA-256: {}\nComputed SHA-256: {}\nThis indicates file corruption or an incorrect expected checksum",
            file_path.display(),
            total_bytes_read,
            total_bytes_read as f64 / 1_048_576.0,
            expected_shasum,
            computed_hex
        );
        tracing::error!(target: TARGET, "CHECKSUM MISMATCH: {}", error_msg);

        // Additional debugging information
        tracing::error!(target: TARGET, "Checksum verification details:");
        tracing::error!(target: TARGET, "  File: {}", file_path.display());
        tracing::error!(target: TARGET, "  Size: {} bytes", total_bytes_read);
        tracing::error!(target: TARGET, "  Expected: {}", expected_shasum);
        tracing::error!(target: TARGET, "  Computed: {}", computed_hex);
        tracing::error!(target: TARGET, "  This may indicate network corruption, storage issues, or incorrect metadata");

        Err(ZvError::General(eyre!(error_msg)))
    }
}