midenup 1.0.0

The Miden toolchain manager
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
//! Acquiring an artifact: HTTP(S) transfers and local copies.
//!
//! # What this module guarantees
//!
//! A body reaches disk only when the response says it should. `response_code()` is read **after**
//! `transfer.perform()`, because before the transfer curl has no response and reports 0: read
//! early, the status check passes for everything, and the body of a 404 or 500 is written to disk
//! *as the artifact*. A component then installs "successfully" and fails to execute, with an HTML
//! error page where its binary should be.
//!
//! Destinations come from the plan, so the URL is never consulted for a name at all.
//!
//! The temporary file an artifact stages through is unique per attempt, not derived from the
//! destination's stem: `core.masp` and `core.wasm` must not stage through the same path.
//!
//! An archived artifact takes the same path with one step inserted: the file inside the transferred
//! bytes is read out (see [crate::install::archive]) and *that* is what gets published. It is
//! streamed into the temporary as it is unpacked, so the artifact is never held in memory alongside
//! the bytes it came out of, and the container is never written anywhere.

use std::{
    cell::RefCell,
    io::Write,
    path::{Path, PathBuf},
};

use crate::{
    artifact::SupportedFormat,
    install::{ArchiveError, archive},
    plan::PlanStep,
    report,
};

/// How many redirects to follow before giving up.
///
/// Release artifacts are routinely served via one or two redirects to object storage. A bounded
/// number is required: unbounded following turns a redirect loop into a hang.
const MAX_REDIRECTS: u32 = 10;

