msvc-kit 0.2.10

A portable MSVC Build Tools installer and manager for Rust development
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
//! File extraction utilities for VSIX, MSI, and CAB files

use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::Mutex;
use std::time::Duration;

use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};

use crate::constants::{extraction as ext_const, progress as progress_const};
use crate::error::{MsvcKitError, Result};

/// Global mutex for MSI extraction.
/// Windows Installer (msiexec) can only run one instance at a time globally.
/// Error 1618 = "Another installation is already in progress"
static MSI_EXTRACT_LOCK: Mutex<()> = Mutex::new(());

/// Maximum retries for MSI extraction when encountering error 1618
const MSI_MAX_RETRIES: u32 = 5;
/// Delay between retries in milliseconds
const MSI_RETRY_DELAY_MS: u64 = 2000;

pub(crate) fn inner_progress_enabled() -> bool {
    matches!(
        env::var("MSVC_KIT_INNER_PROGRESS")
            .unwrap_or_default()
            .to_ascii_lowercase()
            .as_str(),
        "1" | "true" | "yes" | "on"
    )
}

pub(crate) fn progress_style_bytes() -> ProgressStyle {
    ProgressStyle::default_bar()
        .template("{spinner:.green} [{elapsed_precise}] {wide_bar:.cyan/blue} {bytes}/{total_bytes} @ {bytes_per_sec} ETA {eta} | {msg}")
        .unwrap()
        .progress_chars("##-")
}

pub(crate) fn progress_style_items() -> ProgressStyle {
    ProgressStyle::default_bar()
        .template(
            "{spinner:.green} [{elapsed_precise}] {wide_bar:.cyan/blue} {pos}/{len} files | {msg}",
        )
        .unwrap()
        .progress_chars("##-")
}

/// Extract a VSIX file (which is a ZIP archive) with optional progress bar
pub(crate) async fn extract_vsix_with_progress(
    vsix_path: &Path,
    target_dir: &Path,
    show_progress: bool,
) -> Result<()> {
    let vsix_path = vsix_path.to_path_buf();
    let target_dir = target_dir.to_path_buf();

    tokio::task::spawn_blocking(move || extract_vsix_sync(&vsix_path, &target_dir, show_progress))
        .await
        .map_err(|e| MsvcKitError::Other(format!("Task join error: {}", e)))??;

    Ok(())
}

/// Extract a VSIX file (which is a ZIP archive) with progress bar
pub async fn extract_vsix(vsix_path: &Path, target_dir: &Path) -> Result<()> {
    extract_vsix_with_progress(vsix_path, target_dir, inner_progress_enabled()).await
}

fn extract_vsix_sync(vsix_path: &Path, target_dir: &Path, show_progress: bool) -> Result<()> {
    // Pre-compute total bytes for progress bar (skip metadata files)
    let total_bytes = {
        let file = File::open(vsix_path)?;
        let mut archive = zip::ZipArchive::new(file)?;
        let mut total = 0u64;
        for i in 0..archive.len() {
            let file = archive.by_index(i)?;
            let name = file.name();
            if name.starts_with('[') || name == "extension.vsixmanifest" || file.is_dir() {
                continue;
            }
            total = total.saturating_add(file.size());
        }
        total
    };

    let pb = if show_progress {
        let pb = ProgressBar::new(total_bytes.max(1));
        pb.set_draw_target(ProgressDrawTarget::stderr_with_hz(4));
        pb.set_style(progress_style_bytes());
        pb.set_message(
            vsix_path
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_else(|| "extracting".to_string()),
        );
        Some(pb)
    } else {
        None
    };

    let file = File::open(vsix_path)?;
    let mut archive = zip::ZipArchive::new(file)?;

    for i in 0..archive.len() {
        let mut file = archive.by_index(i)?;
        let name = file.name().to_string();

        // Skip metadata files
        if name.starts_with('[') || name == "extension.vsixmanifest" {
            continue;
        }

        // Remove "Contents/" prefix if present
        let relative_path = name.strip_prefix("Contents/").unwrap_or(&name);
        let out_path = target_dir.join(relative_path);

        if let Some(pb) = pb.as_ref() {
            pb.set_message(relative_path.to_string());
        }

        if file.is_dir() {
            std::fs::create_dir_all(&out_path)?;
            continue;
        }

        if let Some(parent) = out_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        let mut out_file = File::create(&out_path)?;
        let mut buffer = [0u8; ext_const::EXTRACT_BUFFER_SIZE];
        loop {
            let n = file.read(&mut buffer)?;
            if n == 0 {
                break;
            }
            out_file.write_all(&buffer[..n])?;
            if let Some(pb) = pb.as_ref() {
                pb.inc(n as u64);
            }
        }
    }

    if let Some(pb) = pb {
        pb.finish_with_message("Extracted");
    }
    Ok(())
}

