Skip to content

Commit 7c87449

Browse files
committed
Move gen_sock() helper into util module
Move the gen_sock() helper function into the util module, where it will be accessible by more than just the qemu module. Also switch over to using std::env::temp_dir() for retrieving the path to the hosts temp directory instead of hard coding it. Signed-off-by: Daniel Müller <[email protected]>
1 parent 7534bb2 commit 7c87449

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ pub use crate::vmtest::*;
1616

1717
mod qemu;
1818
mod qga;
19+
mod util;

src/qemu.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ use std::time::Duration;
2020
use anyhow::{anyhow, bail, Context, Result};
2121
use log::{debug, log_enabled, warn, Level};
2222
use qapi::{qga, qmp, Qmp};
23-
use rand::Rng;
2423
use serde_derive::Serialize;
2524
use tempfile::{Builder, NamedTempFile};
2625
use tinytemplate::{format_unescaped, TinyTemplate};
2726

2827
use crate::output::Output;
2928
use crate::qga::QgaWrapper;
29+
use crate::util::gen_sock;
3030
use crate::{Mount, Target, VMConfig};
3131

3232
const INIT_TEMPLATE: &str = include_str!("init/init.sh.template");
@@ -118,13 +118,6 @@ fn host_supports_kvm(arch: &str) -> bool {
118118
arch == ARCH && Path::new("/dev/kvm").exists()
119119
}
120120

121-
// Generate a path to a randomly named socket
122-
fn gen_sock(prefix: &str) -> PathBuf {
123-
let id = rand::thread_rng().gen_range(100_000..1_000_000);
124-
let sock = format!("/tmp/{prefix}-{id}.sock");
125-
PathBuf::from(sock)
126-
}
127-
128121
// Given a guest temp dir and a host init path, generate the path to the init file
129122
// in the guest.
130123
// This path is the one that will be passed to the guest via the kernel's `init=` parameter.

src/util.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::env::temp_dir;
2+
use std::path::PathBuf;
3+
4+
use rand::Rng as _;
5+
6+
7+
/// Generate a path to a randomly named socket
8+
pub(crate) fn gen_sock(prefix: &str) -> PathBuf {
9+
let id = rand::thread_rng().gen_range(100_000..1_000_000);
10+
temp_dir().join(format!("{prefix}-{id}.sock"))
11+
}

0 commit comments

Comments
 (0)