local-ci 0.20.0

Run the GitHub Actions jobs a branch would trigger, locally, in an isolated worktree, and print a short report
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
//! The artifacts of a run on disk: what `actions/upload-artifact@v4` and
//! `actions/download-artifact@v4` do within one run.
//!
//! An upload copies its files into the artifact area of its workflow in the
//! run (`<run dir>/artifacts/<workflow>/<name>/v<N>/files/`), read-only, and writes the
//! receipt (`receipt.json`: name, version, each file's path, size and SHA-256,
//! the plan digest, the attempt, the job and task) last and atomically; the
//! step completes after that. A download waits for nothing but the receipt:
//! it takes the newest version that has one, checks every file against it and
//! only then places the files, refusing any write outside the workspace.
//! Artifacts belong to a workflow run, as on GitHub: each workflow file has an
//! area of its own ([`workflow_dir`]) and what one workflow uploads is not
//! there for another to download, nor does the same name in two of them
//! conflict.
//! What to decide is [`crate::artifact`]'s; this module does the file work.

use crate::artifact::{self, Kind, NoFiles};
use crate::plan::Plan;
use anyhow::{anyhow, bail, Context, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::BTreeSet;
use std::ffi::CString;
use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::os::fd::{AsRawFd, FromRawFd};
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

/// The directory name of a workflow file in the artifact area: its path with
/// every character other than a letter, digit, `.`, `-` or `_` made `_`, and
/// the start of its SHA-256 after it (two files never share a name, and
/// nothing in it can leave the area).
pub fn workflow_dir(workflow: &str) -> String {
    let safe: String = workflow
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || matches!(c, '-' | '_') {
                c
            } else {
                '_'
            }
        })
        .collect();
    format!(
        "{safe}-{}",
        &hex(&Sha256::digest(workflow.as_bytes()))[..12]
    )
}

/// Where the artifacts of workflow `workflow` are in a run directory.
fn workflow_root(run_dir: &Path, workflow: &str) -> PathBuf {
    run_dir.join("artifacts").join(workflow_dir(workflow))
}

/// A workflow's artifact area in the run and what a receipt records about the
/// run.
pub struct Area {
    pub root: PathBuf,
    pub plan_digest: String,
    pub attempt: String,
}

impl Area {
    /// The area of workflow file `workflow` in a run directory.
    pub fn of_run(run_dir: &Path, plan_digest: &str, workflow: &str) -> Area {
        Area {
            root: workflow_root(run_dir, workflow),
            plan_digest: plan_digest.to_string(),
            attempt: run_dir
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_default(),
        }
    }
}

/// Which job an upload is of.
pub struct Who {
    /// `<workflow file>::<job id>`.
    pub job: String,
    /// The job's name as the run shows it (a matrix entry has its variant).
    pub display: String,
    /// The task, when the plan has exactly one that holds the job.
    pub task: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FileEntry {
    pub path: String,
    pub size: u64,
    pub sha256: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Receipt {
    pub name: String,
    pub version: u32,
    pub files: Vec<FileEntry>,
    pub plan_digest: String,
    pub attempt_id: String,
    pub job: String,
    pub display: String,
    pub task: Option<String>,
    pub retention_days: Option<u32>,
    /// `compression-level` as written: accepted and recorded, with no effect
    /// (the files are stored as they are).
    #[serde(default)]
    pub compression_level: Option<u32>,
    /// The attempt this upload was first made in, when the receipt is a
    /// restore of a kept result (see [`restore_artifacts`]); `attempt_id` and
    /// `plan_digest` are then the restoring run's.
    #[serde(default)]
    pub restored_from: Option<String>,
}

/// What can be said before the run about the artifact steps of the jobs of
/// workflow `wf` that the plan will run (the jobs a filter left out are not
/// looked at; a job whose `if:` is decided when it runs is): every step is of
/// a supported use with supported options. One problem per step, each naming
/// the workflow, job and step.
pub fn workflow_problems(plan: &Plan, wf: usize) -> Vec<String> {
    let w = &plan.selected[wf].wf;
    let mut problems = Vec::new();
    for u in plan.units.iter().filter(|u| u.wf == wf) {
        for id in &u.jobs {
            let Some(job) = w.jobs.iter().find(|j| &j.id == id) else {
                continue;
            };
            let axes = job
                .matrix
                .as_ref()
                .map(|m| artifact::matrix_axes(&crate::matrix::yaml_to_json(m)));
            let matrix = match &axes {
                None => artifact::Matrix::None,
                Some(None) => artifact::Matrix::Unknown,
                Some(Some(a)) => artifact::Matrix::Axes(a),
            };
            for step in &job.steps {
                let Some(uses) = &step.uses else { continue };
                let Some(kind) = artifact::parse_uses(uses) else {
                    continue;
                };
                if let Err(e) = kind.and_then(|k| artifact::validate_with(k, &step.with, matrix)) {
                    problems.push(format!(
                        "{}: job `{}`, step `{}`: {e}",
                        w.file,
                        job.id,
                        step.label()
                    ));
                }
            }
        }
    }
    problems
}

/// Why the jobs of workflow `wf` are not run, when an artifact step of one of
/// them cannot be: the same text for each job of the workflow, so a job whose
/// own steps are fine says what stopped it.
pub fn workflow_refusal(plan: &Plan, wf: usize) -> Option<String> {
    let problems = workflow_problems(plan, wf);
    (!problems.is_empty()).then(|| {
        format!(
            "not run: artifact steps local-ci cannot run: {}",
            problems.join("; ")
        )
    })
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

fn digest_of(bytes: &[u8]) -> (u64, String) {
    (bytes.len() as u64, hex(&Sha256::digest(bytes)))
}

/// Fail when a component of `rel` under `root` that exists is a symlink.
fn no_symlink_in(root: &Path, rel: &str) -> Result<()> {
    let mut at = root.to_path_buf();
    for c in rel.split('/').filter(|c| !c.is_empty()) {
        at.push(c);
        match fs::symlink_metadata(&at) {
            Ok(m) if m.file_type().is_symlink() => {
                bail!("`{rel}` goes through the symlink `{c}`, which is refused")
            }
            Ok(_) => {}
            Err(_) => break,
        }
    }
    Ok(())
}

/// What is at each component of `rel` under `ws`, from the first, up to the
/// first one that is missing.
fn found_along(ws: &Path, rel: &str) -> Vec<artifact::Found> {
    let mut at = ws.to_path_buf();
    let mut out = Vec::new();
    for c in rel.split('/').filter(|c| !c.is_empty()) {
        at.push(c);
        let f = match fs::symlink_metadata(&at) {
            Err(_) => artifact::Found::Missing,
            Ok(m) if m.is_dir() => artifact::Found::Dir,
            Ok(m) if m.is_file() => artifact::Found::File,
            Ok(_) => artifact::Found::Other,
        };
        out.push(f);
        if f == artifact::Found::Missing {
            break;
        }
    }
    out
}

/// Every file under `ws/rel`, as paths relative to `ws`. Symlinks are not
/// followed or taken; `.git` is not entered.
fn walk(ws: &Path, rel: &str, out: &mut BTreeSet<String>) -> Result<()> {
    no_symlink_in(ws, rel)?;
    let path = ws.join(rel);
    let Ok(meta) = fs::symlink_metadata(&path) else {
        return Ok(());
    };
    if meta.is_file() {
        out.insert(rel.to_string());
    } else if meta.is_dir() {
        for e in fs::read_dir(&path)? {
            let e = e?;
            let name = e.file_name().to_string_lossy().into_owned();
            if name == ".git" || e.file_type()?.is_symlink() {
                continue;
            }
            let child = if rel.is_empty() {
                name
            } else {
                format!("{rel}/{name}")
            };
            walk(ws, &child, out)?;
        }
    }
    Ok(())
}

fn version_dirs(dir: &Path) -> Vec<(u32, PathBuf)> {
    let mut v: Vec<(u32, PathBuf)> = fs::read_dir(dir)
        .into_iter()
        .flatten()
        .flatten()
        .filter_map(|e| {
            let n = e
                .file_name()
                .to_string_lossy()
                .strip_prefix('v')?
                .parse()
                .ok()?;
            Some((n, e.path()))
        })
        .collect();
    v.sort();
    v
}

fn read_receipt(vdir: &Path) -> Option<Receipt> {
    serde_json::from_slice(&fs::read(vdir.join("receipt.json")).ok()?).ok()
}

/// The newest version of `name` that has a receipt.
fn latest_receipt(area: &Area, name: &str) -> Option<(u32, PathBuf, Receipt)> {
    version_dirs(&area.root.join(name))
        .into_iter()
        .rev()
        .find_map(|(n, d)| read_receipt(&d).map(|r| (n, d, r)))
}

/// The names of the artifacts of workflow `workflow` that have a receipt.
pub fn published_names(run_dir: &Path, workflow: &str) -> Vec<String> {
    published_names_in(&workflow_root(run_dir, workflow))
}

fn published_names_in(root: &Path) -> Vec<String> {
    let mut out: Vec<String> = fs::read_dir(root)
        .into_iter()
        .flatten()
        .flatten()
        .map(|e| e.file_name().to_string_lossy().into_owned())
        .filter(|n| {
            version_dirs(&root.join(n))
                .iter()
                .any(|(_, d)| d.join("receipt.json").is_file())
        })
        .collect();
    out.sort();
    out
}

/// The names of the artifacts uploaded by one of `jobs` (`<file>::<id>`) of
/// workflow `workflow`.
pub fn published_by(run_dir: &Path, workflow: &str, jobs: &[String]) -> Vec<String> {
    let area = Area::of_run(run_dir, "", workflow);
    published_names(run_dir, workflow)
        .into_iter()
        .filter(|n| {
            version_dirs(&area.root.join(n))
                .iter()
                .filter_map(|(_, d)| read_receipt(d))
                .any(|r| jobs.contains(&r.job))
        })
        .collect()
}

/// A kept version of an artifact: its name, version and the digest of its
/// files ([`artifact::files_digest`]).
pub type Kept = (String, u32, String);

fn receipt_digest(r: &Receipt) -> String {
    artifact::files_digest(
        &r.files
            .iter()
            .map(|f| (f.path.clone(), f.size, f.sha256.clone()))
            .collect::<Vec<_>>(),
    )
}

/// The digest of the files of the newest version of `name` of workflow
/// `workflow` in a run, or `None` when there is none.
pub fn latest_digest(run_dir: &Path, workflow: &str, name: &str) -> Option<String> {
    latest_receipt(&Area::of_run(run_dir, "", workflow), name).map(|(_, _, r)| receipt_digest(&r))
}

/// Copy one version (`receipt.json` and `files/`) from `from` to `to`,
/// checking every file against `receipt` as it is read: what is checked is
/// what is written. The receipt written is `write` (the same, or the restored
/// one), last, so a version without it is not a published one.
fn copy_version(from: &Path, to: &Path, receipt: &Receipt, write: &Receipt) -> Result<()> {
    for f in &receipt.files {
        let rel = artifact::stored_rel(&f.path).map_err(|e| anyhow!(e))?;
        let files = from.join("files");
        no_symlink_in(&files, &rel)?;
        let src = files.join(&rel);
        let meta = fs::symlink_metadata(&src).with_context(|| format!("read {}", src.display()))?;
        if !meta.is_file() {
            bail!("`{}` of `{}` is not a file", f.path, receipt.name);
        }
        let bytes = fs::read(&src).with_context(|| format!("read {}", src.display()))?;
        let (size, sha) = digest_of(&bytes);
        artifact::check_digest(&f.path, &receipt.name, (f.size, &f.sha256), (size, &sha))
            .map_err(|e| anyhow!(e))?;
        let dest = to.join("files").join(&rel);
        fs::create_dir_all(dest.parent().expect("a file has a parent"))?;
        write_atomic(&dest, &bytes, meta.permissions().mode() & 0o555)?;
    }
    fs::create_dir_all(to)?;
    write_atomic(
        &to.join("receipt.json"),
        &serde_json::to_vec_pretty(write)?,
        0o444,
    )
}

/// Keep, in `dest`, every version of an artifact that one of `jobs`
/// (`<file>::<id>`) of workflow `workflow` uploaded in the run (`<name>/v<N>/`
/// as in the workflow's area), each
/// file checked against its receipt. What is kept, by name, version and
/// digest. Fails, leaving `dest` to be removed, when a file does not match.
pub fn save_unit_artifacts(
    run_dir: &Path,
    workflow: &str,
    jobs: &[String],
    dest: &Path,
) -> Result<Vec<Kept>> {
    let area = Area::of_run(run_dir, "", workflow);
    let mut kept = Vec::new();
    for name in published_names(run_dir, workflow) {
        for (n, vdir) in version_dirs(&area.root.join(&name)) {
            let Some(r) = read_receipt(&vdir) else {
                continue;
            };
            if !jobs.contains(&r.job) {
                continue;
            }
            if r.name != name || r.version != n {
                bail!(
                    "the receipt in `{name}/v{n}` is of `{}` v{}",
                    r.name,
                    r.version
                );
            }
            copy_version(&vdir, &dest.join(&name).join(format!("v{n}")), &r, &r)?;
            kept.push((name.clone(), n, receipt_digest(&r)));
        }
    }
    Ok(kept)
}

/// Put what [`save_unit_artifacts`] kept in `src` into the area of workflow
/// `workflow` in the run, as if the unit had uploaded it here: every file is checked against its receipt
/// first, the receipt is rewritten with this run's plan digest and attempt (the
/// original attempt goes to `restored_from`), and a version is only put where
/// none is. Nothing is left behind when it fails. `expect` is what the
/// results cache recorded as kept: anything else in `src` is a failure too.
pub fn restore_artifacts(
    src: &Path,
    run_dir: &Path,
    plan_digest: &str,
    workflow: &str,
    expect: &[Kept],
) -> Result<()> {
    let area = Area::of_run(run_dir, plan_digest, workflow);
    let mut found: Vec<(String, u32, PathBuf, Receipt)> = Vec::new();
    for e in fs::read_dir(src)
        .with_context(|| format!("read {}", src.display()))?
        .flatten()
    {
        if !e.file_type()?.is_dir() {
            continue;
        }
        let name = e.file_name().to_string_lossy().into_owned();
        artifact::runtime_name(&name).map_err(|e| anyhow!(e))?;
        for (n, vdir) in version_dirs(&e.path()) {
            let r = read_receipt(&vdir)
                .ok_or_else(|| anyhow!("`{name}/v{n}` of the kept result has no receipt"))?;
            if r.name != name || r.version != n {
                bail!(
                    "the receipt in `{name}/v{n}` is of `{}` v{}",
                    r.name,
                    r.version
                );
            }
            found.push((name.clone(), n, vdir, r));
        }
    }
    let mut got: Vec<Kept> = found
        .iter()
        .map(|(n, v, _, r)| (n.clone(), *v, receipt_digest(r)))
        .collect();
    got.sort();
    let mut want = expect.to_vec();
    want.sort();
    if got != want {
        bail!("the kept artifacts are not the ones the result recorded");
    }
    let stage = run_dir.join(format!(".restore-{}", std::process::id()));
    let _ = fs::remove_dir_all(&stage);
    let mut moved: Vec<PathBuf> = Vec::new();
    let done = (|| -> Result<()> {
        let existing: Vec<String> = fs::read_dir(&area.root)
            .into_iter()
            .flatten()
            .flatten()
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .collect();
        for (name, n, vdir, r) in &found {
            artifact::name_collision(&existing, name).map_err(|e| anyhow!(e))?;
            if area.root.join(name).join(format!("v{n}")).exists() {
                bail!("`{name}` v{n} is already in this run");
            }
            let again = Receipt {
                plan_digest: area.plan_digest.clone(),
                attempt_id: area.attempt.clone(),
                restored_from: Some(r.attempt_id.clone()),
                ..r.clone()
            };
            copy_version(vdir, &stage.join(name).join(format!("v{n}")), r, &again)?;
        }
        for (name, n, _, _) in &found {
            fs::create_dir_all(area.root.join(name))?;
            let to = area.root.join(name).join(format!("v{n}"));
            fs::rename(stage.join(name).join(format!("v{n}")), &to)?;
            moved.push(to);
        }
        Ok(())
    })();
    if done.is_err() {
        for m in &moved {
            let _ = fs::remove_dir_all(m);
        }
    }
    let _ = fs::remove_dir_all(&stage);
    done
}

// File work relative to a directory that is held open.
//
// Every directory of a path is opened from the one before it with
// `O_NOFOLLOW`, and the file last, so a symlink put in the place of any
// component after a check cannot take a read or a write out of the
// workspace: there is no path to be resolved a second time. (`mkdirat`,
// `openat` and `renameat` act on the held directory, and `renameat` replaces
// a symlink at the final name rather than following it.) The one thing not
// held is the workspace root itself, which is opened once by the path the run
// gave and is the run's own.

fn cstr(s: &str) -> io::Result<CString> {
    CString::new(s).map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))
}

fn cvt(r: libc::c_int) -> io::Result<libc::c_int> {
    if r < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(r)
    }
}