#[derive(Debug, thiserror::Error)]
pub enum ExecError {
    #[error("failed to create '{path}': {source}")]
    Create {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("failed to copy '{src}' to '{dest}': {source}")]
    Copy {
        src: PathBuf,
        dest: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("request for '{uri}' failed with status {status}")]
    HttpStatus { uri: String, status: u32 },
    #[error("request for '{uri}' returned an empty body")]
    EmptyBody { uri: String },
    #[error("transfer of '{uri}' failed: {reason}")]
    Transfer { uri: String, reason: String },
    #[error("failed to publish '{dest}': {source}")]
    Publish {
        dest: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error(transparent)]
    Archive(#[from] ArchiveError),
}

/// Performs one acquisition step.
///
/// Only [PlanStep::Download] and [PlanStep::CopyLocal] are handled; build steps belong to the
/// Cargo executor. Every path is taken verbatim from the plan.
pub fn acquire(step: &PlanStep) -> Result<(), ExecError> {
    match step {
        PlanStep::Download { uri, dest, mode, archive, owner, .. } => {
            download(uri, dest, *mode, *archive, owner)
        },
        PlanStep::CopyLocal { src, dest, mode, archive, .. } => {
            copy_local(src, dest, *mode, *archive)
        },
        PlanStep::CargoBuild { .. } | PlanStep::ExtractPackage { .. } => Ok(()),
    }
}

/// Fetches `uri` to `dest`, atomically, unpacking it first when it is an archive.
///
/// `label` names what is being transferred for the progress display; it is the owning component,
/// never anything derived from the URL.
pub fn download(
    uri: &str,
    dest: &Path,
    mode: u32,
    archive: Option<SupportedFormat>,
    label: &str,
) -> Result<(), ExecError> {
    let body = fetch(uri, label)?;
    publish(&body, archive, uri, dest, mode)
}

/// Copies `src` to `dest`, atomically, unpacking it first when it is an archive.
pub fn copy_local(
    src: &Path,
    dest: &Path,
    mode: u32,
    archive: Option<SupportedFormat>,
) -> Result<(), ExecError> {
    let body = std::fs::read(src).map_err(|source| ExecError::Copy {
        src: src.to_path_buf(),
        dest: dest.to_path_buf(),
        source,
    })?;
    let source = format!("file://{}", src.display());
    publish(&body, archive, &source, dest, mode)
}

/// Retrieves `uri`, rejecting anything that is not a usable body.
fn fetch(uri: &str, label: &str) -> Result<Vec<u8>, ExecError> {
    let mut body = Vec::new();
    let mut handle = curl::easy::Easy::new();

    let setup = |handle: &mut curl::easy::Easy| -> Result<(), curl::Error> {
        handle.url(uri)?;
        handle.follow_location(true)?;
        handle.max_redirections(MAX_REDIRECTS)?;
        // Without this curl never calls the progress function at all.
        handle.progress(true)?;
        Ok(())
    };
    setup(&mut handle).map_err(|err| ExecError::Transfer {
        uri: uri.to_string(),
        reason: err.description().to_string(),
    })?;

    // Shared with the progress callback below, which runs while `write_function` also holds a
    // borrow of `body`. Dropped at the end of this scope, which erases the display whether the
    // transfer succeeded or failed.
    let progress = RefCell::new(report::Transfer::begin(label));

    {
        let mut transfer = handle.transfer();
        transfer
            .write_function(|chunk| {
                body.extend_from_slice(chunk);
                Ok(chunk.len())
            })
            .map_err(|err| ExecError::Transfer {
                uri: uri.to_string(),
                reason: err.description().to_string(),
            })?;
        transfer
            .progress_function(|total, done, _, _| {
                // A server that sends no Content-Length reports a total of zero, which the display
                // reads as "count what has arrived and claim no denominator".
                progress.borrow_mut().update(done as u64, total as u64);
                true
            })
            .map_err(|err| ExecError::Transfer {
                uri: uri.to_string(),
                reason: err.description().to_string(),
            })?;
        crate::trace!("GET {uri}");
        transfer.perform().map_err(|err| ExecError::Transfer {
            uri: uri.to_string(),
            reason: err.description().to_string(),
        })?;
    }

    // *After* the transfer. Before it, curl has no response and reports 0, so this check would
    // pass for every status and error pages would land on disk as artifacts.
    let status = handle.response_code().map_err(|err| ExecError::Transfer {
        uri: uri.to_string(),
        reason: err.description().to_string(),
    })?;
    if !(200..300).contains(&status) {
        return Err(ExecError::HttpStatus { uri: uri.to_string(), status });
    }
    if body.is_empty() {
        return Err(ExecError::EmptyBody { uri: uri.to_string() });
    }

    Ok(body)
}

/// Writes the artifact `body` carries to a unique temporary sibling of `dest`, then renames it into
/// place. `body` can either be the artifact itself or the archive holding it.
///
/// The temporary is a sibling so the rename stays within one filesystem, and unique so that two
/// artifacts sharing a stem cannot stage through the same path.
fn publish(
    body: &[u8],
    archive: Option<SupportedFormat>,
    source: &str,
    dest: &Path,
    mode: u32,
) -> Result<(), ExecError> {
    if let Some(parent) = dest.parent() {
        std::fs::create_dir_all(parent)
            .map_err(|source| ExecError::Create { path: parent.to_path_buf(), source })?;
    }

    let temporary = temporary_sibling(dest);
    let unusable = |source: std::io::Error| ExecError::Create { path: temporary.clone(), source };

    let result = (|| -> Result<(), ExecError> {
        let mut file = std::fs::File::create(&temporary).map_err(unusable)?;
        match archive {
            Some(format) => archive::extract(body, format, source, &mut file)?,
            None => file.write_all(body).map_err(unusable)?,
        }
        file.flush().map_err(unusable)?;
        file.sync_all().map_err(unusable)?;

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            file.set_permissions(std::fs::Permissions::from_mode(mode)).map_err(unusable)?;
        }
        #[cfg(not(unix))]
        let _ = mode;

        Ok(())
    })();

    if let Err(err) = result {
        let _ = std::fs::remove_file(&temporary);
        return Err(err);
    }

    crate::trace!("publishing {} -> {}", temporary.display(), dest.display());
    if let Err(source) = std::fs::rename(&temporary, dest) {
        let _ = std::fs::remove_file(&temporary);
        return Err(ExecError::Publish { dest: dest.to_path_buf(), source });
    }

    Ok(())
}

fn temporary_sibling(dest: &Path) -> PathBuf {
    use std::{
        sync::atomic::{AtomicU64, Ordering},
        time::{SystemTime, UNIX_EPOCH},
    };

    static COUNTER: AtomicU64 = AtomicU64::new(0);

    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.subsec_nanos())
        .unwrap_or(0);
    let name = dest.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
    dest.with_file_name(format!(
        ".{name}.{}.{nanos}.{}.part",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    ))
}

