fn what_distro_name_version()

in src/distro.rs [242:427]


    fn what_distro_name_version(
        partitions: &mut Vec<PartInfo>,
        cli_info: &CliInfo,
        distro: &mut Distro,
    ) -> Option<DistroNameVersion> {
        let recovery_disk_path = helper::get_recovery_disk_path(cli_info);

        debug!("recovery_disk_path: {}", recovery_disk_path);

        if mount::mkdir_assert().is_err() {
            error!("Error creating assert dir. ALAR is not able to proceed. Exiting.");
            process::exit(1);
        }

        // cycling through each of the partitions to figure out what sort of partition we do have
        for partition in partitions {
            // EFI part no need to check
            if partition.part_type == "EF00" {
                continue;
            }

            if partition.part_type == "8E00" && partition.fstype == "LVM2_member" {
                debug!("Found LVM partition. Executing read_distro_name_version_from_lv");
                return Self::read_distro_name_version_from_lv(partition, distro.is_ade);
            }

            // Above we handle any kind of LVM partition including an encrypted one.
            // Below we handle the rest of the non-LVM partitions including one which resides on an encrypted device.

            fn error_condition_mount(e: anyhow::Error) {
                error!("Error mounting partition: {e}");
                process::exit(1);
            }

            fn error_condition_umount(e: anyhow::Error) {
                error!("Error umounting partition: {e}, this may cause side effects");
                process::exit(1);
            }

            fn nouuid_option(fstype: &str) -> &str {
                if fstype == "xfs" {
                    "nouuid"
                } else {
                    ""
                }
            }

            let mount_path = format!("{}{}", &recovery_disk_path, partition.number);
            debug!(
                "Mounting partition number {} to {}",
                partition.number, &mount_path
            );

            match partition.fstype.as_str() {
                fs if fs == "xfs" || fs == "ext4" => {
                    match mount::fsck_partition(&mount_path) {
                        Ok(_) => {}
                        Err(e) => {
                            error!("Error fscking partition: {e}");
                            process::exit(1);
                        }
                    }

                    match mount::mount(
                        &mount_path,
                        constants::ASSERT_PATH,
                        nouuid_option(fs),
                        false,
                    ) {
                        Ok(_) => {}
                        Err(e) => error_condition_mount(e),
                    }
                }
                // If the partition is marked as 'crypt?' the partition path needs to be corrected
                "crypt?" => {
                    let partition_path = constants::ADE_OSENCRYPT_PATH;
                    let fstype = Self::get_partition_filesystem(partition_path)
                        .unwrap_or("xfs".to_string());
                    debug!("Filesystem type for the encrypted partition is: {}", fstype);

                    match mount::fsck_partition(partition_path) {
                        Ok(_) => {}
                        Err(e) => {
                            error!("Error fscking partition: {e}");
                            process::exit(1);
                        }
                    }

                    match mount::mount(
                        partition_path,
                        constants::ASSERT_PATH,
                        nouuid_option(&fstype),
                        false,
                    ) {
                        Ok(_) => {}
                        Err(e) => error_condition_mount(e),
                    }
                }
                _ => {
                    match mount::fsck_partition(&mount_path) {
                        Ok(_) => {}
                        Err(e) => {
                            error!("Error fscking partition: {e}");
                            process::exit(1);
                        }
                    }

                    match mount::mount(&mount_path, constants::ASSERT_PATH, "", false) {
                        Ok(_) => {}
                        Err(e) => error_condition_mount(e),
                    }
                }
            }

            if Path::new(constants::OS_RELEASE).is_file() {
                // If we have found this file we can be sure this is the OS partition
                partition.activate_is_os();

                let mut _name = "".to_string();
                let mut _version_id = "".to_string();
                //unwrap is safe here because we checked if the file exists
                for line in fs::read_to_string(constants::OS_RELEASE).unwrap().lines() {
                    let detail = line.trim();
                    if detail.starts_with("NAME=") {
                        _name = detail
                            .strip_prefix("NAME=")
                            .unwrap()
                            .to_string()
                            .replace('"', "");
                    }
                    if detail.starts_with("VERSION_ID=") {
                        _version_id = detail
                            .strip_prefix("VERSION_ID=")
                            .unwrap()
                            .to_string()
                            .replace('"', "");
                    }
                }

                // What is the architecture of the system to be recovered?
                let file_bash_info = match helper::run_fun("file /tmp/assert/bin/bash") {
                    Ok(info) => info,
                    Err(e) => {
                        error!("Error getting bash info: {e}");
                        "".to_string()
                    }
                };

                if file_bash_info.contains("aarch64") {
                    distro.architecture = Architecture::Aarch64;
                } else {
                    distro.architecture = Architecture::X86_64;
                }

                match mount::umount(constants::ASSERT_PATH, false) {
                    Ok(_) => {}
                    Err(e) => error_condition_umount(e),
                }

                if mount::rmdir(constants::ASSERT_PATH).is_ok() {
                    info!("Removed assert path");
                } else {
                    error!("Erro removing directory ASSER_PATH");
                }

                return Some(DistroNameVersion {
                    name: _name,
                    //version_id: _version_id.parse::<f32>().unwrap(),
                    version_id: _version_id,
                });
            }
            match mount::umount(constants::ASSERT_PATH, false) {
                Ok(_) => {}
                Err(e) => error_condition_umount(e),
            }
        }

        if mount::rmdir(constants::ASSERT_PATH).is_ok() {
            info!("Removed assert path");
        } else {
            error!("Erro removing directory ASSER_PATH");
        }
        // If we reach this point we haven't found the OS partition
        // which could point out to operate on a data disk.
        None
    }