fn open_at(dir: &File, name: &str, flags: libc::c_int, mode: u32) -> io::Result<File> {
    let c = cstr(name)?;
    // SAFETY: `c` is a NUL-terminated string that outlives the call and the
    // descriptor of `dir` is open; the descriptor returned is owned by the File.
    let fd = cvt(unsafe {
        libc::openat(
            dir.as_raw_fd(),
            c.as_ptr(),
            flags | libc::O_CLOEXEC,
            mode as libc::c_uint,
        )
    })?;
    Ok(unsafe { File::from_raw_fd(fd) })
}

/// The file type bits of `name` in `dir`, without following a symlink.
fn type_at(dir: &File, name: &str) -> Option<libc::mode_t> {
    let c = cstr(name).ok()?;
    let mut st = std::mem::MaybeUninit::<libc::stat>::uninit();
    // SAFETY: as for `open_at`; `st` is written by the call when it succeeds.
    let r = unsafe {
        libc::fstatat(
            dir.as_raw_fd(),
            c.as_ptr(),
            st.as_mut_ptr(),
            libc::AT_SYMLINK_NOFOLLOW,
        )
    };
    (r == 0).then(|| unsafe { st.assume_init() }.st_mode & libc::S_IFMT)
}

fn unlink_at(dir: &File, name: &str) -> io::Result<()> {
    let c = cstr(name)?;
    // SAFETY: as for `open_at`.
    cvt(unsafe { libc::unlinkat(dir.as_raw_fd(), c.as_ptr(), 0) }).map(|_| ())
}

