diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-08-15 14:03:30 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-08-15 14:03:30 +0200 |
commit | caa3380ba8d7ed74124d964242557f31a9cc9d24 (patch) | |
tree | cd218cdca33fddaaa3e2f3e8683bf6fa7e7ceee4 | |
parent | 3411fef68cb2821687ae033b064ef44711380182 (diff) |
Revert "Revert "overwrite boot partition to modify cmdline""
This reverts commit 3411fef68cb2821687ae033b064ef44711380182.
-rw-r--r-- | src/main.rs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index c07af1a..89db44c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -261,9 +261,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 cmdline = fs::read_to_string("/boot/cmdline.txt")?; - fs::write("/boot/cmdline.txt", cmdline.replace(old, new))?; + let boot = boot_dev()?; + + let mut cmdline = fs::read(boot)?; + replace_slice(&mut cmdline, old.as_bytes(), new.as_bytes()); + fs::write(boot, cmdline)?; nix::unistd::sync(); Ok(()) |