22
33use std:: collections:: BTreeMap ;
44use std:: ffi:: OsStr ;
5- use std:: path:: Path ;
5+ use std:: path:: { Path , PathBuf } ;
66
77/// Describes how this module can fail
88#[ derive( Debug , thiserror:: Error ) ]
@@ -15,6 +15,8 @@ pub enum Error {
1515 LaunchingVendor ( std:: io:: Error ) ,
1616 #[ error( "Failed to complete cargo vendor" ) ]
1717 RunningVendor ,
18+ #[ error( "Bad path {0:?} whilst scraping files" ) ]
19+ Scraping ( PathBuf ) ,
1820}
1921
2022/// Uniquely describes a package on crates.io
@@ -150,24 +152,38 @@ fn load_important_files(
150152 let entry = entry?;
151153 let metadata = entry. metadata ( ) ?;
152154 let path = entry. path ( ) ;
153- if let Some ( filename) = path. file_name ( ) {
154- let lc_filename = filename. to_ascii_lowercase ( ) ;
155- let lc_filename_str = lc_filename. to_string_lossy ( ) ;
156- let mut keep = false ;
157- for m in [ "copyright" , "licence" , "license" , "author" , "notice" ] {
158- if lc_filename_str. contains ( m) {
159- keep = true ;
160- break ;
161- }
155+ let Some ( filename) = path. file_name ( ) else {
156+ return Err ( Error :: Scraping ( path) ) ;
157+ } ;
158+ let lc_filename = filename. to_ascii_lowercase ( ) ;
159+ let lc_filename_str = lc_filename. to_string_lossy ( ) ;
160+ let mut keep = false ;
161+ for m in [ "copyright" , "licence" , "license" , "author" , "notice" ] {
162+ if lc_filename_str. contains ( m) {
163+ keep = true ;
164+ break ;
162165 }
163- if keep {
164- if metadata. is_dir ( ) {
165- // scoop up whole directory
166- } else if metadata. is_file ( ) {
167- let filename = filename. to_string_lossy ( ) ;
168- println ! ( "Scraping {}" , filename) ;
169- dep. notices . insert ( filename. to_string ( ) , std:: fs:: read_to_string ( path) ?) ;
166+ }
167+ if keep {
168+ if metadata. is_dir ( ) {
169+ for inner_entry in std:: fs:: read_dir ( entry. path ( ) ) ? {
170+ let inner_entry = inner_entry?;
171+ if inner_entry. metadata ( ) ?. is_file ( ) {
172+ let inner_filename = inner_entry. file_name ( ) ;
173+ let inner_filename_str = inner_filename. to_string_lossy ( ) ;
174+ let qualified_filename =
175+ format ! ( "{}/{}" , lc_filename_str, inner_filename_str) ;
176+ println ! ( "Scraping {}" , qualified_filename) ;
177+ dep. notices . insert (
178+ qualified_filename. to_string ( ) ,
179+ std:: fs:: read_to_string ( inner_entry. path ( ) ) ?,
180+ ) ;
181+ }
170182 }
183+ } else if metadata. is_file ( ) {
184+ let filename = filename. to_string_lossy ( ) ;
185+ println ! ( "Scraping {}" , filename) ;
186+ dep. notices . insert ( filename. to_string ( ) , std:: fs:: read_to_string ( path) ?) ;
171187 }
172188 }
173189 }
0 commit comments