/// Why `name` in `dir` could not be opened as what `shown` needs.
fn refuse(dir: &File, name: &str, shown: &str, e: io::Error) -> anyhow::Error {
    match type_at(dir, name) {
        Some(libc::S_IFLNK) => {
            anyhow!("`{shown}` goes through the symlink `{name}`, which is refused")
        }
        Some(t) if t != libc::S_IFDIR && e.raw_os_error() == Some(libc::ENOTDIR) => {
            anyhow!("`{shown}` goes through `{name}`, which is not a directory")
        }
        _ => anyhow!(e).context(format!("open `{shown}`")),
    }
}

/// The directory `comps` under `root`, opened one component at a time without
/// following a symlink; with `create`, the ones that are not there are made.
fn open_dirs(root: &File, comps: &[&str], create: bool, shown: &str) -> Result<File> {
    let mut cur = root.try_clone()?;
    let flags = libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW;
    for c in comps {
        let next = match open_at(&cur, c, flags, 0) {
            Ok(f) => f,
            Err(e) if create && e.raw_os_error() == Some(libc::ENOENT) => {
                let name = cstr(c)?;
                // SAFETY: as for `open_at`.
                let made = unsafe { libc::mkdirat(cur.as_raw_fd(), name.as_ptr(), 0o755) };
                if made < 0 && io::Error::last_os_error().raw_os_error() != Some(libc::EEXIST) {
                    return Err(
                        anyhow!(io::Error::last_os_error()).context(format!("create `{c}`"))
                    );
                }
                open_at(&cur, c, flags, 0).map_err(|e| refuse(&cur, c, shown, e))?
            }
            Err(e) => return Err(refuse(&cur, c, shown, e)),
        };
        cur = next;
    }
    Ok(cur)
}

