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};
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]
)
}
fn workflow_root(run_dir: &Path, workflow: &str) -> PathBuf {
run_dir.join("artifacts").join(workflow_dir(workflow))
}
pub struct Area {
pub root: PathBuf,
pub plan_digest: String,
pub attempt: String,
}
impl Area {
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(),
}
}
}
pub struct Who {
pub job: String,
pub display: String,
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>,
#[serde(default)]
pub compression_level: Option<u32>,
#[serde(default)]
pub restored_from: Option<String>,
}
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
}
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)))
}
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(())
}
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
}
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()
}
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)))
}
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
}
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()
}
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<_>>(),
)
}
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))
}
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,
)
}
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)
}
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
}
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)?;
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) })
}
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();
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)?;
cvt(unsafe { libc::unlinkat(dir.as_raw_fd(), c.as_ptr(), 0) }).map(|_| ())
}
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}`")),
}
}
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)?;
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()
}
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))
}
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");
}
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)?);
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
}
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)
}
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)
}
pub fn upload(
area: &Area,
who: &Who,
ws: &Path,
with: &[(String, String)],
) -> Result<String, String> {
upload_checked(area, who, ws, with, |_, _, _| Ok(()))
}
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())
};
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)?;
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) {
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,
};
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" }
))
}
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;
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))?;
}
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());
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();
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());
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:?}");
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());
let e = place_nofollow(&r, "a/b/c/f/g", b"x", 0o644)
.unwrap_err()
.to_string();
assert!(e.contains("not a directory"), "{e}");
}
}