Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add as_string() to the Cmdline crate #169

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/cmdline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,37 @@ impl Cmdline {
}
}

/// Convert a Cmdline to String
///
/// # 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!(String::try_from(&cl).unwrap(), "foo -- bar");
/// ```
impl TryFrom<&Cmdline> for String {
type Error = Error;

fn try_from(value: &Cmdline) -> result::Result<Self, Self::Error> {
if value.boot_args.is_empty() && value.init_args.is_empty() {
Ok("".to_string())
} else if value.boot_args.is_empty() {
Err(Error::NoBootArgsInserted)
} else if value.init_args.is_empty() {
Ok(value.boot_args.to_string())
} else {
Ok(format!(
"{}{}{}",
value.boot_args, INIT_ARGS_SEPARATOR, value.init_args
)
.to_string())
Comment on lines +534 to +545
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code ends up being almost the same as the as_cstring function. Is there a way to not duplicate the code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. What about this:

impl TryFrom<&Cmdline> for CString {
    type Error = Error;

    fn try_from(value: &Cmdline) -> result::Result<Self, Self::Error> {
        let as_string = <String as TryFrom<&Cmdline>>::try_from(value)?;
        Ok(CString::new(as_string).map_err(|_| Error::NullTerminator)?)
    }
}

}
}
}

impl TryFrom<Cmdline> for Vec<u8> {
type Error = Error;

Expand Down Expand Up @@ -867,4 +898,24 @@ mod tests {
b"console=ttyS0 nomodules -- /etc/password --param\0"
);
}

#[test]
fn test_string_from_cmdline() {
let mut cl = Cmdline::new(CMDLINE_MAX_SIZE).unwrap();

assert_eq!(String::try_from(&cl).unwrap(), "");
cl.insert_init_args("bar").unwrap();
// This must fail as it is not allowed to have only init args
let err = String::try_from(&cl).unwrap_err();
assert_eq!(err, Error::NoBootArgsInserted);

// However, inserting only bootargs is permitted
cl = Cmdline::new(CMDLINE_MAX_SIZE).unwrap();
cl.insert_str("foo").unwrap();
assert_eq!(String::try_from(&cl).unwrap(), "foo");

// Test also the concatenation of arguments
cl.insert_init_args("bar").unwrap();
assert_eq!(String::try_from(&cl).unwrap(), "foo -- bar");
}
}