11//! Functions to deal with the build cache
22use std:: {
33 collections:: { BTreeMap , HashSet } ,
4- io,
5- path:: Path ,
4+ path:: { Path , PathBuf } ,
65} ;
76
87use content_inspector:: ContentType ;
@@ -14,13 +13,7 @@ use sha2::{Digest, Sha256};
1413use crate :: {
1514 env_vars,
1615 metadata:: { Output , build_reindexed_channels} ,
17- packaging:: {
18- Files ,
19- metadata:: {
20- contains_prefix_binary, contains_prefix_text,
21- rewrite_prefix_in_file,
22- } ,
23- } ,
16+ packaging:: { Files , contains_prefix_binary, contains_prefix_text, rewrite_prefix_in_file} ,
2417 recipe:: {
2518 Jinja ,
2619 parser:: { Dependency , Requirements , Source } ,
@@ -37,11 +30,15 @@ use crate::{
3730
3831/// Check if a file contains the prefix and determine if it's binary or text
3932/// Returns (has_prefix, is_text)
40- fn check_file_for_prefix ( file_path : & Path , prefix : & Path , prefix_str : & str ) -> ( bool , bool ) {
41- let content_type = content_inspector:: inspect_path ( file_path) ;
33+ fn check_file_for_prefix ( file_path : & Path , prefix : & Path ) -> ( bool , bool ) {
34+ let content = match fs:: read ( file_path) {
35+ Ok ( content) => content,
36+ Err ( _) => return ( false , false ) ,
37+ } ;
38+ let content_type = content_inspector:: inspect ( & content) ;
4239 let is_text = content_type. is_text ( )
4340 && matches ! ( content_type, ContentType :: UTF_8 | ContentType :: UTF_8_BOM ) ;
44-
41+
4542 if is_text {
4643 match contains_prefix_text ( file_path, prefix) {
4744 Ok ( Some ( _) ) => ( true , true ) ,
@@ -60,15 +57,19 @@ fn check_file_for_prefix(file_path: &Path, prefix: &Path, prefix_str: &str) -> (
6057 {
6158 if let Ok ( contents) = fs:: read ( file_path) {
6259 let prefix_bytes = prefix. to_string_lossy ( ) . as_bytes ( ) ;
63- ( contents. windows ( prefix_bytes. len ( ) ) . any ( |window| window == prefix_bytes) , false )
60+ (
61+ contents
62+ . windows ( prefix_bytes. len ( ) )
63+ . any ( |window| window == prefix_bytes) ,
64+ false ,
65+ )
6466 } else {
6567 ( false , false )
6668 }
6769 }
6870 }
6971}
7072
71-
7273/// Error type for cache key generation
7374#[ derive( Debug , thiserror:: Error ) ]
7475pub enum CacheKeyError {
@@ -314,7 +315,11 @@ impl Output {
314315 // If the cache was built under a different prefix, rewrite occurrences of
315316 // the old prefix in restored text and binary files.
316317 if cache. prefix != * self . prefix ( ) {
317- for rel in cache. files_with_prefix . iter ( ) . chain ( cache. binary_files_with_prefix . iter ( ) ) {
318+ for rel in cache
319+ . files_with_prefix
320+ . iter ( )
321+ . chain ( cache. binary_files_with_prefix . iter ( ) )
322+ {
318323 for base in [
319324 self . prefix ( ) ,
320325 & self . build_configuration . directories . work_dir ,
@@ -323,13 +328,9 @@ impl Output {
323328 if !path. exists ( ) {
324329 continue ;
325330 }
326-
331+
327332 if let Err ( e) = rewrite_prefix_in_file ( & path, & cache. prefix , self . prefix ( ) ) {
328- tracing:: warn!(
329- "Failed to rewrite restored file {}: {}" ,
330- path. display( ) ,
331- e
332- ) ;
333+ tracing:: warn!( "Failed to rewrite restored file {}: {}" , path. display( ) , e) ;
333334 }
334335 }
335336 }
@@ -345,7 +346,11 @@ impl Output {
345346 if !path. exists ( ) {
346347 continue ;
347348 }
348- if let Err ( e) = rewrite_prefix_in_file ( & path, & cache. work_dir , & self . build_configuration . directories . work_dir ) {
349+ if let Err ( e) = rewrite_prefix_in_file (
350+ & path,
351+ & cache. work_dir ,
352+ & self . build_configuration . directories . work_dir ,
353+ ) {
349354 tracing:: warn!(
350355 "Failed to rewrite restored work_dir file {}: {}" ,
351356 path. display( ) ,
@@ -501,7 +506,6 @@ impl Output {
501506 // Track files that contain the old prefix for later path rewriting
502507 let mut files_with_prefix: Vec < PathBuf > = Vec :: new ( ) ;
503508 let mut binary_files_with_prefix: Vec < PathBuf > = Vec :: new ( ) ;
504- let old_prefix_str = self . prefix ( ) . to_string_lossy ( ) . to_string ( ) ;
505509
506510 for file in & new_files. new_files {
507511 if file. is_dir ( ) && !file. is_symlink ( ) {
@@ -511,8 +515,8 @@ impl Output {
511515 let dest = prefix_cache_dir. join ( stripped) ;
512516 copy_file ( file, & dest, & mut creation_cache, & copy_options) . into_diagnostic ( ) ?;
513517 copied_files. push ( stripped. to_path_buf ( ) ) ;
514- let ( has_prefix, is_text) = check_file_for_prefix ( file, self . prefix ( ) , & old_prefix_str ) ;
515-
518+ let ( has_prefix, is_text) = check_file_for_prefix ( file, self . prefix ( ) ) ;
519+
516520 if has_prefix {
517521 match is_text {
518522 true => files_with_prefix. push ( stripped. to_path_buf ( ) ) ,
@@ -541,21 +545,16 @@ impl Output {
541545 binary_files_with_prefix,
542546 files_with_work_dir : {
543547 let mut files = Vec :: new ( ) ;
544- let old_work_dir_str = self
545- . build_configuration
546- . directories
547- . work_dir
548- . to_string_lossy ( )
549- . to_string ( ) ;
550548 for rel in work_dir_files. copied_paths ( ) {
551549 let abs = self . build_configuration . directories . work_dir . join ( rel) ;
552550 if abs. is_dir ( ) {
553551 continue ;
554552 }
555- match contains_prefix_text ( & abs, & self . build_configuration . directories . work_dir ) {
553+ match contains_prefix_text ( & abs, & self . build_configuration . directories . work_dir )
554+ {
556555 Ok ( Some ( _) ) => files. push ( rel. to_path_buf ( ) ) ,
557- Ok ( None ) => { } ,
558- Err ( _) => { } ,
556+ Ok ( None ) => { }
557+ Err ( _) => { }
559558 }
560559 }
561560 files
@@ -729,7 +728,6 @@ impl Output {
729728 let copy_options = CopyOptions :: default ( ) ;
730729 let mut files_with_prefix: Vec < PathBuf > = Vec :: new ( ) ;
731730 let mut binary_files_with_prefix: Vec < PathBuf > = Vec :: new ( ) ;
732- let old_prefix_str = self . prefix ( ) . to_string_lossy ( ) . to_string ( ) ;
733731
734732 for file in & new_files. new_files {
735733 // skip directories (if they are not a symlink)
@@ -743,7 +741,7 @@ impl Output {
743741 let dest = & prefix_cache_dir. join ( stripped) ;
744742 copy_file ( file, dest, & mut creation_cache, & copy_options) . into_diagnostic ( ) ?;
745743 copied_files. push ( stripped. to_path_buf ( ) ) ;
746- let ( has_prefix, is_text) = check_file_for_prefix ( file, self . prefix ( ) , & old_prefix_str ) ;
744+ let ( has_prefix, is_text) = check_file_for_prefix ( file, self . prefix ( ) ) ;
747745 if has_prefix {
748746 match is_text {
749747 true => files_with_prefix. push ( stripped. to_path_buf ( ) ) ,
0 commit comments