From 12b2f3b18d357f41428564617e70d3ea21d84346 Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Tue, 13 Sep 2022 16:20:56 +0800 Subject: [PATCH] warn on nonempty image directory --- tools/copy_bom/src/bom.rs | 3 ++- tools/copy_bom/src/util.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tools/copy_bom/src/bom.rs b/tools/copy_bom/src/bom.rs index cfb9df7f..b41fd5aa 100644 --- a/tools/copy_bom/src/bom.rs +++ b/tools/copy_bom/src/bom.rs @@ -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, ) { + 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(); diff --git a/tools/copy_bom/src/util.rs b/tools/copy_bom/src/util.rs index 44a86af1..d3c296ee 100644 --- a/tools/copy_bom/src/util.rs +++ b/tools/copy_bom/src/util.rs @@ -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::>(); + if dir_entries.len() > 0 { + println!( + "WARNING: {} is not an empty directory before running copy_bom.", + image_dir + ); + } + } + } +}