#[cfg(test)]
mod tests {
    use flate2::{Compression, write::GzEncoder};

    use super::*;

    /// A single-request HTTP server, so status handling is exercised against a real socket.
    struct TestServer {
        url: String,
        _handle: std::thread::JoinHandle<()>,
    }

    impl TestServer {
        /// Serves one response and stops.
        fn responding(status: u16, body: &'static [u8]) -> Self {
            Self::spawn(move |request: tiny_http::Request| {
                let response = tiny_http::Response::from_data(body)
                    .with_status_code(tiny_http::StatusCode(status));
                let _ = request.respond(response);
            })
        }

        /// Redirects `/from` to `/to`, then serves `body` at `/to`.
        fn redirecting(body: &'static [u8], hops: usize) -> Self {
            Self::spawn_with(move |server: &tiny_http::Server, base: String| {
                for hop in 0..hops {
                    let Ok(request) = server.recv() else { return };
                    let next = format!("{base}/hop{}", hop + 1);
                    let response = tiny_http::Response::empty(tiny_http::StatusCode(302))
                        .with_header(
                            tiny_http::Header::from_bytes(&b"Location"[..], next.as_bytes())
                                .unwrap(),
                        );
                    let _ = request.respond(response);
                }
                if let Ok(request) = server.recv() {
                    let _ = request.respond(tiny_http::Response::from_data(body));
                }
            })
        }

        fn spawn(handler: impl FnOnce(tiny_http::Request) + Send + 'static) -> Self {
            Self::spawn_with(move |server, _| {
                if let Ok(request) = server.recv() {
                    handler(request);
                }
            })
        }

        fn spawn_with(handler: impl FnOnce(&tiny_http::Server, String) + Send + 'static) -> Self {
            let server = tiny_http::Server::http("127.0.0.1:0").expect("failed to bind");
            let url = format!("http://{}", server.server_addr());
            let base = url.clone();
            let handle = std::thread::spawn(move || handler(&server, base));
            Self { url, _handle: handle }
        }

        fn url(&self, path: &str) -> String {
            format!("{}{path}", self.url)
        }
    }

    fn temp() -> tempdir::TempDir {
        tempdir::TempDir::new("download").expect("failed to create temp dir")
    }

    fn leftovers(dir: &Path, keep: &str) -> Vec<String> {
        std::fs::read_dir(dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|name| name != keep)
            .collect()
    }

    /// A 404 body must never reach disk.
    ///
    /// The status is read after `perform()`, so the check sees the real response code rather than
    /// the 0 curl reports for a transfer that has not run yet.
    #[test]
    fn a_404_is_rejected_and_writes_nothing() {
        let server = TestServer::responding(404, b"<html>Not Found</html>");
        let dir = temp();
        let dest = dir.path().join("artifact.bin");

        let err = download(&server.url("/x"), &dest, 0o755, None, "fixture")
            .expect_err("a 404 must fail");
        assert!(matches!(err, ExecError::HttpStatus { status: 404, .. }), "{err}");
        assert!(!dest.exists(), "a failed download must not leave a file");
        assert!(leftovers(dir.path(), "artifact.bin").is_empty(), "no partial files");
    }

    #[test]
    fn a_500_is_rejected() {
        let server = TestServer::responding(500, b"boom");
        let dir = temp();
        let dest = dir.path().join("a.bin");

        let err = download(&server.url("/x"), &dest, 0o755, None, "fixture")
            .expect_err("a 500 must fail");
        assert!(matches!(err, ExecError::HttpStatus { status: 500, .. }), "{err}");
        assert!(!dest.exists());
    }

    /// A 200 with nothing in it is not a usable artifact.
    #[test]
    fn an_empty_body_is_rejected() {
        let server = TestServer::responding(200, b"");
        let dir = temp();
        let dest = dir.path().join("a.bin");

        let err = download(&server.url("/x"), &dest, 0o755, None, "fixture")
            .expect_err("an empty body must fail");
        assert!(matches!(err, ExecError::EmptyBody { .. }), "{err}");
        assert!(!dest.exists());
    }

