summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/mac_util.h3
-rw-r--r--base/mac_util.mm18
-rw-r--r--base/mac_util_unittest.mm24
3 files changed, 45 insertions, 0 deletions
diff --git a/base/mac_util.h b/base/mac_util.h
index a65576d..ed49e2f 100644
--- a/base/mac_util.h
+++ b/base/mac_util.h
@@ -89,6 +89,9 @@ void GrabWindowSnapshot(NSWindow* window,
// returns - path to the application bundle, or empty on error
FilePath GetAppBundlePath(const FilePath& exec_name);
+// Set the Time Machine exclusion property for the given file.
+bool SetFileBackupExclusion(const FilePath& file_path, bool exclude);
+
} // namespace mac_util
#endif // BASE_MAC_UTIL_H_
diff --git a/base/mac_util.mm b/base/mac_util.mm
index 0eee0e2..1418453 100644
--- a/base/mac_util.mm
+++ b/base/mac_util.mm
@@ -237,4 +237,22 @@ FilePath GetAppBundlePath(const FilePath& exec_name) {
return FilePath();
}
+bool SetFileBackupExclusion(const FilePath& file_path, bool exclude) {
+ NSString* filePath =
+ [NSString stringWithUTF8String:file_path.value().c_str()];
+ NSURL* url = [NSURL fileURLWithPath:filePath];
+ // Note that we always set CSBackupSetItemExcluded's excludeByPath param
+ // to true. This prevents a problem with toggling the setting: if the file
+ // is excluded with excludeByPath set to true then excludeByPath must
+ // also be true when un-excluding the file, otherwise the un-excluding
+ // will be ignored.
+ bool success =
+ CSBackupSetItemExcluded((CFURLRef)url, exclude, true) == noErr;
+ if (!success)
+ LOG(WARNING) << "Failed to set backup excluson for file '"
+ << file_path.value().c_str() << "'. Continuing.";
+ return success;
+}
+
+
} // namespace mac_util
diff --git a/base/mac_util_unittest.mm b/base/mac_util_unittest.mm
index 0abdee7..5e97356 100644
--- a/base/mac_util_unittest.mm
+++ b/base/mac_util_unittest.mm
@@ -8,6 +8,7 @@
#include "base/mac_util.h"
#include "base/file_path.h"
+#include "base/file_util.h"
#include "base/scoped_nsobject.h"
#include "base/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -101,3 +102,26 @@ TEST_F(MacUtilTest, TestGetAppBundlePath) {
out.value().c_str()) << "loop: " << i;
}
}
+
+TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
+ NSString* homeDirectory = NSHomeDirectory();
+ NSString* dummyFilePath =
+ [homeDirectory stringByAppendingPathComponent:@"DummyFile"];
+ const char* dummy_file_path = [dummyFilePath fileSystemRepresentation];
+ ASSERT_TRUE(dummy_file_path);
+ FilePath file_path(dummy_file_path);
+ // It is not actually necessary to have a physical file in order to
+ // set its exclusion property.
+ NSURL* fileURL = [NSURL URLWithString:dummyFilePath];
+ // Reset the exclusion in case it was set previously.
+ mac_util::SetFileBackupExclusion(file_path, false);
+ Boolean excludeByPath;
+ // Initial state should be non-excluded.
+ EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
+ // Exclude the file.
+ EXPECT_TRUE(mac_util::SetFileBackupExclusion(file_path, true));
+ EXPECT_TRUE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
+ // Un-exclude the file.
+ EXPECT_TRUE(mac_util::SetFileBackupExclusion(file_path, false));
+ EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
+}