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

Unified Diff: runtime/bin/file_system_watcher_linux.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_linux.cc
diff --git a/runtime/bin/file_system_watcher_linux.cc b/runtime/bin/file_system_watcher_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..27e4d24ba0bcd0fe47e8b269e9670f559de6d793
--- /dev/null
+++ b/runtime/bin/file_system_watcher_linux.cc
@@ -0,0 +1,94 @@
+// 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_LINUX)
+
+#include "bin/file_system_watcher.h"
+
+#include <errno.h> // NOLINT
+#include <sys/inotify.h> // NOLINT
+
+
+namespace dart {
+namespace bin {
+
+intptr_t FileSystemWatcher::Init() {
+ int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC));
+ if (fd < 0) return -1;
+ return fd;
+}
+
+
+void FileSystemWatcher::Stop(intptr_t id) {
+ // Don't close it manually, the eventhandler will do it for us.
+}
+
+
+intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) {
+ if (path_id != -1) return -1;
+ return id;
+}
+
+
+intptr_t FileSystemWatcher::AddPath(intptr_t id,
+ const char* path,
+ int events,
+ bool recursive) {
+ int list_events = 0;
+ if (events & kCreate) list_events |= IN_CREATE;
+ if (events & kModifyContent) list_events |= IN_MODIFY | IN_ATTRIB;
+ if (events & kDelete) list_events |= IN_DELETE;
+ if (events & kMove) list_events |= IN_MOVE;
+ int fd = TEMP_FAILURE_RETRY(inotify_add_watch(id, path, list_events));
+ if (fd < 0) return -1;
+ return fd;
+}
+
+
+bool FileSystemWatcher::RemovePath(intptr_t id, intptr_t path_id) {
+ return TEMP_FAILURE_RETRY(inotify_rm_watch(id, path_id)) == 0;
+}
+
+
+intptr_t FileSystemWatcher::ReadEvents(intptr_t id,
+ intptr_t path_id,
+ Event** events) {
+ const intptr_t kEventSize = sizeof(struct inotify_event);
+ const intptr_t kBufferSize = kEventSize + NAME_MAX + 1;
+ uint8_t buffer[kBufferSize];
+ intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize));
+ if (bytes < 0) return -1;
+ const intptr_t kMaxCount = kBufferSize / kEventSize + 1;
+ *events = new Event[kMaxCount];
+ intptr_t offset = 0;
+ intptr_t i = 0;
+ while (offset < bytes) {
+ struct inotify_event* e =
+ reinterpret_cast<struct inotify_event*>(buffer + offset);
+ Event* event = *events + i;
+ event->event = 0;
+ event->path_id = e->wd;
+ event->link = e->cookie;
+ if (e->mask & IN_MODIFY) event->event |= kModifyContent;
+ if (e->mask & IN_ATTRIB) event->event |= kModefyAttribute;
+ if (e->mask & IN_CREATE) event->event |= kCreate;
+ if (e->mask & IN_MOVE) event->event |= kMove;
+ if (e->mask & IN_DELETE) event->event |= kDelete;
+ if (e->len > 0) {
+ event->filename = strdup(e->name);
+ } else {
+ event->filename = NULL;
+ }
+ i++;
+ offset += kEventSize + e->len;
+ }
+ return i;
+}
+
+} // namespace bin
+} // namespace dart
+
+#endif // defined(TARGET_OS_LINUX)
+

Powered by Google App Engine
This is Rietveld 408576698