diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-08-15 14:09:23 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-08-15 14:09:23 +0200 |
commit | 0e3ad481706ff2f1e83487517415d251179ce3c3 (patch) | |
tree | 00fc87b930844f1e33826c6fcdb3cc6fba42e8a1 | |
parent | caa3380ba8d7ed74124d964242557f31a9cc9d24 (diff) |
boost modify_cmdline speed by starting replacement at its offset on disk
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 15 |
2 files changed, 14 insertions, 3 deletions
diff --git a/src/error.rs b/src/error.rs index 6f8e217..97fa882 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,6 +4,8 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum Error { + #[error("can't find cmdline (/cmdline.txt) on boot partition")] + NoCmdline, #[error("can't find disk device")] NoDiskDev, #[error("no private keys found in file")] diff --git a/src/main.rs b/src/main.rs index 89db44c..f72da42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -279,12 +279,21 @@ where fn modify_cmdline(old: &str, new: &str) -> Result<()> { let boot = boot_dev()?; + let mut buf = fs::read(boot)?; - let mut cmdline = fs::read(boot)?; - replace_slice(&mut cmdline, old.as_bytes(), new.as_bytes()); - fs::write(boot, cmdline)?; + let cmdline_buf = fs::read("/boot/cmdline.txt")?; + let cmdline_offset = buf + .windows(cmdline_buf.len()) + .position(|window| window == cmdline_buf) + .ok_or(Error::NoCmdline)? + / 512 + + 1; + replace_slice(&mut buf[cmdline_offset..], old.as_bytes(), new.as_bytes()); + + fs::write(boot, buf)?; nix::unistd::sync(); + Ok(()) } |