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 #include "bin/fdutils.h" | 13 #include "bin/fdutils.h" |
14 | 14 |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 namespace bin { | 17 namespace bin { |
18 | 18 |
19 bool FileSystemWatcher::IsSupported() { | 19 bool FileSystemWatcher::IsSupported() { |
20 return true; | 20 return true; |
21 } | 21 } |
22 | 22 |
23 | 23 |
24 intptr_t FileSystemWatcher::WatchPath(const char* path, | 24 intptr_t FileSystemWatcher::Init() { |
25 int events, | 25 int id = TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); |
26 bool recursive) { | 26 if (id < 0) return -1; |
27 int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_CLOEXEC)); | |
28 if (fd < 0) return -1; | |
29 // Some systems dosn't support setting this as non-blocking. Since watching | 27 // Some systems dosn't support setting this as non-blocking. Since watching |
30 // internals are kept away from the user, we know it's possible to continue, | 28 // internals are kept away from the user, we know it's possible to continue, |
31 // even if setting non-blocking fails. | 29 // even if setting non-blocking fails. |
32 FDUtils::SetNonBlocking(fd); | 30 FDUtils::SetNonBlocking(id); |
31 return id; | |
32 } | |
33 | |
34 | |
35 void FileSystemWatcher::Close(intptr_t id) { | |
Søren Gjesse
2013/12/03 14:42:19
Why are we not closing the inotify fd here?
| |
36 USE(id); | |
37 } | |
38 | |
39 | |
40 intptr_t FileSystemWatcher::WatchPath(intptr_t id, | |
41 const char* path, | |
42 int events, | |
43 bool recursive) { | |
33 int list_events = IN_DELETE_SELF | IN_MOVE_SELF; | 44 int list_events = IN_DELETE_SELF | IN_MOVE_SELF; |
34 if (events & kCreate) list_events |= IN_CREATE; | 45 if (events & kCreate) list_events |= IN_CREATE; |
35 if (events & kModifyContent) list_events |= IN_CLOSE_WRITE | IN_ATTRIB; | 46 if (events & kModifyContent) list_events |= IN_CLOSE_WRITE | IN_ATTRIB; |
36 if (events & kDelete) list_events |= IN_DELETE; | 47 if (events & kDelete) list_events |= IN_DELETE; |
37 if (events & kMove) list_events |= IN_MOVE; | 48 if (events & kMove) list_events |= IN_MOVE; |
38 int path_fd = TEMP_FAILURE_RETRY(inotify_add_watch(fd, path, list_events)); | 49 int path_id = TEMP_FAILURE_RETRY(inotify_add_watch(id, path, list_events)); |
39 if (path_fd < 0) { | 50 if (path_id < 0) { |
40 close(fd); | |
41 return -1; | 51 return -1; |
42 } | 52 } |
43 return fd; | 53 return path_id; |
44 } | 54 } |
45 | 55 |
46 | 56 |
47 void FileSystemWatcher::UnwatchPath(intptr_t id) { | 57 void FileSystemWatcher::UnwatchPath(intptr_t id, intptr_t path_id) { |
48 // Nothing to do. | 58 VOID_TEMP_FAILURE_RETRY(inotify_rm_watch(id, path_id)); |
49 } | 59 } |
50 | 60 |
51 | 61 |
52 intptr_t FileSystemWatcher::GetSocketId(intptr_t id) { | 62 intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) { |
63 USE(path_id); | |
53 return id; | 64 return id; |
54 } | 65 } |
55 | 66 |
56 | 67 |
57 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id) { | 68 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id, intptr_t path_id) { |
69 USE(path_id); | |
58 const intptr_t kEventSize = sizeof(struct inotify_event); | 70 const intptr_t kEventSize = sizeof(struct inotify_event); |
59 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1; | 71 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1; |
60 uint8_t buffer[kBufferSize]; | 72 uint8_t buffer[kBufferSize]; |
61 intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize)); | 73 intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize)); |
62 if (bytes < 0) { | 74 if (bytes < 0) { |
63 return DartUtils::NewDartOSError(); | 75 return DartUtils::NewDartOSError(); |
64 } | 76 } |
65 const intptr_t kMaxCount = bytes / kEventSize; | 77 const intptr_t kMaxCount = bytes / kEventSize; |
66 Dart_Handle events = Dart_NewList(kMaxCount); | 78 Dart_Handle events = Dart_NewList(kMaxCount); |
67 intptr_t offset = 0; | 79 intptr_t offset = 0; |
68 intptr_t i = 0; | 80 intptr_t i = 0; |
69 while (offset < bytes) { | 81 while (offset < bytes) { |
70 struct inotify_event* e = | 82 struct inotify_event* e = |
71 reinterpret_cast<struct inotify_event*>(buffer + offset); | 83 reinterpret_cast<struct inotify_event*>(buffer + offset); |
72 Dart_Handle event = Dart_NewList(4); | 84 if ((e->mask & IN_IGNORED) == 0) {; |
73 int mask = 0; | 85 Dart_Handle event = Dart_NewList(5); |
74 if (e->mask & IN_CLOSE_WRITE) mask |= kModifyContent; | 86 int mask = 0; |
75 if (e->mask & IN_ATTRIB) mask |= kModefyAttribute; | 87 if (e->mask & IN_CLOSE_WRITE) mask |= kModifyContent; |
76 if (e->mask & IN_CREATE) mask |= kCreate; | 88 if (e->mask & IN_ATTRIB) mask |= kModefyAttribute; |
77 if (e->mask & IN_MOVE) mask |= kMove; | 89 if (e->mask & IN_CREATE) mask |= kCreate; |
78 if (e->mask & IN_DELETE) mask |= kDelete; | 90 if (e->mask & IN_MOVE) mask |= kMove; |
79 if (e->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) mask |= kDeleteSelf; | 91 if (e->mask & IN_DELETE) mask |= kDelete; |
80 if (e->mask & IN_ISDIR) mask |= kIsDir; | 92 if (e->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) mask |= kDeleteSelf; |
81 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); | 93 if (e->mask & IN_ISDIR) mask |= kIsDir; |
82 Dart_ListSetAt(event, 1, Dart_NewInteger(e->cookie)); | 94 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); |
83 if (e->len > 0) { | 95 Dart_ListSetAt(event, 1, Dart_NewInteger(e->cookie)); |
84 Dart_ListSetAt(event, 2, Dart_NewStringFromUTF8( | 96 if (e->len > 0) { |
85 reinterpret_cast<uint8_t*>(e->name), strlen(e->name))); | 97 Dart_ListSetAt(event, 2, Dart_NewStringFromUTF8( |
86 } else { | 98 reinterpret_cast<uint8_t*>(e->name), strlen(e->name))); |
87 Dart_ListSetAt(event, 2, Dart_Null()); | 99 } else { |
100 Dart_ListSetAt(event, 2, Dart_Null()); | |
101 } | |
102 Dart_ListSetAt(event, 3, Dart_NewBoolean(e->mask & IN_MOVED_TO)); | |
103 Dart_ListSetAt(event, 4, Dart_NewInteger(e->wd)); | |
104 Dart_ListSetAt(events, i, event); | |
105 i++; | |
88 } | 106 } |
89 Dart_ListSetAt(event, 3, Dart_NewBoolean(e->mask & IN_MOVED_TO)); | |
90 Dart_ListSetAt(events, i, event); | |
91 i++; | |
92 offset += kEventSize + e->len; | 107 offset += kEventSize + e->len; |
93 } | 108 } |
94 ASSERT(offset == bytes); | 109 ASSERT(offset == bytes); |
95 return events; | 110 return events; |
96 } | 111 } |
97 | 112 |
98 } // namespace bin | 113 } // namespace bin |
99 } // namespace dart | 114 } // namespace dart |
100 | 115 |
101 #endif // defined(TARGET_OS_LINUX) | 116 #endif // defined(TARGET_OS_LINUX) |
102 | 117 |
OLD | NEW |