/// Extract an MSI file
///
/// On Windows, uses msiexec. On other platforms, attempts to use msitools.
pub(crate) async fn extract_msi_with_progress(
    msi_path: &Path,
    target_dir: &Path,
    show_progress: bool,
) -> Result<()> {
    let msi_path = msi_path.to_path_buf();
    let target_dir = target_dir.to_path_buf();

    tokio::task::spawn_blocking(move || extract_msi_sync(&msi_path, &target_dir, show_progress))
        .await
        .map_err(|e| MsvcKitError::Other(format!("Task join error: {}", e)))??;

    Ok(())
}

pub async fn extract_msi(msi_path: &Path, target_dir: &Path) -> Result<()> {
    extract_msi_with_progress(msi_path, target_dir, inner_progress_enabled()).await
}

fn extract_msi_sync(msi_path: &Path, target_dir: &Path, show_progress: bool) -> Result<()> {
    let file_name = msi_path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown.msi")
        .to_string();

    let pb = if show_progress {
        let pb = ProgressBar::new_spinner();
        pb.set_draw_target(ProgressDrawTarget::stderr_with_hz(4));
        pb.set_style(
            ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] {msg}")
                .unwrap()
                .tick_chars("⠁⠃⠇⠋⠙⠸⠴⠦"),
        );
        pb.set_message(format!("msiexec extracting {}", file_name));
        pb.enable_steady_tick(Duration::from_millis(progress_const::PROGRESS_TICK_MS));
        Some(pb)
    } else {
        None
    };

    // Acquire global MSI lock to prevent concurrent msiexec invocations.
    // Windows Installer can only run one instance at a time (error 1618).
    let _lock = MSI_EXTRACT_LOCK
        .lock()
        .map_err(|e| MsvcKitError::Other(format!("Failed to acquire MSI lock: {}", e)))?;

    #[cfg(windows)]
    {
        use std::process::Command;

        let msi_path_str = msi_path
            .to_str()
            .ok_or_else(|| MsvcKitError::Other("Invalid MSI path".to_string()))?;
        let target_dir_str = format!("TARGETDIR={}", target_dir.display());

        // Retry loop for handling error 1618 (another installation in progress)
        // This can happen if system Windows Installer is busy with other operations
        let mut last_error = None;
        for attempt in 1..=MSI_MAX_RETRIES {
            let status = Command::new("msiexec")
                .args(["/a", msi_path_str, "/qn", &target_dir_str])
                .status()?;

            if status.success() {
                if let Some(pb) = pb {
                    pb.finish_with_message(format!("MSI extracted: {}", file_name));
                }
                return Ok(());
            }

            // Check for error 1618 (another installation in progress)
            // This can still happen if system-level installers are running
            if let Some(code) = status.code() {
                if code == 1618 && attempt < MSI_MAX_RETRIES {
                    tracing::warn!(
                        "msiexec returned 1618 (another installation in progress) for {}, retry {}/{}",
                        file_name,
                        attempt,
                        MSI_MAX_RETRIES
                    );
                    if let Some(pb) = pb.as_ref() {
                        pb.set_message(format!(
                            "msiexec waiting (retry {}/{}) {}",
                            attempt, MSI_MAX_RETRIES, file_name
                        ));
                    }
                    std::thread::sleep(Duration::from_millis(MSI_RETRY_DELAY_MS));
                    continue;
                }
            }

            last_error = Some(status);
            break;
        }

        if let Some(status) = last_error {
            if let Some(pb) = pb.as_ref() {
                pb.abandon_with_message(format!("msiexec failed: {}", file_name));
            }
            return Err(MsvcKitError::Other(format!(
                "msiexec failed with status: {} for {}",
                status, file_name
            )));
        }
    }

    #[cfg(not(windows))]
    {
        // On non-Windows, try using msitools (msiextract)
        use std::process::Command;

        let status = Command::new("msiextract")
            .args([
                "-C",
                target_dir
                    .to_str()
                    .ok_or_else(|| MsvcKitError::Other("Invalid target path".to_string()))?,
                msi_path
                    .to_str()
                    .ok_or_else(|| MsvcKitError::Other("Invalid MSI path".to_string()))?,
            ])
            .status();

        match status {
            Ok(s) if s.success() => {
                if let Some(pb) = pb {
                    pb.finish_with_message(format!("MSI extracted: {}", file_name));
                }
                return Ok(());
            }
            Ok(s) => {
                if let Some(pb) = pb.as_ref() {
                    pb.abandon_with_message("msiextract failed");
                }
                return Err(MsvcKitError::Other(format!(
                    "msiextract failed with status: {}",
                    s
                )));
            }
            Err(e) => {
                if let Some(pb) = pb.as_ref() {
                    pb.abandon_with_message("msiextract failed");
                }
                return Err(MsvcKitError::Other(format!(
                    "Failed to run msiextract (is msitools installed?): {}",
                    e
                )));
            }
        }
    }

    #[cfg(windows)]
    {
        if let Some(pb) = pb {
            pb.finish_with_message(format!("MSI extracted: {}", file_name));
        }
        Ok(())
    }
}

