Skip to content

Commit 01d059a

Browse files
committed
Auto merge of #9635 - toothbrush7777777:patch-1, r=alexcrichton
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
2 parents a15fec7 + c973a6d commit 01d059a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 30 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,35 @@ 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+
#[cfg(windows)]
683+
{
684+
use std::iter::once;
685+
use std::os::windows::prelude::OsStrExt;
686+
use winapi::um::fileapi::{GetFileAttributesW, SetFileAttributesW};
687+
use winapi::um::winnt::FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
688+
689+
let path: Vec<u16> = path.as_os_str().encode_wide().chain(once(0)).collect();
690+
unsafe {
691+
SetFileAttributesW(
692+
path.as_ptr(),
693+
GetFileAttributesW(path.as_ptr()) | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
694+
);
695+
}
696+
}
697+
#[cfg(not(windows))]
698+
{
699+
let _ = path;
700+
}
701+
}
702+
673703
#[cfg(not(target_os = "macos"))]
674704
fn exclude_from_time_machine(_: &Path) {}
675705

0 commit comments

Comments
 (0)