| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
| 8 #if defined(HOST_OS_LINUX) | 8 #if defined(HOST_OS_LINUX) |
| 9 | 9 |
| 10 #include "bin/file_system_watcher.h" | 10 #include "bin/file_system_watcher.h" |
| 11 | 11 |
| 12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
| 13 #include <sys/inotify.h> // NOLINT | 13 #include <sys/inotify.h> // NOLINT |
| 14 | 14 |
| 15 #include "bin/fdutils.h" | 15 #include "bin/fdutils.h" |
| 16 #include "bin/socket.h" | 16 #include "bin/socket.h" |
| 17 #include "platform/signal_blocker.h" | 17 #include "platform/signal_blocker.h" |
| 18 | 18 |
| 19 namespace dart { | 19 namespace dart { |
| 20 namespace bin { | 20 namespace bin { |
| 21 | 21 |
| 22 bool FileSystemWatcher::IsSupported() { | 22 bool FileSystemWatcher::IsSupported() { |
| 23 return true; | 23 return true; |
| 24 } | 24 } |
| 25 | 25 |
| 26 | |
| 27 intptr_t FileSystemWatcher::Init() { | 26 intptr_t FileSystemWatcher::Init() { |
| 28 int id = NO_RETRY_EXPECTED(inotify_init1(IN_CLOEXEC)); | 27 int id = NO_RETRY_EXPECTED(inotify_init1(IN_CLOEXEC)); |
| 29 if (id < 0) { | 28 if (id < 0) { |
| 30 return -1; | 29 return -1; |
| 31 } | 30 } |
| 32 // Some systems dosn't support setting this as non-blocking. Since watching | 31 // Some systems dosn't support setting this as non-blocking. Since watching |
| 33 // internals are kept away from the user, we know it's possible to continue, | 32 // internals are kept away from the user, we know it's possible to continue, |
| 34 // even if setting non-blocking fails. | 33 // even if setting non-blocking fails. |
| 35 FDUtils::SetNonBlocking(id); | 34 FDUtils::SetNonBlocking(id); |
| 36 return id; | 35 return id; |
| 37 } | 36 } |
| 38 | 37 |
| 39 | |
| 40 void FileSystemWatcher::Close(intptr_t id) { | 38 void FileSystemWatcher::Close(intptr_t id) { |
| 41 USE(id); | 39 USE(id); |
| 42 } | 40 } |
| 43 | 41 |
| 44 | |
| 45 intptr_t FileSystemWatcher::WatchPath(intptr_t id, | 42 intptr_t FileSystemWatcher::WatchPath(intptr_t id, |
| 46 const char* path, | 43 const char* path, |
| 47 int events, | 44 int events, |
| 48 bool recursive) { | 45 bool recursive) { |
| 49 int list_events = IN_DELETE_SELF | IN_MOVE_SELF; | 46 int list_events = IN_DELETE_SELF | IN_MOVE_SELF; |
| 50 if ((events & kCreate) != 0) { | 47 if ((events & kCreate) != 0) { |
| 51 list_events |= IN_CREATE; | 48 list_events |= IN_CREATE; |
| 52 } | 49 } |
| 53 if ((events & kModifyContent) != 0) { | 50 if ((events & kModifyContent) != 0) { |
| 54 list_events |= IN_CLOSE_WRITE | IN_ATTRIB; | 51 list_events |= IN_CLOSE_WRITE | IN_ATTRIB; |
| 55 } | 52 } |
| 56 if ((events & kDelete) != 0) { | 53 if ((events & kDelete) != 0) { |
| 57 list_events |= IN_DELETE; | 54 list_events |= IN_DELETE; |
| 58 } | 55 } |
| 59 if ((events & kMove) != 0) { | 56 if ((events & kMove) != 0) { |
| 60 list_events |= IN_MOVE; | 57 list_events |= IN_MOVE; |
| 61 } | 58 } |
| 62 int path_id = NO_RETRY_EXPECTED(inotify_add_watch(id, path, list_events)); | 59 int path_id = NO_RETRY_EXPECTED(inotify_add_watch(id, path, list_events)); |
| 63 if (path_id < 0) { | 60 if (path_id < 0) { |
| 64 return -1; | 61 return -1; |
| 65 } | 62 } |
| 66 return path_id; | 63 return path_id; |
| 67 } | 64 } |
| 68 | 65 |
| 69 | |
| 70 void FileSystemWatcher::UnwatchPath(intptr_t id, intptr_t path_id) { | 66 void FileSystemWatcher::UnwatchPath(intptr_t id, intptr_t path_id) { |
| 71 VOID_NO_RETRY_EXPECTED(inotify_rm_watch(id, path_id)); | 67 VOID_NO_RETRY_EXPECTED(inotify_rm_watch(id, path_id)); |
| 72 } | 68 } |
| 73 | 69 |
| 74 | |
| 75 intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) { | 70 intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) { |
| 76 USE(path_id); | 71 USE(path_id); |
| 77 return id; | 72 return id; |
| 78 } | 73 } |
| 79 | 74 |
| 80 | |
| 81 static int InotifyEventToMask(struct inotify_event* e) { | 75 static int InotifyEventToMask(struct inotify_event* e) { |
| 82 int mask = 0; | 76 int mask = 0; |
| 83 if ((e->mask & IN_CLOSE_WRITE) != 0) { | 77 if ((e->mask & IN_CLOSE_WRITE) != 0) { |
| 84 mask |= FileSystemWatcher::kModifyContent; | 78 mask |= FileSystemWatcher::kModifyContent; |
| 85 } | 79 } |
| 86 if ((e->mask & IN_ATTRIB) != 0) { | 80 if ((e->mask & IN_ATTRIB) != 0) { |
| 87 mask |= FileSystemWatcher::kModefyAttribute; | 81 mask |= FileSystemWatcher::kModefyAttribute; |
| 88 } | 82 } |
| 89 if ((e->mask & IN_CREATE) != 0) { | 83 if ((e->mask & IN_CREATE) != 0) { |
| 90 mask |= FileSystemWatcher::kCreate; | 84 mask |= FileSystemWatcher::kCreate; |
| 91 } | 85 } |
| 92 if ((e->mask & IN_MOVE) != 0) { | 86 if ((e->mask & IN_MOVE) != 0) { |
| 93 mask |= FileSystemWatcher::kMove; | 87 mask |= FileSystemWatcher::kMove; |
| 94 } | 88 } |
| 95 if ((e->mask & IN_DELETE) != 0) { | 89 if ((e->mask & IN_DELETE) != 0) { |
| 96 mask |= FileSystemWatcher::kDelete; | 90 mask |= FileSystemWatcher::kDelete; |
| 97 } | 91 } |
| 98 if ((e->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) != 0) { | 92 if ((e->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) != 0) { |
| 99 mask |= FileSystemWatcher::kDeleteSelf; | 93 mask |= FileSystemWatcher::kDeleteSelf; |
| 100 } | 94 } |
| 101 if ((e->mask & IN_ISDIR) != 0) { | 95 if ((e->mask & IN_ISDIR) != 0) { |
| 102 mask |= FileSystemWatcher::kIsDir; | 96 mask |= FileSystemWatcher::kIsDir; |
| 103 } | 97 } |
| 104 return mask; | 98 return mask; |
| 105 } | 99 } |
| 106 | 100 |
| 107 | |
| 108 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id, intptr_t path_id) { | 101 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id, intptr_t path_id) { |
| 109 USE(path_id); | 102 USE(path_id); |
| 110 const intptr_t kEventSize = sizeof(struct inotify_event); | 103 const intptr_t kEventSize = sizeof(struct inotify_event); |
| 111 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1; | 104 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1; |
| 112 uint8_t buffer[kBufferSize]; | 105 uint8_t buffer[kBufferSize]; |
| 113 intptr_t bytes = | 106 intptr_t bytes = |
| 114 SocketBase::Read(id, buffer, kBufferSize, SocketBase::kAsync); | 107 SocketBase::Read(id, buffer, kBufferSize, SocketBase::kAsync); |
| 115 if (bytes < 0) { | 108 if (bytes < 0) { |
| 116 return DartUtils::NewDartOSError(); | 109 return DartUtils::NewDartOSError(); |
| 117 } | 110 } |
| 118 const intptr_t kMaxCount = bytes / kEventSize; | 111 const intptr_t kMaxCount = bytes / kEventSize; |
| 119 Dart_Handle events = Dart_NewList(kMaxCount); | 112 Dart_Handle events = Dart_NewList(kMaxCount); |
| 120 intptr_t offset = 0; | 113 intptr_t offset = 0; |
| 121 intptr_t i = 0; | 114 intptr_t i = 0; |
| 122 while (offset < bytes) { | 115 while (offset < bytes) { |
| 123 struct inotify_event* e = | 116 struct inotify_event* e = |
| 124 reinterpret_cast<struct inotify_event*>(buffer + offset); | 117 reinterpret_cast<struct inotify_event*>(buffer + offset); |
| 125 if ((e->mask & IN_IGNORED) == 0) { | 118 if ((e->mask & IN_IGNORED) == 0) { |
| 126 Dart_Handle event = Dart_NewList(5); | 119 Dart_Handle event = Dart_NewList(5); |
| 127 int mask = InotifyEventToMask(e); | 120 int mask = InotifyEventToMask(e); |
| 128 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); | 121 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); |
| 129 Dart_ListSetAt(event, 1, Dart_NewInteger(e->cookie)); | 122 Dart_ListSetAt(event, 1, Dart_NewInteger(e->cookie)); |
| 130 if (e->len > 0) { | 123 if (e->len > 0) { |
| 131 Dart_ListSetAt(event, 2, Dart_NewStringFromUTF8( | 124 Dart_ListSetAt( |
| 132 reinterpret_cast<uint8_t*>(e->name), | 125 event, 2, |
| 133 strlen(e->name))); | 126 Dart_NewStringFromUTF8(reinterpret_cast<uint8_t*>(e->name), |
| 127 strlen(e->name))); |
| 134 } else { | 128 } else { |
| 135 Dart_ListSetAt(event, 2, Dart_Null()); | 129 Dart_ListSetAt(event, 2, Dart_Null()); |
| 136 } | 130 } |
| 137 Dart_ListSetAt(event, 3, Dart_NewBoolean(e->mask & IN_MOVED_TO)); | 131 Dart_ListSetAt(event, 3, Dart_NewBoolean(e->mask & IN_MOVED_TO)); |
| 138 Dart_ListSetAt(event, 4, Dart_NewInteger(e->wd)); | 132 Dart_ListSetAt(event, 4, Dart_NewInteger(e->wd)); |
| 139 Dart_ListSetAt(events, i, event); | 133 Dart_ListSetAt(events, i, event); |
| 140 i++; | 134 i++; |
| 141 } | 135 } |
| 142 offset += kEventSize + e->len; | 136 offset += kEventSize + e->len; |
| 143 } | 137 } |
| 144 ASSERT(offset == bytes); | 138 ASSERT(offset == bytes); |
| 145 return events; | 139 return events; |
| 146 } | 140 } |
| 147 | 141 |
| 148 } // namespace bin | 142 } // namespace bin |
| 149 } // namespace dart | 143 } // namespace dart |
| 150 | 144 |
| 151 #endif // defined(HOST_OS_LINUX) | 145 #endif // defined(HOST_OS_LINUX) |
| 152 | 146 |
| 153 #endif // !defined(DART_IO_DISABLED) | 147 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |