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