Skip to main content

agentmux_srv_core/backend/container/
upload.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Writing a small file into a container's `/tmp`.
5
6use crate::backend::container::{ContainerError, ContainerManager};
7
8impl ContainerManager {
9    /// Write `bytes` to `/tmp/<file_name>` in the container as the exec user, mode 0600; returns the path. The turn
10    /// prompt and the install files of the non-npm CLIs (hash locks too large for an argument) go this way.
11    pub(super) async fn upload_tmp_file(&self, container_name: &str, file_name: &str, bytes: &[u8]) -> Result<String, ContainerError> {
12        // No usable uid means no usable file: at 0600 a root-owned prompt is
13        // unreadable by the exec user, and /tmp's sticky bit makes it
14        // un-unlinkable too. Failing here is a clear, retryable error; writing
15        // it anyway would be a permission-denied on open plus a leaked file.
16        let (uid, gid) = self.exec_identity(container_name).await.ok_or_else(|| {
17            ContainerError::NotAvailable(format!(
18                "could not resolve the exec user of container {container_name}; \
19                 cannot write a readable file"
20            ))
21        })?;
22
23        let mut header = tar::Header::new_gnu();
24        header.set_size(bytes.len() as u64);
25        header.set_mode(0o600);
26        header.set_uid(uid);
27        header.set_gid(gid);
28        header.set_mtime(0);
29        header.set_cksum();
30
31        let mut archive = tar::Builder::new(Vec::new());
32        archive
33            .append_data(&mut header, file_name, bytes)
34            .map_err(|e| ContainerError::NotAvailable(format!("tar build failed: {e}")))?;
35        let tar_bytes = archive
36            .into_inner()
37            .map_err(|e| ContainerError::NotAvailable(format!("tar finish failed: {e}")))?;
38
39        self.inner
40            .docker
41            .upload_to_container(
42                container_name,
43                Some(bollard::container::UploadToContainerOptions {
44                    path: "/tmp",
45                    no_overwrite_dir_non_dir: "true",
46                }),
47                tar_bytes.into(),
48            )
49            .await?;
50
51        Ok(format!("/tmp/{file_name}"))
52    }
53}