Skip to content

Commit bcba0fd

Browse files
committed
chore: get rid of unnecessary PathBuf allocations
1 parent 302cc1c commit bcba0fd

File tree

9 files changed

+86
-93
lines changed

9 files changed

+86
-93
lines changed

src/console.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ pub fn setup_console(console_socket: File, win_size: Option<&nix::pty::Winsize>)
2121
nix::pty::unlockpt(&master_fd).expect("Could not unlockpt!");
2222

2323
// Get the name of the slave
24-
let slave_name = nix::pty::ptsname_r(&master_fd).expect("Could not get ptsname!");
24+
let slave_name =
25+
PathBuf::from(nix::pty::ptsname_r(&master_fd).expect("Could not get ptsname!"));
2526

2627
if let Some(winsize) = win_size {
2728
unsafe { ioctl_set_winsize(master_fd.as_raw_fd(), winsize as *const libc::winsize) }
2829
.expect("Could not set winsize using ioctl!");
2930
}
3031

31-
mounts::mount_console(&PathBuf::from(&slave_name));
32+
mounts::mount_console(&slave_name);
3233

3334
//Send master fd over console_socket
3435
nix::sys::socket::sendmsg::<()>(
@@ -40,12 +41,8 @@ pub fn setup_console(console_socket: File, win_size: Option<&nix::pty::Winsize>)
4041
)
4142
.expect("Could not send message to console socket!");
4243

