Skip to content

Commit 8c6b080

Browse files
committed
Auto merge of #4386 - pornel:backups, r=alexcrichton
Exclude target directory from Time Machine Fixes #3884
2 parents 7704f7b + 8e0a7ca commit 8c6b080

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ url = "1.1"
5454
[target.'cfg(unix)'.dependencies]
5555
openssl = "0.9"
5656

57+
[target.'cfg(target_os = "macos")'.dependencies]
58+
core-foundation = { version = "0.4.4", features = ["mac_os_10_7_support"] }
59+
5760
[target.'cfg(windows)'.dependencies]
5861
advapi32-sys = "0.2"
5962
kernel32-sys = "0.2"

src/cargo/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ extern crate tempdir;
3434
extern crate termcolor;
3535
extern crate toml;
3636
extern crate url;
37+
#[cfg(target_os = "macos")]
38+
extern crate core_foundation;
3739

3840
use std::fmt;
3941
use std::error::Error;

src/cargo/ops/cargo_rustc/layout.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,42 @@ impl Layout {
108108
})
109109
}
110110

111+
#[cfg(not(target_os = "macos"))]
112+
fn exclude_from_backups(&self, _: &Path) {}
113+
114+
#[cfg(target_os = "macos")]
115+
/// Marks files or directories as excluded from Time Machine on macOS
116+
///
117+
/// This is recommended to prevent derived/temporary files from bloating backups.
118+
fn exclude_from_backups(&self, path: &Path) {
119+
use std::ptr;
120+
use core_foundation::{url, number, string};
121+
use core_foundation::base::TCFType;
122+
123+
// For compatibility with 10.7 a string is used instead of global kCFURLIsExcludedFromBackupKey
124+
let is_excluded_key: Result<string::CFString, _> = "NSURLIsExcludedFromBackupKey".parse();
125+
match (url::CFURL::from_path(path, false), is_excluded_key) {
126+
(Some(path), Ok(is_excluded_key)) => unsafe {
127+
url::CFURLSetResourcePropertyForKey(
128+
path.as_concrete_TypeRef(),
129+
is_excluded_key.as_concrete_TypeRef(),
130+
number::kCFBooleanTrue as *const _,
131+
ptr::null_mut(),
132+
);
133+
},
134+
// Errors are ignored, since it's an optional feature and failure
135+
// doesn't prevent Cargo from working
136+
_ => {}
137+
}
138+
}
139+
111140
pub fn prepare(&mut self) -> io::Result<()> {
112141
if fs::metadata(&self.root).is_err() {
113142
fs::create_dir_all(&self.root)?;
114143
}
115144

145+
self.exclude_from_backups(&self.root);
146+
116147
mkdir(&self.deps)?;
117148
mkdir(&self.native)?;
118149
mkdir(&self.incremental)?;

0 commit comments

Comments
 (0)