From 53d1d0010d7ad929cede7699278583a9bb78e4da Mon Sep 17 00:00:00 2001 From: jiangjianfeng Date: Fri, 17 Sep 2021 14:28:09 +0800 Subject: [PATCH] Add operations to manage NormalFile --- tools/copy_bom/src/bom.rs | 61 ++++++++++++++++++++++++++++++++++++++ tools/copy_bom/src/util.rs | 25 ++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/tools/copy_bom/src/bom.rs b/tools/copy_bom/src/bom.rs index 981f39d2..88987e0a 100644 --- a/tools/copy_bom/src/bom.rs +++ b/tools/copy_bom/src/bom.rs @@ -291,6 +291,67 @@ impl Source { } } +impl NormalFile { + fn get_file_to_copy_and_autodep( + &self, + src_dir: &str, + target_dir: &str, + ) -> (Vec<(String, String)>, Vec) { + let target_dir_path = PathBuf::from(target_dir); + let src_dir_path = PathBuf::from(src_dir); + let mut file_to_copy = Vec::new(); + let mut file_autodep = Vec::new(); + match self { + NormalFile::FileName(file_name) => { + let src_file_path = src_dir_path.join(file_name); + // This unwrap should never fail + let src_file_name = src_file_path + .file_name() + .unwrap() + .to_string_lossy() + .to_string(); + let src_file = src_file_path.to_string_lossy().to_string(); + let target_file = target_dir_path + .join(src_file_name) + .to_string_lossy() + .to_string(); + file_to_copy.push((src_file.clone(), target_file)); + // default : autodep is true + file_autodep.push(src_file); + } + NormalFile::FileWithOption(file_with_option) => { + let file_name = &file_with_option.name; + let src_file_path = src_dir_path.join(file_name); + //This unwrap should never fail + let src_file_name = src_file_path + .file_name() + .unwrap() + .to_string_lossy() + .to_string(); + let src_file = src_file_path.to_string_lossy().to_string(); + // check file hash + if let Some(ref hash) = file_with_option.hash { + check_file_hash(&src_file, hash); + } + // autodep + if file_with_option.autodep.clone().unwrap_or(true) { + file_autodep.push(src_file.clone()) + } + // rename file + let target_file = match file_with_option.rename { + Some(ref rename) => target_dir_path.join(rename).to_string_lossy().to_string(), + None => target_dir_path + .join(src_file_name) + .to_string_lossy() + .to_string(), + }; + file_to_copy.push((src_file, target_file)); + } + } + (file_to_copy, file_autodep) + } +} + impl BomManagement { fn add_target_management(&mut self, mut target_management: TargetManagement, root_dir: &str) { // First, we need to resolve environmental variables diff --git a/tools/copy_bom/src/util.rs b/tools/copy_bom/src/util.rs index 9a97360a..5749393f 100644 --- a/tools/copy_bom/src/util.rs +++ b/tools/copy_bom/src/util.rs @@ -22,6 +22,31 @@ pub fn dest_in_root(root_dir: &str, dest: &str) -> PathBuf { return root_path.join(dest_relative); } +/// check if hash of the file is equal to the passed hash value. +pub fn check_file_hash(filename: &str, hash: &str) { + let file_hash = calculate_file_hash(filename); + if file_hash != hash.to_string() { + error!( + "The hash value of {} should be {:?}. Please correct it.", + filename, file_hash + ); + std::process::exit(INCORRECT_HASH_ERROR); + } +} + +/// Use sha256 to calculate hash for file content. The returned hash is a hex-encoded string. +pub fn calculate_file_hash(filename: &str) -> String { + let mut file = std::fs::File::open(filename).unwrap_or_else(|e| { + println!("can not open file {}. {}", filename, e); + std::process::exit(FILE_NOT_EXISTS_ERROR); + }); + let mut hasher = Sha256::new(); + std::io::copy(&mut file, &mut hasher).unwrap(); + let hash = hasher.finalize(); + let hash = HEXUPPER.encode(&hash); + hash +} + /// find an included file in the file system. If we can find the bom file, return the path /// otherwise, the process exit with error /// if included dir is relative path, if will be viewed as path relative to the `current` path (where we execute command)