/// Extract a CAB file with a simple file-count progress bar
pub(crate) async fn extract_cab_with_progress(
    cab_path: &Path,
    target_dir: &Path,
    show_progress: bool,
) -> Result<()> {
    let cab_path = cab_path.to_path_buf();
    let target_dir = target_dir.to_path_buf();

    tokio::task::spawn_blocking(move || extract_cab_sync(&cab_path, &target_dir, show_progress))
        .await
        .map_err(|e| MsvcKitError::Other(format!("Task join error: {}", e)))??;

    Ok(())
}

pub async fn extract_cab(cab_path: &Path, target_dir: &Path) -> Result<()> {
    extract_cab_with_progress(cab_path, target_dir, inner_progress_enabled()).await
}

fn extract_cab_sync(cab_path: &Path, target_dir: &Path, show_progress: bool) -> Result<()> {
    let file = File::open(cab_path)?;
    let cabinet = cab::Cabinet::new(file)
        .map_err(|e| MsvcKitError::Cab(format!("Failed to open CAB: {}", e)))?;

    // Collect file names first by iterating through folders
    let file_names: Vec<String> = cabinet
        .folder_entries()
        .flat_map(|folder| folder.file_entries())
        .map(|entry| entry.name().to_string())
        .collect();

    let total_files = file_names.len() as u64;
    let pb = if show_progress {
        let pb = ProgressBar::new(total_files.max(1));
        pb.set_draw_target(ProgressDrawTarget::stderr_with_hz(4));
        pb.set_style(progress_style_items());
        pb.set_message(
            cab_path
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_else(|| "Extracting CAB".to_string()),
        );
        Some(pb)
    } else {
        None
    };

    // Re-open cabinet for extraction (cab crate requires this pattern)
    // Note: The cab crate's API requires re-opening for each file read.
    // This is a limitation of the crate, not an efficiency issue we can fix here.
    // A future optimization would be to use a different CAB library or implement
    // streaming extraction.
    for (idx, name) in file_names.iter().enumerate() {
        let out_path = target_dir.join(name);

        if let Some(parent) = out_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        if let Some(pb) = pb.as_ref() {
            pb.set_message(format!("{} ({}/{})", name, idx + 1, total_files));
        }

        // Re-open cabinet to read the file (cab crate limitation)
        let file = File::open(cab_path)?;
        let mut cabinet = cab::Cabinet::new(file)
            .map_err(|e| MsvcKitError::Cab(format!("Failed to open CAB: {}", e)))?;

        let mut reader = cabinet
            .read_file(name)
            .map_err(|e| MsvcKitError::Cab(format!("Failed to read file {}: {}", name, e)))?;

        let mut out_file = File::create(&out_path)?;
        let mut buffer = [0u8; ext_const::EXTRACT_BUFFER_SIZE];
        loop {
            let n = reader
                .read(&mut buffer)
                .map_err(|e| MsvcKitError::Cab(format!("Failed to read file content: {}", e)))?;
            if n == 0 {
                break;
            }
            out_file.write_all(&buffer[..n])?;
        }

        if let Some(pb) = pb.as_ref() {
            pb.inc(1);
        }
    }

    if let Some(pb) = pb {
        pb.finish_with_message("CAB extracted");
    }
    Ok(())
}

/// Determine the extraction method based on file extension
pub fn get_extractor(path: &Path) -> Option<fn(&Path, &Path) -> Result<()>> {
    let extension = path.extension()?.to_str()?.to_lowercase();

    match extension.as_str() {
        "vsix" | "zip" => {
            Some(|p, t| tokio::runtime::Handle::current().block_on(extract_vsix(p, t)))
        }
        "msi" => Some(|p, t| tokio::runtime::Handle::current().block_on(extract_msi(p, t))),
        "cab" => Some(|p, t| tokio::runtime::Handle::current().block_on(extract_cab(p, t))),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[allow(unused_imports)]
    use tempfile::TempDir;

    #[test]
    fn test_get_extractor() {
        assert!(get_extractor(Path::new("test.vsix")).is_some());
        assert!(get_extractor(Path::new("test.msi")).is_some());
        assert!(get_extractor(Path::new("test.cab")).is_some());
        assert!(get_extractor(Path::new("test.unknown")).is_none());
    }
}