Skip to content

Commit 3654f2e

Browse files
committed
Avoid several dynamic allocations, make others fallible
1 parent aab4dda commit 3654f2e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/container.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::process::Stdio;
44

55
use anyhow::anyhow;
6+
use fallible_collections::tryformat;
67
use tokio::process::Command;
78
use tokio_seqpacket::UnixSeqpacket;
89
use uuid::Uuid;
@@ -27,7 +28,16 @@ pub struct Container {
2728
impl Container {
2829
/// Spawns a new container with the given `id` from the `rt` OCI bundle.
2930
pub async fn create(id: &str, rt: OciBundle) -> anyhow::Result<Self> {
31+
let id = tryformat!(64, "{}", id).map_err(|e| anyhow!("OOM error: {:?}", e))?;
3032
let uuid = Uuid::new_v4();
33+
let uuid_str = tryformat!(36, "{}", uuid).map_err(|e| anyhow!("OOM error: {:?}", e))?;
34+
35+
let bundle_dir = rt.bundle_dir.to_str().expect("$TMPDIR is invalid UTF-8");
36+
let exits_dir = rt.exits_dir.to_str().expect("$TMPDIR is invalid UTF-8");
37+
let log_file = rt.log_file.to_str().expect("$TMPDIR is invalid UTF-8");
38+
let pid_file = rt.pid_file.to_str().expect("$TMPDIR is invalid UTF-8");
39+
let sock_dir = rt.base_dir().to_str().expect("$TMPDIR is invalid UTF-8");
40+
3141
let start_pipe = StartPipe::new()?;
3242
let mut sync_pipe = SyncPipe::new()?;
3343

@@ -38,15 +48,15 @@ impl Container {
3848
.args(&["--syslog", "--log-level=debug"])
3949
.arg("--terminal") // Passes `--console-sock` to `crun`.
4050
.args(&["--cid", &id])
41-
.args(&["--cuuid", &uuid.to_string()])
51+
.args(&["--cuuid", &uuid_str])
4252
.args(&["--name", &id])
4353
.args(&["--runtime", RUNTIME_BIN])
4454
.args(&["--runtime-arg", "--rootless=true"])
45-
.args(&["--bundle", &rt.bundle_dir.display().to_string()])
46-
.args(&["--exit-dir", &rt.exits_dir.display().to_string()])
47-
.args(&["--log-path", &rt.log_file.display().to_string()])
48-
.args(&["--container-pidfile", &rt.pid_file.display().to_string()])
49-
.args(&["--socket-dir-path", &rt.base_dir().display().to_string()])
55+
.args(&["--bundle", bundle_dir])
56+
.args(&["--exit-dir", exits_dir])
57+
.args(&["--log-path", log_file])
58+
.args(&["--container-pidfile", pid_file])
59+
.args(&["--socket-dir-path", sock_dir])
5060
.inherit_oci_pipes(&start_pipe, &sync_pipe)
5161
.spawn()?;
5262

@@ -86,12 +96,12 @@ impl Container {
8696
eprintln!("received PID {}, connecting to console socket...", pid);
8797

8898
// Setup is complete, so connect to the console socket.
89-
let sock_path = rt.base_dir().join(uuid.to_string()).join("attach");
99+
let sock_path = rt.base_dir().join(uuid_str).join("attach");
90100
let console_sock = UnixSeqpacket::connect(sock_path).await?;
91101
eprintln!("connected to console socket!");
92102

93103
Ok(Container {
94-
id: id.to_string(),
104+
id,
95105
uuid,
96106
pid,
97107
console_sock,

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use anyhow::anyhow;
44
use dashmap::DashMap;
5+
use fallible_collections::tryformat;
56
use fallible_collections::FallibleArc;
67

78
use self::container::Container;
@@ -46,7 +47,9 @@ impl Engine {
4647
container.start().await?;
4748
eprintln!("started container");
4849

49-
self.containers.insert(container_name.into(), container);
50+
let id = tryformat!(64, "{}", container_name).map_err(|e| anyhow!("OOM error: {:?}", e))?;
51+
self.containers.insert(id, container);
52+
5053
Ok(())
5154
}
5255

0 commit comments

Comments
 (0)