fn parse_rootdisk_device()

in metalos/metalctl/src/switch_root.rs [118:158]


fn parse_rootdisk_device(mounts: String) -> Result<(String, String)> {
    let (mut dev, opts): (String, String) = mounts
        .lines()
        .filter_map(|l| {
            let fields: Vec<_> = l.split_whitespace().collect();
            if Path::new(fields[1]) == metalos_paths::control() {
                Some((fields[0].into(), fields[3].into()))
            } else {
                None
            }
        })
        .next()
        .ok_or_else(|| anyhow!("{} not in mounts", metalos_paths::control().display()))?;

    // /proc/mounts escapes characters with octal
    if dev.contains('\\') {
        let mut octal_chars: Option<String> = None;
        dev = dev.chars().fold("".to_string(), |mut s, ch| {
            if let Some(ref mut oc) = octal_chars {
                oc.push(ch);
                if oc.len() == 3 {
                    let escaped = u32::from_str_radix(&oc, 8)
                        .with_context(|| format!("'{}' is not a valid octal number", &oc))
                        .unwrap();
                    let escaped = char::from_u32(escaped)
                        .with_context(|| format!("0o{} is not a valid character", escaped))
                        .unwrap();
                    s.push(escaped);

                    octal_chars = None;
                }
            } else if ch == '\\' {
                octal_chars = Some(String::new());
            } else {
                s.push(ch);
            }
            s
        });
    }
    Ok((dev, opts))
}