#include "base/files/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/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/stl_util.h"
#include "base/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_file_util.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace files {
namespace {
class TestDelegate;
class NotificationCollector
: public base::RefCountedThreadSafe<NotificationCollector> {
public:
NotificationCollector()
: loop_(base::MessageLoopProxy::current()) {}
void OnChange(TestDelegate* delegate) {
loop_->PostTask(FROM_HERE,
base::Bind(&NotificationCollector::RecordChange, this,
make_scoped_refptr(delegate)));
}
void Register(TestDelegate* delegate) {
delegates_.insert(delegate);
}
void Reset() {
signaled_.clear();
}
bool Success() {
return signaled_ == delegates_;
}
private:
friend class base::RefCountedThreadSafe<NotificationCollector>;
~NotificationCollector() {}
void RecordChange(TestDelegate* delegate) {
ASSERT_TRUE(loop_->BelongsToCurrentThread());
ASSERT_TRUE(delegates_.count(delegate));
signaled_.insert(delegate);
if (signaled_ == delegates_)
loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
}
std::set<TestDelegate*> delegates_;
std::set<TestDelegate*> signaled_;
scoped_refptr<base::MessageLoopProxy> loop_;
};
class TestDelegate : public FilePathWatcher::Delegate {
public:
explicit TestDelegate(NotificationCollector* collector)
: collector_(collector) {
collector_->Register(this);
}
virtual void OnFilePathChanged(const FilePath&) {
collector_->OnChange(this);
}
virtual void OnFilePathError(const FilePath& path) {
ADD_FAILURE() << "Error " << path.value();
}
protected:
virtual ~TestDelegate() {}
private:
scoped_refptr<NotificationCollector> collector_;
DISALLOW_COPY_AND_ASSIGN(TestDelegate);
};
void SetupWatchDelegate(const FilePath& target,
FilePathWatcher* watcher,
FilePathWatcher::Delegate* delegate,
bool* result,
base::WaitableEvent* completion) {
*result = watcher->Watch(target, delegate);
completion->Signal();
}
void SetupWatchCallback(const FilePath& target,
FilePathWatcher* watcher,
const FilePathWatcher::Callback& callback) {
ASSERT_TRUE(watcher->Watch(target, callback));
}
void QuitLoopWatchCallback(MessageLoop* loop,
const FilePath& expected_path,
bool expected_error,
bool* flag,
const FilePath& path,
bool error) {
ASSERT_TRUE(flag);
*flag = true;
EXPECT_EQ(expected_path, path);
EXPECT_EQ(expected_error, error);
loop->PostTask(FROM_HERE, loop->QuitClosure());
}
class FilePathWatcherTest : public testing::Test {
public:
FilePathWatcherTest()
: file_thread_("FilePathWatcherTest") {}
virtual ~FilePathWatcherTest() {}
protected:
virtual void SetUp() {
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();
}
FilePath test_file() {
return temp_dir_.path().AppendASCII("FilePathWatcherTest");
}
FilePath test_link() {
return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk");
}
bool WriteFile(const FilePath& file, const std::string& content) {
int write_size = file_util::WriteFile(file, content.c_str(),
content.length());
return write_size == static_cast<int>(content.length());
}
bool SetupWatch(const FilePath& target,
FilePathWatcher* watcher,
FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT {
base::WaitableEvent completion(false, false);
bool result;
file_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(SetupWatchDelegate, target, watcher,
make_scoped_refptr(delegate), &result, &completion));
completion.Wait();
return result;
}
bool WaitForEvents() WARN_UNUSED_RESULT {
collector_->Reset();
loop_.Run();
return collector_->Success();
}
NotificationCollector* collector() { return collector_.get(); }
MessageLoop loop_;
base::Thread file_thread_;
ScopedTempDir temp_dir_;
scoped_refptr<NotificationCollector> collector_;
};
TEST_F(FilePathWatcherTest, NewFile) {
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, ModifiedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
ASSERT_TRUE(WriteFile(test_file(), "new content"));
ASSERT_TRUE(WaitForEvents());
}
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()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
ASSERT_TRUE(file_util::Move(source_file, test_file()));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, DeletedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
file_util::Delete(test_file(), false);
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, Callback) {
FilePathWatcher* watcher = new FilePathWatcher();
bool called_back = false;
MessageLoop* file_loop = file_thread_.message_loop();
ASSERT_TRUE(file_loop);
FilePathWatcher::Callback callback = base::Bind(
QuitLoopWatchCallback, &loop_, test_file(), false, &called_back);
file_thread_.message_loop_proxy()->PostTaskAndReply(
FROM_HERE,
base::Bind(SetupWatchCallback, test_file(), watcher, callback),
base::Bind(&MessageLoop::Quit, base::Unretained(&loop_)));
loop_.Run();
ASSERT_TRUE(WriteFile(test_file(), "content"));
loop_.Run();
EXPECT_TRUE(called_back);
file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
file_thread_.message_loop_proxy()->PostTaskAndReply(
FROM_HERE,
base::Bind(base::DoNothing),
base::Bind(&MessageLoop::Quit, base::Unretained(&loop_)));
loop_.Run();
}
class Deleter : public FilePathWatcher::Delegate {
public:
Deleter(FilePathWatcher* watcher, MessageLoop* loop)
: watcher_(watcher),
loop_(loop) {
}
virtual void OnFilePathChanged(const FilePath& path) {
watcher_.reset();
loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
}
scoped_ptr<FilePathWatcher> watcher_;
MessageLoop* loop_;
private:
virtual ~Deleter() {}
};
TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
FilePathWatcher* watcher = new FilePathWatcher;
scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_));
ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get()));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(deleter->watcher_.get() == NULL);
}
#if defined(OS_MACOSX)
#define MAYBE_DestroyWithPendingNotification DISABLED_DestroyWithPendingNotification
#else
#define MAYBE_DestroyWithPendingNotification DestroyWithPendingNotification
#endif
TEST_F(FilePathWatcherTest, MAYBE_DestroyWithPendingNotification) {
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
FilePathWatcher* watcher = new FilePathWatcher;
ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get()));
ASSERT_TRUE(WriteFile(test_file(), "content"));
file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
}
TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) {
FilePathWatcher watcher1, watcher2;
scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector()));
scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get()));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, NonExistentDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath file(dir.AppendASCII("file"));
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
ASSERT_TRUE(file_util::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(file_util::Delete(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, DirectoryChain) {
FilePath path(temp_dir_.path());
std::vector<std::string> dir_names;
for (int i = 0; i < 20; i++) {
std::string dir(base::StringPrintf("d%d", i));
dir_names.push_back(dir);
path = path.AppendASCII(dir);
}
FilePathWatcher watcher;
FilePath file(path.AppendASCII("file"));
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
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";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file modification";
ASSERT_TRUE(WaitForEvents());
}
#if defined(OS_MACOSX)
#define DisappearingDirectory DISABLED_DisappearingDirectory
#endif
TEST_F(FilePathWatcherTest, DisappearingDirectory) {
FilePathWatcher watcher;
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()));
ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
ASSERT_TRUE(file_util::Delete(dir, true));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
ASSERT_TRUE(file_util::Delete(test_file(), false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(test_file(), "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, WatchDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath file1(dir.AppendASCII("file1"));
FilePath file2(dir.AppendASCII("file2"));
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get()));
ASSERT_TRUE(file_util::CreateDirectory(dir));
VLOG(1) << "Waiting for directory creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file1, "content"));
VLOG(1) << "Waiting for file1 creation";
ASSERT_TRUE(WaitForEvents());
#if !defined(OS_MACOSX)
ASSERT_TRUE(WriteFile(file1, "content v2"));
VLOG(1) << "Waiting for file1 modification";
ASSERT_TRUE(WaitForEvents());
#endif
ASSERT_TRUE(file_util::Delete(file1, false));
VLOG(1) << "Waiting for file1 deletion";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file2, "content"));
VLOG(1) << "Waiting for file2 creation";
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 subdir(dir.AppendASCII("subdir"));
FilePath file(subdir.AppendASCII("file"));
scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get()));
scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()));
ASSERT_TRUE(file_util::CreateDirectory(subdir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
file_util::Move(dir, dest);
VLOG(1) << "Waiting for directory move";
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, MoveChild) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
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_subdir(dest_dir.AppendASCII("subdir"));
FilePath dest_file(dest_subdir.AppendASCII("file"));
ASSERT_TRUE(file_util::CreateDirectory(source_subdir));
ASSERT_TRUE(WriteFile(source_file, "content"));
scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get()));
scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()));
ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
ASSERT_TRUE(WaitForEvents());
}
#if !defined(OS_LINUX)
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()));
ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
ASSERT_TRUE(WaitForEvents());
}
#endif
#if defined(OS_LINUX)
TEST_F(FilePathWatcherTest, CreateLink) {
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, DeleteLink) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
ASSERT_TRUE(file_util::Delete(test_link(), false));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, ModifiedLinkedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
ASSERT_TRUE(WriteFile(test_file(), "new content"));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) {
ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
ASSERT_TRUE(file_util::Delete(test_file(), false));
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(file_util::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get()));
ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
VLOG(1) << "Waiting for link creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(file_util::Delete(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get()));
ASSERT_TRUE(file_util::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for dir/file creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(file_util::Delete(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
}
TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
FilePath file(dir.AppendASCII("file"));
FilePath linkfile(link_dir.AppendASCII("file"));
scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
ASSERT_TRUE(file_util::CreateDirectory(dir));
ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get()));
ASSERT_TRUE(WriteFile(file, "content"));
VLOG(1) << "Waiting for file creation";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(WriteFile(file, "content v2"));
VLOG(1) << "Waiting for file change";
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(file_util::Delete(file, false));
VLOG(1) << "Waiting for file deletion";
ASSERT_TRUE(WaitForEvents());
}
#endif
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;
}
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)
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"));
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()));
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
loop_.PostDelayedTask(FROM_HERE,
MessageLoop::QuitClosure(),
TestTimeouts::tiny_timeout());
ASSERT_FALSE(WaitForEvents());
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
ASSERT_TRUE(WaitForEvents());
ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
}
#endif
}
}
}