Chromium Code Reviews| Index: runtime/bin/file_system_watcher_macos.cc |
| diff --git a/runtime/bin/file_system_watcher_macos.cc b/runtime/bin/file_system_watcher_macos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f1eec2e5018981c0f71a0d3b02947cbafe261fda |
| --- /dev/null |
| +++ b/runtime/bin/file_system_watcher_macos.cc |
| @@ -0,0 +1,244 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#include "platform/globals.h" |
| +#if defined(TARGET_OS_MACOS) |
| + |
| +#include "bin/file_system_watcher.h" |
| + |
| +#include <errno.h> // NOLINT |
| +#include <fcntl.h> // NOLINT |
| +#include <unistd.h> // NOLINT |
| +#include <CoreServices/CoreServices.h> // NOLINT |
| + |
| +#include "bin/eventhandler.h" |
| +#include "bin/fdutils.h" |
| +#include "bin/socket.h" |
| +#include "bin/thread.h" |
| + |
| + |
| +namespace dart { |
| +namespace bin { |
| + |
| +static Mutex* watcher_mutex = new Mutex(); |
| +static Monitor* watcher_monitor = new Monitor(); |
| + |
| +class FSEventsWatcher; |
| +static FSEventsWatcher* watcher = NULL; |
| + |
| +union FSEvent { |
| + struct { |
| + uint32_t flags; |
| + char path[PATH_MAX]; |
| + } data; |
| + uint8_t bytes[PATH_MAX + 4]; |
| +}; |
| + |
| +class FSEventsWatcher { |
| + public: |
| + class Node { |
| + public: |
| + Node(FSEventStreamRef ref, int fds[2]) |
| + : ref_(ref) { |
| + fds_[0] = fds[0]; |
| + fds_[1] = fds[1]; |
| + } |
| + ~Node() { |
| + close(fds_[1]); |
| + FSEventStreamInvalidate(ref_); |
| + FSEventStreamRelease(ref_); |
| + } |
| + |
| + void Start() { |
| + FSEventStreamStart(ref_); |
| + } |
| + |
| + void Stop() { |
| + FSEventStreamStop(ref_); |
| + } |
| + |
| + int read_fd() const { return fds_[0]; } |
| + |
| + private: |
| + FSEventStreamRef ref_; |
| + int fds_[2]; |
| + }; |
| + |
| + FSEventsWatcher() : run_loop_(0), users_(0) { |
| + Thread::Start(Run, reinterpret_cast<uword>(this)); |
| + } |
| + |
| + static void TimerCallback(CFRunLoopTimerRef timer, void* context) { |
| + // Dummy callback to keep RunLoop alive. |
| + } |
| + |
| + static void Run(uword arg) { |
| + FSEventsWatcher* watcher = reinterpret_cast<FSEventsWatcher*>(arg); |
| + watcher->run_loop_ = CFRunLoopGetCurrent(); |
| + |
| + // Notify, as the run-loop is set. |
| + watcher_monitor->Notify(); |
| + |
| + CFRunLoopTimerRef timer = CFRunLoopTimerCreate( |
| + NULL, |
| + CFAbsoluteTimeGetCurrent() + 1, |
| + 1, |
| + 0, |
| + 0, |
| + TimerCallback, |
| + NULL); |
| + |
| + CFRunLoopAddTimer(watcher->run_loop_, timer, kCFRunLoopCommonModes); |
| + |
| + CFRunLoopRun(); |
| + } |
| + |
| + void Increment() { |
| + users_++; |
| + } |
| + |
| + void Decrement() { |
| + ASSERT(users_ > 0); |
| + users_--; |
| + if (users_ == 0) { |
| + CFRunLoopStop(run_loop_); |
| + watcher = NULL; |
| + delete this; |
| + } |
| + } |
| + |
| + Node* AddPath(const char* path, int events) { |
| + int fds[2]; |
| + VOID_TEMP_FAILURE_RETRY(pipe(fds)); |
| + Socket::SetNonBlocking(fds[0]); |
| + Socket::SetBlocking(fds[1]); |
| + |
| + CFStringRef path_ref = CFStringCreateWithCString( |
| + NULL, path, kCFStringEncodingUTF8); |
| + |
| + FSEventStreamContext context; |
| + context.version = 0; |
| + context.info = reinterpret_cast<void*>(fds[1]); |
| + context.retain = NULL; |
| + context.release = NULL; |
| + context.copyDescription = NULL; |
| + FSEventStreamRef ref = FSEventStreamCreate( |
| + NULL, |
| + Callback, |
| + &context, |
| + CFArrayCreate(NULL, reinterpret_cast<const void**>(&path_ref), 1, NULL), |
| + kFSEventStreamEventIdSinceNow, |
| + 0.10, |
| + kFSEventStreamCreateFlagFileEvents); |
| + |
| + |
| + FSEventStreamScheduleWithRunLoop( |
| + ref, |
| + run_loop_, |
| + kCFRunLoopDefaultMode); |
| + |
| + return new Node(ref, fds); |
| + } |
| + |
| + private: |
| + static void Callback(ConstFSEventStreamRef ref, |
| + void* client, |
| + size_t num_events, |
| + void* event_paths, |
| + const FSEventStreamEventFlags event_flags[], |
| + const FSEventStreamEventId event_ids[]) { |
| + int fd = reinterpret_cast<int>(client); |
| + for (size_t i = 0; i < num_events; i++) { |
| + char *path = reinterpret_cast<char**>(event_paths)[i]; |
| + FSEvent event; |
| + event.data.flags = event_flags[i]; |
| + memmove(event.data.path, path, strlen(path) + 1); |
| + write(fd, event.bytes, sizeof(event)); |
| + } |
| + } |
| + |
| + CFRunLoopRef run_loop_; |
| + int users_; |
| +}; |
| + |
| +intptr_t FileSystemWatcher::Init() { |
| + MutexLocker lock(watcher_mutex); |
| + if (watcher == NULL) { |
| + watcher = new FSEventsWatcher(); |
| + watcher_monitor->Enter(); |
| + watcher_monitor->Wait(Monitor::kNoTimeout); |
| + watcher_monitor->Exit(); |
| + } |
| + watcher->Increment(); |
| + return 0; |
| +} |
| + |
| + |
| +void FileSystemWatcher::Stop(intptr_t id) { |
| + MutexLocker lock(watcher_mutex); |
| + watcher->Decrement(); |
| +} |
| + |
| + |
| +intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) { |
| + if (path_id == -1) return -1; |
| + FSEventsWatcher::Node* node = |
| + reinterpret_cast<FSEventsWatcher::Node*>(path_id); |
| + return node->read_fd(); |
| +} |
| + |
| + |
| +intptr_t FileSystemWatcher::AddPath(intptr_t id, const char* path, int events) { |
| + FSEventsWatcher::Node* node = watcher->AddPath(path, events); |
| + node->Start(); |
| + return reinterpret_cast<intptr_t>(node); |
| +} |
| + |
| + |
| +bool FileSystemWatcher::RemovePath(intptr_t id, intptr_t path_id) { |
| + FSEventsWatcher::Node* node = |
| + reinterpret_cast<FSEventsWatcher::Node*>(path_id); |
| + node->Stop(); |
| + delete node; |
| + return true; |
| +} |
| + |
| + |
| +intptr_t FileSystemWatcher::ReadEvents(intptr_t id, |
|
Søren Gjesse
2013/08/26 07:51:55
Again this should just be the _NativeSocket. If th
Anders Johnsen
2013/09/03 11:36:23
Done.
|
| + intptr_t path_id, |
| + Event** events) { |
| + intptr_t fd = GetSocketId(id, path_id); |
| + intptr_t avail = FDUtils::AvailableBytes(fd); |
| + int count = avail / sizeof(FSEvent); |
| + if (count <= 0) return 0; |
| + *events = new Event[count]; |
| + FSEvent e; |
| + for (int i = 0; i < count; i++) { |
| + intptr_t bytes = TEMP_FAILURE_RETRY(read(fd, e.bytes, sizeof(e))); |
| + if (bytes < 0) return -1; |
| + Event* event = *events + i; |
| + event->event = 0; |
| + event->path_id = id; |
| + event->link = 1; |
| + int flags = e.data.flags; |
| + if (flags & kFSEventStreamEventFlagItemModified) { |
| + event->event |= kModifyContent; |
| + } |
| + if (flags & kFSEventStreamEventFlagItemRenamed) event->event |= kMove; |
| + if (flags & kFSEventStreamEventFlagItemXattrMod) { |
| + event->event |= kModefyAttribute; |
| + } |
| + if (flags & kFSEventStreamEventFlagItemCreated) event->event |= kCreate; |
| + if (flags & kFSEventStreamEventFlagItemRemoved) event->event |= kDelete; |
| + event->filename = strdup(e.data.path); |
| + } |
| + return count; |
| +} |
| + |
| +} // namespace bin |
| +} // namespace dart |
| + |
| +#endif // defined(TARGET_OS_MACOS) |
| + |
| + |