Skip to main content

runnerd_core/
docker.rs

1//! Runner containers through the Docker Engine API: the connection, container, volume and image requests, and the
2//! `DockerApi` trait the engine and the caches call. It decides nothing about scheduling, and a JIT config only
3//! ever reaches a container as a root-only file, never the create request or a log line.
4
5use std::collections::{BTreeMap, BTreeSet, HashMap};
6use std::fmt;
7use std::time::{Duration, Instant};
8
9use bollard::exec::{CreateExecOptions, StartExecResults};
10use std::pin::Pin;
11
12use bollard::models::{
13    ChangeType, ContainerConfig, ContainerCreateBody, CreateImageInfo, HostConfig, Mount,
14    NetworkCreateRequest, SystemInfo, VolumeCreateRequest,
15};
16use bollard::query_parameters::{
17    AttachContainerOptionsBuilder, CommitContainerOptionsBuilder, CreateContainerOptionsBuilder,
18    CreateImageOptionsBuilder, DataUsageOptionsBuilder, DownloadFromContainerOptionsBuilder,
19    EventsOptionsBuilder, InspectContainerOptionsBuilder, ListContainersOptionsBuilder,
20    ListImagesOptionsBuilder, ListVolumesOptionsBuilder, LogsOptionsBuilder,
21    RemoveContainerOptionsBuilder, RemoveImageOptionsBuilder, RemoveVolumeOptionsBuilder,
22    ResizeContainerTTYOptionsBuilder, UploadToContainerOptionsBuilder, WaitContainerOptionsBuilder,
23};
24use futures_util::stream::BoxStream;
25use futures_util::{StreamExt, TryStreamExt};
26use tracing::info;
27
28use crate::config::ByteSize;
29use crate::isolation::Isolation;
30
31pub use bollard::errors::Error;
32
33/// One runner container: a profile's image stage with the profile's limits.
34#[derive(Debug, Clone)]
35pub struct RunnerSpec {
36    /// The container's name; for a runner, also the runner's.
37    pub name: String,
38    /// The image, such as `runnerd-runner:base`.
39    pub image: String,
40    /// The profile, put on the `runnerd.profile` label.
41    pub profile: String,
42    /// The systemd slice the container is placed in.
43    pub slice: String,
44    /// CPUs the container may use.
45    pub cpus: u32,
46    /// Memory the container may use, swap included.
47    pub memory: ByteSize,
48    /// `NAME=value` environment variables.
49    pub env: Vec<String>,
50    /// Volumes to mount.
51    pub mounts: Vec<Mount>,
52    /// The network the container joins; `None` is Docker's default bridge.
53    pub network: Option<String>,
54    /// Labels on top of `runnerd.profile` and `runnerd.isolation`.
55    pub labels: BTreeMap<String, String>,
56    /// Replaces the image's entrypoint; `None` runs the runner.
57    pub entrypoint: Option<Vec<String>>,
58    /// The runner's just-in-time config, which the entrypoint reads from `JIT_CONFIG_PATH`.
59    pub jit_config: Option<JitConfig>,
60}
61
62/// Where `start` puts the just-in-time config in the container: a root-only file, never the create request,
63/// which Docker stores and clients may log.
64pub const JIT_CONFIG_PATH: &str = "/run/runnerd/jitconfig";
65
66/// What a runner container's environment holds to have its image answer the runner's sync calls without syncing
67/// (`[host] skip_syncs`, ADR 0141). runnerd sets it only for an image that carries `SKIP_SYNCS_LABEL`; an image
68/// without `runnerd-skip-syncs` would ignore it and sync.
69pub const SKIP_SYNCS: &str = "RUNNERD_SKIP_SYNCS=1";
70
71/// The label of a runner image whose entrypoint runs `runnerd-skip-syncs` (`image/Dockerfile`), with the version of
72/// what it answers to: `1`, `SKIP_SYNCS`. Runner images before it lack it, and their runners keep their syncs.
73pub const SKIP_SYNCS_LABEL: &str = "runnerd.skip-syncs";
74
75/// The label of a runner image whose entrypoint, asked `SKIP_SYNCS`, also mounts the slot's layers `volatile`, so
76/// unmounting them syncs nothing (ADR 0142): `1`. The crash guard is the same as for the sync calls, so runnerd asks
77/// by `SKIP_SYNCS_LABEL` alone; this one tells doctor what such a runner skips. Any value reads as volatile layers: a
78/// later version of the label still says the image has them.
79pub const VOLATILE_LAYERS_LABEL: &str = "runnerd.volatile-layers";
80
81/// Whether an image's labels say it answers `SKIP_SYNCS`.
82pub fn skips_syncs<S: std::hash::BuildHasher>(labels: &HashMap<String, String, S>) -> bool {
83    labels
84        .get(SKIP_SYNCS_LABEL)
85        .is_some_and(|version| version == "1")
86}
87
88/// What an image's runners skip when runnerd asks them, as its labels say: sync calls and their layers' unmount
89/// syncs, sync calls only, or nothing.
90pub fn image_syncs<S: std::hash::BuildHasher>(
91    labels: &HashMap<String, String, S>,
92) -> crate::config::ImageSyncs {
93    use crate::config::ImageSyncs;
94    if !skips_syncs(labels) {
95        ImageSyncs::Syncs
96    } else if labels.contains_key(VOLATILE_LAYERS_LABEL) {
97        ImageSyncs::Skips
98    } else {
99        ImageSyncs::SkipsCalls
100    }
101}
102
103/// A runner's just-in-time config from GitHub: credentials, never printed.
104#[derive(Clone)]
105pub struct JitConfig(pub String);
106
107impl fmt::Debug for JitConfig {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        f.write_str("JitConfig(<redacted>)")
110    }
111}
112
113/// What `inspect` reports about a runner container.
114#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct RunnerState {
116    /// Whether the container runs.
117    pub running: bool,
118    /// Its exit code, once it stopped.
119    pub exit_code: Option<i64>,
120    /// Whether the kernel killed a process in it for memory, as Docker saw.
121    pub oom_killed: bool,
122    /// True under `unsafe-dind`: a job in the container can take over the host.
123    pub privileged: bool,
124    /// The runtime Docker runs it with, such as `sysbox-runc`.
125    pub runtime: Option<String>,
126    /// Its main process on the host, while it runs.
127    pub pid: Option<u32>,
128    /// Its address on its first network.
129    pub address: Option<String>,
130    /// When it started, in milliseconds since the epoch.
131    pub started_at: Option<u64>,
132    /// When it stopped, in milliseconds since the epoch; `None` while it runs.
133    pub finished_at: Option<u64>,
134    /// Its memory cap in bytes; `None` when it has none.
135    pub memory: Option<u64>,
136    /// Its CPU limit in millicores; `None` when it has none.
137    pub cpus: Option<u64>,
138}
139
140/// What a command run in a container prints on stdout, as it prints it.
141pub type Output = BoxStream<'static, Result<Vec<u8>, Error>>;
142
143/// An image `images` found.
144#[derive(Debug, Clone, PartialEq, Eq)]
145pub struct Image {
146    /// Its first tag, such as `runnerd-record:42`; its ID when it has none.
147    pub reference: String,
148    /// Its labels.
149    pub labels: HashMap<String, String>,
150    /// The disk it takes beyond its parent image: what removing it frees.
151    pub bytes: u64,
152}
153
154/// An image `tagged_images` found.
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct TaggedImage {
157    /// Its ID, such as `sha256:224e…`.
158    pub id: String,
159    /// Its tags, such as `ghcr.io/ayagmar/runnerd-runner:node-0.1.3`.
160    pub tags: Vec<String>,
161    /// The registry digests it was pulled by, such as `ghcr.io/ayagmar/runnerd-runner@sha256:9e79…`.
162    pub digests: Vec<String>,
163}
164
165/// A file `files` read from a container.
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct File {
168    /// Its name in the directory read, such as `Worker_20261031-120000-utc.log`.
169    pub name: String,
170    /// What it holds, as text: invalid UTF-8 is replaced. Cut short past `FILES_LIMIT`.
171    pub contents: String,
172}
173
174/// What `files` read of a directory.
175#[derive(Debug, Clone, Default, PartialEq, Eq)]
176pub struct Files {
177    /// The files read, in the archive's order.
178    pub files: Vec<File>,
179    /// Whether they are all there and whole: the archive ended as a tar does, and none was cut at `FILES_LIMIT`. A
180    /// file missing from an incomplete read may be in the directory.
181    pub complete: bool,
182}
183
184/// The most of the files `files` keeps, together. What it skips, such as a runner's job log pages below `_diag`,
185/// does not count: it is read through and dropped.
186pub(crate) const FILES_LIMIT: usize = 16 << 20;
187
188/// A container's terminal, attached once it was created and before it started, so none of its output is missed.
189pub struct Tty {
190    /// The container's ID.
191    pub container: String,
192    /// What the terminal shows, as it shows it; it ends when the container's process exits.
193    pub output: BoxStream<'static, Result<Vec<u8>, Error>>,
194    /// What is typed into it.
195    pub input: Pin<Box<dyn tokio::io::AsyncWrite + Send>>,
196}
197
198impl fmt::Debug for Tty {
199    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200        f.debug_struct("Tty")
201            .field("container", &self.container)
202            .finish_non_exhaustive()
203    }
204}
205
206/// A terminal's size.
207#[derive(Debug, Clone, Copy, PartialEq, Eq)]
208pub struct TtySize {
209    /// Columns.
210    pub cols: u16,
211    /// Rows.
212    pub rows: u16,
213}
214
215/// The CPU shares of a runner's container or a record's shell with `cpus` CPUs: 1024 a CPU, within the 2 to 262144
216/// Docker takes, which it writes as the container's `cpu.weight`. They sit in the slice beside each other, so when the
217/// slice's CPUs are all in use they share them in proportion to their `cpus`, not evenly; a container with work still
218/// gets at most its `cpus` (`nano_cpus`). The mirror keeps Docker's default weight, which every job's pulls go
219/// through, and programs outside the slice share the host's CPUs with the slice by its own weight (ADR 0079).
220pub fn cpu_shares(cpus: u32) -> i64 {
221    (i64::from(cpus) * 1024).clamp(2, 262_144)
222}
223
224/// The create-container request for a runner container under an isolation backend.
225pub fn create_body(spec: &RunnerSpec, isolation: &dyn Isolation) -> ContainerCreateBody {
226    // Clamped to 8 EiB, the most Docker takes.
227    let memory = i64::try_from(spec.memory.0).unwrap_or(i64::MAX);
228    let mut host_config = HostConfig {
229        cgroup_parent: Some(spec.slice.clone()),
230        memory: Some(memory),
231        // Swap equal to memory: the container gets no swap on top of its memory.
232        memory_swap: Some(memory),
233        nano_cpus: Some(i64::from(spec.cpus) * 1_000_000_000),
234        cpu_shares: Some(cpu_shares(spec.cpus)),
235        init: Some(true),
236        mounts: Some(spec.mounts.clone()),
237        network_mode: spec.network.clone(),
238        ..Default::default()
239    };
240    isolation.configure(&mut host_config);
241    let mut labels: HashMap<String, String> = spec.labels.clone().into_iter().collect();
242    labels.insert("runnerd.profile".to_owned(), spec.profile.clone());
243    labels.insert("runnerd.isolation".to_owned(), isolation.name().to_owned());
244    ContainerCreateBody {
245        image: Some(spec.image.clone()),
246        env: Some(spec.env.clone()),
247        entrypoint: spec.entrypoint.clone(),
248        labels: Some(labels),
249        host_config: Some(host_config),
250        ..Default::default()
251    }
252}
253
254/// A container `list` found.
255#[derive(Debug, Clone, PartialEq, Eq)]
256pub struct Container {
257    /// The Docker container ID.
258    pub id: String,
259    /// Without Docker's leading `/`.
260    pub name: String,
261    /// Whether it runs.
262    pub running: bool,
263    /// Its labels.
264    pub labels: HashMap<String, String>,
265}
266
267/// A runner start that failed, with the container it may have left: Docker may have made, or even started, it before
268/// the request failed, as when its answer is lost.
269#[derive(Debug)]
270pub struct StartError {
271    /// The container the start left, by ID, or by name when Docker could not say whether one exists; `None` when
272    /// none does. It may run.
273    pub container: Option<String>,
274    /// Whether Docker may have started the container before the start failed: its runner may have taken a job,
275    /// even if the container is gone now.
276    pub may_have_run: bool,
277    /// Why the start failed.
278    pub error: Error,
279}
280
281impl fmt::Display for StartError {
282    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283        self.error.fmt(f)
284    }
285}
286
287impl std::error::Error for StartError {
288    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
289        Some(&self.error)
290    }
291}
292
293/// For callers that go on whether or not a container was left.
294impl From<StartError> for Error {
295    fn from(failed: StartError) -> Error {
296        failed.error
297    }
298}
299
300/// Whether Docker answered a request with a refusal, so it did nothing: a 4xx answer. Any other error may come after
301/// Docker carried the request out.
302fn refused(error: &Error) -> bool {
303    matches!(
304        error,
305        Error::DockerResponseServerError {
306            status_code: 400..=499,
307            ..
308        }
309    )
310}
311
312/// Whether a Docker error says the Engine did not answer at all: its socket is gone, refuses connections or closed
313/// the connection, as while the machine shuts down. Not an answer the Engine gave, nor a timeout, which a busy Engine
314/// also gives.
315pub fn is_unreachable(error: &Error) -> bool {
316    matches!(
317        error,
318        Error::HyperLegacyError { .. }
319            | Error::HyperResponseError { .. }
320            | Error::IOError { .. }
321            | Error::SocketNotFoundError(_)
322    )
323}
324
325/// Whether Docker answered that it cannot or will not do what was asked: a 4xx, or 501 for what it lacks, such as
326/// the log of a container whose log driver keeps none. Asking again gets the same answer; any other error may not.
327pub fn is_declined(error: &Error) -> bool {
328    matches!(
329        error,
330        Error::DockerResponseServerError {
331            status_code: 400..=499 | 501,
332            ..
333        }
334    )
335}
336
337/// Whether a Docker error says the container, volume or image does not exist.
338pub fn is_not_found(error: &Error) -> bool {
339    matches!(
340        error,
341        Error::DockerResponseServerError {
342            status_code: 404,
343            ..
344        }
345    )
346}
347
348/// Whether Docker refused because it is removing the container already, as after a `docker rm -f`: it says so only in
349/// the words of its 409, which a running container's refusal shares. It may still be stopping.
350pub fn is_being_removed(error: &Error) -> bool {
351    matches!(
352        error,
353        Error::DockerResponseServerError { status_code: 409, message }
354            if message.contains("is already in progress") || message.contains("marked for removal")
355    )
356}
357
358/// Whether Docker refused an exec because the container stopped, as one that ended while it was adopted: its log
359/// is read once it is released.
360pub fn is_not_running(error: &Error) -> bool {
361    matches!(
362        error,
363        Error::DockerResponseServerError { status_code: 409, message } if message.ends_with(" is not running")
364    )
365}
366
367/// Whether a container's log is out of reach because it is gone or going. A removal under way can still fail, so this
368/// says nothing of whether the container will go.
369pub fn is_log_gone(error: &Error) -> bool {
370    is_not_found(error) || is_being_removed(error)
371}
372
373/// The Docker Engine calls the scheduler and the caches make. `Docker` is the real one; the engine's tests use a
374/// fake.
375pub trait DockerApi: Send + Sync {
376    /// Creates and starts a runner container; returns its ID. A start that fails removes what it made, and says
377    /// which container is left when it cannot.
378    fn start(
379        &self,
380        spec: &RunnerSpec,
381        isolation: &dyn Isolation,
382    ) -> impl Future<Output = Result<String, StartError>> + Send;
383    /// A container's state and isolation.
384    fn inspect(&self, container: &str) -> impl Future<Output = Result<RunnerState, Error>> + Send;
385    /// Removes a runner container, stopping it first if it runs.
386    fn remove(&self, container: &str) -> impl Future<Output = Result<(), Error>> + Send;
387    /// Creates and starts a container that is not a runner, such as the mirror; returns its ID.
388    fn run(
389        &self,
390        name: &str,
391        body: ContainerCreateBody,
392    ) -> impl Future<Output = Result<String, Error>> + Send;
393    /// Every container, running or not, that has the label.
394    fn list(&self, label: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
395    /// The IDs of runner containers (labelled `runnerd.job`) as each stops, from Docker's events. A hint: Docker sends
396    /// an event once at most, none while the stream is down, and keeps none to replay. The stream fails or ends with
397    /// the connection, or when Docker restarts.
398    fn exits(&self) -> BoxStream<'static, Result<String, Error>>;
399    /// Waits for a container to exit; returns its exit code.
400    fn wait(&self, container: &str) -> impl Future<Output = Result<i64, Error>> + Send;
401    /// A container's output so far, stdout and stderr interleaved.
402    fn logs(&self, container: &str) -> impl Future<Output = Result<String, Error>> + Send;
403    /// A container's output since `since` (seconds since the epoch), each line prefixed with Docker's RFC 3339
404    /// timestamp and a space.
405    fn logs_since(
406        &self,
407        container: &str,
408        since: u64,
409    ) -> impl Future<Output = Result<String, Error>> + Send;
410    /// Runs a command as root in a running container; its stdout streams until it exits or the container stops.
411    /// Its stderr is dropped.
412    fn exec(
413        &self,
414        container: &str,
415        command: Vec<String>,
416    ) -> impl Future<Output = Result<Output, Error>> + Send;
417    /// Whether Docker has an image.
418    fn image_present(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
419    /// Whether Docker has an image and it answers `SKIP_SYNCS`, as its labels say (`skips_syncs`).
420    fn image_skips_syncs(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
421    /// Pulls an image, logging its progress every few seconds.
422    fn pull(&self, image: &str) -> impl Future<Output = Result<(), Error>> + Send;
423    /// Creates a bridge network unless it exists.
424    fn ensure_network(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
425    /// Creates a volume unless it exists.
426    fn ensure_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
427    /// Removes a volume; a volume that does not exist is not an error. Docker refuses one a container mounts, even
428    /// a stopped one.
429    fn remove_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
430    /// Every container, running or not, that mounts the volume.
431    fn holders(&self, volume: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
432    /// The disk size of every volume Docker knows, by name. Slow: Docker measures each volume.
433    fn volume_sizes(&self) -> impl Future<Output = Result<BTreeMap<String, u64>, Error>> + Send;
434    /// The names of the volumes Docker has whose names start with `prefix`. Fast: nothing is measured.
435    fn volumes(&self, prefix: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
436    /// The paths a container's own layer added or changed, not those it deleted.
437    fn changes(&self, container: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
438    /// The bytes a container's own layer holds.
439    fn written(&self, container: &str) -> impl Future<Output = Result<u64, Error>> + Send;
440    /// The files directly in a container's `directory` whose names start with `prefix`, not those in the
441    /// directories below it, read from its filesystem whether it runs or stopped, without starting it.
442    fn files(
443        &self,
444        container: &str,
445        directory: &str,
446        prefix: &str,
447    ) -> impl Future<Output = Result<Files, Error>> + Send;
448    /// Commits a stopped container's own layer as the image `reference` (`repository:tag`) with `labels`; returns
449    /// the disk the image takes beyond its parent.
450    fn commit(
451        &self,
452        container: &str,
453        reference: &str,
454        labels: BTreeMap<String, String>,
455    ) -> impl Future<Output = Result<u64, Error>> + Send;
456    /// Every image with the label.
457    fn images(&self, label: &str) -> impl Future<Output = Result<Vec<Image>, Error>> + Send;
458    /// The ID of the image a reference (tag, digest or ID) names; `None` when Docker does not have it.
459    fn image_id(
460        &self,
461        reference: &str,
462    ) -> impl Future<Output = Result<Option<String>, Error>> + Send;
463    /// Every image Docker has a tag for.
464    fn tagged_images(&self) -> impl Future<Output = Result<Vec<TaggedImage>, Error>> + Send;
465    /// The image ID of every container, running or stopped.
466    fn container_images(&self) -> impl Future<Output = Result<BTreeSet<String>, Error>> + Send;
467    /// Removes an image and the layers only it used; one that does not exist is not an error.
468    fn remove_image(&self, reference: &str) -> impl Future<Output = Result<(), Error>> + Send;
469    /// The free disk where Docker keeps its images and containers: on a root in a file, on a loop device, the least
470    /// of its own and the free disk under the file (`observe::disk::free_space`).
471    fn free_space(&self) -> impl Future<Output = Result<u64, Error>> + Send;
472    /// Where Docker keeps its images, containers and volumes (`DockerRootDir`), whose disks runners write to.
473    fn root_dir(&self) -> impl Future<Output = Result<String, Error>> + Send;
474    /// Creates a container with a terminal of `size`, attaches to it and starts it. The container's stdin closes
475    /// when `Tty::input` is dropped.
476    fn shell(
477        &self,
478        name: &str,
479        body: ContainerCreateBody,
480        size: TtySize,
481    ) -> impl Future<Output = Result<Tty, Error>> + Send;
482}
483
484/// The local Docker Engine.
485#[derive(Debug)]
486pub struct Docker {
487    api: bollard::Docker,
488}
489
490impl Docker {
491    /// Removes the container a failed start `made`, if any; the start's `error` is the one worth reporting. The
492    /// container stays in the answer unless its removal is confirmed.
493    async fn clean_up(&self, made: Option<String>, error: Error, may_have_run: bool) -> StartError {
494        let container = match made {
495            Some(container) => match self.remove(&container).await {
496                Err(failed) if !is_not_found(&failed) => Some(container),
497                _ => None,
498            },
499            None => None,
500        };
501        StartError {
502            container,
503            may_have_run,
504            error,
505        }
506    }
507
508    /// Connects to the Docker Engine at `DOCKER_HOST`, or its local socket. Only `unix://` hosts are supported.
509    pub fn connect() -> Result<Docker, Error> {
510        connect_to(std::env::var("DOCKER_HOST").ok().as_deref())
511    }
512
513    /// The Engine's system information.
514    pub async fn info(&self) -> Result<SystemInfo, Error> {
515        self.api.info().await
516    }
517
518    /// An image's labels; `None` when Docker does not have it.
519    pub async fn image_labels(
520        &self,
521        image: &str,
522    ) -> Result<Option<HashMap<String, String>>, Error> {
523        match self.api.inspect_image(image).await {
524            Ok(inspect) => Ok(Some(
525                inspect
526                    .config
527                    .and_then(|config| config.labels)
528                    .unwrap_or_default(),
529            )),
530            Err(error) if is_not_found(&error) => Ok(None),
531            Err(error) => Err(error),
532        }
533    }
534
535    /// Every container, running or not, that `filter` matches with `value`, as `docker ps --all --filter` does.
536    async fn containers(&self, filter: &str, value: &str) -> Result<Vec<Container>, Error> {
537        let options = ListContainersOptionsBuilder::new()
538            .all(true)
539            .filters(&HashMap::from([(filter, vec![value])]))
540            .build();
541        let containers = self.api.list_containers(Some(options)).await?;
542
543        Ok(containers
544            .into_iter()
545            .map(|container| Container {
546                id: container.id.unwrap_or_default(),
547                name: container
548                    .names
549                    .and_then(|names| names.into_iter().next())
550                    .unwrap_or_default()
551                    .trim_start_matches('/')
552                    .to_owned(),
553                running: container.state
554                    == Some(bollard::models::ContainerSummaryStateEnum::RUNNING),
555                labels: container.labels.unwrap_or_default(),
556            })
557            .collect())
558    }
559
560    /// Asks the image's registry for its manifest, without pulling it, and returns the platforms it is built for,
561    /// such as `linux/amd64`. An error means `pull` could not get it.
562    pub async fn registry_platforms(&self, image: &str) -> Result<Vec<String>, Error> {
563        let inspect = self.api.inspect_registry_image(image, None).await?;
564        Ok(inspect
565            .platforms
566            .into_iter()
567            .filter_map(|platform| Some(format!("{}/{}", platform.os?, platform.architecture?)))
568            // Attestation manifests in an index carry the platform unknown/unknown.
569            .filter(|platform| platform != "unknown/unknown")
570            .collect())
571    }
572}
573
574impl DockerApi for Docker {
575    async fn start(
576        &self,
577        spec: &RunnerSpec,
578        isolation: &dyn Isolation,
579    ) -> Result<String, StartError> {
580        let options = CreateContainerOptionsBuilder::new()
581            .name(&spec.name)
582            .build();
583        let id = match self
584            .api
585            .create_container(Some(options), create_body(spec, isolation))
586            .await
587        {
588            Ok(created) => created.id,
589            Err(error) if refused(&error) => {
590                return Err(StartError {
591                    container: None,
592                    may_have_run: false,
593                    error,
594                });
595            }
596            // Docker may have made it and lost the answer: its name, unique to this start, finds it.
597            Err(error) => {
598                let made = match self.api.inspect_container(&spec.name, None).await {
599                    Ok(found) => Some(found.id.unwrap_or_else(|| spec.name.clone())),
600                    Err(missing) if is_not_found(&missing) => None,
601                    Err(_) => Some(spec.name.clone()),
602                };
603                // Made at most, never started.
604                return Err(self.clean_up(made, error, false).await);
605            }
606        };
607        let uploaded = async {
608            if let Some(jit_config) = &spec.jit_config {
609                let options = UploadToContainerOptionsBuilder::new().path("/").build();
610                let tar = jit_config_tar(jit_config)?;
611                // As a stream: bollard traces a full body's bytes, but a stream only as "(stream)".
612                let tar = futures_util::stream::once(std::future::ready(tar.into()));
613                self.api
614                    .upload_to_container(&id, Some(options), bollard::body_stream(tar))
615                    .await?;
616            }
617            Ok::<(), Error>(())
618        };
619        if let Err(error) = uploaded.await {
620            return Err(self.clean_up(Some(id), error, false).await);
621        }
622        // A start whose answer is lost may have started it.
623        if let Err(error) = self.api.start_container(&id, None).await {
624            return Err(self.clean_up(Some(id), error, true).await);
625        }
626        Ok(id)
627    }
628
629    async fn inspect(&self, container: &str) -> Result<RunnerState, Error> {
630        let response = self.api.inspect_container(container, None).await?;
631        let state = response.state.unwrap_or_default();
632        let host_config = response.host_config.unwrap_or_default();
633        let address = response
634            .network_settings
635            .and_then(|settings| settings.networks)
636            .and_then(|networks| {
637                networks
638                    .into_values()
639                    .find_map(|network| network.ip_address.filter(|address| !address.is_empty()))
640            });
641        Ok(RunnerState {
642            running: state.running.unwrap_or(false),
643            exit_code: state.exit_code,
644            oom_killed: state.oom_killed.unwrap_or(false),
645            privileged: host_config.privileged.unwrap_or(false),
646            runtime: host_config.runtime,
647            pid: state
648                .pid
649                .and_then(|pid| u32::try_from(pid).ok())
650                .filter(|pid| *pid > 0),
651            address,
652            started_at: state.started_at.as_deref().and_then(crate::time::parse_ms),
653            // Docker says 0001-01-01 for one that never stopped, which reads as none.
654            finished_at: state.finished_at.as_deref().and_then(crate::time::parse_ms),
655            memory: host_config
656                .memory
657                .and_then(|memory| u64::try_from(memory).ok())
658                .filter(|memory| *memory > 0),
659            cpus: host_config
660                .nano_cpus
661                .and_then(|nano| u64::try_from(nano / 1_000_000).ok())
662                .filter(|cpus| *cpus > 0),
663        })
664    }
665
666    async fn remove(&self, container: &str) -> Result<(), Error> {
667        // With its anonymous volumes, such as a shell's /state.
668        let options = RemoveContainerOptionsBuilder::new()
669            .force(true)
670            .v(true)
671            .build();
672        self.api.remove_container(container, Some(options)).await
673    }
674
675    async fn run(&self, name: &str, body: ContainerCreateBody) -> Result<String, Error> {
676        let options = CreateContainerOptionsBuilder::new().name(name).build();
677        let id = self.api.create_container(Some(options), body).await?.id;
678        if let Err(error) = self.api.start_container(&id, None).await {
679            let _ = self.remove(&id).await;
680            return Err(error);
681        }
682        Ok(id)
683    }
684
685    async fn list(&self, label: &str) -> Result<Vec<Container>, Error> {
686        self.containers("label", label).await
687    }
688
689    async fn holders(&self, volume: &str) -> Result<Vec<Container>, Error> {
690        self.containers("volume", volume).await
691    }
692
693    fn exits(&self) -> BoxStream<'static, Result<String, Error>> {
694        let filters = HashMap::from([
695            ("type", vec!["container"]),
696            ("event", vec!["die"]),
697            ("label", vec!["runnerd.job"]),
698        ]);
699        let options = EventsOptionsBuilder::new().filters(&filters).build();
700        self.api
701            .events(Some(options))
702            .try_filter_map(|event| std::future::ready(Ok(event.actor.and_then(|actor| actor.id))))
703            .boxed()
704    }
705
706    async fn wait(&self, container: &str) -> Result<i64, Error> {
707        let options = WaitContainerOptionsBuilder::new()
708            .condition("not-running")
709            .build();
710        let mut responses = self.api.wait_container(container, Some(options));
711        match responses.next().await {
712            Some(Ok(response)) => Ok(response.status_code),
713            // Bollard reports a non-zero exit as an error.
714            Some(Err(Error::DockerContainerWaitError { code, .. })) => Ok(code),
715            Some(Err(error)) => Err(error),
716            None => Ok(0),
717        }
718    }
719
720    async fn logs(&self, container: &str) -> Result<String, Error> {
721        let options = LogsOptionsBuilder::new().stdout(true).stderr(true).build();
722        let chunks: Vec<_> = self
723            .api
724            .logs(container, Some(options))
725            .try_collect()
726            .await?;
727        Ok(chunks.iter().map(ToString::to_string).collect())
728    }
729
730    async fn logs_since(&self, container: &str, since: u64) -> Result<String, Error> {
731        let options = LogsOptionsBuilder::new()
732            .stdout(true)
733            .stderr(true)
734            .timestamps(true)
735            .since(i32::try_from(since).unwrap_or(i32::MAX))
736            .build();
737        let chunks: Vec<_> = self
738            .api
739            .logs(container, Some(options))
740            .try_collect()
741            .await?;
742        Ok(chunks.iter().map(ToString::to_string).collect())
743    }
744
745    async fn exec(&self, container: &str, command: Vec<String>) -> Result<Output, Error> {
746        let options = CreateExecOptions {
747            cmd: Some(command),
748            user: Some("root".to_owned()),
749            attach_stdout: Some(true),
750            attach_stderr: Some(true),
751            ..Default::default()
752        };
753        let exec = self.api.create_exec(container, options).await?;
754        match self.api.start_exec(&exec.id, None).await? {
755            StartExecResults::Attached { output, .. } => Ok(output
756                .try_filter_map(|output| {
757                    std::future::ready(Ok(match output {
758                        bollard::container::LogOutput::StdOut { message } => Some(message.to_vec()),
759                        _ => None,
760                    }))
761                })
762                .boxed()),
763            StartExecResults::Detached => Ok(futures_util::stream::empty().boxed()),
764        }
765    }
766
767    async fn image_present(&self, image: &str) -> Result<bool, Error> {
768        Ok(self.image_labels(image).await?.is_some())
769    }
770
771    async fn image_skips_syncs(&self, image: &str) -> Result<bool, Error> {
772        Ok(self
773            .image_labels(image)
774            .await?
775            .is_some_and(|labels| skips_syncs(&labels)))
776    }
777
778    async fn pull(&self, image: &str) -> Result<(), Error> {
779        let options = CreateImageOptionsBuilder::new().from_image(image).build();
780        let mut stream = self.api.create_image(Some(options), None, None);
781        let mut progress = PullProgress::default();
782        let mut logged = Instant::now();
783        info!("pulling {image}");
784        while let Some(update) = stream.try_next().await? {
785            progress.update(&update);
786            if logged.elapsed() >= PULL_LOG_INTERVAL {
787                logged = Instant::now();
788                info!("pulling {image}: {progress}");
789            }
790        }
791        info!("pulled {image}");
792        Ok(())
793    }
794
795    async fn ensure_network(&self, name: &str) -> Result<(), Error> {
796        match self.api.inspect_network(name, None).await {
797            Err(error) if is_not_found(&error) => {
798                let request = NetworkCreateRequest {
799                    name: name.to_owned(),
800                    driver: Some("bridge".to_owned()),
801                    ..Default::default()
802                };
803                self.api.create_network(request).await.map(|_| ())
804            }
805            other => other.map(|_| ()),
806        }
807    }
808
809    async fn ensure_volume(&self, name: &str) -> Result<(), Error> {
810        // Creating a volume that exists is a no-op in Docker.
811        let request = VolumeCreateRequest {
812            name: Some(name.to_owned()),
813            ..Default::default()
814        };
815        self.api.create_volume(request).await.map(|_| ())
816    }
817
818    async fn remove_volume(&self, name: &str) -> Result<(), Error> {
819        let options = RemoveVolumeOptionsBuilder::new().build();
820        match self.api.remove_volume(name, Some(options)).await {
821            Err(error) if is_not_found(&error) => Ok(()),
822            other => other,
823        }
824    }
825
826    async fn volumes(&self, prefix: &str) -> Result<Vec<String>, Error> {
827        // Docker's name filter matches anywhere in the name; the prefix is checked here.
828        let options = ListVolumesOptionsBuilder::new()
829            .filters(&HashMap::from([("name", vec![prefix])]))
830            .build();
831        Ok(self
832            .api
833            .list_volumes(Some(options))
834            .await?
835            .volumes
836            .unwrap_or_default()
837            .into_iter()
838            .map(|volume| volume.name)
839            .filter(|name| name.starts_with(prefix))
840            .collect())
841    }
842
843    async fn volume_sizes(&self) -> Result<BTreeMap<String, u64>, Error> {
844        // Without a type filter: bollard cannot encode one in the query.
845        let options = DataUsageOptionsBuilder::new().verbose(true).build();
846        let usage = self.api.df(Some(options)).await?;
847        // API 1.53 leaves the items untyped; they are volumes as `GET /volumes` describes them.
848        Ok(usage
849            .volume_usage
850            .and_then(|volumes| volumes.items)
851            .unwrap_or_default()
852            .into_iter()
853            .filter_map(|volume| {
854                let name = volume.get("Name")?.as_str()?.to_owned();
855                let size = volume.get("UsageData")?.get("Size")?.as_u64()?;
856                Some((name, size))
857            })
858            .collect())
859    }
860
861    async fn changes(&self, container: &str) -> Result<Vec<String>, Error> {
862        Ok(self
863            .api
864            .container_changes(container)
865            .await?
866            .unwrap_or_default()
867            .into_iter()
868            .filter(|change| change.kind != ChangeType::_2)
869            .map(|change| change.path)
870            .collect())
871    }
872
873    async fn written(&self, container: &str) -> Result<u64, Error> {
874        let options = InspectContainerOptionsBuilder::new().size(true).build();
875        let response = self.api.inspect_container(container, Some(options)).await?;
876        Ok(response
877            .size_rw
878            .and_then(|bytes| u64::try_from(bytes).ok())
879            .unwrap_or(0))
880    }
881
882    async fn files(&self, container: &str, directory: &str, prefix: &str) -> Result<Files, Error> {
883        let options = DownloadFromContainerOptionsBuilder::new()
884            .path(directory)
885            .build();
886        let mut archive = self.api.download_from_container(container, Some(options));
887        // The archive is read as it arrives, by a blocking reader, so what it skips is never held.
888        let (chunks, received) = tokio::sync::mpsc::channel(4);
889        let prefix = prefix.to_owned();
890        let reader = tokio::task::spawn_blocking(move || {
891            files_in(
892                Chunks {
893                    received,
894                    chunk: Vec::new(),
895                    at: 0,
896                },
897                &prefix,
898            )
899        });
900        let mut failed = None;
901        while let Some(chunk) = archive.next().await {
902            match chunk {
903                // Fails once the reader is done, at the archive's end or `FILES_LIMIT`.
904                Ok(chunk) => {
905                    if chunks.send(chunk.to_vec()).await.is_err() {
906                        break;
907                    }
908                }
909                Err(error) => {
910                    failed = Some(error);
911                    break;
912                }
913            }
914        }
915        drop(chunks);
916        let files = match reader.await {
917            Ok(files) => files,
918            Err(error) => match error.try_into_panic() {
919                Ok(panic) => std::panic::resume_unwind(panic),
920                // Only while the runtime shuts down.
921                Err(_) => Files::default(),
922            },
923        };
924        match failed {
925            Some(error) => Err(error),
926            None => Ok(files),
927        }
928    }
929
930    async fn commit(
931        &self,
932        container: &str,
933        reference: &str,
934        labels: BTreeMap<String, String>,
935    ) -> Result<u64, Error> {
936        let (repository, tag) = reference.rsplit_once(':').unwrap_or((reference, "latest"));
937        let options = CommitContainerOptionsBuilder::new()
938            .container(container)
939            .repo(repository)
940            .tag(tag)
941            .pause(false)
942            .build();
943        let config = ContainerConfig {
944            labels: Some(labels.into_iter().collect()),
945            ..Default::default()
946        };
947        // The image the container runs is the record's parent.
948        let parent = match self.api.inspect_container(container, None).await?.image {
949            Some(parent) => self.api.inspect_image(&parent).await?.size.unwrap_or(0),
950            None => 0,
951        };
952        let id = self.api.commit_container(options, config).await?.id;
953        let size = self.api.inspect_image(&id).await?.size.unwrap_or(0);
954        Ok(u64::try_from(size - parent).unwrap_or(0))
955    }
956
957    async fn images(&self, label: &str) -> Result<Vec<Image>, Error> {
958        let options = ListImagesOptionsBuilder::new()
959            .filters(&HashMap::from([("label", vec![label])]))
960            .build();
961        let images = self.api.list_images(Some(options)).await?;
962        let mut parents: HashMap<String, i64> = HashMap::new();
963        let mut found = Vec::new();
964        for image in images {
965            let parent = if image.parent_id.is_empty() {
966                0
967            } else if let Some(size) = parents.get(&image.parent_id) {
968                *size
969            } else {
970                let size = self
971                    .api
972                    .inspect_image(&image.parent_id)
973                    .await
974                    .ok()
975                    .and_then(|parent| parent.size)
976                    .unwrap_or(0);
977                parents.insert(image.parent_id.clone(), size);
978                size
979            };
980            found.push(Image {
981                reference: image.repo_tags.into_iter().next().unwrap_or(image.id),
982                labels: image.labels,
983                bytes: u64::try_from(image.size - parent).unwrap_or(0),
984            });
985        }
986        Ok(found)
987    }
988
989    async fn image_id(&self, reference: &str) -> Result<Option<String>, Error> {
990        match self.api.inspect_image(reference).await {
991            Ok(inspect) => Ok(inspect.id),
992            Err(error) if is_not_found(&error) => Ok(None),
993            Err(error) => Err(error),
994        }
995    }
996
997    async fn tagged_images(&self) -> Result<Vec<TaggedImage>, Error> {
998        let images = self
999            .api
1000            .list_images(Some(ListImagesOptionsBuilder::new().build()))
1001            .await?;
1002        Ok(images
1003            .into_iter()
1004            .map(|image| TaggedImage {
1005                id: image.id,
1006                tags: image
1007                    .repo_tags
1008                    .into_iter()
1009                    .filter(|tag| tag != "<none>:<none>")
1010                    .collect(),
1011                digests: image
1012                    .repo_digests
1013                    .into_iter()
1014                    .filter(|digest| digest != "<none>@<none>")
1015                    .collect(),
1016            })
1017            .filter(|image| !image.tags.is_empty())
1018            .collect())
1019    }
1020
1021    async fn container_images(&self) -> Result<BTreeSet<String>, Error> {
1022        let options = ListContainersOptionsBuilder::new().all(true).build();
1023        let containers = self.api.list_containers(Some(options)).await?;
1024        Ok(containers
1025            .into_iter()
1026            .filter_map(|container| container.image_id)
1027            .collect())
1028    }
1029
1030    async fn remove_image(&self, reference: &str) -> Result<(), Error> {
1031        let options = RemoveImageOptionsBuilder::new().build();
1032        match self.api.remove_image(reference, Some(options), None).await {
1033            Err(error) if is_not_found(&error) => Ok(()),
1034            other => other.map(|_| ()),
1035        }
1036    }
1037
1038    async fn root_dir(&self) -> Result<String, Error> {
1039        Ok(self.api.info().await?.docker_root_dir.unwrap_or_default())
1040    }
1041
1042    async fn free_space(&self) -> Result<u64, Error> {
1043        let root = self.api.info().await?.docker_root_dir.unwrap_or_default();
1044        // Only the directories above Docker's root need to be searchable: the daemon's user cannot read the root.
1045        let statfs = |path: &std::path::Path| {
1046            let stat = rustix::fs::statvfs(path).map_err(std::io::Error::from)?;
1047            Ok(stat.f_bavail.saturating_mul(stat.f_frsize))
1048        };
1049        let paths = crate::observe::metrics::Paths::default();
1050        crate::observe::disk::free_space(&paths, std::path::Path::new(&root), statfs).map_err(
1051            |error| Error::IOError {
1052                err: std::io::Error::other(format!("cannot measure the disk of {root}: {error}")),
1053            },
1054        )
1055    }
1056
1057    async fn shell(
1058        &self,
1059        name: &str,
1060        body: ContainerCreateBody,
1061        size: TtySize,
1062    ) -> Result<Tty, Error> {
1063        let options = CreateContainerOptionsBuilder::new().name(name).build();
1064        let id = self.api.create_container(Some(options), body).await?.id;
1065        let attached = async {
1066            let options = AttachContainerOptionsBuilder::new()
1067                .stream(true)
1068                .stdin(true)
1069                .stdout(true)
1070                .stderr(true)
1071                .build();
1072            let attached = self.api.attach_container(&id, Some(options)).await?;
1073            self.api.start_container(&id, None).await?;
1074            let resize = ResizeContainerTTYOptionsBuilder::new()
1075                .w(i32::from(size.cols))
1076                .h(i32::from(size.rows))
1077                .build();
1078            self.api.resize_container_tty(&id, resize).await?;
1079            Ok::<_, Error>(attached)
1080        };
1081        match attached.await {
1082            Ok(attached) => Ok(Tty {
1083                container: id,
1084                output: attached
1085                    .output
1086                    .map_ok(|output| output.into_bytes().to_vec())
1087                    .boxed(),
1088                input: attached.input,
1089            }),
1090            Err(error) => {
1091                let _ = self.remove(&id).await;
1092                Err(error)
1093            }
1094        }
1095    }
1096}
1097
1098/// How often `pull` logs its progress.
1099const PULL_LOG_INTERVAL: Duration = Duration::from_secs(10);
1100
1101/// A pull's layers so far, from the statuses Docker streams per layer.
1102#[derive(Debug, Default)]
1103struct PullProgress {
1104    layers: BTreeMap<String, Layer>,
1105}
1106
1107#[derive(Debug, Default)]
1108struct Layer {
1109    downloaded: u64,
1110    size: u64,
1111    done: bool,
1112}
1113
1114impl PullProgress {
1115    fn update(&mut self, update: &CreateImageInfo) {
1116        let (Some(id), Some(status)) = (&update.id, &update.status) else {
1117            return;
1118        };
1119        let bytes = |value: Option<i64>| value.and_then(|value| u64::try_from(value).ok());
1120        let detail = update.progress_detail.as_ref();
1121        match status.as_str() {
1122            "Pulling fs layer" | "Waiting" => {
1123                self.layers.entry(id.clone()).or_default();
1124            }
1125            "Downloading" => {
1126                let layer = self.layers.entry(id.clone()).or_default();
1127                layer.downloaded =
1128                    bytes(detail.and_then(|detail| detail.current)).unwrap_or(layer.downloaded);
1129                layer.size = bytes(detail.and_then(|detail| detail.total)).unwrap_or(layer.size);
1130            }
1131            "Download complete" => {
1132                if let Some(layer) = self.layers.get_mut(id) {
1133                    layer.downloaded = layer.size;
1134                }
1135            }
1136            "Pull complete" | "Already exists" => {
1137                let layer = self.layers.entry(id.clone()).or_default();
1138                layer.downloaded = layer.size;
1139                layer.done = true;
1140            }
1141            _ => {}
1142        }
1143    }
1144}
1145
1146impl fmt::Display for PullProgress {
1147    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1148        let done = self.layers.values().filter(|layer| layer.done).count();
1149        let downloaded: u64 = self.layers.values().map(|layer| layer.downloaded).sum();
1150        let size: u64 = self.layers.values().map(|layer| layer.size).sum();
1151        write!(
1152            f,
1153            "{done} of {} layers done, {} of {} downloaded",
1154            self.layers.len(),
1155            ByteSize(downloaded),
1156            ByteSize(size)
1157        )
1158    }
1159}
1160
1161fn connect_to(docker_host: Option<&str>) -> Result<Docker, Error> {
1162    let api = match docker_host.filter(|host| !host.is_empty()) {
1163        None => bollard::Docker::connect_with_socket_defaults()?,
1164        Some(host) if host.starts_with("unix://") => {
1165            bollard::Docker::connect_with_socket(host, 120, bollard::API_DEFAULT_VERSION)?
1166        }
1167        // Bollard would quietly fall back to the local socket: a different Engine than the one asked for.
1168        Some(host) => {
1169            return Err(Error::UnsupportedURISchemeError {
1170                uri: host.to_owned(),
1171            });
1172        }
1173    };
1174    Ok(Docker { api })
1175}
1176
1177/// A tar archive, to extract at `/`, holding the config as a root-only file at `JIT_CONFIG_PATH`.
1178/// The chunks of an archive as they arrive, for a blocking reader: it ends when no more come.
1179struct Chunks {
1180    received: tokio::sync::mpsc::Receiver<Vec<u8>>,
1181    chunk: Vec<u8>,
1182    at: usize,
1183}
1184
1185impl std::io::Read for Chunks {
1186    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1187        while self.at == self.chunk.len() {
1188            match self.received.blocking_recv() {
1189                Some(chunk) => {
1190                    self.chunk = chunk;
1191                    self.at = 0;
1192                }
1193                None => return Ok(0),
1194            }
1195        }
1196        let n = buf.len().min(self.chunk.len() - self.at);
1197        buf[..n].copy_from_slice(&self.chunk[self.at..self.at + n]);
1198        self.at += n;
1199        Ok(n)
1200    }
1201}
1202
1203/// A reader that notes whether its last read found the end: `tar` reads past a zero block, the archive's end, no
1204/// further, and takes running out between entries as the end too.
1205struct Ends<R> {
1206    inner: R,
1207    ran_out: bool,
1208}
1209
1210impl<R: std::io::Read> std::io::Read for Ends<R> {
1211    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1212        let n = self.inner.read(buf)?;
1213        self.ran_out = n == 0 && !buf.is_empty();
1214        Ok(n)
1215    }
1216}
1217
1218/// The regular files at the top of a directory's `tar` whose names start with `prefix`. Docker roots the archive at
1219/// the directory's own name. Every other entry is read through and dropped, so only the files kept count against
1220/// `FILES_LIMIT`. Complete only once the archive's end marker is read: one that breaks off, between entries too, or
1221/// a file cut at the limit is not; what was read is kept.
1222pub(crate) fn files_in(tar: impl std::io::Read, prefix: &str) -> Files {
1223    let mut tar = Ends {
1224        inner: tar,
1225        ran_out: false,
1226    };
1227    let mut read = Files::default();
1228    let ended = files_until_end(&mut tar, prefix, &mut read.files);
1229    read.complete = ended && !tar.ran_out;
1230    read
1231}
1232
1233/// Reads `tar` into `files` as `files_in` says; whether it got to the archive's end, which `files_in` checks was its
1234/// marker.
1235fn files_until_end(tar: impl std::io::Read, prefix: &str, files: &mut Vec<File>) -> bool {
1236    let mut archive = tar::Archive::new(tar);
1237    let Ok(entries) = archive.entries() else {
1238        return false;
1239    };
1240    let mut kept = 0;
1241    for entry in entries {
1242        let Ok(mut entry) = entry else {
1243            return false;
1244        };
1245        let name = match entry.path() {
1246            Ok(path) if path.components().count() == 2 => path
1247                .file_name()
1248                .map(|name| name.to_string_lossy().into_owned()),
1249            _ => None,
1250        };
1251        let Some(name) =
1252            name.filter(|name| name.starts_with(prefix) && entry.header().entry_type().is_file())
1253        else {
1254            continue;
1255        };
1256        let room = FILES_LIMIT - kept;
1257        let mut contents = Vec::new();
1258        // One byte past the room tells a file cut at the limit from one that fits it exactly.
1259        let whole = std::io::Read::read_to_end(
1260            &mut std::io::Read::take(&mut entry, room as u64 + 1),
1261            &mut contents,
1262        )
1263        .is_ok()
1264            && contents.len() <= room;
1265        contents.truncate(room);
1266        kept += contents.len();
1267        files.push(File {
1268            name,
1269            contents: String::from_utf8_lossy(&contents).into_owned(),
1270        });
1271        if !whole {
1272            return false;
1273        }
1274    }
1275    true
1276}
1277
1278fn jit_config_tar(jit_config: &JitConfig) -> std::io::Result<Vec<u8>> {
1279    let mut tar = tar::Builder::new(Vec::new());
1280    let path = JIT_CONFIG_PATH.trim_start_matches('/');
1281    let mut dir = tar::Header::new_gnu();
1282    dir.set_entry_type(tar::EntryType::Directory);
1283    dir.set_mode(0o700);
1284    dir.set_size(0);
1285    tar.append_data(
1286        &mut dir,
1287        path.rsplit_once('/').map_or("", |(dir, _)| dir),
1288        std::io::empty(),
1289    )?;
1290    let mut file = tar::Header::new_gnu();
1291    file.set_mode(0o400);
1292    file.set_size(jit_config.0.len() as u64);
1293    tar.append_data(&mut file, path, jit_config.0.as_bytes())?;
1294    tar.into_inner()
1295}
1296
1297#[cfg(test)]
1298pub(crate) mod tests;