fn components(rel: &str) -> Vec<&str> {
    rel.split('/').filter(|c| !c.is_empty()).collect()
}

/// A regular file of the workspace, read through descriptors: its bytes and
/// mode. A symlink at any component, a directory, a device or a pipe is
/// refused, whatever it was when the files were listed.
fn read_nofollow(root: &File, rel: &str) -> Result<(Vec<u8>, fs::Metadata)> {
    let comps = components(rel);
    let (name, dirs) = comps.split_last().context("no file named")?;
    let dir = open_dirs(root, dirs, false, rel)?;
    let mut f = open_at(
        &dir,
        name,
        libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_NONBLOCK,
        0,
    )
    .map_err(|e| refuse(&dir, name, rel, e))?;
    let meta = f.metadata()?;
    if !meta.is_file() {
        bail!("`{rel}` is not a regular file");
    }
    let mut bytes = Vec::with_capacity(meta.len() as usize);
    f.read_to_end(&mut bytes)
        .with_context(|| format!("read `{rel}`"))?;
    Ok((bytes, meta))
}

/// Write `bytes` as `name` of `dir`: into a temp file made new, under a name
/// that is this placement's (`O_EXCL`, `O_NOFOLLOW`: never through a link
/// someone left there, and what is there is never removed), then renamed over
/// the name. A name that is a directory or a symlink is not replaced.
fn write_at(dir: &File, name: &str, bytes: &[u8], mode: u32, shown: &str) -> Result<()> {
    if matches!(type_at(dir, name), Some(libc::S_IFLNK | libc::S_IFDIR)) {
        bail!("`{shown}` exists and is not a file; it is not replaced");
    }
    // a name of its own for this placement: nothing that is there is touched
    let flags = libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW;
    let (tmp, mut f) = {
        let mut tries = 0;
        loop {
            static SEQ: AtomicU64 = AtomicU64::new(0);
            let tmp = format!(
                "{name}.lci-tmp-{}-{}",
                std::process::id(),
                SEQ.fetch_add(1, Ordering::Relaxed)
            );
            match open_at(dir, &tmp, flags, 0o600) {
                Ok(f) => break (tmp, f),
                Err(e) if e.raw_os_error() == Some(libc::EEXIST) && tries < 16 => tries += 1,
                Err(e) => return Err(anyhow!(e).context(format!("create `{tmp}`"))),
            }
        }
    };
    let done = (|| -> Result<()> {
        f.write_all(bytes)?;
        f.set_permissions(fs::Permissions::from_mode(mode))?;
        f.sync_all()?;
        let (from, to) = (cstr(&tmp)?, cstr(name)?);
        // SAFETY: as for `open_at`.
        cvt(unsafe {
            libc::renameat(dir.as_raw_fd(), from.as_ptr(), dir.as_raw_fd(), to.as_ptr())
        })?;
        Ok(())
    })();
    if done.is_err() {
        let _ = unlink_at(dir, &tmp);
    }
    done
}

/// Put `bytes` at `dest` under the workspace root, making the directories on
/// the way; none of them is followed if it is a symlink.
fn place_nofollow(root: &File, dest: &str, bytes: &[u8], mode: u32) -> Result<()> {
    let comps = components(dest);
    let (name, dirs) = comps.split_last().context("no file named")?;
    let dir = open_dirs(root, dirs, true, dest)?;
    write_at(&dir, name, bytes, mode, dest)
}

/// Write a file of the run's own artifact area.
fn write_atomic(path: &Path, bytes: &[u8], mode: u32) -> Result<()> {
    let parent = File::open(path.parent().expect("a file has a parent"))?;
    let name = path
        .file_name()
        .and_then(|n| n.to_str())
        .context("a file name that is text")?;
    write_at(&parent, name, bytes, mode, name)
}

/// `actions/upload-artifact@v4`: `with` holds the step's values, already
/// expanded. The note says what was done.
pub fn upload(
    area: &Area,
    who: &Who,
    ws: &Path,
    with: &[(String, String)],
) -> Result<String, String> {
    upload_checked(area, who, ws, with, |_, _, _| Ok(()))
}

/// Upload with a host policy applied to the exact opened file and bytes,
/// before they are written into the artifact area. An error aborts publication.
pub fn upload_checked(
    area: &Area,
    who: &Who,
    ws: &Path,
    with: &[(String, String)],
    check: impl FnMut(&str, &fs::Metadata, &[u8]) -> Result<(), String>,
) -> Result<String, String> {
    upload_inner(area, who, ws, with, check).map_err(|e| format!("{e:#}"))
}

