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..daa49e07d0d7622399a2d157b98602ae99e0e018 |
--- /dev/null |
+++ b/runtime/bin/file_system_watcher_linux.cc |
@@ -0,0 +1,82 @@ |
+// 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::WatchPath(const char* path, |
+ int events, |
+ bool recursive) { |
+ int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); |
+ if (fd < 0) return -1; |
+ 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 path_fd = TEMP_FAILURE_RETRY(inotify_add_watch(fd, path, list_events)); |
+ if (path_fd < 0) { |
+ close(fd); |
+ return -1; |
+ } |
+ return fd; |
+} |
+ |
+ |
+void FileSystemWatcher::UnwatchPath(intptr_t id) { |
+ // Nothing to do. |
+} |
+ |
+ |
+intptr_t FileSystemWatcher::GetSocketId(intptr_t id) { |
+ return id; |
+} |
+ |
+ |
+intptr_t FileSystemWatcher::ReadEvents(intptr_t id, Event** events) { |
Søren Gjesse
2013/08/26 07:51:55
Here we should just pass in the _NativeSocket obje
Anders Johnsen
2013/09/03 11:36:23
I've tried to clean up as much as possible by usin
|
+ 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]; |
Søren Gjesse
2013/08/26 07:51:55
I think we should just do the Dart allocation in h
Anders Johnsen
2013/09/03 11:36:23
Done.
|
+ 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->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) |
+ |