Skip to content

Commit c4227bc

Browse files
committed
treewide: use create_dir_all directly
create_dir_all doesn't fail if the folder already exists. Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
1 parent 464c6ff commit c4227bc

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

src/main.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::env;
55
use std::env::current_exe;
66
use std::ffi::CString;
77
use std::fmt::Write as _;
8-
use std::fs::{create_dir, read_to_string, File, OpenOptions};
8+
use std::fs::{read_to_string, File, OpenOptions};
99
use std::io;
1010
use std::io::Write as _;
1111
use std::os::fd::{AsFd, AsRawFd, RawFd};
@@ -37,15 +37,6 @@ mod usbg_9pfs;
3737

3838
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
3939

40-
pub fn mkdir(dir: &str) -> Result<()> {
41-
if let Err(e) = create_dir(dir) {
42-
if e.kind() != io::ErrorKind::AlreadyExists {
43-
return Err(format!("Failed to create {dir}: {e}",).into());
44-
}
45-
}
46-
Ok(())
47-
}
48-
4940
/*
5041
* Setup stdout/stderr. The kernel will create /dev/console in the
5142
* initramfs, so we can use that.

src/mount.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

3-
use std::fs::remove_dir;
3+
use std::fs::{create_dir_all, remove_dir};
44
use std::path::Path;
55

66
use log::debug;
77
use nix::mount::{mount, MsFlags};
88

99
use crate::cmdline::CmdlineOptions;
10-
use crate::{mkdir, Result};
10+
use crate::Result;
1111

1212
pub fn do_mount(
1313
src: Option<&str>,
@@ -16,7 +16,7 @@ pub fn do_mount(
1616
flags: MsFlags,
1717
data: Option<&str>,
1818
) -> Result<()> {
19-
mkdir(dst)?;
19+
create_dir_all(dst)?;
2020

2121
mount(src, dst, fstype, flags, data).map_err(|e| {
2222
format!(
@@ -49,7 +49,7 @@ pub fn mount_root(options: &CmdlineOptions) -> Result<()> {
4949
return Err("root= not found in /proc/cmdline".into());
5050
}
5151

52-
mkdir("/root")?;
52+
create_dir_all("/root")?;
5353

5454
debug!(
5555
"Mounting rootfs {} -> /root as {} with flags = {:#x}, data = '{}'",

src/systemd.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
use std::collections::BinaryHeap;
44
use std::env;
5-
use std::fs::read_to_string;
5+
use std::fs::{create_dir_all, read_to_string};
66
use std::path::Path;
77

88
use nix::mount::{umount, MsFlags};
99
use nix::sys::reboot::{reboot, RebootMode};
1010

1111
use crate::cmdline::CmdlineOptions;
1212
use crate::mount::do_mount;
13-
use crate::{mkdir, Result};
13+
use crate::Result;
1414

1515
pub fn mount_systemd(options: &mut CmdlineOptions) -> Result<()> {
1616
do_mount(
@@ -30,8 +30,8 @@ pub fn mount_systemd(options: &mut CmdlineOptions) -> Result<()> {
3030
options.cleanup = false;
3131

3232
/* expected by systemd when going back to the initramfs during shutdown */
33-
mkdir("/run")?;
34-
mkdir("/oldroot")?;
33+
create_dir_all("/run")?;
34+
create_dir_all("/oldroot")?;
3535

3636
do_mount(
3737
Some("/"),

src/usbg_9pfs.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

3-
use std::fs::{read_dir, write};
3+
use std::fs::{create_dir_all, read_dir, write};
44
use std::os::unix::ffi::OsStrExt;
55
use std::os::unix::fs::symlink;
66
use std::{thread, time};
@@ -9,7 +9,7 @@ use log::debug;
99

1010
use crate::cmdline::CmdlineOptions;
1111
use crate::mount::mount_apivfs;
12-
use crate::{mkdir, Result};
12+
use crate::Result;
1313

1414
fn write_file<C: AsRef<[u8]>>(path: &str, content: C) -> Result<()> {
1515
write(path, content).map_err(|e| format!("Failed to write to {path}: {e}").into())
@@ -27,12 +27,12 @@ fn setup_9pfs_gadget(device: &String) -> Result<()> {
2727

2828
mount_apivfs("/sys/kernel/config", "configfs")?;
2929

30-
mkdir("/sys/kernel/config/usb_gadget/9pfs")?;
30+
create_dir_all("/sys/kernel/config/usb_gadget/9pfs")?;
3131

3232
write_file("/sys/kernel/config/usb_gadget/9pfs/idVendor", "0x1d6b")?;
3333
write_file("/sys/kernel/config/usb_gadget/9pfs/idProduct", "0x0109")?;
3434

35-
mkdir("/sys/kernel/config/usb_gadget/9pfs/strings/0x409")?;
35+
create_dir_all("/sys/kernel/config/usb_gadget/9pfs/strings/0x409")?;
3636
write_file(
3737
"/sys/kernel/config/usb_gadget/9pfs/strings/0x409/serialnumber",
3838
"01234567",
@@ -46,12 +46,12 @@ fn setup_9pfs_gadget(device: &String) -> Result<()> {
4646
"9PFS Gadget",
4747
)?;
4848

49-
mkdir("/sys/kernel/config/usb_gadget/9pfs/configs/c.1")?;
50-
mkdir("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/strings/0x409")?;
49+
create_dir_all("/sys/kernel/config/usb_gadget/9pfs/configs/c.1")?;
50+
create_dir_all("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/strings/0x409")?;
5151

5252
let function = format!("/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{device}");
5353
let link = format!("/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{device}");
54-
mkdir(&function)?;
54+
create_dir_all(&function)?;
5555
symlink(&function, &link)?;
5656

5757
debug!(

0 commit comments

Comments
 (0)