OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_LINUX) |
| 7 |
| 8 #include "bin/file_system_watcher.h" |
| 9 |
| 10 #include <errno.h> // NOLINT |
| 11 #include <sys/inotify.h> // NOLINT |
| 12 |
| 13 |
| 14 namespace dart { |
| 15 namespace bin { |
| 16 |
| 17 intptr_t FileSystemWatcher::WatchPath(const char* path, |
| 18 int events, |
| 19 bool recursive) { |
| 20 int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); |
| 21 if (fd < 0) return -1; |
| 22 int list_events = 0; |
| 23 if (events & kCreate) list_events |= IN_CREATE; |
| 24 if (events & kModifyContent) list_events |= IN_MODIFY | IN_ATTRIB; |
| 25 if (events & kDelete) list_events |= IN_DELETE; |
| 26 if (events & kMove) list_events |= IN_MOVE; |
| 27 int path_fd = TEMP_FAILURE_RETRY(inotify_add_watch(fd, path, list_events)); |
| 28 if (path_fd < 0) { |
| 29 close(fd); |
| 30 return -1; |
| 31 } |
| 32 return fd; |
| 33 } |
| 34 |
| 35 |
| 36 void FileSystemWatcher::UnwatchPath(intptr_t id) { |
| 37 // Nothing to do. |
| 38 } |
| 39 |
| 40 |
| 41 intptr_t FileSystemWatcher::GetSocketId(intptr_t id) { |
| 42 return id; |
| 43 } |
| 44 |
| 45 |
| 46 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id) { |
| 47 const intptr_t kEventSize = sizeof(struct inotify_event); |
| 48 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1; |
| 49 uint8_t buffer[kBufferSize]; |
| 50 intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize)); |
| 51 if (bytes < 0) { |
| 52 return DartUtils::NewDartOSError(); |
| 53 } |
| 54 const intptr_t kMaxCount = kBufferSize / kEventSize + 1; |
| 55 Dart_Handle events = Dart_NewList(kMaxCount); |
| 56 intptr_t offset = 0; |
| 57 intptr_t i = 0; |
| 58 while (offset < bytes) { |
| 59 struct inotify_event* e = |
| 60 reinterpret_cast<struct inotify_event*>(buffer + offset); |
| 61 Dart_Handle event = Dart_NewList(3); |
| 62 int mask = 0; |
| 63 if (e->mask & IN_MODIFY) mask |= kModifyContent; |
| 64 if (e->mask & IN_ATTRIB) mask |= kModefyAttribute; |
| 65 if (e->mask & IN_CREATE) mask |= kCreate; |
| 66 if (e->mask & IN_MOVE) mask |= kMove; |
| 67 if (e->mask & IN_DELETE) mask |= kDelete; |
| 68 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); |
| 69 Dart_ListSetAt(event, 1, Dart_NewInteger(e->cookie)); |
| 70 if (e->len > 0) { |
| 71 Dart_ListSetAt(event, 2, Dart_NewStringFromUTF8( |
| 72 reinterpret_cast<uint8_t*>(e->name), strlen(e->name))); |
| 73 } else { |
| 74 Dart_ListSetAt(event, 2, Dart_Null()); |
| 75 } |
| 76 Dart_ListSetAt(events, i, event); |
| 77 i++; |
| 78 offset += kEventSize + e->len; |
| 79 } |
| 80 ASSERT(offset == bytes); |
| 81 return events; |
| 82 } |
| 83 |
| 84 } // namespace bin |
| 85 } // namespace dart |
| 86 |
| 87 #endif // defined(TARGET_OS_LINUX) |
| 88 |
OLD | NEW |