Skip to content

Commit

Permalink
Refactor early checks to a separate module
Browse files Browse the repository at this point in the history
This is in preparation for a bigger change that will come next.

Signed-off-by: Leandro Motta Barros <[email protected]>
Change-type: patch
  • Loading branch information
lmbarros committed May 1, 2024
1 parent b0f4b9f commit e96d4b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/stage1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod device_impl;

mod exe_copy;

mod checks;
mod image_retrieval;
mod utils;
mod wifi_config;
Expand All @@ -45,7 +46,7 @@ use crate::{
SYSTEM_CONNECTIONS_DIR, SYSTEM_PROXY_DIR, SYS_EFIVARS_DIR, SYS_EFI_DIR, TELINIT_CMD,
},
error::{Error, ErrorKind, Result, ToError},
file_exists, format_size_with_unit, get_mem_info, get_os_name, is_admin,
file_exists, format_size_with_unit, get_mem_info, get_os_name,
options::Options,
path_append,
stage2_config::{Stage2Config, UmountPart},
Expand All @@ -63,6 +64,8 @@ use crate::common::stage2_config::LogDevice;
use crate::common::system::{is_dir, mkdir, stat};
use mod_logger::{LogDestination, Logger, NO_STREAM};

use self::checks::do_early_checks;

const S1_XTRA_FS_SIZE: u64 = 10 * 1024 * 1024; // const XTRA_MEM_FREE: u64 = 10 * 1024 * 1024; // 10 MB

fn prepare_configs<P1: AsRef<Path>>(
Expand Down Expand Up @@ -582,9 +585,16 @@ pub fn stage1(opts: &Options) -> Result<()> {
}
};

if !is_admin()? {
error!("please run this program as root");
return Err(Error::displayed());
match do_early_checks(opts) {
Ok(_) => {
info!("Early checks passed");
}
Err(why) => {
return Err(Error::from_upstream(
Box::new(why),
"Failed early checks, exiting",
));
}
}

if !opts.no_ack() {
Expand Down
15 changes: 15 additions & 0 deletions src/stage1/checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use log::error;

use crate::common::{is_admin, Error, Options, Result};

/// Performs checks to ensure that the program can run properly with the
/// provided command-line options. Returns an error if the program cannot run
/// for some reason.
pub(crate) fn do_early_checks(_opts: &Options) -> Result<()> {
if !is_admin()? {
error!("please run this program as root");
return Err(Error::displayed());
}

Ok(())
}

0 comments on commit e96d4b5

Please sign in to comment.