Skip to content

Commit ca648bd

Browse files
committed
Add persistent_temp_path
1 parent 9d1a486 commit ca648bd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/cargo/util/paths.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,36 @@ fn exclude_from_time_machine(path: &Path) {
547547
// Errors are ignored, since it's an optional feature and failure
548548
// doesn't prevent Cargo from working
549549
}
550+
551+
/// Absolute path to an on-disk temporary directory.
552+
/// It may be system-wide or user-specific depending on system conventions.
553+
/// Used in layout.rs, only needed on Unix systems.
554+
#[cfg(unix)]
555+
pub fn persistent_temp_path() -> Option<PathBuf> {
556+
#[cfg(target_os = "macos")]
557+
{
558+
// "Library/Caches" should be obtained via NSFileManager's
559+
// URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask
560+
// However, cocoa-foundation doesn't have bindings for it yet.
561+
// This path has remained stable for two decades,
562+
// so hardcode it for simplicity (dirs crate does the same).
563+
Some(home::home_dir()?.join("Library/Caches/Cargo"))
564+
}
565+
#[cfg(not(target_os = "macos"))]
566+
{
567+
// XDG standard
568+
if let Some(path) = env::var_os("XDG_CACHE_HOME") {
569+
let path = PathBuf::from(path);
570+
if path.is_absolute() {
571+
return Some(path.join("cargo"));
572+
}
573+
}
574+
// FHS standard
575+
// This is not using /tmp, because /tmp could be using ramfs.
576+
let path = Path::new("/var/tmp");
577+
if path.exists() {
578+
return Some(path.join("cargo"));
579+
}
580+
None
581+
}
582+
}

0 commit comments

Comments
 (0)