43-
let slave_fd = nix::fcntl::open(
44-
std::path::Path::new(&slave_name),
45-
OFlag::O_RDWR,
46-
Mode::empty(),
47-
)
48-
.expect("Could not open pty slave path!");
44+
let slave_fd = nix::fcntl::open(&slave_name, OFlag::O_RDWR, Mode::empty())
45+
.expect("Could not open pty slave path!");
4946
for i in 0..3 {
5047
nix::unistd::dup3(slave_fd, i, OFlag::empty())
5148
.unwrap_or_else(|_| panic!("Could not dup3 pty slave_fd onto fd {}", i));

src/container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ pub struct OCIContainer {
2424

2525
impl OCIContainer {
2626
pub fn new(bundle: String, id: String, pidfile: String) -> Self {
27-
let mut config = std::path::PathBuf::from(bundle.clone());
28-
config.push("config.json");
2927
let can_path = std::fs::canonicalize(bundle).expect("Unable to determine absolute path");
28+
let mut config = can_path.clone();
29+
config.push("config.json");
3030

3131
Self {
3232
id,

src/create.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::os::unix::fs;
1717
use std::os::unix::fs::OpenOptionsExt;
1818
use std::os::unix::net::UnixStream;
1919
use std::os::unix::prelude::CommandExt;
20-
use std::path::PathBuf;
20+
use std::path::{Path, PathBuf};
2121
use std::str::FromStr;
2222

2323
use crate::container::OCIContainer;
@@ -34,7 +34,7 @@ pub fn create_container(
3434
) {
3535
let _ = std::fs::create_dir(&project_dir);
3636

37-
let container_dir = rootfs::resolve_in_rootfs(&PathBuf::from(id), &project_dir);
37+
let container_dir = rootfs::resolve_in_rootfs(Path::new(id), &project_dir);
3838
std::fs::create_dir(container_dir.clone()).expect("Unable to create container directory");
3939
let container = OCIContainer::new(
4040
bundle.to_str().unwrap().to_owned(),
@@ -74,9 +74,9 @@ pub fn create_container(
7474
}
7575

7676
// find rootfs
77-
let bundle_rootfs_path = PathBuf::from(&container.spec().root().as_ref().unwrap().path());
77+
let bundle_rootfs_path = container.spec().root().as_ref().unwrap().path();
7878
let bundle_rootfs_path_abs = std::fs::canonicalize(if bundle_rootfs_path.is_absolute() {
79-
bundle_rootfs_path
79+
bundle_rootfs_path.to_path_buf()
8080
} else {
8181
let rootfs = &bundle;
8282
rootfs.join(bundle_rootfs_path)
@@ -93,12 +93,12 @@ pub fn create_container(
9393
.as_ref()
9494
.unwrap();
9595

96-
let exec_path_rel = PathBuf::from(
96+
let exec_path_rel = Path::new(
9797
exec_args
9898
.first()
9999
.expect("Container spec does not contain any args!"),
100100
);
101-
let exec_path_abs = rootfs::resolve_in_rootfs(&exec_path_rel, &bundle_rootfs_path_abs);
101+
let exec_path_abs = rootfs::resolve_in_rootfs(exec_path_rel, &bundle_rootfs_path_abs);
102102
let is_hermit_container = if exec_path_abs.exists() {
103103
hermit::is_hermit_app(&exec_path_abs)
104104
} else {

src/delete.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use std::path::PathBuf;
1616
// let network_file = std::fs::File::open(network_file_path)?;
1717
// let buf_reader = BufReader::new(network_file);
1818
// let network_config: network::HermitNetworkConfig = serde_json::from_reader(buf_reader)?;
19-
// let namespace_file = File::open(PathBuf::from(
19+
// let namespace_file = File::open(
2020
// network_config.network_namespace.as_ref().unwrap(),
21-
// ))?;
21+
// )?;
2222
// nix::sched::setns(namespace_file.as_raw_fd(), CloneFlags::CLONE_NEWNET)?;
2323

2424
// network::undo_tap_creation(&network_config)?;

src/devices.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::{
2-
convert::TryInto, fs::OpenOptions, os::unix::prelude::OpenOptionsExt, path::Path, path::PathBuf,
3-
};
1+
use std::{convert::TryInto, fs::OpenOptions, os::unix::prelude::OpenOptionsExt, path::Path};
42

53
use nix::{
64
mount::MsFlags,
@@ -14,7 +12,7 @@ use crate::{mounts, rootfs};
1412
pub fn create_devices(spec_devices: &Option<Vec<runtime::LinuxDevice>>, rootfs: &Path) {
1513
let mut default_devices = vec![
1614
runtime::LinuxDeviceBuilder::default()
17-
.path(PathBuf::from("/dev/null"))
15+
.path("/dev/null")
1816
.file_mode(0o666u32)
1917
.typ(runtime::LinuxDeviceType::C)
2018
.major(1)
@@ -24,7 +22,7 @@ pub fn create_devices(spec_devices: &Option<Vec<runtime::LinuxDevice>>, rootfs:
2422
.build()
2523
.unwrap(),
2624
runtime::LinuxDeviceBuilder::default()
27-
.path(PathBuf::from("/dev/zero"))
25+
.path("/dev/zero")
2826
.file_mode(0o666u32)
2927
.typ(runtime::LinuxDeviceType::C)
3028
.major(1)
@@ -34,7 +32,7 @@ pub fn create_devices(spec_devices: &Option<Vec<runtime::LinuxDevice>>, rootfs:
3432
.build()
3533
.unwrap(),
3634
runtime::LinuxDeviceBuilder::default()
37-
.path(PathBuf::from("/dev/full"))
35+
.path("/dev/full")
3836
.file_mode(0o666u32)
3937
.typ(runtime::LinuxDeviceType::C)
4038
.major(1)
@@ -44,7 +42,7 @@ pub fn create_devices(spec_devices: &Option<Vec<runtime::LinuxDevice>>, rootfs:
4442
.build()
4543
.unwrap(),
4644
runtime::LinuxDeviceBuilder::default()
47-
.path(PathBuf::from("/dev/random"))
45+
.path("/dev/random")
4846
.file_mode(0o666u32)
4947
.typ(runtime::LinuxDeviceType::C)
5048
.major(1)
@@ -54,7 +52,7 @@ pub fn create_devices(spec_devices: &Option<Vec<runtime::LinuxDevice>>, rootfs:
5452
.build()
5553
.unwrap(),
5654
runtime::LinuxDeviceBuilder::default()
57-
.path(PathBuf::from("/dev/urandom"))
55+
.path("/dev/urandom")
5856
.file_mode(0o666u32)
5957
.typ(runtime::LinuxDeviceType::C)
6058
.major(1)
@@ -64,7 +62,7 @@ pub fn create_devices(spec_devices: &Option<Vec<runtime::LinuxDevice>>, rootfs:
6462
.build()
6563
.unwrap(),
6664
runtime::LinuxDeviceBuilder::default()
67-
.path(PathBuf::from("/dev/tty"))
65+
.path("/dev/tty")
6866
.file_mode(0o666u32)
6967
.typ(runtime::LinuxDeviceType::C)
7068
.major(5)
@@ -96,14 +94,12 @@ pub fn create_devices(spec_devices: &Option<Vec<runtime::LinuxDevice>>, rootfs:
9694
if !destination_resolved.starts_with(rootfs) {
9795
panic!("Device at {:?} cannot be mounted into rootfs!", dev.path());
9896
}
99-
mounts::create_all_dirs(&PathBuf::from(
100-
&destination_resolved.parent().unwrap_or_else(|| {
101-
panic!(
102-
"Could create device at destination {:?} which has no parent dir!",
103-
destination_resolved
104-
)
105-
}),
106-
));
97+
mounts::create_all_dirs(destination_resolved.parent().unwrap_or_else(|| {
98+
panic!(
99+
"Could create device at destination {:?} which has no parent dir!",
100+
destination_resolved
101+
)
102+
}));
107103

108104
//Just assume we are not in a user namespace and that mknod will work. Error out if it does not (FIXME ?)
109105
let node_kind = match dev.typ() {
@@ -158,8 +154,8 @@ fn verify_device(path: &Path, major_exp: u64, minor_exp: u64) {
158154
}
159155

160156
pub fn create_tun(rootfs: &Path, uid: Uid, gid: Gid) {
161-
let destination_relative = PathBuf::from("/dev/net/tun");
162-
let destination_resolved = rootfs::resolve_in_rootfs(&destination_relative, rootfs);
157+
let destination_relative = Path::new("/dev/net/tun");
158+
let destination_resolved = rootfs::resolve_in_rootfs(destination_relative, rootfs);
163159
if !destination_resolved.starts_with(rootfs) {
164160
panic!(
165161
"Device at {:?} cannot be mounted into rootfs!",
@@ -174,7 +170,7 @@ pub fn create_tun(rootfs: &Path, uid: Uid, gid: Gid) {
174170
)
175171
});
176172
if !parent.exists() {
177-
mounts::create_all_dirs(&PathBuf::from(&destination_resolved.parent().unwrap()));
173+
mounts::create_all_dirs(destination_resolved.parent().unwrap());
178174
}
179175

180176
if destination_resolved.exists() {
@@ -194,13 +190,13 @@ pub fn create_tun(rootfs: &Path, uid: Uid, gid: Gid) {
194190

195191
pub fn mount_hermit_devices(rootfs: &Path) {
196192
if std::fs::metadata("/dev/kvm").is_ok() {
197-
mount_device(rootfs, &PathBuf::from("/dev/kvm"), 10, 232);
193+
mount_device(rootfs, Path::new("/dev/kvm"), 10, 232);
198194
} else {
199195
warn!("/dev/kvm doesn't exist and is consequently not supported!");
200196
}
201197

202198
if std::fs::metadata("/dev/vhost-net").is_ok() {
203-
mount_device(rootfs, &PathBuf::from("/dev/vhost-net"), 10, 238);
199+
mount_device(rootfs, Path::new("/dev/vhost-net"), 10, 238);
204200
} else {
205201
warn!("/dev/vhost-net doesn't exist and is consequently not supported!");
206202
}
@@ -216,7 +212,7 @@ fn mount_device(rootfs: &Path, destination_rel: &Path, major: u64, minor: u64) {
216212
});
217213

218214
if !parent.exists() {
219-
mounts::create_all_dirs(&PathBuf::from(&destination.parent().unwrap()));
215+
mounts::create_all_dirs(destination.parent().unwrap());
220216
}
221217

222218
if destination.exists() {
@@ -253,7 +249,7 @@ fn mount_device(rootfs: &Path, destination_rel: &Path, major: u64, minor: u64) {
253249
}
254250

255251
pub fn setup_dev_symlinks(rootfs: &Path) {
256-
// if PathBuf::from("/proc/kcore").exists() {
252+
// if Path::new("/proc/kcore").exists() {
257253
// nix::unistd::symlinkat("/proc/kcore", None, &rootfs.join("dev/core"))
258254
// .expect("Could not symlink /proc/kcore to /dev/core");
259255
// }

src/init.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::{Read, Write};
33
use std::os::fd::{AsRawFd, OwnedFd};
44
use std::os::unix::prelude::{IntoRawFd, OpenOptionsExt};
55
use std::os::unix::process::CommandExt;
6-
use std::path::PathBuf;
6+
use std::path::{Path, PathBuf};
77
use std::{
88
env,
99
fs::File,
@@ -324,34 +324,34 @@ fn init_stage_child(args: SetupArgs) -> ! {
324324
.expect("Could not setup network lo interface!");
325325
}
326326

327-
let rootfs_path = PathBuf::from(args.config.rootfs);
328-
let bundle_rootfs_path = PathBuf::from(args.config.bundle_rootfs);
327+
let rootfs_path = Path::new(&args.config.rootfs);
328+
let bundle_rootfs_path = Path::new(&args.config.bundle_rootfs);
329329

330330
//Mount root file system
331-
rootfs::mount_rootfs(&args.config.spec, &rootfs_path);
331+
rootfs::mount_rootfs(&args.config.spec, rootfs_path);
332332

333333
//Setup mounts and devices
334334
let setup_dev = if let Some(mounts) = args.config.spec.mounts() {
335335
mounts::configure_mounts(
336336
mounts,
337-
&rootfs_path,
338-
&bundle_rootfs_path,
337+
rootfs_path,
338+
bundle_rootfs_path,
339339
args.config.spec.linux().as_ref().unwrap().mount_label(),
340340
)
341341
} else {
342342
true
343343
};
344344

345345
if setup_dev {
346-
devices::create_devices(linux_spec.devices(), &rootfs_path);
347-
devices::setup_ptmx(&rootfs_path);
348-
devices::setup_dev_symlinks(&rootfs_path);
346+
devices::create_devices(linux_spec.devices(), rootfs_path);
347+
devices::setup_ptmx(rootfs_path);
348+
devices::setup_dev_symlinks(rootfs_path);
349349
}
350350

351351
if args.config.is_hermit_container {
352-
devices::mount_hermit_devices(&rootfs_path);
352+
devices::mount_hermit_devices(rootfs_path);
353353
devices::create_tun(
354-
&rootfs_path,
354+
rootfs_path,
355355
Uid::from_raw(args.config.spec.process().as_ref().unwrap().user().uid()),
356356
Gid::from_raw(args.config.spec.process().as_ref().unwrap().user().gid()),
357357
);
@@ -376,7 +376,7 @@ fn init_stage_child(args: SetupArgs) -> ! {
376376
);
377377
}
378378

379-
nix::unistd::chdir(&rootfs_path).unwrap_or_else(|_| {
379+
nix::unistd::chdir(rootfs_path).unwrap_or_else(|_| {
380380
panic!(
381381
"Could not change directory to rootfs path {:?}",
382382
rootfs_path
@@ -386,7 +386,7 @@ fn init_stage_child(args: SetupArgs) -> ! {
386386
//TODO: Run create hooks
387387

388388
if args.config.cloneflags.contains(CloneFlags::CLONE_NEWNS) {
389-
rootfs::pivot_root(&rootfs_path);
389+
rootfs::pivot_root(rootfs_path);
390390
} else {
391391
nix::unistd::chroot(".").expect("Could not chroot into current directory!");
392392
nix::unistd::chdir("/").expect("Could not chdir to / after chroot!");
@@ -419,7 +419,7 @@ fn init_stage_child(args: SetupArgs) -> ! {
419419

420420
let cwd = args.config.spec.process().as_ref().unwrap().cwd();
421421
if !cwd.as_os_str().is_empty() {
422-
mounts::create_all_dirs(&PathBuf::from(cwd));
422+
mounts::create_all_dirs(Path::new(&cwd));
423423
}
424424

425425
//Setup console
@@ -524,7 +524,7 @@ fn init_stage_child(args: SetupArgs) -> ! {
524524
.first()
525525
.expect("Container spec does not contain any args!")
526526
.as_str();
527-
let app_root = PathBuf::from(app)
527+
let app_root = Path::new(app)
528528
.parent()
529529
.expect("App path does not have a parent!")
530530
.to_owned();
@@ -575,7 +575,7 @@ fn init_stage_child(args: SetupArgs) -> ! {
575575
.clone()
576576
};
577577

578-
let exec_path_rel = PathBuf::from(
578+
let exec_path_rel = Path::new(
579579
exec_args
580580
.first()
581581
.expect("Container spec does not contain any args!"),
@@ -637,7 +637,7 @@ fn init_stage_child(args: SetupArgs) -> ! {
637637

638638
info!("Initialize virtiofsd: {}", virtiofsd_args.join(" "));
639639

640-
let virtiofsd_path_rel = PathBuf::from(
640+
let virtiofsd_path_rel = Path::new(
641641
virtiofsd_args
642642
.first()
643643
.expect("Container spec does not contain any args!"),

0 commit comments

Comments
 (0)