Non-path Time Machine Exclusions

Enable setting of file exclusion for non-admin/non-root users. We no longer support setting exclusion-by-path.
Enhanced error reporting in SetFileBackupExclusion.
Change exclusion calls for History and Thumbnail databases.
Made MacUtilTest.TestExcludeFileFromBackups more robust.

BUG=74053
TEST=Updated base_unittests's MacUtilTest.TestExcludeFileFromBackups.
Review URL: http://codereview.chromium.org/7069021

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86778 0039d316-1c4b-4281-b951-d872f2087c98
parent b5fe7445
...@@ -77,8 +77,8 @@ bool ShouldWindowsMiniaturizeOnDoubleClick(); ...@@ -77,8 +77,8 @@ bool ShouldWindowsMiniaturizeOnDoubleClick();
// Activates the process with the given PID. // Activates the process with the given PID.
void ActivateProcess(pid_t pid); void ActivateProcess(pid_t pid);
// Set the Time Machine exclusion property for the given file. // Excludes the file given by |file_path| from being backed up by Time Machine.
bool SetFileBackupExclusion(const FilePath& file_path, bool exclude); bool SetFileBackupExclusion(const FilePath& file_path);
// Sets the process name as displayed in Activity Monitor to process_name. // Sets the process name as displayed in Activity Monitor to process_name.
void SetProcessName(CFStringRef process_name); void SetProcessName(CFStringRef process_name);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/file_path.h" #include "base/file_path.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_nsobject.h" #include "base/memory/scoped_nsobject.h"
#include "base/sys_string_conversions.h" #include "base/sys_string_conversions.h"
...@@ -238,47 +239,29 @@ void ActivateProcess(pid_t pid) { ...@@ -238,47 +239,29 @@ void ActivateProcess(pid_t pid) {
} }
} }
bool SetFileBackupExclusion(const FilePath& file_path, bool exclude) { bool SetFileBackupExclusion(const FilePath& file_path) {
NSString* filePath = NSString* filePath =
[NSString stringWithUTF8String:file_path.value().c_str()]; [NSString stringWithUTF8String:file_path.value().c_str()];
// If being asked to exclude something in a tmp directory, just lie and say it
// was done. TimeMachine will already ignore tmp directories. This keeps the
// temporary profiles used by unittests from being added to the exclude list.
// Otherwise, as /Library/Preferences/com.apple.TimeMachine.plist grows the
// bots slow down due to reading/writing all the temporary profiles used over
// time.
NSString* tmpDir = NSTemporaryDirectory();
// Make sure the temp dir is terminated with a slash
if (tmpDir && ![tmpDir hasSuffix:@"/"])
tmpDir = [tmpDir stringByAppendingString:@"/"];
// '/var' is a link to '/private/var', make sure to check both forms.
NSString* privateTmpDir = nil;
if ([tmpDir hasPrefix:@"/var/"])
privateTmpDir = [@"/private" stringByAppendingString:tmpDir];
if ((tmpDir && [filePath hasPrefix:tmpDir]) ||
(privateTmpDir && [filePath hasPrefix:privateTmpDir]) ||
[filePath hasPrefix:@"/tmp/"] ||
[filePath hasPrefix:@"/var/tmp/"] ||
[filePath hasPrefix:@"/private/tmp/"] ||
[filePath hasPrefix:@"/private/var/tmp/"]) {
return true;
}
NSURL* url = [NSURL fileURLWithPath:filePath]; NSURL* url = [NSURL fileURLWithPath:filePath];
// Note that we always set CSBackupSetItemExcluded's excludeByPath param // Do a pre-emptive unexclude by-path since by-path exclusions may have been
// to true. This prevents a problem with toggling the setting: if the file // performed on this file in the past.
// is excluded with excludeByPath set to true then excludeByPath must CSBackupSetItemExcluded(base::mac::NSToCFCast(url), FALSE, TRUE);
// also be true when un-excluding the file, otherwise the un-excluding // When excludeByPath is true the application must be running with root
// will be ignored. // privileges (admin for 10.6 and earlier) but the URL does not have to
bool success = // already exist. When excludeByPath is false the URL must already exist but
CSBackupSetItemExcluded((CFURLRef)url, exclude, true) == noErr; // can be used in non-root (or admin as above) mode. We use false so that
if (!success) // non-root (or admin) users don't get their TimeMachine drive filled up with
// unnecessary backups.
OSStatus os_err =
CSBackupSetItemExcluded(base::mac::NSToCFCast(url), TRUE, FALSE);
if (os_err != noErr) {
LOG(WARNING) << "Failed to set backup exclusion for file '" LOG(WARNING) << "Failed to set backup exclusion for file '"
<< file_path.value().c_str() << "'. Continuing."; << file_path.value().c_str() << "' with error "
return success; << os_err << " (" << GetMacOSStatusErrorString(os_err)
<< ": " << GetMacOSStatusCommentString(os_err)
<< "). Continuing.";
}
return os_err == noErr;
} }
void SetProcessName(CFStringRef process_name) { void SetProcessName(CFStringRef process_name) {
......
...@@ -8,8 +8,10 @@ ...@@ -8,8 +8,10 @@
#include "base/file_path.h" #include "base/file_path.h"
#include "base/file_util.h" #include "base/file_util.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_nsobject.h" #include "base/memory/scoped_nsobject.h"
#include "base/scoped_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -95,26 +97,27 @@ TEST_F(MacUtilTest, TestGetAppBundlePath) { ...@@ -95,26 +97,27 @@ TEST_F(MacUtilTest, TestGetAppBundlePath) {
} }
TEST_F(MacUtilTest, TestExcludeFileFromBackups) { TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
NSString* homeDirectory = NSHomeDirectory(); // The file must already exist in order to set its exclusion property.
NSString* dummyFilePath = ScopedTempDir temp_dir_;
[homeDirectory stringByAppendingPathComponent:@"DummyFile"]; ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
const char* dummy_file_path = [dummyFilePath fileSystemRepresentation]; FilePath dummy_file_path = temp_dir_.path().Append("DummyFile");
ASSERT_TRUE(dummy_file_path); const char dummy_data[] = "All your base are belong to us!";
FilePath file_path(dummy_file_path); // Dump something real into the file.
// It is not actually necessary to have a physical file in order to ASSERT_EQ(static_cast<int>(arraysize(dummy_data)),
// set its exclusion property. file_util::WriteFile(dummy_file_path, dummy_data, arraysize(dummy_data)));
NSURL* fileURL = [NSURL URLWithString:dummyFilePath]; NSString* fileURLString =
// Reset the exclusion in case it was set previously. [NSString stringWithUTF8String:dummy_file_path.value().c_str()];
SetFileBackupExclusion(file_path, false); NSURL* fileURL = [NSURL URLWithString:fileURLString];
Boolean excludeByPath;
// Initial state should be non-excluded. // Initial state should be non-excluded.
EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); EXPECT_FALSE(CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), NULL));
// Exclude the file. // Exclude the file.
EXPECT_TRUE(SetFileBackupExclusion(file_path, true)); EXPECT_TRUE(SetFileBackupExclusion(dummy_file_path));
EXPECT_TRUE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); // SetFileBackupExclusion never excludes by path.
// Un-exclude the file. Boolean excluded_by_path = FALSE;
EXPECT_TRUE(SetFileBackupExclusion(file_path, false)); Boolean excluded =
EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath)); CSBackupIsItemExcluded(base::mac::NSToCFCast(fileURL), &excluded_by_path);
EXPECT_TRUE(excluded);
EXPECT_FALSE(excluded_by_path);
} }
TEST_F(MacUtilTest, TestGetValueFromDictionary) { TEST_F(MacUtilTest, TestGetValueFromDictionary) {
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -97,12 +97,8 @@ sql::InitStatus HistoryDatabase::Init(const FilePath& history_name, ...@@ -97,12 +97,8 @@ sql::InitStatus HistoryDatabase::Init(const FilePath& history_name,
return sql::INIT_FAILURE; return sql::INIT_FAILURE;
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// Exclude the history file and its journal from backups. // Exclude the history file from backups.
base::mac::SetFileBackupExclusion(history_name, true); base::mac::SetFileBackupExclusion(history_name);
FilePath::StringType history_name_string(history_name.value());
history_name_string += "-journal";
FilePath history_journal_name(history_name_string);
base::mac::SetFileBackupExclusion(history_journal_name, true);
#endif #endif
// Prime the cache. // Prime the cache.
......
...@@ -66,12 +66,8 @@ sql::InitStatus ThumbnailDatabase::Init( ...@@ -66,12 +66,8 @@ sql::InitStatus ThumbnailDatabase::Init(
transaction.Begin(); transaction.Begin();
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// Exclude the thumbnails file and its journal from backups. // Exclude the thumbnails file from backups.
base::mac::SetFileBackupExclusion(db_name, true); base::mac::SetFileBackupExclusion(db_name);
FilePath::StringType db_name_string(db_name.value());
db_name_string += "-journal";
FilePath db_journal_name(db_name_string);
base::mac::SetFileBackupExclusion(db_journal_name, true);
#endif #endif
// Create the tables. // Create the tables.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment