From 431d52ceeab222ffa5dbd24b77ab8c7cbbcd7661 Mon Sep 17 00:00:00 2001 From: Alvise Rigo Date: Wed, 18 Oct 2023 09:54:09 +0000 Subject: [PATCH] add as_string() to the Cmdline crate In some cases, having a String representation of the Linux command line can be useful. This is the case of vmm-reference which otherwise would require a conversion dance between string types. Signed-off-by: Alvise Rigo --- src/cmdline/mod.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/cmdline/mod.rs b/src/cmdline/mod.rs index 2ac7c312..0e56396b 100644 --- a/src/cmdline/mod.rs +++ b/src/cmdline/mod.rs @@ -357,6 +357,33 @@ impl Cmdline { } } + /// Returns a String representation of the command line + /// + /// # Examples + /// + /// ```rust + /// # use linux_loader::cmdline::*; + /// let mut cl = Cmdline::new(20).unwrap(); + /// cl.insert_str("foo").unwrap(); + /// cl.insert_init_args("bar").unwrap(); + /// assert_eq!(cl.as_string().unwrap(), "foo -- bar"); + /// ``` + pub fn as_string(&self) -> Result { + if self.boot_args.is_empty() && self.init_args.is_empty() { + Ok("".to_string()) + } else if self.boot_args.is_empty() { + Err(Error::NoBootArgsInserted) + } else if self.init_args.is_empty() { + Ok(self.boot_args.to_string()) + } else { + Ok(format!( + "{}{}{}", + self.boot_args, INIT_ARGS_SEPARATOR, self.init_args + ) + .to_owned()) + } + } + /// Adds a virtio MMIO device to the kernel command line. /// /// Multiple devices can be specified, with multiple `virtio_mmio.device=` options. This