Skip to content

Commit bf8d6b0

Browse files
committed
Auto merge of #7192 - alexcrichton:fix-backups, r=ehuss
Fix excluding target dirs from backups on OSX This fixes an accidental regression from #6880 identified in #7189 by moving where the configuration of backup preferences happens since it was accidentally never happening due to the folder always having been created. Closes #7189
2 parents 0237953 + 643b660 commit bf8d6b0

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/cargo/core/compiler/layout.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ impl Layout {
109109
///
110110
/// This function will block if the directory is already locked.
111111
pub fn at(config: &Config, root: Filesystem) -> CargoResult<Layout> {
112+
// If the root directory doesn't already exist go ahead and create it
113+
// here. Use this opportunity to exclude it from backups as well if the
114+
// system supports it since this is a freshly created folder.
115+
if !root.as_path_unlocked().exists() {
116+
root.create_dir()?;
117+
exclude_from_backups(root.as_path_unlocked());
118+
}
119+
112120
// For now we don't do any more finer-grained locking on the artifact
113121
// directory, so just lock the entire thing for the duration of this
114122
// compile.
@@ -127,42 +135,8 @@ impl Layout {
127135
})
128136
}
129137

130-
#[cfg(not(target_os = "macos"))]
131-
fn exclude_from_backups(&self, _: &Path) {}
132-
133-
#[cfg(target_os = "macos")]
134-
/// Marks files or directories as excluded from Time Machine on macOS
135-
///
136-
/// This is recommended to prevent derived/temporary files from bloating backups.
137-
fn exclude_from_backups(&self, path: &Path) {
138-
use core_foundation::base::TCFType;
139-
use core_foundation::{number, string, url};
140-
use std::ptr;
141-
142-
// For compatibility with 10.7 a string is used instead of global kCFURLIsExcludedFromBackupKey
143-
let is_excluded_key: Result<string::CFString, _> = "NSURLIsExcludedFromBackupKey".parse();
144-
let path = url::CFURL::from_path(path, false);
145-
if let (Some(path), Ok(is_excluded_key)) = (path, is_excluded_key) {
146-
unsafe {
147-
url::CFURLSetResourcePropertyForKey(
148-
path.as_concrete_TypeRef(),
149-
is_excluded_key.as_concrete_TypeRef(),
150-
number::kCFBooleanTrue as *const _,
151-
ptr::null_mut(),
152-
);
153-
}
154-
}
155-
// Errors are ignored, since it's an optional feature and failure
156-
// doesn't prevent Cargo from working
157-
}
158-
159138
/// Makes sure all directories stored in the Layout exist on the filesystem.
160139
pub fn prepare(&mut self) -> io::Result<()> {
161-
if fs::metadata(&self.root).is_err() {
162-
fs::create_dir_all(&self.root)?;
163-
self.exclude_from_backups(&self.root);
164-
}
165-
166140
mkdir(&self.deps)?;
167141
mkdir(&self.native)?;
168142
mkdir(&self.incremental)?;
@@ -209,3 +183,32 @@ impl Layout {
209183
&self.build
210184
}
211185
}
186+
187+
#[cfg(not(target_os = "macos"))]
188+
fn exclude_from_backups(_: &Path) {}
189+
190+
#[cfg(target_os = "macos")]
191+
/// Marks files or directories as excluded from Time Machine on macOS
192+
///
193+
/// This is recommended to prevent derived/temporary files from bloating backups.
194+
fn exclude_from_backups(path: &Path) {
195+
use core_foundation::base::TCFType;
196+
use core_foundation::{number, string, url};
197+
use std::ptr;
198+
199+
// For compatibility with 10.7 a string is used instead of global kCFURLIsExcludedFromBackupKey
200+
let is_excluded_key: Result<string::CFString, _> = "NSURLIsExcludedFromBackupKey".parse();
201+
let path = url::CFURL::from_path(path, false);
202+
if let (Some(path), Ok(is_excluded_key)) = (path, is_excluded_key) {
203+
unsafe {
204+
url::CFURLSetResourcePropertyForKey(
205+
path.as_concrete_TypeRef(),
206+
is_excluded_key.as_concrete_TypeRef(),
207+
number::kCFBooleanTrue as *const _,
208+
ptr::null_mut(),
209+
);
210+
}
211+
}
212+
// Errors are ignored, since it's an optional feature and failure
213+
// doesn't prevent Cargo from working
214+
}

0 commit comments

Comments
 (0)