    #[test]
    fn a_successful_download_lands_at_the_planned_path() {
        let server = TestServer::responding(200, b"payload");
        let dir = temp();
        let dest = dir.path().join("planned-name.bin");

        download(&server.url("/x"), &dest, 0o755, None, "fixture").expect("should succeed");
        assert_eq!(std::fs::read(&dest).unwrap(), b"payload");
        assert!(leftovers(dir.path(), "planned-name.bin").is_empty());
    }

    /// Release artifacts are routinely served through redirects to object storage.
    #[test]
    fn redirects_are_followed() {
        let server = TestServer::redirecting(b"payload", 2);
        let dir = temp();
        let dest = dir.path().join("a.bin");

        download(&server.url("/from"), &dest, 0o755, None, "fixture")
            .expect("redirects must be followed");
        assert_eq!(std::fs::read(&dest).unwrap(), b"payload");
    }

    /// The destination name comes from the plan, never from the URL.
    #[test]
    fn the_destination_name_is_never_derived_from_the_uri() {
        let server = TestServer::responding(200, b"payload");
        let dir = temp();
        let dest = dir.path().join("nothing-like-the-url");

        download(&server.url("/some/deep/path/other-name"), &dest, 0o755, None, "fixture").unwrap();
        assert!(dest.exists(), "the planned name must win");
    }

