Skip to content

Commit

Permalink
treewide: refactor mkdir, write_file and read_file
Browse files Browse the repository at this point in the history
mkdir, read_file write_file augment the error case with the path in the
error case. This is useful as we otherwise don't know which location
failed. This commit refactors the functions to take any argument that
coerces into a Path - which is implemented by many types.

mkdir now uses create_dir_all as it is not meant to fail if the
directory already exists.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
  • Loading branch information
KarlK90 committed Feb 2, 2025
1 parent 5d1fe17 commit 6bb7ae5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
17 changes: 3 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;
use std::env::current_exe;
use std::ffi::CString;
use std::fmt::Write as _;
use std::fs::{create_dir, read_to_string, File, OpenOptions};
use std::fs::{File, OpenOptions};
use std::io;
use std::io::Write as _;
use std::os::fd::{AsFd, AsRawFd, RawFd};
Expand All @@ -25,6 +25,7 @@ use nix::unistd::{chdir, chroot, dup2, execv, unlink};
use systemd::{mount_systemd, shutdown};
#[cfg(feature = "usb9pfs")]
use usbg_9pfs::prepare_9pfs_gadget;
use util::read_file;

mod cmdline;
#[cfg(feature = "dmverity")]
Expand All @@ -34,22 +35,10 @@ mod mount;
mod systemd;
#[cfg(feature = "usb9pfs")]
mod usbg_9pfs;
mod util;

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

pub fn mkdir(dir: &str) -> Result<()> {
if let Err(e) = create_dir(dir) {
if e.kind() != io::ErrorKind::AlreadyExists {
return Err(format!("Failed to create {dir}: {e}",).into());
}
}
Ok(())
}

fn read_file(filename: &str) -> std::result::Result<String, String> {
read_to_string(filename).map_err(|e| format!("Failed to read {filename}: {e}"))
}

/*
* Setup stdout/stderr. The kernel will create /dev/console in the
* initramfs, so we can use that.
Expand Down
3 changes: 2 additions & 1 deletion src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use log::debug;
use nix::mount::{mount, MsFlags};

use crate::cmdline::CmdlineOptions;
use crate::{mkdir, Result};
use crate::util::mkdir;
use crate::Result;

pub fn do_mount(
src: Option<&str>,
Expand Down
3 changes: 2 additions & 1 deletion src/systemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use nix::sys::reboot::{reboot, RebootMode};

use crate::cmdline::CmdlineOptions;
use crate::mount::do_mount;
use crate::{mkdir, Result};
use crate::util::mkdir;
use crate::Result;

pub fn mount_systemd(options: &mut CmdlineOptions) -> Result<()> {
do_mount(
Expand Down
9 changes: 3 additions & 6 deletions src/usbg_9pfs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only

use std::fs::{read_dir, write};
use std::fs::read_dir;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::symlink;
use std::{thread, time};
Expand All @@ -9,11 +9,8 @@ use log::debug;

use crate::cmdline::CmdlineOptions;
use crate::mount::mount_apivfs;
use crate::{mkdir, Result};

fn write_file<C: AsRef<[u8]>>(path: &str, content: C) -> Result<()> {
write(path, content).map_err(|e| format!("Failed to write to {path}: {e}").into())
}
use crate::util::{mkdir, write_file};
use crate::Result;

fn setup_9pfs_gadget(device: &String) -> Result<()> {
debug!("Initializing USB 9pfs gadget ...");
Expand Down
38 changes: 38 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-2.0-only

use std::{
fs::{create_dir_all, read_to_string, write},
path::Path,
};

use crate::Result;

pub(crate) fn mkdir(dir: impl AsRef<Path>) -> Result<()> {
create_dir_all(dir.as_ref()).map_err(|e| {
format!(
"Failed to create directory {}: {e}",
dir.as_ref().to_string_lossy()
)
.into()
})
}

pub(crate) fn read_file(filename: impl AsRef<Path>) -> Result<String> {
read_to_string(filename.as_ref()).map_err(|e| {
format!(
"Failed to read {}: {e}",
filename.as_ref().to_string_lossy()
)
.into()
})
}

pub(crate) fn write_file<C: AsRef<[u8]>>(path: impl AsRef<Path>, content: C) -> Result<()> {
write(&path, content).map_err(|e| {
format!(
"Failed to write to {}: {e}",
path.as_ref().to_string_lossy()
)
.into()
})
}

0 comments on commit 6bb7ae5

Please sign in to comment.