warn on nonempty image directory

This commit is contained in:
Jianfeng Jiang 2022-09-13 16:20:56 +08:00 committed by Zongmin.Gu
parent f1058e4ebb
commit 12b2f3b18d
2 changed files with 21 additions and 1 deletions

@ -9,7 +9,7 @@ use crate::error::{FILE_NOT_EXISTS_ERROR, INVALID_BOM_FILE_ERROR};
use crate::util::{
check_file_hash, copy_dir, copy_file, copy_shared_object, create_link, dest_in_root,
find_dependent_shared_objects, find_included_bom_file, infer_default_loader,
lazy_check_missing_libraries, mkdir, resolve_envs,
lazy_check_missing_libraries, mkdir, resolve_envs, warn_on_nonempty_image_dir,
};
use serde::{Deserialize, Serialize};
use serde_yaml;
@ -107,6 +107,7 @@ impl Bom {
dry_run: bool,
included_dirs: &Vec<String>,
) {
warn_on_nonempty_image_dir(root_dir);
// We need to keep the order of boms and bom managements
let mut sorted_boms = Vec::new();
let mut bom_managements = Vec::new();

@ -579,3 +579,22 @@ pub fn lazy_check_missing_libraries(image_dir: &str) {
std::process::exit(MISSING_LIBRARY_ERROR);
}
}
/// Print warn message if image directory is not empty.
/// The image dir should be empty before running copy_bom so copy bom can track all files to copy in bom file.
/// Users can ignore the warning now if they are sure the image directory contains correct contents.
pub fn warn_on_nonempty_image_dir(image_dir: &str) {
let image_path_buf = PathBuf::from(image_dir);
let image_path = image_path_buf.as_path();
if image_path.is_dir() {
if let Ok(read_dir) = image_path.read_dir() {
let dir_entries = read_dir.collect::<Vec<_>>();
if dir_entries.len() > 0 {
println!(
"WARNING: {} is not an empty directory before running copy_bom.",
image_dir
);
}
}
}
}