| Index: content/common/file_path_watcher/file_path_watcher_browsertest.cc
|
| diff --git a/chrome/browser/file_path_watcher/file_path_watcher_browsertest.cc b/content/common/file_path_watcher/file_path_watcher_browsertest.cc
|
| similarity index 56%
|
| rename from chrome/browser/file_path_watcher/file_path_watcher_browsertest.cc
|
| rename to content/common/file_path_watcher/file_path_watcher_browsertest.cc
|
| index b733a9a2afec97e68a4b61fced868690244f8f15..461d8fbd50564fccc2b59321877a3f6878cac54b 100644
|
| --- a/chrome/browser/file_path_watcher/file_path_watcher_browsertest.cc
|
| +++ b/content/common/file_path_watcher/file_path_watcher_browsertest.cc
|
| @@ -1,12 +1,20 @@
|
| -// 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
|
| // found in the LICENSE file.
|
|
|
| -#include "chrome/browser/file_path_watcher/file_path_watcher.h"
|
| +#include "content/common/file_path_watcher/file_path_watcher.h"
|
|
|
| #include <set>
|
|
|
| +#if defined(OS_WIN)
|
| +#include <windows.h>
|
| +#include <aclapi.h>
|
| +#elif defined(OS_POSIX)
|
| +#include <sys/stat.h>
|
| +#endif
|
| +
|
| #include "base/basictypes.h"
|
| +#include "base/compiler_specific.h"
|
| #include "base/file_path.h"
|
| #include "base/file_util.h"
|
| #include "base/message_loop.h"
|
| @@ -16,16 +24,10 @@
|
| #include "base/string_util.h"
|
| #include "base/stl_util-inl.h"
|
| #include "base/synchronization/waitable_event.h"
|
| +#include "base/test/test_timeouts.h"
|
| +#include "base/threading/thread.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| -#if defined(OS_MACOSX)
|
| -// TODO(mnissler): There are flakes on Mac (http://crbug.com/54822) at least for
|
| -// FilePathWatcherTest.MultipleWatchersSingleFile.
|
| -#define MAYBE(name) FLAKY_ ## name
|
| -#else
|
| -#define MAYBE(name) name
|
| -#endif
|
| -
|
| namespace {
|
|
|
| class TestDelegate;
|
| @@ -54,6 +56,10 @@ class NotificationCollector
|
| signaled_.clear();
|
| }
|
|
|
| + bool Success() {
|
| + return signaled_ == delegates_;
|
| + }
|
| +
|
| private:
|
| void RecordChange(TestDelegate* delegate) {
|
| ASSERT_TRUE(loop_->BelongsToCurrentThread());
|
| @@ -93,6 +99,10 @@ class TestDelegate : public FilePathWatcher::Delegate {
|
| collector_->OnChange(this);
|
| }
|
|
|
| + virtual void OnFilePathError(const FilePath& path) {
|
| + ADD_FAILURE() << "Error " << path.value();
|
| + }
|
| +
|
| private:
|
| scoped_refptr<NotificationCollector> collector_;
|
|
|
| @@ -130,29 +140,26 @@ class SetupWatchTask : public Task {
|
|
|
| class FilePathWatcherTest : public testing::Test {
|
| public:
|
| - // Implementation of FilePathWatcher on Mac requires UI loop.
|
| FilePathWatcherTest()
|
| - : loop_(MessageLoop::TYPE_UI),
|
| - ui_thread_(BrowserThread::UI, &loop_) {
|
| - }
|
| + : file_thread_("FilePathWatcherTest") {}
|
| +
|
| + virtual ~FilePathWatcherTest() {}
|
|
|
| protected:
|
| virtual void SetUp() {
|
| // Create a separate file thread in order to test proper thread usage.
|
| - file_thread_.reset(new BrowserThread(BrowserThread::FILE));
|
| - file_thread_->Start();
|
| - temp_dir_.reset(new ScopedTempDir);
|
| - ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
|
| + base::Thread::Options options(MessageLoop::TYPE_IO, 0);
|
| + ASSERT_TRUE(file_thread_.StartWithOptions(options));
|
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
| collector_ = new NotificationCollector();
|
| }
|
|
|
| virtual void TearDown() {
|
| loop_.RunAllPending();
|
| - file_thread_.reset();
|
| }
|
|
|
| FilePath test_file() {
|
| - return temp_dir_->path().AppendASCII("FilePathWatcherTest");
|
| + return temp_dir_.path().AppendASCII("FilePathWatcherTest");
|
| }
|
|
|
| // Write |content| to |file|. Returns true on success.
|
| @@ -162,78 +169,82 @@ class FilePathWatcherTest : public testing::Test {
|
| return write_size == static_cast<int>(content.length());
|
| }
|
|
|
| - void SetupWatch(const FilePath& target,
|
| + bool SetupWatch(const FilePath& target,
|
| FilePathWatcher* watcher,
|
| - FilePathWatcher::Delegate* delegate) {
|
| + FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT {
|
| base::WaitableEvent completion(false, false);
|
| bool result;
|
| - BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
|
| - new SetupWatchTask(target, watcher, delegate, &result, &completion));
|
| + file_thread_.message_loop_proxy()->PostTask(FROM_HERE,
|
| + new SetupWatchTask(target,
|
| + watcher,
|
| + delegate,
|
| + &result,
|
| + &completion));
|
| completion.Wait();
|
| - ASSERT_TRUE(result);
|
| + return result;
|
| }
|
|
|
| - void WaitForEvents() {
|
| + bool WaitForEvents() WARN_UNUSED_RESULT {
|
| collector_->Reset();
|
| loop_.Run();
|
| + return collector_->Success();
|
| }
|
|
|
| NotificationCollector* collector() { return collector_.get(); }
|
|
|
| MessageLoop loop_;
|
| - BrowserThread ui_thread_;
|
| - scoped_ptr<BrowserThread> file_thread_;
|
| - scoped_ptr<ScopedTempDir> temp_dir_;
|
| + base::Thread file_thread_;
|
| + ScopedTempDir temp_dir_;
|
| scoped_refptr<NotificationCollector> collector_;
|
| };
|
|
|
| // Basic test: Create the file and verify that we notice.
|
| -TEST_F(FilePathWatcherTest, MAYBE(NewFile)) {
|
| +TEST_F(FilePathWatcherTest, NewFile) {
|
| FilePathWatcher watcher;
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(test_file(), &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
|
|
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| // Verify that modifying the file is caught.
|
| -TEST_F(FilePathWatcherTest, MAYBE(ModifiedFile)) {
|
| +TEST_F(FilePathWatcherTest, ModifiedFile) {
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
|
|
| FilePathWatcher watcher;
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(test_file(), &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
|
|
|
| // Now make sure we get notified if the file is modified.
|
| ASSERT_TRUE(WriteFile(test_file(), "new content"));
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| // Verify that moving the file into place is caught.
|
| -TEST_F(FilePathWatcherTest, MAYBE(MovedFile)) {
|
| - FilePath source_file(temp_dir_->path().AppendASCII("source"));
|
| +TEST_F(FilePathWatcherTest, MovedFile) {
|
| + FilePath source_file(temp_dir_.path().AppendASCII("source"));
|
| ASSERT_TRUE(WriteFile(source_file, "content"));
|
|
|
| FilePathWatcher watcher;
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(test_file(), &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
|
|
|
| // Now make sure we get notified if the file is modified.
|
| ASSERT_TRUE(file_util::Move(source_file, test_file()));
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| -TEST_F(FilePathWatcherTest, MAYBE(DeletedFile)) {
|
| +TEST_F(FilePathWatcherTest, DeletedFile) {
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
|
|
| FilePathWatcher watcher;
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(test_file(), &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
|
|
|
| // Now make sure we get notified if the file is deleted.
|
| file_util::Delete(test_file(), false);
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| // Used by the DeleteDuringNotify test below.
|
| @@ -246,7 +257,7 @@ class Deleter : public FilePathWatcher::Delegate {
|
| }
|
|
|
| virtual void OnFilePathChanged(const FilePath& path) {
|
| - watcher_.reset(NULL);
|
| + watcher_.reset();
|
| loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
|
| }
|
|
|
| @@ -259,10 +270,10 @@ TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
|
| FilePathWatcher* watcher = new FilePathWatcher;
|
| // Takes ownership of watcher.
|
| scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_));
|
| - SetupWatch(test_file(), watcher, deleter.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get()));
|
|
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| // We win if we haven't crashed yet.
|
| // Might as well double-check it got deleted, too.
|
| @@ -274,50 +285,51 @@ TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
|
| TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) {
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| FilePathWatcher* watcher = new FilePathWatcher;
|
| - SetupWatch(test_file(), watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get()));
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
| - BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, watcher);
|
| + file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
|
| }
|
|
|
| -TEST_F(FilePathWatcherTest, MAYBE(MultipleWatchersSingleFile)) {
|
| +TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) {
|
| FilePathWatcher watcher1, watcher2;
|
| scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector()));
|
| scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector()));
|
| - SetupWatch(test_file(), &watcher1, delegate1.get());
|
| - SetupWatch(test_file(), &watcher2, delegate2.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get()));
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get()));
|
|
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| // Verify that watching a file whose parent directory doesn't exist yet works if
|
| // the directory and file are created eventually.
|
| TEST_F(FilePathWatcherTest, NonExistentDirectory) {
|
| FilePathWatcher watcher;
|
| - FilePath dir(temp_dir_->path().AppendASCII("dir"));
|
| + FilePath dir(temp_dir_.path().AppendASCII("dir"));
|
| FilePath file(dir.AppendASCII("file"));
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(file, &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
|
|
|
| ASSERT_TRUE(file_util::CreateDirectory(dir));
|
|
|
| ASSERT_TRUE(WriteFile(file, "content"));
|
| +
|
| VLOG(1) << "Waiting for file creation";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| ASSERT_TRUE(WriteFile(file, "content v2"));
|
| VLOG(1) << "Waiting for file change";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| ASSERT_TRUE(file_util::Delete(file, false));
|
| VLOG(1) << "Waiting for file deletion";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| // Exercises watch reconfiguration for the case that directories on the path
|
| // are rapidly created.
|
| TEST_F(FilePathWatcherTest, DirectoryChain) {
|
| - FilePath path(temp_dir_->path());
|
| + FilePath path(temp_dir_.path());
|
| std::vector<std::string> dir_names;
|
| for (int i = 0; i < 20; i++) {
|
| std::string dir(StringPrintf("d%d", i));
|
| @@ -328,34 +340,35 @@ TEST_F(FilePathWatcherTest, DirectoryChain) {
|
| FilePathWatcher watcher;
|
| FilePath file(path.AppendASCII("file"));
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(file, &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
|
|
|
| - FilePath sub_path(temp_dir_->path());
|
| + FilePath sub_path(temp_dir_.path());
|
| for (std::vector<std::string>::const_iterator d(dir_names.begin());
|
| d != dir_names.end(); ++d) {
|
| sub_path = sub_path.AppendASCII(*d);
|
| ASSERT_TRUE(file_util::CreateDirectory(sub_path));
|
| }
|
| + VLOG(1) << "Create File";
|
| ASSERT_TRUE(WriteFile(file, "content"));
|
| VLOG(1) << "Waiting for file creation";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| ASSERT_TRUE(WriteFile(file, "content v2"));
|
| VLOG(1) << "Waiting for file modification";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| TEST_F(FilePathWatcherTest, DisappearingDirectory) {
|
| FilePathWatcher watcher;
|
| - FilePath dir(temp_dir_->path().AppendASCII("dir"));
|
| + FilePath dir(temp_dir_.path().AppendASCII("dir"));
|
| FilePath file(dir.AppendASCII("file"));
|
| ASSERT_TRUE(file_util::CreateDirectory(dir));
|
| ASSERT_TRUE(WriteFile(file, "content"));
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(file, &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
|
|
|
| ASSERT_TRUE(file_util::Delete(dir, true));
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| // Tests that a file that is deleted and reappears is tracked correctly.
|
| @@ -363,77 +376,80 @@ TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
| FilePathWatcher watcher;
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(test_file(), &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
|
|
|
| ASSERT_TRUE(file_util::Delete(test_file(), false));
|
| VLOG(1) << "Waiting for file deletion";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| ASSERT_TRUE(WriteFile(test_file(), "content"));
|
| VLOG(1) << "Waiting for file creation";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| TEST_F(FilePathWatcherTest, WatchDirectory) {
|
| FilePathWatcher watcher;
|
| - FilePath dir(temp_dir_->path().AppendASCII("dir"));
|
| + FilePath dir(temp_dir_.path().AppendASCII("dir"));
|
| FilePath file1(dir.AppendASCII("file1"));
|
| FilePath file2(dir.AppendASCII("file2"));
|
| scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| - SetupWatch(dir, &watcher, delegate.get());
|
| + ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get()));
|
|
|
| ASSERT_TRUE(file_util::CreateDirectory(dir));
|
| VLOG(1) << "Waiting for directory creation";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| ASSERT_TRUE(WriteFile(file1, "content"));
|
| VLOG(1) << "Waiting for file1 creation";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| +#if !defined(OS_MACOSX)
|
| + // Mac implementation does not detect files modified in a directory.
|
| ASSERT_TRUE(WriteFile(file1, "content v2"));
|
| VLOG(1) << "Waiting for file1 modification";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| +#endif // !OS_MACOSX
|
|
|
| ASSERT_TRUE(file_util::Delete(file1, false));
|
| VLOG(1) << "Waiting for file1 deletion";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| ASSERT_TRUE(WriteFile(file2, "content"));
|
| VLOG(1) << "Waiting for file2 creation";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| TEST_F(FilePathWatcherTest, MoveParent) {
|
| FilePathWatcher file_watcher;
|
| FilePathWatcher subdir_watcher;
|
| - FilePath dir(temp_dir_->path().AppendASCII("dir"));
|
| - FilePath dest(temp_dir_->path().AppendASCII("dest"));
|
| + FilePath dir(temp_dir_.path().AppendASCII("dir"));
|
| + FilePath dest(temp_dir_.path().AppendASCII("dest"));
|
| FilePath subdir(dir.AppendASCII("subdir"));
|
| FilePath file(subdir.AppendASCII("file"));
|
| scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
|
| - SetupWatch(file, &file_watcher, file_delegate.get());
|
| + ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get()));
|
| scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
|
| - SetupWatch(subdir, &subdir_watcher, subdir_delegate.get());
|
| + ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()));
|
|
|
| // Setup a directory hierarchy.
|
| ASSERT_TRUE(file_util::CreateDirectory(subdir));
|
| ASSERT_TRUE(WriteFile(file, "content"));
|
| VLOG(1) << "Waiting for file creation";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
|
|
| // Move the parent directory.
|
| file_util::Move(dir, dest);
|
| VLOG(1) << "Waiting for directory move";
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| }
|
|
|
| TEST_F(FilePathWatcherTest, MoveChild) {
|
| FilePathWatcher file_watcher;
|
| FilePathWatcher subdir_watcher;
|
| - FilePath source_dir(temp_dir_->path().AppendASCII("source"));
|
| + FilePath source_dir(temp_dir_.path().AppendASCII("source"));
|
| FilePath source_subdir(source_dir.AppendASCII("subdir"));
|
| FilePath source_file(source_subdir.AppendASCII("file"));
|
| - FilePath dest_dir(temp_dir_->path().AppendASCII("dest"));
|
| + FilePath dest_dir(temp_dir_.path().AppendASCII("dest"));
|
| FilePath dest_subdir(dest_dir.AppendASCII("subdir"));
|
| FilePath dest_file(dest_subdir.AppendASCII("file"));
|
|
|
| @@ -442,13 +458,159 @@ TEST_F(FilePathWatcherTest, MoveChild) {
|
| ASSERT_TRUE(WriteFile(source_file, "content"));
|
|
|
| scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
|
| - SetupWatch(dest_file, &file_watcher, file_delegate.get());
|
| + ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get()));
|
| scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
|
| - SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get());
|
| + ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()));
|
|
|
| // Move the directory into place, s.t. the watched file appears.
|
| ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
|
| - WaitForEvents();
|
| + ASSERT_TRUE(WaitForEvents());
|
| +}
|
| +
|
| +#if !defined(OS_LINUX)
|
| +// Linux implementation of FilePathWatcher doesn't catch attribute changes.
|
| +// http://crbug.com/78043
|
| +
|
| +// Verify that changing attributes on a file is caught
|
| +TEST_F(FilePathWatcherTest, FileAttributesChanged) {
|
| + ASSERT_TRUE(WriteFile(test_file(), "content"));
|
| + FilePathWatcher watcher;
|
| + scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| + ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
|
| +
|
| + // Now make sure we get notified if the file is modified.
|
| + ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
|
| + ASSERT_TRUE(WaitForEvents());
|
| +}
|
| +
|
| +#endif // !OS_LINUX
|
| +
|
| +enum Permission {
|
| + Read,
|
| + Write,
|
| + Execute
|
| +};
|
| +
|
| +bool ChangeFilePermissions(const FilePath& path, Permission perm, bool allow) {
|
| +#if defined(OS_POSIX)
|
| + struct stat stat_buf;
|
| +
|
| + if (stat(path.value().c_str(), &stat_buf) != 0)
|
| + return false;
|
| +
|
| + mode_t mode = 0;
|
| + switch (perm) {
|
| + case Read:
|
| + mode = S_IRUSR | S_IRGRP | S_IROTH;
|
| + break;
|
| + case Write:
|
| + mode = S_IWUSR | S_IWGRP | S_IWOTH;
|
| + break;
|
| + case Execute:
|
| + mode = S_IXUSR | S_IXGRP | S_IXOTH;
|
| + break;
|
| + default:
|
| + ADD_FAILURE() << "unknown perm " << perm;
|
| + return false;
|
| + }
|
| + if (allow) {
|
| + stat_buf.st_mode |= mode;
|
| + } else {
|
| + stat_buf.st_mode &= ~mode;
|
| + }
|
| + return chmod(path.value().c_str(), stat_buf.st_mode) == 0;
|
| +
|
| +#elif defined(OS_WIN)
|
| + PACL old_dacl;
|
| + PSECURITY_DESCRIPTOR security_descriptor;
|
| + if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
|
| + SE_FILE_OBJECT,
|
| + DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl,
|
| + NULL, &security_descriptor) != ERROR_SUCCESS)
|
| + return false;
|
| +
|
| + DWORD mode = 0;
|
| + switch (perm) {
|
| + case Read:
|
| + mode = GENERIC_READ;
|
| + break;
|
| + case Write:
|
| + mode = GENERIC_WRITE;
|
| + break;
|
| + case Execute:
|
| + mode = GENERIC_EXECUTE;
|
| + break;
|
| + default:
|
| + ADD_FAILURE() << "unknown perm " << perm;
|
| + return false;
|
| + }
|
| +
|
| + // Deny Read access for the current user.
|
| + EXPLICIT_ACCESS change;
|
| + change.grfAccessPermissions = mode;
|
| + change.grfAccessMode = allow ? GRANT_ACCESS : DENY_ACCESS;
|
| + change.grfInheritance = 0;
|
| + change.Trustee.pMultipleTrustee = NULL;
|
| + change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
|
| + change.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
|
| + change.Trustee.TrusteeType = TRUSTEE_IS_USER;
|
| + change.Trustee.ptstrName = L"CURRENT_USER";
|
| +
|
| + PACL new_dacl;
|
| + if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) {
|
| + LocalFree(security_descriptor);
|
| + return false;
|
| + }
|
| +
|
| + DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
|
| + SE_FILE_OBJECT, DACL_SECURITY_INFORMATION,
|
| + NULL, NULL, new_dacl, NULL);
|
| + LocalFree(security_descriptor);
|
| + LocalFree(new_dacl);
|
| +
|
| + return rc == ERROR_SUCCESS;
|
| +#else
|
| + NOTIMPLEMENTED();
|
| + return false;
|
| +#endif
|
| +}
|
| +
|
| +#if defined(OS_MACOSX)
|
| +// Linux implementation of FilePathWatcher doesn't catch attribute changes.
|
| +// http://crbug.com/78043
|
| +// Windows implementation of FilePathWatcher catches attribute changes that
|
| +// don't affect the path being watched.
|
| +// http://crbug.com/78045
|
| +
|
| +// Verify that changing attributes on a directory works.
|
| +TEST_F(FilePathWatcherTest, DirAttributesChanged) {
|
| + FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1"));
|
| + FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2"));
|
| + FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile"));
|
| + // Setup a directory hierarchy.
|
| + ASSERT_TRUE(file_util::CreateDirectory(test_dir1));
|
| + ASSERT_TRUE(file_util::CreateDirectory(test_dir2));
|
| + ASSERT_TRUE(WriteFile(test_file, "content"));
|
| +
|
| + FilePathWatcher watcher;
|
| + scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
|
| + ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get()));
|
| +
|
| + // We should not get notified in this case as it hasn't affected our ability
|
| + // to access the file.
|
| + ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
|
| + loop_.PostDelayedTask(FROM_HERE,
|
| + new MessageLoop::QuitTask,
|
| + TestTimeouts::tiny_timeout_ms());
|
| + ASSERT_FALSE(WaitForEvents());
|
| + ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
|
| +
|
| + // We should get notified in this case because filepathwatcher can no
|
| + // longer access the file
|
| + ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
|
| + ASSERT_TRUE(WaitForEvents());
|
| + ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
|
| }
|
|
|
| +#endif // OS_MACOSX
|
| } // namespace
|
|
|