Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
file_system_watcher_win.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_WINDOWS)
7
9
10#include <winioctl.h> // NOLINT
11
12#include "bin/builtin.h"
13#include "bin/eventhandler.h"
14#include "bin/utils.h"
15#include "bin/utils_win.h"
16#include "platform/syslog.h"
17
18namespace dart {
19namespace bin {
20
22 return true;
23}
24
25intptr_t FileSystemWatcher::Init() {
26 return 0;
27}
28
29void FileSystemWatcher::Close(intptr_t id) {
30 USE(id);
31}
32
33intptr_t FileSystemWatcher::WatchPath(intptr_t id,
34 Namespace* namespc,
35 const char* path,
36 int events,
37 bool recursive) {
38 USE(id);
39 const auto name = Utf8ToWideChar(path);
40 HANDLE dir =
41 CreateFileW(name.get(), FILE_LIST_DIRECTORY,
42 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
43 nullptr, OPEN_EXISTING,
44 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, nullptr);
45
46 if (dir == INVALID_HANDLE_VALUE) {
47 return -1;
48 }
49
50 int list_events = 0;
51 if ((events & (kCreate | kMove | kDelete)) != 0) {
52 list_events |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
53 }
54 if ((events & kModifyContent) != 0) {
55 list_events |= FILE_NOTIFY_CHANGE_LAST_WRITE;
56 }
57
58 DirectoryWatchHandle* handle =
59 new DirectoryWatchHandle(dir, list_events, recursive);
60 // Issue a read directly, to be sure events are tracked from now on. This is
61 // okay, since in Dart, we create the socket and start reading immediately.
62 handle->EnsureInitialized(EventHandler::delegate());
63 handle->IssueRead();
64 return reinterpret_cast<intptr_t>(handle);
65}
66
67void FileSystemWatcher::UnwatchPath(intptr_t id, intptr_t path_id) {
68 USE(id);
69 DirectoryWatchHandle* handle =
70 reinterpret_cast<DirectoryWatchHandle*>(path_id);
71 handle->Stop();
72}
73
74intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) {
75 USE(id);
76 return path_id;
77}
78
79Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id, intptr_t path_id) {
80 USE(id);
81 const intptr_t kEventSize = sizeof(FILE_NOTIFY_INFORMATION);
82 DirectoryWatchHandle* dir = reinterpret_cast<DirectoryWatchHandle*>(path_id);
83 intptr_t available = dir->Available();
84 if (available <= 0) {
85 return Dart_NewList(0);
86 }
87 intptr_t max_count = available / kEventSize + 1;
88 Dart_Handle events = Dart_NewList(max_count);
89 uint8_t* buffer = Dart_ScopeAllocate(available);
90 intptr_t bytes = dir->Read(buffer, available);
91 intptr_t offset = 0;
92 intptr_t i = 0;
93 while (offset < bytes) {
94 FILE_NOTIFY_INFORMATION* e =
95 reinterpret_cast<FILE_NOTIFY_INFORMATION*>(buffer + offset);
96
97 Dart_Handle event = Dart_NewList(5);
98 int mask = 0;
99 if (e->Action == FILE_ACTION_ADDED) {
100 mask |= kCreate;
101 }
102 if (e->Action == FILE_ACTION_REMOVED) {
103 mask |= kDelete;
104 }
105 if (e->Action == FILE_ACTION_MODIFIED) {
106 mask |= kModifyContent;
107 }
108 if (e->Action == FILE_ACTION_RENAMED_OLD_NAME) {
109 mask |= kMove;
110 }
111 if (e->Action == FILE_ACTION_RENAMED_NEW_NAME) {
112 mask |= kMove;
113 }
115 // Move events come in pairs. Just 'enable' by default.
118 event, 2,
119 Dart_NewStringFromUTF16(reinterpret_cast<uint16_t*>(e->FileName),
120 e->FileNameLength / 2));
123 Dart_ListSetAt(events, i, event);
124 i++;
125 if (e->NextEntryOffset == 0) {
126 break;
127 }
128 offset += e->NextEntryOffset;
129 }
130 return events;
131}
132
133} // namespace bin
134} // namespace dart
135
136#endif // defined(DART_HOST_OS_WINDOWS)
static EventHandlerImplementation * delegate()
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)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
FlKeyEvent * event
static const uint8_t buffer[]
std::unique_ptr< wchar_t[]> Utf8ToWideChar(const char *path)
const char *const name
DART_EXPORT Dart_Handle Dart_NewStringFromUTF16(const uint16_t *utf16_array, intptr_t length)
DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value)
DART_EXPORT uint8_t * Dart_ScopeAllocate(intptr_t size)
DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list, intptr_t index, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_NewBoolean(bool value)
static void USE(T &&)
Definition globals.h:618
DART_EXPORT Dart_Handle Dart_NewList(intptr_t length)
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 to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets dir
Definition switches.h:145
Point offset
#define INVALID_HANDLE_VALUE
void * HANDLE