@@ -637,6 +637,7 @@ pub fn create_dir_all_excluded_from_backups_atomic(p: impl AsRef<Path>) -> Resul
637
637
// point as the old one).
638
638
let tempdir = TempFileBuilder :: new ( ) . prefix ( base) . tempdir_in ( parent) ?;
639
639
exclude_from_backups ( tempdir. path ( ) ) ;
640
+ exclude_from_content_indexing ( tempdir. path ( ) ) ;
640
641
// Previously std::fs::create_dir_all() (through paths::create_dir_all()) was used
641
642
// here to create the directory directly and fs::create_dir_all() explicitly treats
642
643
// the directory being created concurrently by another thread or process as success,
@@ -670,6 +671,29 @@ fn exclude_from_backups(path: &Path) {
670
671
// Similarly to exclude_from_time_machine() we ignore errors here as it's an optional feature.
671
672
}
672
673
674
+ /// Marks the directory as excluded from content indexing.
675
+ ///
676
+ /// This is recommended to prevent the content of derived/temporary files from being indexed.
677
+ /// This is very important for Windows users, as the live content indexing significantly slows
678
+ /// cargo's I/O operations.
679
+ ///
680
+ /// This is currently a no-op on non-Windows platforms.
681
+ fn exclude_from_content_indexing ( path : & Path ) {
682
+ if cfg ! ( windows) {
683
+ use std:: iter:: once;
684
+ use std:: os:: windows:: prelude:: OsStrExt ;
685
+ use winapi:: um:: fileapi:: { GetFileAttributesW , SetFileAttributesW } ;
686
+ use winapi:: um:: winnt:: FILE_ATTRIBUTE_NOT_CONTENT_INDEXED ;
687
+
688
+ let lp_path: Vec < u16 > = path. as_os_str ( ) . encode_wide ( ) . chain ( once ( 0 ) ) . collect ( ) ;
689
+ unsafe {
690
+ SetFileAttributesW ( lp_path. as_ptr ( ) , GetFileAttributesW ( lp_path. as_ptr ( ) ) | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED ) ;
691
+ }
692
+ } else {
693
+ let _ = path;
694
+ }
695
+ }
696
+
673
697
#[ cfg( not( target_os = "macos" ) ) ]
674
698
fn exclude_from_time_machine ( _: & Path ) { }
675
699
0 commit comments