aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-05-05 18:29:41 +0200
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-05-05 18:29:41 +0200
commit02534ae910d0251789e15e278ccfec93e5f4278f (patch)
tree99f33c33e244a85cf85aca6dd7b93d12c2c3d31d /src
parentce337ea566a7cda7c9a89c4080fa24a15256a1ed (diff)
use non-utf8-sensitive cmdline replacement routine
Diffstat (limited to 'src')
-rw-r--r--src/main.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 696a2ba..5c198b0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -156,11 +156,28 @@ fn validate_credentials(user_id: &str, user_password: &str) -> io::Result<bool>
))
}
+fn replace_slice<T>(src: &mut [T], old: &[T], new: &[T])
+where
+ T: Clone + PartialEq,
+{
+ let iteration = if src.starts_with(old) {
+ src[..old.len()].clone_from_slice(new);
+ old.len()
+ } else {
+ 1
+ };
+
+ if src.len() > old.len() {
+ replace_slice(&mut src[iteration..], old, new);
+ }
+}
+
fn modify_cmdline(old: &str, new: &str) -> Result<()> {
let boot = boot_dev()?;
- let cmdline = fs::read_to_string(boot)?;
- fs::write(boot, cmdline.replace(old, new))?;
+ let mut cmdline = fs::read(boot)?;
+ replace_slice(&mut cmdline, old.as_bytes(), new.as_bytes());
+ fs::write(boot, cmdline)?;
Ok(())
}