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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
7 | 7 |
8 #include "bin/file_system_watcher.h" | 8 #include "bin/file_system_watcher.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
11 #include <sys/inotify.h> // NOLINT | 11 #include <sys/inotify.h> // NOLINT |
12 | 12 |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 namespace bin { | 15 namespace bin { |
16 | 16 |
17 bool FileSystemWatcher::IsSupported() { | 17 bool FileSystemWatcher::IsSupported() { |
18 return true; | 18 return true; |
19 } | 19 } |
20 | 20 |
21 | 21 |
22 intptr_t FileSystemWatcher::WatchPath(const char* path, | 22 intptr_t FileSystemWatcher::WatchPath(const char* path, |
23 int events, | 23 int events, |
24 bool recursive) { | 24 bool recursive) { |
25 int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); | 25 int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); |
26 if (fd < 0) return -1; | 26 if (fd < 0) return -1; |
27 int list_events = 0; | 27 int list_events = IN_DELETE_SELF; |
28 if (events & kCreate) list_events |= IN_CREATE; | 28 if (events & kCreate) list_events |= IN_CREATE; |
29 if (events & kModifyContent) list_events |= IN_MODIFY | IN_ATTRIB; | 29 if (events & kModifyContent) list_events |= IN_MODIFY | IN_ATTRIB; |
30 if (events & kDelete) list_events |= IN_DELETE; | 30 if (events & kDelete) list_events |= IN_DELETE; |
31 if (events & kMove) list_events |= IN_MOVE; | 31 if (events & kMove) list_events |= IN_MOVE; |
32 int path_fd = TEMP_FAILURE_RETRY(inotify_add_watch(fd, path, list_events)); | 32 int path_fd = TEMP_FAILURE_RETRY(inotify_add_watch(fd, path, list_events)); |
33 if (path_fd < 0) { | 33 if (path_fd < 0) { |
34 close(fd); | 34 close(fd); |
35 return -1; | 35 return -1; |
36 } | 36 } |
37 return fd; | 37 return fd; |
(...skipping 11 matching lines...) Expand all Loading... |
49 | 49 |
50 | 50 |
51 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id) { | 51 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id) { |
52 const intptr_t kEventSize = sizeof(struct inotify_event); | 52 const intptr_t kEventSize = sizeof(struct inotify_event); |
53 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1; | 53 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1; |
54 uint8_t buffer[kBufferSize]; | 54 uint8_t buffer[kBufferSize]; |
55 intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize)); | 55 intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize)); |
56 if (bytes < 0) { | 56 if (bytes < 0) { |
57 return DartUtils::NewDartOSError(); | 57 return DartUtils::NewDartOSError(); |
58 } | 58 } |
59 const intptr_t kMaxCount = kBufferSize / kEventSize + 1; | 59 const intptr_t kMaxCount = bytes / kEventSize; |
60 Dart_Handle events = Dart_NewList(kMaxCount); | 60 Dart_Handle events = Dart_NewList(kMaxCount); |
61 intptr_t offset = 0; | 61 intptr_t offset = 0; |
62 intptr_t i = 0; | 62 intptr_t i = 0; |
63 while (offset < bytes) { | 63 while (offset < bytes) { |
64 struct inotify_event* e = | 64 struct inotify_event* e = |
65 reinterpret_cast<struct inotify_event*>(buffer + offset); | 65 reinterpret_cast<struct inotify_event*>(buffer + offset); |
66 Dart_Handle event = Dart_NewList(3); | 66 Dart_Handle event = Dart_NewList(3); |
67 int mask = 0; | 67 int mask = 0; |
68 if (e->mask & IN_MODIFY) mask |= kModifyContent; | 68 if (e->mask & IN_MODIFY) mask |= kModifyContent; |
69 if (e->mask & IN_ATTRIB) mask |= kModefyAttribute; | 69 if (e->mask & IN_ATTRIB) mask |= kModefyAttribute; |
70 if (e->mask & IN_CREATE) mask |= kCreate; | 70 if (e->mask & IN_CREATE) mask |= kCreate; |
71 if (e->mask & IN_MOVE) mask |= kMove; | 71 if (e->mask & IN_MOVE) mask |= kMove; |
72 if (e->mask & IN_DELETE) mask |= kDelete; | 72 if (e->mask & IN_DELETE) mask |= kDelete; |
| 73 if (e->mask & IN_DELETE_SELF) mask |= kDeleteSelf; |
73 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); | 74 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); |
74 Dart_ListSetAt(event, 1, Dart_NewInteger(e->cookie)); | 75 Dart_ListSetAt(event, 1, Dart_NewInteger(e->cookie)); |
75 if (e->len > 0) { | 76 if (e->len > 0) { |
76 Dart_ListSetAt(event, 2, Dart_NewStringFromUTF8( | 77 Dart_ListSetAt(event, 2, Dart_NewStringFromUTF8( |
77 reinterpret_cast<uint8_t*>(e->name), strlen(e->name))); | 78 reinterpret_cast<uint8_t*>(e->name), strlen(e->name))); |
78 } else { | 79 } else { |
79 Dart_ListSetAt(event, 2, Dart_Null()); | 80 Dart_ListSetAt(event, 2, Dart_Null()); |
80 } | 81 } |
81 Dart_ListSetAt(events, i, event); | 82 Dart_ListSetAt(events, i, event); |
82 i++; | 83 i++; |
83 offset += kEventSize + e->len; | 84 offset += kEventSize + e->len; |
84 } | 85 } |
85 ASSERT(offset == bytes); | 86 ASSERT(offset == bytes); |
86 return events; | 87 return events; |
87 } | 88 } |
88 | 89 |
89 } // namespace bin | 90 } // namespace bin |
90 } // namespace dart | 91 } // namespace dart |
91 | 92 |
92 #endif // defined(TARGET_OS_LINUX) | 93 #endif // defined(TARGET_OS_LINUX) |
93 | 94 |
OLD | NEW |