Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(785)

Unified Diff: runtime/bin/file_system_watcher_macos.cc

Issue 19263003: Add FileSystemWatcher class to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Cleanup Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 {
Søren Gjesse 2013/08/23 07:47:45 Please rename this to FileSystemEventsWatcher. The
+ 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];
Søren Gjesse 2013/08/23 07:47:45 Use two named members read_fd_ write_fd_
+ };
+
+ 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]);
Søren Gjesse 2013/08/23 07:47:45 SetCloseOnExec on both fds?
+
+ 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);
Søren Gjesse 2013/08/23 07:47:45 Two blank lines.
+
+
+ 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,
+ 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)
+
+

Powered by Google App Engine
This is Rietveld 408576698