    #[test]
    fn the_planned_mode_is_applied() {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;

            let server = TestServer::responding(200, b"x");
            let dir = temp();
            let dest = dir.path().join("pkg.masp");

            download(&server.url("/x"), &dest, 0o644, None, "fixture").unwrap();
            assert_eq!(
                std::fs::metadata(&dest).unwrap().permissions().mode() & 0o777,
                0o644,
                "packages must not be marked executable"
            );
        }
    }

    /// A gzipped tarball with the binary nested under a release directory, as releases ship them.
    fn tarball(member: &str, contents: &[u8]) -> Vec<u8> {
        use std::io::Write;

        let mut tar = tar::Builder::new(Vec::new());
        let mut header = tar::Header::new_gnu();
        header.set_size(contents.len() as u64);
        // Deliberately not 0755: the mode comes from the plan, never from the archive.
        header.set_mode(0o600);
        header.set_cksum();
        tar.append_data(&mut header, member, contents).unwrap();

        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(&tar.into_inner().unwrap()).unwrap();
        encoder.finish().unwrap()
    }

    /// A gzipped tarball of several members, for the archives that must be refused.
    fn tarball_of(members: &[(&str, &[u8])]) -> Vec<u8> {
        use std::io::Write;

        let mut tar = tar::Builder::new(Vec::new());
        for (path, contents) in members {
            let mut header = tar::Header::new_gnu();
            header.set_size(contents.len() as u64);
            header.set_mode(0o600);
            header.set_cksum();
            tar.append_data(&mut header, path, *contents).unwrap();
        }

        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(&tar.into_inner().unwrap()).unwrap();
        encoder.finish().unwrap()
    }

    /// The archived file lands at the planned path with the planned name and mode.
    #[test]
    fn an_archived_download_installs_the_file_the_archive_holds() {
        let body = Box::leak(tarball("vm-0.16.0/miden-vm", b"binary").into_boxed_slice());
        let server = TestServer::responding(200, body);
        let dir = temp();
        let dest = dir.path().join("miden-vm");

        download(&server.url("/vm.tar.gz"), &dest, 0o755, Some(SupportedFormat::TarGz), "fixture")
            .expect("should succeed");

        assert_eq!(std::fs::read(&dest).unwrap(), b"binary");
        assert!(leftovers(dir.path(), "miden-vm").is_empty(), "nothing else may be unpacked");

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            assert_eq!(
                std::fs::metadata(&dest).unwrap().permissions().mode() & 0o777,
                0o755,
                "the mode comes from the plan, not from the archive entry"
            );
        }
    }

    /// A local archive is unpacked the same way a fetched one is.
    #[test]
    fn an_archived_local_copy_installs_the_file_the_archive_holds() {
        let dir = temp();
        let src = dir.path().join("core.tar.gz");
        std::fs::write(&src, tarball("packages/core.masp", b"package")).unwrap();
        let dest = dir.path().join("lib").join("core.masp");

        copy_local(&src, &dest, 0o644, Some(SupportedFormat::TarGz)).expect("should succeed");
        assert_eq!(std::fs::read(&dest).unwrap(), b"package");
    }

    /// An unreadable archive fails like any other unusable body: nothing on disk, container
    /// included.
    #[test]
    fn an_unreadable_archive_writes_nothing() {
        let server = TestServer::responding(200, b"<html>Not a tarball</html>");
        let dir = temp();
        let dest = dir.path().join("miden-vm");

        let err = download(
            &server.url("/vm.tar.gz"),
            &dest,
            0o755,
            Some(SupportedFormat::TarGz),
            "fixture",
        )
        .expect_err("must fail");
        assert!(matches!(err, ExecError::Archive(_)), "{err}");
        assert!(!dest.exists(), "the container must never be installed as the artifact");
        assert!(leftovers(dir.path(), "miden-vm").is_empty(), "no partial files");
    }

    /// The temporary an archive unpacks into is written before the archive has been shown to hold
    /// exactly one file. Only the rename publishes anything, so a rejected archive leaves nothing.
    #[test]
    fn an_archive_holding_more_than_one_file_writes_nothing() {
        let dir = temp();
        let src = dir.path().join("two.tar.gz");
        std::fs::write(&src, tarball_of(&[("first", b"one"), ("second", b"two")])).unwrap();
        let dest = dir.path().join("miden-vm");

        let err =
            copy_local(&src, &dest, 0o755, Some(SupportedFormat::TarGz)).expect_err("must fail");
        assert!(matches!(err, ExecError::Archive(_)), "{err}");
        assert!(!dest.exists(), "a rejected archive must not be installed");
        assert_eq!(
            leftovers(dir.path(), "two.tar.gz"),
            Vec::<String>::new(),
            "the temporary the first file was written to must be gone"
        );
    }

    #[test]
    fn a_local_copy_lands_at_the_planned_path() {
        let dir = temp();
        let src = dir.path().join("source");
        std::fs::write(&src, b"local").unwrap();
        let dest = dir.path().join("nested").join("dest");

        copy_local(&src, &dest, 0o644, None).expect("should succeed");
        assert_eq!(std::fs::read(&dest).unwrap(), b"local", "parent dirs must be created");
    }

    #[test]
    fn a_missing_local_source_is_an_error() {
        let dir = temp();
        let err = copy_local(&dir.path().join("nope"), &dir.path().join("dest"), 0o644, None)
            .expect_err("must fail");
        assert!(matches!(err, ExecError::Copy { .. }), "{err}");
    }

    /// Two artifacts sharing a stem must not stage through the same temporary path, and neither
    /// must two attempts at the same destination.
    #[test]
    fn temporary_names_do_not_collide_for_a_shared_stem() {
        let masp = temporary_sibling(Path::new("/tmp/core.masp"));
        let wasm = temporary_sibling(Path::new("/tmp/core.wasm"));
        assert_ne!(masp, wasm);

        // Nor for repeated staging of the same destination.
        let first = temporary_sibling(Path::new("/tmp/core.masp"));
        let second = temporary_sibling(Path::new("/tmp/core.masp"));
        assert_ne!(first, second);

        for name in [masp, wasm, first, second] {
            assert_eq!(name.parent(), Some(Path::new("/tmp")), "must be a sibling");
        }
    }

    /// Build steps are not this module's business.
    #[test]
    fn acquire_ignores_build_steps() {
        let step = PlanStep::CargoBuild {
            crate_name: "c".to_string(),
            authority: crate::plan::ResolvedAuthority::Registry {
                version: semver::Version::new(1, 0, 0),
            },
            features: vec![],
            rustup_channel: None,
            expect_binary: "b".to_string(),
            dest: PathBuf::from("/tmp/b"),
            owner: "c".to_string(),
        };
        acquire(&step).expect("a build step is not an acquisition");
    }
}