Skip to content

Commit 9d96a88

Browse files
Exclude target from content-indexing on Windows
This has a noticeable performance improvement for most projects, especially when the storage device is a hard drive. Closes #8694
1 parent 3691752 commit 9d96a88

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ pub fn create_dir_all_excluded_from_backups_atomic(p: impl AsRef<Path>) -> Resul
637637
// point as the old one).
638638
let tempdir = TempFileBuilder::new().prefix(base).tempdir_in(parent)?;
639639
exclude_from_backups(tempdir.path());
640+
exclude_from_content_indexing(tempdir.path());
640641
// Previously std::fs::create_dir_all() (through paths::create_dir_all()) was used
641642
// here to create the directory directly and fs::create_dir_all() explicitly treats
642643
// the directory being created concurrently by another thread or process as success,
@@ -670,6 +671,29 @@ fn exclude_from_backups(path: &Path) {
670671
// Similarly to exclude_from_time_machine() we ignore errors here as it's an optional feature.
671672
}
672673

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+
673697
#[cfg(not(target_os = "macos"))]
674698
fn exclude_from_time_machine(_: &Path) {}
675699

0 commit comments

Comments
 (0)