fn upload_inner(
    area: &Area,
    who: &Who,
    ws: &Path,
    with: &[(String, String)],
    mut check: impl FnMut(&str, &fs::Metadata, &[u8]) -> Result<(), String>,
) -> Result<String> {
    artifact::validate_run(Kind::Upload, with).map_err(|e| anyhow!(e))?;
    let get = |k: &str| {
        with.iter()
            .find(|(n, _)| n == k)
            .map(|(_, v)| v.as_str())
            .filter(|v| !v.trim().is_empty())
    };
    // checked here whatever the plan could check: the name is a directory
    let name = artifact::runtime_name(get("name").unwrap_or("artifact")).map_err(|e| anyhow!(e))?;
    let name = name.as_str();
    let policy = artifact::missing_policy(get("if-no-files-found")).map_err(|e| anyhow!(e))?;
    let overwrite = artifact::parse_bool("overwrite", get("overwrite")).map_err(|e| anyhow!(e))?;
    let hidden = artifact::parse_bool("include-hidden-files", get("include-hidden-files"))
        .map_err(|e| anyhow!(e))?;
    let retention = artifact::parse_retention(get("retention-days")).map_err(|e| anyhow!(e))?;
    let compression =
        artifact::parse_compression(get("compression-level")).map_err(|e| anyhow!(e))?;
    let lines = artifact::path_lines(get("path").unwrap_or(""), &ws.to_string_lossy())
        .map_err(|e| anyhow!(e))?;

    let mut all = BTreeSet::new();
    for l in lines.iter().filter(|l| !l.exclude) {
        walk(ws, &artifact::literal_prefix(&l.pattern), &mut all)?;
    }
    let all: Vec<String> = all.into_iter().collect();
    let selected = artifact::select_files(&lines, &all, hidden);
    match artifact::when_no_files(policy, selected.len()) {
        NoFiles::Upload => {}
        NoFiles::Warn => {
            return Ok(format!(
                "warning: no files were found with the path, so nothing was uploaded as `{name}`"
            ))
        }
        NoFiles::Nothing => return Ok(format!("no files found; nothing uploaded as `{name}`")),
        NoFiles::Fail => bail!("no files were found with the path `{}` for `{name}`", {
            lines
                .iter()
                .map(|l| l.pattern.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        }),
    }

    let existing: Vec<String> = fs::read_dir(&area.root)
        .into_iter()
        .flatten()
        .flatten()
        .map(|e| e.file_name().to_string_lossy().into_owned())
        .collect();
    artifact::name_collision(&existing, name).map_err(|e| anyhow!(e))?;
    let dir = area.root.join(name);
    fs::create_dir_all(&dir)?;
    // creating the directory is what claims the version: a second upload of
    // the name at the same moment finds it taken, and goes on to the next
    // version (overwrite) or reports the conflict
    let latest = version_dirs(&dir).last().map(|(n, _)| *n);
    let mut version = artifact::decide_version(name, latest, overwrite).map_err(|e| anyhow!(e))?;
    let vdir = loop {
        let vdir = dir.join(format!("v{version}"));
        match fs::create_dir(&vdir) {
            Ok(()) => break vdir,
            Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
                version = artifact::version_after_race(name, version, overwrite)
                    .map_err(|e| anyhow!(e))?;
            }
            Err(e) => return Err(e.into()),
        }
    };
    let root = File::open(ws).with_context(|| format!("open {}", ws.display()))?;
    let mut files = Vec::new();
    for (src, stored) in artifact::stored_paths(&lines, &selected) {
        // one read: what is hashed is what is stored
        let (bytes, meta) = read_nofollow(&root, &src)?;
        check(&src, &meta, &bytes).map_err(anyhow::Error::msg)?;
        let mode = meta.permissions().mode();
        let to = vdir.join("files").join(&stored);
        fs::create_dir_all(to.parent().expect("a file has a parent"))?;
        write_atomic(&to, &bytes, mode & 0o555)?;
        let (size, sha256) = digest_of(&bytes);
        files.push(FileEntry {
            path: stored,
            size,
            sha256,
        });
    }
    let total: u64 = files.iter().map(|f| f.size).sum();
    let count = files.len();
    let receipt = Receipt {
        name: name.to_string(),
        version,
        files,
        plan_digest: area.plan_digest.clone(),
        attempt_id: area.attempt.clone(),
        job: who.job.clone(),
        display: who.display.clone(),
        task: who.task.clone(),
        retention_days: retention,
        compression_level: compression,
        restored_from: None,
    };
    // the receipt is written last: a version without one is not published
    write_atomic(
        &vdir.join("receipt.json"),
        &serde_json::to_vec_pretty(&receipt)?,
        0o444,
    )?;
    Ok(format!(
        "uploaded `{name}` v{version}: {count} file{}, {total} bytes",
        if count == 1 { "" } else { "s" }
    ))
}

/// `actions/download-artifact@v4`. The note says what was done.
pub fn download(area: &Area, ws: &Path, with: &[(String, String)]) -> Result<String, String> {
    download_inner(area, ws, with).map_err(|e| format!("{e:#}"))
}

