Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
file_system_watcher_linux.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
7
9
10#include <errno.h> // NOLINT
11#include <sys/inotify.h> // NOLINT
12
13#include "bin/fdutils.h"
14#include "bin/file.h"
15#include "bin/socket.h"
17
18namespace dart {
19namespace bin {
20
22 return true;
23}
24
25intptr_t FileSystemWatcher::Init() {
26 int id = NO_RETRY_EXPECTED(inotify_init1(IN_CLOEXEC));
27 if (id < 0) {
28 return -1;
29 }
30 // Some systems don't support setting this as non-blocking. Since watching
31 // internals are kept away from the user, we know it's possible to continue,
32 // even if setting non-blocking fails.
34 return id;
35}
36
37void FileSystemWatcher::Close(intptr_t id) {
38 USE(id);
39}
40
41intptr_t FileSystemWatcher::WatchPath(intptr_t id,
42 Namespace* namespc,
43 const char* path,
44 int events,
45 bool recursive) {
46 int list_events = IN_DELETE_SELF | IN_MOVE_SELF;
47 if ((events & kCreate) != 0) {
48 list_events |= IN_CREATE;
49 }
50 if ((events & kModifyContent) != 0) {
51 list_events |= IN_CLOSE_WRITE | IN_ATTRIB | IN_MODIFY;
52 }
53 if ((events & kDelete) != 0) {
54 list_events |= IN_DELETE;
55 }
56 if ((events & kMove) != 0) {
57 list_events |= IN_MOVE;
58 }
59 const char* resolved_path = File::GetCanonicalPath(namespc, path);
60 path = resolved_path != nullptr ? resolved_path : path;
61 int path_id = NO_RETRY_EXPECTED(inotify_add_watch(id, path, list_events));
62 if (path_id < 0) {
63 return -1;
64 }
65 return path_id;
66}
67
68void FileSystemWatcher::UnwatchPath(intptr_t id, intptr_t path_id) {
69 VOID_NO_RETRY_EXPECTED(inotify_rm_watch(id, path_id));
70}
71
72intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) {
73 USE(path_id);
74 return id;
75}
76
77static int InotifyEventToMask(struct inotify_event* e) {
78 int mask = 0;
79 if ((e->mask & IN_CLOSE_WRITE) != 0 || (e->mask & IN_MODIFY) != 0) {
81 }
82 if ((e->mask & IN_ATTRIB) != 0) {
84 }
85 if ((e->mask & IN_CREATE) != 0) {
87 }
88 if ((e->mask & IN_MOVE) != 0) {
90 }
91 if ((e->mask & IN_DELETE) != 0) {
93 }
94 if ((e->mask & (IN_DELETE_SELF | IN_MOVE_SELF)) != 0) {
96 }
97 if ((e->mask & IN_ISDIR) != 0) {
99 }
100 return mask;
101}
102
103Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id, intptr_t path_id) {
104 USE(path_id);
105 const intptr_t kEventSize = sizeof(struct inotify_event);
106 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1;
107 uint8_t buffer[kBufferSize];
108 intptr_t bytes =
110 if (bytes < 0) {
112 }
113 const intptr_t kMaxCount = bytes / kEventSize;
114 Dart_Handle events = Dart_NewList(kMaxCount);
115 intptr_t offset = 0;
116 intptr_t i = 0;
117 while (offset < bytes) {
118 struct inotify_event* e =
119 reinterpret_cast<struct inotify_event*>(buffer + offset);
120 if ((e->mask & IN_IGNORED) == 0) {
121 Dart_Handle event = Dart_NewList(5);
122 int mask = InotifyEventToMask(e);
125 if (e->len > 0) {
127 reinterpret_cast<uint8_t*>(e->name), strlen(e->name));
128 if (Dart_IsError(name)) {
129 return name;
130 }
132 } else {
134 }
135 Dart_ListSetAt(event, 3, Dart_NewBoolean((e->mask & IN_MOVED_TO) != 0u));
137 Dart_ListSetAt(events, i, event);
138 i++;
139 }
140 offset += kEventSize + e->len;
141 }
142 ASSERT(offset == bytes);
143 return events;
144}
145
146} // namespace bin
147} // namespace dart
148
149#endif // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
static const size_t kBufferSize
Definition SkString.cpp:27
static Dart_Handle NewDartOSError()
Definition dartutils.cc:706
static bool SetNonBlocking(intptr_t fd)
static intptr_t GetSocketId(intptr_t id, intptr_t path_id)
static void Close(intptr_t id)
static void UnwatchPath(intptr_t id, intptr_t path_id)
static Dart_Handle ReadEvents(intptr_t id, intptr_t path_id)
static intptr_t WatchPath(intptr_t id, Namespace *namespc, const char *path, int events, bool recursive)
static const char * GetCanonicalPath(Namespace *namespc, const char *path, char *dest=nullptr, int dest_size=0)
static intptr_t Read(intptr_t fd, void *buffer, intptr_t num_bytes, SocketOpKind sync)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
#define ASSERT(E)
FlKeyEvent * event
static const uint8_t buffer[]
const char *const name
DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value)
DART_EXPORT Dart_Handle Dart_NewStringFromUTF8(const uint8_t *utf8_array, intptr_t length)
DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list, intptr_t index, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_NewBoolean(bool value)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
static void USE(T &&)
Definition globals.h:618
DART_EXPORT Dart_Handle Dart_NewList(intptr_t length)
DART_EXPORT Dart_Handle Dart_Null()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switches.h:57
#define NO_RETRY_EXPECTED(expression)
#define VOID_NO_RETRY_EXPECTED(expression)
Point offset
const uintptr_t id