Add operations to manage NormalFile
This commit is contained in:
		
							parent
							
								
									d2656a3571
								
							
						
					
					
						commit
						53d1d0010d
					
				| @ -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<String>) { | ||||||
|  |         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 { | impl BomManagement { | ||||||
|     fn add_target_management(&mut self, mut target_management: TargetManagement, root_dir: &str) { |     fn add_target_management(&mut self, mut target_management: TargetManagement, root_dir: &str) { | ||||||
|         // First, we need to resolve environmental variables
 |         // First, we need to resolve environmental variables
 | ||||||
|  | |||||||
| @ -22,6 +22,31 @@ pub fn dest_in_root(root_dir: &str, dest: &str) -> PathBuf { | |||||||
|     return root_path.join(dest_relative); |     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
 | /// find an included file in the file system. If we can find the bom file, return the path
 | ||||||
| /// otherwise, the process exit with error
 | /// 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)
 | /// if included dir is relative path, if will be viewed as path relative to the `current` path (where we execute command)
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user