fn download_inner(area: &Area, ws: &Path, with: &[(String, String)]) -> Result<String> {
    artifact::validate_run(Kind::Download, with).map_err(|e| anyhow!(e))?;
    let get = |k: &str| {
        with.iter()
            .find(|(n, _)| n == k)
            .map(|(_, v)| v.as_str())
            .filter(|v| !v.trim().is_empty())
    };
    let merge =
        artifact::parse_bool("merge-multiple", get("merge-multiple")).map_err(|e| anyhow!(e))?;
    let base = artifact::workspace_rel(get("path").unwrap_or(""), &ws.to_string_lossy())
        .map_err(|e| anyhow!(e))?;
    let name = get("name");
    if let Some(n) = name {
        artifact::runtime_name(n).map_err(|e| anyhow!(e))?;
    }
    let available = published_names_in(&area.root);
    let picked = artifact::select_artifacts(&available, name, get("pattern")).map_err(|e| {
        anyhow!("{e} (the job must `needs` the job of its own workflow that uploads it)")
    })?;
    let direct = name.is_some() || merge;

    // every rejection comes before the first file is placed
    struct Item {
        artifact: String,
        src: PathBuf,
        dest: String,
        entry: FileEntry,
        mode: u32,
    }
    let mut items: Vec<Item> = Vec::new();
    for n in &picked {
        let (version, vdir, receipt) =
            latest_receipt(area, n).ok_or_else(|| anyhow!("artifact `{n}` has no receipt"))?;
        artifact::check_receipt(
            (n, version, &area.plan_digest),
            (&receipt.name, receipt.version, &receipt.plan_digest),
        )
        .map_err(|e| anyhow!(e))?;
        let dir = artifact::place_dir(&base, n, direct);
        for f in receipt.files {
            let dest = artifact::target_rel(&dir, &f.path).map_err(|e| anyhow!(e))?;
            let src = vdir
                .join("files")
                .join(artifact::stored_rel(&f.path).map_err(|e| anyhow!(e))?);
            let mode = fs::metadata(&src)
                .with_context(|| format!("read {}", src.display()))?
                .permissions()
                .mode();
            items.push(Item {
                artifact: n.clone(),
                src,
                dest,
                entry: f,
                mode,
            });
        }
    }
    let dests: Vec<String> = items.iter().map(|i| i.dest.clone()).collect();
    artifact::find_collision(&dests).map_err(|e| anyhow!(e))?;
    let load = |i: &Item| -> Result<Vec<u8>> {
        let bytes = fs::read(&i.src).with_context(|| format!("read {}", i.src.display()))?;
        let (size, sha) = digest_of(&bytes);
        artifact::check_digest(
            &i.entry.path,
            &i.artifact,
            (i.entry.size, &i.entry.sha256),
            (size, &sha),
        )
        .map_err(|e| anyhow!(e))?;
        Ok(bytes)
    };
    for i in &items {
        load(i)?;
        no_symlink_in(ws, &i.dest)?;
        artifact::check_path_kinds(&i.dest, &found_along(ws, &i.dest)).map_err(|e| anyhow!(e))?;
    }
    // placing: each file is read once more, checked against the receipt
    // again, and that same buffer is what is written
    let root = File::open(ws).with_context(|| format!("open {}", ws.display()))?;
    for i in &items {
        let bytes = load(i)?;
        place_nofollow(&root, &i.dest, &bytes, (i.mode & 0o755) | 0o200)?;
    }
    Ok(format!(
        "downloaded {} artifact{} ({} file{}) into `{}`",
        picked.len(),
        if picked.len() == 1 { "" } else { "s" },
        items.len(),
        if items.len() == 1 { "" } else { "s" },
        if base.is_empty() { "." } else { &base }
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::unix::fs::symlink;

    fn root(p: &Path) -> File {
        File::open(p).unwrap()
    }

    #[test]
    fn a_host_rejection_keeps_the_selected_file_out_of_the_artifact_area() {
        let scratch = tempfile::tempdir().unwrap();
        let workspace = scratch.path().join("workspace");
        fs::create_dir(&workspace).unwrap();
        fs::write(workspace.join("secret.env"), b"protected").unwrap();
        let area = Area::of_run(&scratch.path().join("run"), "plan", "ci");
        let who = Who {
            job: "ci::build".into(),
            display: "build".into(),
            task: None,
        };
        let reason = upload_checked(
            &area,
            &who,
            &workspace,
            &[("path".into(), "secret.env".into())],
            |path, meta, bytes| {
                assert_eq!(path, "secret.env");
                assert!(meta.is_file());
                assert_eq!(bytes, b"protected");
                Err("protected file".into())
            },
        )
        .unwrap_err();
        assert!(reason.contains("protected file"));
        assert!(!area.root.join("artifact/v1/files/secret.env").exists());
        assert!(published_names_in(&area.root).is_empty());
    }

    #[test]
    fn a_read_refuses_a_symlink_at_the_end_in_the_middle_or_a_non_file() {
        let t = tempfile::tempdir().unwrap();
        let ws = t.path().join("ws");
        let out = t.path().join("out");
        fs::create_dir_all(ws.join("d")).unwrap();
        fs::create_dir_all(&out).unwrap();
        fs::write(out.join("secret"), "s").unwrap();
        fs::write(ws.join("d/f"), "data").unwrap();
        symlink(out.join("secret"), ws.join("d/leaf")).unwrap();
        symlink(&out, ws.join("mid")).unwrap();
        let r = root(&ws);
        let (bytes, mode) = read_nofollow(&r, "d/f").unwrap();
        assert_eq!(bytes, b"data");
        assert_eq!(mode.permissions().mode() & 0o200, 0o200);
        let e = read_nofollow(&r, "d/leaf").unwrap_err().to_string();
        assert!(e.contains("symlink `leaf`"), "{e}");
        let e = read_nofollow(&r, "mid/secret").unwrap_err().to_string();
        assert!(e.contains("symlink `mid`"), "{e}");
        let e = read_nofollow(&r, "d").unwrap_err().to_string();
        assert!(e.contains("not a regular file"), "{e}");
        let e = read_nofollow(&r, "d/f/x").unwrap_err().to_string();
        assert!(e.contains("not a directory"), "{e}");
        assert!(read_nofollow(&r, "d/none").is_err());
        assert!(read_nofollow(&r, "").is_err());
        // a pipe is not read (and does not block)
        let fifo = cstr(&ws.join("p").to_string_lossy()).unwrap();
        assert_eq!(unsafe { libc::mkfifo(fifo.as_ptr(), 0o644) }, 0);
        let e = read_nofollow(&r, "p").unwrap_err().to_string();
        assert!(e.contains("not a regular file"), "{e}");
    }

    #[test]
    fn a_write_does_not_follow_a_link_left_as_its_temp_file_or_as_a_directory() {
        let t = tempfile::tempdir().unwrap();
        let ws = t.path().join("ws");
        let out = t.path().join("out");
        fs::create_dir_all(&ws).unwrap();
        fs::create_dir_all(&out).unwrap();
        fs::write(out.join("victim"), "keep").unwrap();
        // what sits under the old temp name is not ours: left as it is, not written through
        symlink(out.join("victim"), ws.join("f.lci-tmp")).unwrap();
        let r = root(&ws);
        place_nofollow(&r, "f", b"new", 0o644).unwrap();
        assert_eq!(fs::read(ws.join("f")).unwrap(), b"new");
        assert_eq!(fs::read(out.join("victim")).unwrap(), b"keep");
        assert!(ws.join("f.lci-tmp").symlink_metadata().is_ok());
        fs::write(ws.join("g.lci-tmp"), "someone's").unwrap();
        fs::create_dir(ws.join("h.lci-tmp")).unwrap();
        place_nofollow(&r, "g", b"x", 0o644).unwrap();
        place_nofollow(&r, "h", b"x", 0o644).unwrap();
        assert_eq!(fs::read(ws.join("g.lci-tmp")).unwrap(), b"someone's");
        assert!(ws.join("h.lci-tmp").is_dir());
        // every placement has a temp name of its own, and none stays
        for _ in 0..3 {
            place_nofollow(&r, "g", b"y", 0o644).unwrap();
        }
        let names: Vec<String> = fs::read_dir(&ws)
            .unwrap()
            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names.len(), 6, "{names:?}");
        // the name itself being a symlink or a directory is not replaced
        symlink(out.join("victim"), ws.join("l")).unwrap();
        let e = place_nofollow(&r, "l", b"x", 0o644)
            .unwrap_err()
            .to_string();
        assert!(e.contains("not replaced"), "{e}");
        fs::create_dir(ws.join("dd")).unwrap();
        assert!(place_nofollow(&r, "dd", b"x", 0o644).is_err());
        assert_eq!(fs::read(out.join("victim")).unwrap(), b"keep");
    }

    #[test]
    fn directories_are_made_one_at_a_time_and_a_link_among_them_stops_it() {
        let t = tempfile::tempdir().unwrap();
        let ws = t.path().join("ws");
        let out = t.path().join("out");
        fs::create_dir_all(ws.join("a")).unwrap();
        fs::create_dir_all(&out).unwrap();
        symlink(&out, ws.join("a/link")).unwrap();
        let r = root(&ws);
        place_nofollow(&r, "a/b/c/f", b"ok", 0o644).unwrap();
        assert_eq!(fs::read(ws.join("a/b/c/f")).unwrap(), b"ok");
        let e = place_nofollow(&r, "a/link/sub/f", b"x", 0o644)
            .unwrap_err()
            .to_string();
        assert!(e.contains("symlink `link`"), "{e}");
        assert!(fs::read_dir(&out).unwrap().next().is_none());
        // a file where a directory is needed
        let e = place_nofollow(&r, "a/b/c/f/g", b"x", 0o644)
            .unwrap_err()
            .to_string();
        assert!(e.contains("not a directory"), "{e}");
    }
}