proto_core 0.58.2

Core proto APIs.
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
use super::process::{ProtoProcessError, exec_command_piped, handle_exec};
use proto_pdk_api::ArchiveSource;
use starbase_archive::{ArchiveError, Archiver};
use starbase_styles::{Style, Stylize};
use starbase_utils::fs::FsError;
use starbase_utils::net::{DownloadOptions, NetError};
use starbase_utils::{fs, net};
use std::path::{Path, PathBuf};
use std::time::Duration;
use thiserror::Error;
use tokio::process::Command;
use tokio::time::sleep;
use tracing::trace;
use warpgate::extract_file_name_from_url;

#[derive(Error, Debug, miette::Diagnostic)]
pub enum ProtoArchiveError {
    #[diagnostic(transparent)]
    #[error(transparent)]
    Archive(#[from] Box<ArchiveError>),

    #[diagnostic(transparent)]
    #[error(transparent)]
    Fs(#[from] Box<FsError>),

    #[diagnostic(transparent)]
    #[error(transparent)]
    Net(#[from] Box<NetError>),

    #[diagnostic(transparent)]
    #[error(transparent)]
    Process(#[from] Box<ProtoProcessError>),

    #[diagnostic(code(proto::archive::missing_pkg_payload))]
    #[error("Unable to find a payload in macOS package {}.", .path.style(Style::Path))]
    MissingPkgPayload { path: PathBuf },

    #[diagnostic(code(proto::archive::missing_dmg_volume))]
    #[error("Unable to find a mounted volume for macOS disk image {}.", .path.style(Style::Path))]
    MissingDmgVolume { path: PathBuf },

    #[diagnostic(code(proto::archive::missing_contents))]
    #[error(
        "Unable to extract contents from {format} {}, using directory prefix {}.",
        .path.style(Style::Path),
        .prefix.style(Style::Label)
    )]
    MissingArchiveContents {
        format: String,
        path: PathBuf,
        prefix: String,
    },
}

impl From<ArchiveError> for ProtoArchiveError {
    fn from(e: ArchiveError) -> ProtoArchiveError {
        ProtoArchiveError::Archive(Box::new(e))
    }
}

impl From<FsError> for ProtoArchiveError {
    fn from(e: FsError) -> ProtoArchiveError {
        ProtoArchiveError::Fs(Box::new(e))
    }
}

impl From<NetError> for ProtoArchiveError {
    fn from(e: NetError) -> ProtoArchiveError {
        ProtoArchiveError::Net(Box::new(e))
    }
}

impl From<ProtoProcessError> for ProtoArchiveError {
    fn from(error: ProtoProcessError) -> ProtoArchiveError {
        ProtoArchiveError::Process(Box::new(error))
    }
}

pub fn should_unpack(src: &ArchiveSource, target_dir: &Path) -> Result<bool, ProtoArchiveError> {
    let url_file = target_dir.join(".archive-url");
    let mut unpack = true;

    // If the URLs have changed at some point, we need to remove
    // the current files, and download new ones
    if url_file.exists() {
        let previous_url = fs::read_file(&url_file)?;

        if src.url.trim() == previous_url.trim() {
            unpack = false;
        } else {
            fs::remove_dir_all(target_dir)?;
        }
    }

    fs::create_dir_all(target_dir)?;

    Ok(unpack)
}

pub async fn download(
    src: &ArchiveSource,
    temp_dir: &Path,
    options: DownloadOptions,
) -> Result<PathBuf, ProtoArchiveError> {
    let filename = extract_file_name_from_url(&src.url);
    let archive_file = temp_dir.join(&filename);

    net::download_from_url_with_options(&src.url, &archive_file, options).await?;

    Ok(archive_file)
}

pub async fn download_and_unpack(
    src: &ArchiveSource,
    target_dir: &Path,
    temp_dir: &Path,
    options: DownloadOptions,
) -> Result<(), ProtoArchiveError> {
    if should_unpack(src, target_dir)? {
        let archive_file = download(src, temp_dir, options).await?;

        unpack_source(src, target_dir, temp_dir, &archive_file).await?;
    }

    Ok(())
}

pub async fn unpack_source(
    src: &ArchiveSource,
    target_dir: &Path,
    temp_dir: &Path,
    archive_file: &Path,
) -> Result<(String, PathBuf), ProtoArchiveError> {
    let result = unpack(target_dir, temp_dir, archive_file, src.prefix.as_deref()).await;

    fs::write_file(target_dir.join(".archive-url"), &src.url)?;

    result
}

pub async fn unpack(
    target_dir: &Path,
    temp_dir: &Path,
    archive_file: &Path,
    prefix: Option<&str>,
) -> Result<(String, PathBuf), ProtoArchiveError> {
    match archive_file.extension().and_then(|ext| ext.to_str()) {
        Some(ext) if ext.eq_ignore_ascii_case("pkg") => {
            unpack_pkg(target_dir, temp_dir, archive_file, prefix).await?;

            Ok(("pkg".into(), target_dir.to_path_buf()))
        }
        Some(ext) if ext.eq_ignore_ascii_case("dmg") => {
            unpack_dmg(target_dir, temp_dir, archive_file, prefix).await?;

            Ok(("dmg".into(), target_dir.to_path_buf()))
        }
        _ => {
            let mut archiver = Archiver::new(target_dir, archive_file);

            if let Some(prefix) = prefix {
                archiver.set_prefix(prefix);
            }

            Ok(archiver.unpack_from_ext()?)
        }
    }
}

async fn unpack_pkg(
    target_dir: &Path,
    temp_dir: &Path,
    archive_file: &Path,
    prefix: Option<&str>,
) -> Result<(), ProtoArchiveError> {
    let expanded_dir = temp_dir.join("pkg");
    let payload_dir = expanded_dir.join("Payload");

    fs::create_dir_all(temp_dir)?;

    // Remove expanded dir if it exists
    fs::remove_dir_all(&expanded_dir)?;

    let result = async {
        handle_exec(
            exec_command_piped(
                Command::new("pkgutil")
                    .arg("--expand-full")
                    .arg(archive_file)
                    .arg(&expanded_dir),
            )
            .await?,
        )?;

        if !payload_dir.exists() {
            return Err(ProtoArchiveError::MissingPkgPayload {
                path: expanded_dir.to_path_buf(),
            });
        }

        copy_extracted_contents("macOS package", &payload_dir, target_dir, prefix)
    }
    .await;

    let _ = fs::remove_dir_all(&expanded_dir);

    result
}

async fn unpack_dmg(
    target_dir: &Path,
    temp_dir: &Path,
    archive_file: &Path,
    prefix: Option<&str>,
) -> Result<(), ProtoArchiveError> {
    let mount_dir = temp_dir.join("dmg");

    fs::create_dir_all(temp_dir)?;

    // Remove mount dir if it exists
    fs::remove_dir_all(&mount_dir)?;

    let result = async {
        attach_dmg(archive_file, &mount_dir).await?;

        if !mount_dir.exists() {
            return Err(ProtoArchiveError::MissingDmgVolume {
                path: mount_dir.to_path_buf(),
            });
        }

        copy_extracted_contents("macOS disk image", &mount_dir, target_dir, prefix)
    }
    .await;

    // Always detach the volume, even if extracting the contents failed
    let _ = detach_dmg(&mount_dir).await;
    let _ = fs::remove_dir_all(&mount_dir);

    result
}

async fn attach_dmg(archive_file: &Path, mount_dir: &Path) -> Result<(), ProtoArchiveError> {
    // macOS DiskArbitration only permits a limited number of concurrent attach
    // operations. When proto installs multiple tools in parallel, `hdiutil attach`
    // can transiently fail with "Resource temporarily unavailable", so retry a
    // handful of times with a backoff before giving up.
    let max_attempts = 5;
    let mut attempt = 1;

    loop {
        let result = handle_exec(
            exec_command_piped(
                Command::new("hdiutil")
                    .arg("attach")
                    .arg(archive_file)
                    .arg("-nobrowse")
                    .arg("-readonly")
                    .arg("-noautoopen")
                    .arg("-mountpoint")
                    .arg(mount_dir),
            )
            .await?,
        );

        match result {
            Ok(_) => return Ok(()),
            Err(error) => {
                if attempt >= max_attempts {
                    return Err(error.into());
                }

                trace!(
                    archive = ?archive_file,
                    attempt,
                    error = error.to_string(),
                    "Failed to attach macOS disk image, retrying",
                );

                sleep(Duration::from_millis(250 * attempt)).await;
                attempt += 1;
            }
        }
    }
}

async fn detach_dmg(mount_dir: &Path) -> Result<(), ProtoArchiveError> {
    handle_exec(
        exec_command_piped(
            Command::new("hdiutil")
                .arg("detach")
                .arg(mount_dir)
                .arg("-force"),
        )
        .await?,
    )?;

    Ok(())
}

fn copy_extracted_contents(
    format: &str,
    source_dir: &Path,
    target_dir: &Path,
    prefix: Option<&str>,
) -> Result<(), ProtoArchiveError> {
    let source = match prefix {
        Some(prefix) => source_dir.join(prefix),
        None => source_dir.to_path_buf(),
    };

    if !source.exists() {
        return Err(ProtoArchiveError::MissingArchiveContents {
            format: format.into(),
            path: source_dir.to_path_buf(),
            prefix: prefix.unwrap_or("N/A").into(),
        });
    } else if source.is_file() {
        fs::copy_file(&source, target_dir.join(fs::file_name(&source)))?;
    } else {
        fs::copy_dir_all(&source, target_dir)?;
    }

    Ok(())
}

#[cfg(all(test, target_os = "macos"))]
mod tests {
    use super::*;
    use starbase_sandbox::{Sandbox, create_empty_sandbox};
    use std::process::{Command as StdCommand, Stdio};
    use std::sync::OnceLock;
    use tokio::sync::Mutex;

    // hdiutil/DiskArbitration serializes attach operations and transiently fails
    // under concurrency, and `create_dmg` drives it as well, so run the disk image
    // tests one at a time to keep them deterministic.
    fn dmg_test_lock() -> &'static Mutex<()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(()))
    }

    fn has_macos_pkg_tools() -> bool {
        ["pkgbuild", "pkgutil"].into_iter().all(|bin| {
            StdCommand::new("which")
                .arg(bin)
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .is_ok_and(|status| status.success())
        })
    }

    fn has_macos_dmg_tools() -> bool {
        StdCommand::new("which")
            .arg("hdiutil")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .is_ok_and(|status| status.success())
    }

    fn create_pkg(sandbox: &Sandbox, name: &str, files: &[(&str, &str, bool)]) -> PathBuf {
        let root = sandbox.path().join(format!("{name}-root"));

        for (relative_path, contents, executable) in files {
            let file = root.join(relative_path);

            fs::create_dir_all(file.parent().unwrap()).unwrap();
            fs::write_file(&file, contents).unwrap();

            #[cfg(unix)]
            if *executable {
                fs::update_perms(&file, Some(0o755)).unwrap();
            }
        }

        let package = sandbox.path().join(format!("{name}.pkg"));
        let output = StdCommand::new("pkgbuild")
            .arg("--root")
            .arg(&root)
            .arg("--identifier")
            .arg(format!("dev.proto.{name}"))
            .arg("--version")
            .arg("1.0.0")
            .arg(&package)
            .output()
            .unwrap();

        assert!(
            output.status.success(),
            "pkgbuild failed:\nstdout:\n{}\nstderr:\n{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );

        package
    }

    #[tokio::test]
    async fn unpacks_pkg_payload_with_prefix() {
        if !has_macos_pkg_tools() {
            return;
        }

        let sandbox = create_empty_sandbox();
        let target_dir = sandbox.path().join("target");
        let temp_dir = sandbox.path().join("temp");
        let package = create_pkg(
            &sandbox,
            "prefixed",
            &[
                (
                    "Library/Developer/Toolchains/swift/bin/swift",
                    "#!/bin/sh\n",
                    true,
                ),
                (
                    "Library/Developer/Toolchains/swift/lib/libswift.dylib",
                    "library",
                    false,
                ),
            ],
        );

        fs::create_dir_all(&target_dir).unwrap();

        let (ext, unpacked_path) = unpack(
            &target_dir,
            &temp_dir,
            &package,
            Some("Library/Developer/Toolchains/swift"),
        )
        .await
        .unwrap();

        assert_eq!(ext, "pkg");
        assert_eq!(unpacked_path, target_dir);
        assert!(target_dir.join("bin/swift").is_file());
        assert!(target_dir.join("lib/libswift.dylib").is_file());
        assert!(!target_dir.join("Library").exists());
        assert!(!target_dir.join("Payload").exists());
        assert!(!target_dir.join("PackageInfo").exists());
        assert!(!temp_dir.join("pkg").exists());
    }

    #[tokio::test]
    async fn unpack_source_writes_archive_url_for_pkg() {
        if !has_macos_pkg_tools() {
            return;
        }

        let sandbox = create_empty_sandbox();
        let target_dir = sandbox.path().join("target");
        let temp_dir = sandbox.path().join("temp");
        let package = create_pkg(
            &sandbox,
            "source",
            &[("usr/local/bin/proto-tool", "#!/bin/sh\n", true)],
        );
        let source = ArchiveSource {
            url: "https://example.com/proto-tool.pkg".into(),
            prefix: Some("usr/local".into()),
        };

        fs::create_dir_all(&target_dir).unwrap();

        let (ext, unpacked_path) = unpack_source(&source, &target_dir, &temp_dir, &package)
            .await
            .unwrap();

        assert_eq!(ext, "pkg");
        assert_eq!(unpacked_path, target_dir);
        assert!(target_dir.join("bin/proto-tool").is_file());
        assert_eq!(
            fs::read_file(target_dir.join(".archive-url")).unwrap(),
            "https://example.com/proto-tool.pkg"
        );
        assert!(!temp_dir.join("pkg").exists());
    }

    fn create_dmg(sandbox: &Sandbox, name: &str, files: &[(&str, &str, bool)]) -> PathBuf {
        let root = sandbox.path().join(format!("{name}-root"));

        for (relative_path, contents, executable) in files {
            let file = root.join(relative_path);

            fs::create_dir_all(file.parent().unwrap()).unwrap();
            fs::write_file(&file, contents).unwrap();

            #[cfg(unix)]
            if *executable {
                fs::update_perms(&file, Some(0o755)).unwrap();
            }
        }

        let image = sandbox.path().join(format!("{name}.dmg"));
        let output = StdCommand::new("hdiutil")
            .arg("create")
            .arg("-volname")
            .arg(name)
            .arg("-srcfolder")
            .arg(&root)
            .arg("-format")
            .arg("UDZO")
            .arg("-ov")
            .arg(&image)
            .output()
            .unwrap();

        assert!(
            output.status.success(),
            "hdiutil create failed:\nstdout:\n{}\nstderr:\n{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );

        image
    }

    #[tokio::test]
    async fn unpacks_dmg_volume_with_prefix() {
        if !has_macos_dmg_tools() {
            return;
        }

        let _guard = dmg_test_lock().lock().await;
        let sandbox = create_empty_sandbox();
        let target_dir = sandbox.path().join("target");
        let temp_dir = sandbox.path().join("temp");
        let image = create_dmg(
            &sandbox,
            "prefixed",
            &[
                ("swift/bin/swift", "#!/bin/sh\n", true),
                ("swift/lib/libswift.dylib", "library", false),
            ],
        );

        fs::create_dir_all(&target_dir).unwrap();

        let (ext, unpacked_path) = unpack(&target_dir, &temp_dir, &image, Some("swift"))
            .await
            .unwrap();

        assert_eq!(ext, "dmg");
        assert_eq!(unpacked_path, target_dir);
        assert!(target_dir.join("bin/swift").is_file());
        assert!(target_dir.join("lib/libswift.dylib").is_file());
        assert!(!target_dir.join("swift").exists());
        assert!(!temp_dir.join("dmg").exists());
    }

    #[tokio::test]
    async fn unpack_source_writes_archive_url_for_dmg() {
        if !has_macos_dmg_tools() {
            return;
        }

        let _guard = dmg_test_lock().lock().await;
        let sandbox = create_empty_sandbox();
        let target_dir = sandbox.path().join("target");
        let temp_dir = sandbox.path().join("temp");
        let image = create_dmg(
            &sandbox,
            "source",
            &[("bin/proto-tool", "#!/bin/sh\n", true)],
        );
        let source = ArchiveSource {
            url: "https://example.com/proto-tool.dmg".into(),
            prefix: Some("bin".into()),
        };

        fs::create_dir_all(&target_dir).unwrap();

        let (ext, unpacked_path) = unpack_source(&source, &target_dir, &temp_dir, &image)
            .await
            .unwrap();

        assert_eq!(ext, "dmg");
        assert_eq!(unpacked_path, target_dir);
        assert!(target_dir.join("proto-tool").is_file());
        assert_eq!(
            fs::read_file(target_dir.join(".archive-url")).unwrap(),
            "https://example.com/proto-tool.dmg"
        );
        assert!(!temp_dir.join("dmg").exists());
    }
}