From 62b8b5889d167d02db929d65f71bd6050a696f6a Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Tue, 27 Sep 2022 14:17:19 +0800 Subject: [PATCH] fix copy_bom error message if rsync is not installed --- tools/copy_bom/src/error.rs | 3 ++- tools/copy_bom/src/main.rs | 2 ++ tools/copy_bom/src/util.rs | 22 ++++++++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tools/copy_bom/src/error.rs b/tools/copy_bom/src/error.rs index 2dec2e67..7d345c94 100644 --- a/tools/copy_bom/src/error.rs +++ b/tools/copy_bom/src/error.rs @@ -1,4 +1,4 @@ -/// This file defines error number to indicate different errors +//! This file defines error number to indicate different errors pub static FILE_NOT_EXISTS_ERROR: i32 = -1; pub static COPY_FILE_ERROR: i32 = -2; pub static INVALID_BOM_FILE_ERROR: i32 = -3; @@ -7,3 +7,4 @@ pub static CREATE_SYMLINK_ERROR: i32 = -5; pub static COPY_DIR_ERROR: i32 = -6; pub static INCORRECT_HASH_ERROR: i32 = -7; pub static MISSING_LIBRARY_ERROR: i32 = -8; +pub static RSYNC_NOT_FOUND_ERROR: i32 = -9; diff --git a/tools/copy_bom/src/main.rs b/tools/copy_bom/src/main.rs index c3cf4de6..70fe6850 100644 --- a/tools/copy_bom/src/main.rs +++ b/tools/copy_bom/src/main.rs @@ -10,6 +10,7 @@ extern crate walkdir; use bom::Bom; use env_logger::Env; use structopt::StructOpt; +use util::check_rsync; mod bom; mod error; @@ -49,6 +50,7 @@ fn main() { // the copy_bom log environmental variable let env = Env::new().filter("OCCLUM_LOG_LEVEL"); env_logger::init_from_env(env); + check_rsync(); let copy_bom_option = CopyBomOption::from_args(); copy_bom_option.copy_files(); diff --git a/tools/copy_bom/src/util.rs b/tools/copy_bom/src/util.rs index d3c296ee..ca647edb 100644 --- a/tools/copy_bom/src/util.rs +++ b/tools/copy_bom/src/util.rs @@ -1,6 +1,6 @@ use crate::error::{ COPY_DIR_ERROR, COPY_FILE_ERROR, CREATE_DIR_ERROR, CREATE_SYMLINK_ERROR, FILE_NOT_EXISTS_ERROR, - INCORRECT_HASH_ERROR, MISSING_LIBRARY_ERROR, + INCORRECT_HASH_ERROR, MISSING_LIBRARY_ERROR, RSYNC_NOT_FOUND_ERROR, }; use data_encoding::HEXUPPER; use elf::types::{Type, ET_DYN, ET_EXEC}; @@ -8,7 +8,7 @@ use regex::Regex; use sha2::{Digest, Sha256}; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; -use std::process::{Command, Output}; +use std::process::{Command, Output, Stdio}; use std::sync::Mutex; use std::vec; @@ -598,3 +598,21 @@ pub fn warn_on_nonempty_image_dir(image_dir: &str) { } } } + +/// Check rsync is installed by running command `rsync --version`. +/// The exit code should be zero if rsync is installed. +/// If rsync is not installed, copy_bom will abort. +pub fn check_rsync() { + let mut command = Command::new("rsync"); + command.arg("--version"); + command.stdout(Stdio::null()); + if let Ok(status) = command.status() { + if let Some(exit_code) = status.code() { + if exit_code == 0 { + return; + } + } + } + println!("rsync is not installed."); + std::process::exit(RSYNC_NOT_FOUND_ERROR); +}