Revert 274735 "Use FSEvents for recursive file watch on Mac"
Broke Mac tests:
http://build.chromium.org/p/chromium.mac/buildstatus?builder=Mac10.6%20Tests%20%281%29&number=52768
http://build.chromium.org/p/chromium.mac/buildstatus?builder=Mac10.6%20Tests%20%282%29&number=52019
http://build.chromium.org/p/chromium.mac/buildstatus?builder=Mac%2010.6%20Tests%20%28dbg%29%283%29&number=50243
http://build.chromium.org/p/chromium.mac/buildstatus?builder=Mac%2010.6%20Tests%20%28dbg%29%284%29&number=41693
> Use FSEvents for recursive file watch on Mac
>
> Brings back the old FSEvents code, changing it to support recursive watches only, and use dispatch queues instead of using the CFRunLoop from the UI thread.
>
> BUG=144491
>
> Review URL: https://codereview.chromium.org/283423003
TBR=vandebo@chromium.org
Review URL: https://codereview.chromium.org/316853003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274757 0039d316-1c4b-4281-b951-d872f2087c98
CrOS-Libchrome-Original-Commit: 1595e8383f59e8c348d97c918df3ffdacaa8d987
diff --git a/base/files/file_path_watcher.h b/base/files/file_path_watcher.h
index 229254c..3c1941f 100644
--- a/base/files/file_path_watcher.h
+++ b/base/files/file_path_watcher.h
@@ -93,7 +93,7 @@
// true, to watch |path| and its children. The callback will be invoked on
// the same loop. Returns true on success.
//
- // Recursive watch is not supported on all platforms and file systems.
+ // NOTE: Recursive watch is not supported on all platforms and file systems.
// Watch() will return false in the case of failure.
bool Watch(const FilePath& path, bool recursive, const Callback& callback);
diff --git a/base/files/file_path_watcher_browsertest.cc b/base/files/file_path_watcher_browsertest.cc
index 4ec8aba..2bdcf13 100644
--- a/base/files/file_path_watcher_browsertest.cc
+++ b/base/files/file_path_watcher_browsertest.cc
@@ -205,8 +205,9 @@
bool result;
file_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
- base::Bind(SetupWatchCallback, target, watcher, delegate, recursive_watch,
- &result, &completion));
+ base::Bind(SetupWatchCallback,
+ target, watcher, delegate, recursive_watch, &result,
+ &completion));
completion.Wait();
return result;
}
@@ -482,7 +483,7 @@
DeleteDelegateOnFileThread(subdir_delegate.release());
}
-#if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX)
+#if defined(OS_WIN) || defined(OS_LINUX)
TEST_F(FilePathWatcherTest, RecursiveWatch) {
FilePathWatcher watcher;
FilePath dir(temp_dir_.path().AppendASCII("dir"));
@@ -547,45 +548,6 @@
}
#endif
-#if defined(OS_LINUX) || defined(OS_MACOSX)
-TEST_F(FilePathWatcherTest, RecursiveWithSymLink) {
- FilePathWatcher watcher;
- FilePath test_dir(temp_dir_.path().AppendASCII("test_dir"));
- ASSERT_TRUE(base::CreateDirectory(test_dir));
- FilePath symlink(test_dir.AppendASCII("symlink"));
- scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
- ASSERT_TRUE(SetupWatch(symlink, &watcher, delegate.get(), true));
-
- // Link creation.
- FilePath target1(temp_dir_.path().AppendASCII("target1"));
- ASSERT_TRUE(base::CreateSymbolicLink(target1, symlink));
- ASSERT_TRUE(WaitForEvents());
-
- // Target1 creation.
- ASSERT_TRUE(base::CreateDirectory(target1));
- ASSERT_TRUE(WaitForEvents());
-
- // Create a file in target1.
- FilePath target1_file(target1.AppendASCII("file"));
- ASSERT_TRUE(WriteFile(target1_file, "content"));
- ASSERT_TRUE(WaitForEvents());
-
- // Link change.
- FilePath target2(temp_dir_.path().AppendASCII("target2"));
- ASSERT_TRUE(base::CreateDirectory(target2));
- ASSERT_TRUE(base::DeleteFile(symlink, false));
- ASSERT_TRUE(base::CreateSymbolicLink(target2, symlink));
- ASSERT_TRUE(WaitForEvents());
-
- // Create a file in target2.
- FilePath target2_file(target2.AppendASCII("file"));
- ASSERT_TRUE(WriteFile(target2_file, "content"));
- ASSERT_TRUE(WaitForEvents());
-
- DeleteDelegateOnFileThread(delegate.release());
-}
-#endif
-
TEST_F(FilePathWatcherTest, MoveChild) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
diff --git a/base/files/file_path_watcher_fsevents.cc b/base/files/file_path_watcher_fsevents.cc
deleted file mode 100644
index d7fa657..0000000
--- a/base/files/file_path_watcher_fsevents.cc
+++ /dev/null
@@ -1,263 +0,0 @@
-// Copyright 2014 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 "base/files/file_path_watcher_fsevents.h"
-
-#include <list>
-
-#include "base/bind.h"
-#include "base/file_util.h"
-#include "base/lazy_instance.h"
-#include "base/logging.h"
-#include "base/mac/libdispatch_task_runner.h"
-#include "base/mac/scoped_cftyperef.h"
-#include "base/message_loop/message_loop.h"
-
-namespace base {
-
-namespace {
-
-// The latency parameter passed to FSEventsStreamCreate().
-const CFAbsoluteTime kEventLatencySeconds = 0.3;
-
-class FSEventsTaskRunner : public mac::LibDispatchTaskRunner {
- public:
- FSEventsTaskRunner()
- : mac::LibDispatchTaskRunner("chromium.org.FilePathWatcherFSEvents") {
- }
-
- protected:
- virtual ~FSEventsTaskRunner() {}
-};
-
-static LazyInstance<FSEventsTaskRunner>::Leaky g_task_runner =
- LAZY_INSTANCE_INITIALIZER;
-
-// Resolve any symlinks in the path.
-FilePath ResolvePath(const FilePath& path) {
- const unsigned kMaxLinksToResolve = 255;
-
- std::vector<FilePath::StringType> component_vector;
- path.GetComponents(&component_vector);
- std::list<FilePath::StringType>
- components(component_vector.begin(), component_vector.end());
-
- FilePath result;
- unsigned resolve_count = 0;
- while (resolve_count < kMaxLinksToResolve && !components.empty()) {
- FilePath component(*components.begin());
- components.pop_front();
-
- FilePath current;
- if (component.IsAbsolute()) {
- current = component;
- } else {
- current = result.Append(component);
- }
-
- FilePath target;
- if (ReadSymbolicLink(current, &target)) {
- if (target.IsAbsolute())
- result.clear();
- std::vector<FilePath::StringType> target_components;
- target.GetComponents(&target_components);
- components.insert(components.begin(), target_components.begin(),
- target_components.end());
- resolve_count++;
- } else {
- result = current;
- }
- }
-
- if (resolve_count >= kMaxLinksToResolve)
- result.clear();
- return result;
-}
-
-// The callback passed to FSEventStreamCreate().
-void FSEventsCallback(ConstFSEventStreamRef stream,
- void* event_watcher, size_t num_events,
- void* event_paths, const FSEventStreamEventFlags flags[],
- const FSEventStreamEventId event_ids[]) {
- FilePathWatcherFSEvents* watcher =
- reinterpret_cast<FilePathWatcherFSEvents*>(event_watcher);
- DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
-
- bool root_changed = watcher->ResolveTargetPath();
- std::vector<FilePath> paths;
- FSEventStreamEventId root_change_at = FSEventStreamGetLatestEventId(stream);
- for (size_t i = 0; i < num_events; i++) {
- if (flags[i] & kFSEventStreamEventFlagRootChanged)
- root_changed = true;
- if (event_ids[i])
- root_change_at = std::min(root_change_at, event_ids[i]);
- paths.push_back(FilePath(
- reinterpret_cast<char**>(event_paths)[i]).StripTrailingSeparators());
- }
-
- // Reinitialize the event stream if we find changes to the root. This is
- // necessary since FSEvents doesn't report any events for the subtree after
- // the directory to be watched gets created.
- if (root_changed) {
- // Resetting the event stream from within the callback fails (FSEvents spews
- // bad file descriptor errors), so post a task to do the reset.
- g_task_runner.Get().PostTask(
- FROM_HERE,
- Bind(&FilePathWatcherFSEvents::UpdateEventStream, watcher,
- root_change_at));
- }
-
- watcher->OnFilePathsChanged(paths);
-}
-
-} // namespace
-
-FilePathWatcherFSEvents::FilePathWatcherFSEvents() : fsevent_stream_(NULL) {
-}
-
-void FilePathWatcherFSEvents::OnFilePathsChanged(
- const std::vector<FilePath>& paths) {
- if (!message_loop()->BelongsToCurrentThread()) {
- message_loop()->PostTask(
- FROM_HERE,
- Bind(&FilePathWatcherFSEvents::OnFilePathsChanged, this, paths));
- return;
- }
-
- DCHECK(message_loop()->BelongsToCurrentThread());
- if (resolved_target_.empty())
- return;
-
- for (size_t i = 0; i < paths.size(); i++) {
- if (resolved_target_.IsParent(paths[i]) || resolved_target_ == paths[i]) {
- callback_.Run(target_, false);
- return;
- }
- }
-}
-
-bool FilePathWatcherFSEvents::Watch(const FilePath& path,
- bool recursive,
- const FilePathWatcher::Callback& callback) {
- DCHECK(resolved_target_.empty());
- DCHECK(MessageLoopForIO::current());
- DCHECK(!callback.is_null());
-
- // This class could support non-recursive watches, but that is currently
- // left to FilePathWatcherKQueue.
- if (!recursive)
- return false;
-
- set_message_loop(MessageLoopProxy::current());
- callback_ = callback;
- target_ = path;
-
- FSEventStreamEventId start_event = FSEventsGetCurrentEventId();
- g_task_runner.Get().PostTask(
- FROM_HERE,
- Bind(&FilePathWatcherFSEvents::StartEventStream, this, start_event));
- return true;
-}
-
-void FilePathWatcherFSEvents::Cancel() {
- if (callback_.is_null()) {
- // Watch was never called, so exit.
- set_cancelled();
- return;
- }
-
- // Switch to the dispatch queue thread if necessary, so we can tear down
- // the event stream.
- if (!g_task_runner.Get().RunsTasksOnCurrentThread()) {
- g_task_runner.Get().PostTask(
- FROM_HERE,
- Bind(&FilePathWatcherFSEvents::CancelOnMessageLoopThread, this));
- } else {
- CancelOnMessageLoopThread();
- }
-}
-
-void FilePathWatcherFSEvents::CancelOnMessageLoopThread() {
- // For all other implementations, the "message loop thread" is the IO thread,
- // as returned by message_loop(). This implementation, however, needs to
- // cancel pending work on the Dipatch Queue thread.
- DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
-
- set_cancelled();
- if (fsevent_stream_) {
- DestroyEventStream();
- callback_.Reset();
- target_.clear();
- resolved_target_.clear();
- }
-}
-
-void FilePathWatcherFSEvents::UpdateEventStream(
- FSEventStreamEventId start_event) {
- DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
-
- // It can happen that the watcher gets canceled while tasks that call this
- // function are still in flight, so abort if this situation is detected.
- if (is_cancelled() || resolved_target_.empty())
- return;
-
- if (fsevent_stream_)
- DestroyEventStream();
-
- ScopedCFTypeRef<CFStringRef> cf_path(CFStringCreateWithCString(
- NULL, resolved_target_.value().c_str(), kCFStringEncodingMacHFS));
- ScopedCFTypeRef<CFStringRef> cf_dir_path(CFStringCreateWithCString(
- NULL, resolved_target_.DirName().value().c_str(),
- kCFStringEncodingMacHFS));
- CFStringRef paths_array[] = { cf_path.get(), cf_dir_path.get() };
- ScopedCFTypeRef<CFArrayRef> watched_paths(CFArrayCreate(
- NULL, reinterpret_cast<const void**>(paths_array), arraysize(paths_array),
- &kCFTypeArrayCallBacks));
-
- FSEventStreamContext context;
- context.version = 0;
- context.info = this;
- context.retain = NULL;
- context.release = NULL;
- context.copyDescription = NULL;
-
- fsevent_stream_ = FSEventStreamCreate(NULL, &FSEventsCallback, &context,
- watched_paths,
- start_event,
- kEventLatencySeconds,
- kFSEventStreamCreateFlagWatchRoot);
- FSEventStreamSetDispatchQueue(fsevent_stream_,
- g_task_runner.Get().GetDispatchQueue());
-
- if (!FSEventStreamStart(fsevent_stream_))
- message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true));
-}
-
-bool FilePathWatcherFSEvents::ResolveTargetPath() {
- DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
- FilePath resolved = ResolvePath(target_).StripTrailingSeparators();
- bool changed = resolved != resolved_target_;
- resolved_target_ = resolved;
- if (resolved_target_.empty())
- message_loop()->PostTask(FROM_HERE, Bind(callback_, target_, true));
- return changed;
-}
-
-void FilePathWatcherFSEvents::DestroyEventStream() {
- FSEventStreamStop(fsevent_stream_);
- FSEventStreamInvalidate(fsevent_stream_);
- FSEventStreamRelease(fsevent_stream_);
- fsevent_stream_ = NULL;
-}
-
-void FilePathWatcherFSEvents::StartEventStream(
- FSEventStreamEventId start_event) {
- DCHECK(g_task_runner.Get().RunsTasksOnCurrentThread());
- ResolveTargetPath();
- UpdateEventStream(start_event);
-}
-
-FilePathWatcherFSEvents::~FilePathWatcherFSEvents() {}
-
-} // namespace base
diff --git a/base/files/file_path_watcher_fsevents.h b/base/files/file_path_watcher_fsevents.h
deleted file mode 100644
index 5640b4d..0000000
--- a/base/files/file_path_watcher_fsevents.h
+++ /dev/null
@@ -1,73 +0,0 @@
-// Copyright 2014 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.
-
-#ifndef BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
-#define BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
-
-#include <CoreServices/CoreServices.h>
-
-#include <vector>
-
-#include "base/files/file_path.h"
-#include "base/files/file_path_watcher.h"
-
-namespace base {
-
-// Mac-specific file watcher implementation based on FSEvents.
-// There are trade-offs between the FSEvents implementation and a kqueue
-// implementation. The biggest issues are that FSEvents on 10.6 sometimes drops
-// events and kqueue does not trigger for modifications to a file in a watched
-// directory. See file_path_watcher_mac.cc for the code that decides when to
-// use which one.
-class FilePathWatcherFSEvents : public FilePathWatcher::PlatformDelegate {
- public:
- FilePathWatcherFSEvents();
-
- // Called from the FSEvents callback whenever there is a change to the paths.
- void OnFilePathsChanged(const std::vector<FilePath>& paths);
-
- // (Re-)Initialize the event stream to start reporting events from
- // |start_event|.
- void UpdateEventStream(FSEventStreamEventId start_event);
-
- // Returns true if resolving the target path got a different result than
- // last time it was done.
- bool ResolveTargetPath();
-
- // FilePathWatcher::PlatformDelegate overrides.
- virtual bool Watch(const FilePath& path,
- bool recursive,
- const FilePathWatcher::Callback& callback) OVERRIDE;
- virtual void Cancel() OVERRIDE;
-
- private:
- virtual ~FilePathWatcherFSEvents();
-
- // Destroy the event stream.
- void DestroyEventStream();
-
- // Start watching the FSEventStream.
- void StartEventStream(FSEventStreamEventId start_event);
-
- // Cleans up and stops the event stream.
- virtual void CancelOnMessageLoopThread() OVERRIDE;
-
- // Callback to notify upon changes.
- FilePathWatcher::Callback callback_;
-
- // Target path to watch (passed to callback).
- FilePath target_;
-
- // Target path with all symbolic links resolved.
- FilePath resolved_target_;
-
- // Backend stream we receive event callbacks from (strong reference).
- FSEventStreamRef fsevent_stream_;
-
- DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFSEvents);
-};
-
-} // namespace base
-
-#endif // BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
diff --git a/base/files/file_path_watcher_kqueue.cc b/base/files/file_path_watcher_kqueue.cc
index c38e344..e035f22 100644
--- a/base/files/file_path_watcher_kqueue.cc
+++ b/base/files/file_path_watcher_kqueue.cc
@@ -2,14 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/files/file_path_watcher_kqueue.h"
+#include "base/files/file_path_watcher.h"
#include <fcntl.h>
+#include <sys/event.h>
#include <sys/param.h>
+#include <vector>
+
#include "base/bind.h"
#include "base/file_util.h"
#include "base/logging.h"
+#include "base/message_loop/message_loop.h"
+#include "base/message_loop/message_loop_proxy.h"
#include "base/strings/stringprintf.h"
// On some platforms these are not defined.
@@ -22,18 +27,136 @@
namespace base {
-FilePathWatcherKQueue::FilePathWatcherKQueue() : kqueue_(-1) {}
+namespace {
-FilePathWatcherKQueue::~FilePathWatcherKQueue() {}
+// Mac-specific file watcher implementation based on kqueue.
+// Originally it was based on FSEvents so that the semantics were equivalent
+// on Linux, OSX and Windows where it was able to detect:
+// - file creation/deletion/modification in a watched directory
+// - file creation/deletion/modification for a watched file
+// - modifications to the paths to a watched object that would affect the
+// object such as renaming/attibute changes etc.
+// The FSEvents version did all of the above except handling attribute changes
+// to path components. Unfortunately FSEvents appears to have an issue where the
+// current implementation (Mac OS X 10.6.7) sometimes drops events and doesn't
+// send notifications. See
+// http://code.google.com/p/chromium/issues/detail?id=54822#c31 for source that
+// will reproduce the problem. FSEvents also required having a CFRunLoop
+// backing the thread that it was running on, that caused added complexity
+// in the interfaces.
+// The kqueue implementation will handle all of the items in the list above
+// except for detecting modifications to files in a watched directory. It will
+// detect the creation and deletion of files, just not the modification of
+// files. It does however detect the attribute changes that the FSEvents impl
+// would miss.
+class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate,
+ public MessageLoopForIO::Watcher,
+ public MessageLoop::DestructionObserver {
+ public:
+ FilePathWatcherImpl() : kqueue_(-1) {}
-void FilePathWatcherKQueue::ReleaseEvent(struct kevent& event) {
+ // MessageLoopForIO::Watcher overrides.
+ virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
+ virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
+
+ // MessageLoop::DestructionObserver overrides.
+ virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
+
+ // FilePathWatcher::PlatformDelegate overrides.
+ virtual bool Watch(const FilePath& path,
+ bool recursive,
+ const FilePathWatcher::Callback& callback) OVERRIDE;
+ virtual void Cancel() OVERRIDE;
+
+ protected:
+ virtual ~FilePathWatcherImpl() {}
+
+ private:
+ class EventData {
+ public:
+ EventData(const FilePath& path, const FilePath::StringType& subdir)
+ : path_(path), subdir_(subdir) { }
+ FilePath path_; // Full path to this item.
+ FilePath::StringType subdir_; // Path to any sub item.
+ };
+ typedef std::vector<struct kevent> EventVector;
+
+ // Can only be called on |io_message_loop_|'s thread.
+ virtual void CancelOnMessageLoopThread() OVERRIDE;
+
+ // Returns true if the kevent values are error free.
+ bool AreKeventValuesValid(struct kevent* kevents, int count);
+
+ // Respond to a change of attributes of the path component represented by
+ // |event|. Sets |target_file_affected| to true if |target_| is affected.
+ // Sets |update_watches| to true if |events_| need to be updated.
+ void HandleAttributesChange(const EventVector::iterator& event,
+ bool* target_file_affected,
+ bool* update_watches);
+
+ // Respond to a move or deletion of the path component represented by
+ // |event|. Sets |target_file_affected| to true if |target_| is affected.
+ // Sets |update_watches| to true if |events_| need to be updated.
+ void HandleDeleteOrMoveChange(const EventVector::iterator& event,
+ bool* target_file_affected,
+ bool* update_watches);
+
+ // Respond to a creation of an item in the path component represented by
+ // |event|. Sets |target_file_affected| to true if |target_| is affected.
+ // Sets |update_watches| to true if |events_| need to be updated.
+ void HandleCreateItemChange(const EventVector::iterator& event,
+ bool* target_file_affected,
+ bool* update_watches);
+
+ // Update |events_| with the current status of the system.
+ // Sets |target_file_affected| to true if |target_| is affected.
+ // Returns false if an error occurs.
+ bool UpdateWatches(bool* target_file_affected);
+
+ // Fills |events| with one kevent per component in |path|.
+ // Returns the number of valid events created where a valid event is
+ // defined as one that has a ident (file descriptor) field != -1.
+ static int EventsForPath(FilePath path, EventVector *events);
+
+ // Release a kevent generated by EventsForPath.
+ static void ReleaseEvent(struct kevent& event);
+
+ // Returns a file descriptor that will not block the system from deleting
+ // the file it references.
+ static uintptr_t FileDescriptorForPath(const FilePath& path);
+
+ static const uintptr_t kNoFileDescriptor = static_cast<uintptr_t>(-1);
+
+ // Closes |*fd| and sets |*fd| to -1.
+ static void CloseFileDescriptor(uintptr_t* fd);
+
+ // Returns true if kevent has open file descriptor.
+ static bool IsKeventFileDescriptorOpen(const struct kevent& event) {
+ return event.ident != kNoFileDescriptor;
+ }
+
+ static EventData* EventDataForKevent(const struct kevent& event) {
+ return reinterpret_cast<EventData*>(event.udata);
+ }
+
+ EventVector events_;
+ scoped_refptr<base::MessageLoopProxy> io_message_loop_;
+ MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_;
+ FilePathWatcher::Callback callback_;
+ FilePath target_;
+ int kqueue_;
+
+ DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl);
+};
+
+void FilePathWatcherImpl::ReleaseEvent(struct kevent& event) {
CloseFileDescriptor(&event.ident);
EventData* entry = EventDataForKevent(event);
delete entry;
event.udata = NULL;
}
-int FilePathWatcherKQueue::EventsForPath(FilePath path, EventVector* events) {
+int FilePathWatcherImpl::EventsForPath(FilePath path, EventVector* events) {
DCHECK(MessageLoopForIO::current());
// Make sure that we are working with a clean slate.
DCHECK(events->empty());
@@ -75,14 +198,14 @@
return last_existing_entry;
}
-uintptr_t FilePathWatcherKQueue::FileDescriptorForPath(const FilePath& path) {
+uintptr_t FilePathWatcherImpl::FileDescriptorForPath(const FilePath& path) {
int fd = HANDLE_EINTR(open(path.value().c_str(), O_EVTONLY));
if (fd == -1)
return kNoFileDescriptor;
return fd;
}
-void FilePathWatcherKQueue::CloseFileDescriptor(uintptr_t* fd) {
+void FilePathWatcherImpl::CloseFileDescriptor(uintptr_t* fd) {
if (*fd == kNoFileDescriptor) {
return;
}
@@ -93,7 +216,7 @@
*fd = kNoFileDescriptor;
}
-bool FilePathWatcherKQueue::AreKeventValuesValid(struct kevent* kevents,
+bool FilePathWatcherImpl::AreKeventValuesValid(struct kevent* kevents,
int count) {
if (count < 0) {
DPLOG(ERROR) << "kevent";
@@ -127,7 +250,7 @@
return valid;
}
-void FilePathWatcherKQueue::HandleAttributesChange(
+void FilePathWatcherImpl::HandleAttributesChange(
const EventVector::iterator& event,
bool* target_file_affected,
bool* update_watches) {
@@ -151,7 +274,7 @@
}
}
-void FilePathWatcherKQueue::HandleDeleteOrMoveChange(
+void FilePathWatcherImpl::HandleDeleteOrMoveChange(
const EventVector::iterator& event,
bool* target_file_affected,
bool* update_watches) {
@@ -167,7 +290,7 @@
}
}
-void FilePathWatcherKQueue::HandleCreateItemChange(
+void FilePathWatcherImpl::HandleCreateItemChange(
const EventVector::iterator& event,
bool* target_file_affected,
bool* update_watches) {
@@ -187,7 +310,7 @@
}
}
-bool FilePathWatcherKQueue::UpdateWatches(bool* target_file_affected) {
+bool FilePathWatcherImpl::UpdateWatches(bool* target_file_affected) {
// Iterate over events adding kevents for items that exist to the kqueue.
// Then check to see if new components in the path have been created.
// Repeat until no new components in the path are detected.
@@ -228,7 +351,7 @@
return true;
}
-void FilePathWatcherKQueue::OnFileCanReadWithoutBlocking(int fd) {
+void FilePathWatcherImpl::OnFileCanReadWithoutBlocking(int fd) {
DCHECK(MessageLoopForIO::current());
DCHECK_EQ(fd, kqueue_);
DCHECK(events_.size());
@@ -301,24 +424,24 @@
}
}
-void FilePathWatcherKQueue::OnFileCanWriteWithoutBlocking(int fd) {
+void FilePathWatcherImpl::OnFileCanWriteWithoutBlocking(int fd) {
NOTREACHED();
}
-void FilePathWatcherKQueue::WillDestroyCurrentMessageLoop() {
+void FilePathWatcherImpl::WillDestroyCurrentMessageLoop() {
CancelOnMessageLoopThread();
}
-bool FilePathWatcherKQueue::Watch(const FilePath& path,
- bool recursive,
- const FilePathWatcher::Callback& callback) {
+bool FilePathWatcherImpl::Watch(const FilePath& path,
+ bool recursive,
+ const FilePathWatcher::Callback& callback) {
DCHECK(MessageLoopForIO::current());
DCHECK(target_.value().empty()); // Can only watch one path.
DCHECK(!callback.is_null());
DCHECK_EQ(kqueue_, -1);
if (recursive) {
- // Recursive watch is not supported using kqueue.
+ // Recursive watch is not supported on this platform.
NOTIMPLEMENTED();
return false;
}
@@ -355,7 +478,7 @@
kqueue_, true, MessageLoopForIO::WATCH_READ, &kqueue_watcher_, this);
}
-void FilePathWatcherKQueue::Cancel() {
+void FilePathWatcherImpl::Cancel() {
base::MessageLoopProxy* proxy = io_message_loop_.get();
if (!proxy) {
set_cancelled();
@@ -363,13 +486,13 @@
}
if (!proxy->BelongsToCurrentThread()) {
proxy->PostTask(FROM_HERE,
- base::Bind(&FilePathWatcherKQueue::Cancel, this));
+ base::Bind(&FilePathWatcherImpl::Cancel, this));
return;
}
CancelOnMessageLoopThread();
}
-void FilePathWatcherKQueue::CancelOnMessageLoopThread() {
+void FilePathWatcherImpl::CancelOnMessageLoopThread() {
DCHECK(MessageLoopForIO::current());
if (!is_cancelled()) {
set_cancelled();
@@ -386,4 +509,10 @@
}
}
+} // namespace
+
+FilePathWatcher::FilePathWatcher() {
+ impl_ = new FilePathWatcherImpl();
+}
+
} // namespace base
diff --git a/base/files/file_path_watcher_kqueue.h b/base/files/file_path_watcher_kqueue.h
deleted file mode 100644
index 703fda6..0000000
--- a/base/files/file_path_watcher_kqueue.h
+++ /dev/null
@@ -1,132 +0,0 @@
-// Copyright 2014 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.
-
-#ifndef BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_
-#define BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_
-
-#include <sys/event.h>
-#include <vector>
-
-#include "base/files/file_path.h"
-#include "base/files/file_path_watcher.h"
-#include "base/message_loop/message_loop.h"
-#include "base/message_loop/message_loop_proxy.h"
-
-namespace base {
-
-// Mac-specific file watcher implementation based on kqueue.
-// The Linux and Windows versions are able to detect:
-// - file creation/deletion/modification in a watched directory
-// - file creation/deletion/modification for a watched file
-// - modifications to the paths to a watched object that would affect the
-// object such as renaming/attibute changes etc.
-// The kqueue implementation will handle all of the items in the list above
-// except for detecting modifications to files in a watched directory. It will
-// detect the creation and deletion of files, just not the modification of
-// files. It does however detect the attribute changes that the FSEvents impl
-// would miss.
-class FilePathWatcherKQueue : public FilePathWatcher::PlatformDelegate,
- public MessageLoopForIO::Watcher,
- public MessageLoop::DestructionObserver {
- public:
- FilePathWatcherKQueue();
-
- // MessageLoopForIO::Watcher overrides.
- virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
- virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
-
- // MessageLoop::DestructionObserver overrides.
- virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
-
- // FilePathWatcher::PlatformDelegate overrides.
- virtual bool Watch(const FilePath& path,
- bool recursive,
- const FilePathWatcher::Callback& callback) OVERRIDE;
- virtual void Cancel() OVERRIDE;
-
- protected:
- virtual ~FilePathWatcherKQueue();
-
- private:
- class EventData {
- public:
- EventData(const FilePath& path, const FilePath::StringType& subdir)
- : path_(path), subdir_(subdir) { }
- FilePath path_; // Full path to this item.
- FilePath::StringType subdir_; // Path to any sub item.
- };
-
- typedef std::vector<struct kevent> EventVector;
-
- // Can only be called on |io_message_loop_|'s thread.
- virtual void CancelOnMessageLoopThread() OVERRIDE;
-
- // Returns true if the kevent values are error free.
- bool AreKeventValuesValid(struct kevent* kevents, int count);
-
- // Respond to a change of attributes of the path component represented by
- // |event|. Sets |target_file_affected| to true if |target_| is affected.
- // Sets |update_watches| to true if |events_| need to be updated.
- void HandleAttributesChange(const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches);
-
- // Respond to a move or deletion of the path component represented by
- // |event|. Sets |target_file_affected| to true if |target_| is affected.
- // Sets |update_watches| to true if |events_| need to be updated.
- void HandleDeleteOrMoveChange(const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches);
-
- // Respond to a creation of an item in the path component represented by
- // |event|. Sets |target_file_affected| to true if |target_| is affected.
- // Sets |update_watches| to true if |events_| need to be updated.
- void HandleCreateItemChange(const EventVector::iterator& event,
- bool* target_file_affected,
- bool* update_watches);
-
- // Update |events_| with the current status of the system.
- // Sets |target_file_affected| to true if |target_| is affected.
- // Returns false if an error occurs.
- bool UpdateWatches(bool* target_file_affected);
-
- // Fills |events| with one kevent per component in |path|.
- // Returns the number of valid events created where a valid event is
- // defined as one that has a ident (file descriptor) field != -1.
- static int EventsForPath(FilePath path, EventVector *events);
-
- // Release a kevent generated by EventsForPath.
- static void ReleaseEvent(struct kevent& event);
-
- // Returns a file descriptor that will not block the system from deleting
- // the file it references.
- static uintptr_t FileDescriptorForPath(const FilePath& path);
-
- static const uintptr_t kNoFileDescriptor = static_cast<uintptr_t>(-1);
-
- // Closes |*fd| and sets |*fd| to -1.
- static void CloseFileDescriptor(uintptr_t* fd);
-
- // Returns true if kevent has open file descriptor.
- static bool IsKeventFileDescriptorOpen(const struct kevent& event) {
- return event.ident != kNoFileDescriptor;
- }
-
- static EventData* EventDataForKevent(const struct kevent& event) {
- return reinterpret_cast<EventData*>(event.udata);
- }
-
- EventVector events_;
- scoped_refptr<base::MessageLoopProxy> io_message_loop_;
- MessageLoopForIO::FileDescriptorWatcher kqueue_watcher_;
- FilePathWatcher::Callback callback_;
- FilePath target_;
- int kqueue_;
-
- DISALLOW_COPY_AND_ASSIGN(FilePathWatcherKQueue);
-};
-
-} // namespace base
-
-#endif // BASE_FILES_FILE_PATH_WATCHER_KQUEUE_H_
diff --git a/base/files/file_path_watcher_linux.cc b/base/files/file_path_watcher_linux.cc
index 915ad50..0bdca62 100644
--- a/base/files/file_path_watcher_linux.cc
+++ b/base/files/file_path_watcher_linux.cc
@@ -463,7 +463,6 @@
}
void FilePathWatcherImpl::CancelOnMessageLoopThread() {
- DCHECK(message_loop()->BelongsToCurrentThread());
set_cancelled();
if (!callback_.is_null()) {