OLD | NEW |
---|---|
(Empty) | |
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(TARGET_OS_WINDOWS) | |
7 | |
8 #include "bin/file_system_watcher.h" | |
9 #include "bin/eventhandler.h" | |
10 | |
11 #include <WinIoCtl.h> // NOLINT | |
12 | |
13 #include "bin/builtin.h" | |
14 #include "bin/log.h" | |
15 #include "bin/utils.h" | |
16 | |
17 | |
18 namespace dart { | |
19 namespace bin { | |
20 | |
21 intptr_t FileSystemWatcher::Init() { | |
22 return 0; | |
23 } | |
24 | |
25 | |
26 void FileSystemWatcher::Stop(intptr_t id) { | |
27 // Nothing to do. | |
28 } | |
29 | |
30 | |
31 intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) { | |
32 return path_id; | |
33 } | |
34 | |
35 | |
36 intptr_t FileSystemWatcher::AddPath(intptr_t id, | |
37 const char* path, | |
38 int events, | |
39 bool recursive) { | |
40 const wchar_t* name = StringUtils::Utf8ToWide(path); | |
41 HANDLE dir = CreateFileW(name, | |
42 FILE_LIST_DIRECTORY, | |
43 FILE_SHARE_READ | | |
44 FILE_SHARE_WRITE | | |
45 FILE_SHARE_DELETE, | |
46 NULL, | |
47 OPEN_EXISTING, | |
48 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, | |
49 NULL); | |
50 free(const_cast<wchar_t*>(name)); | |
51 | |
52 if (dir == INVALID_HANDLE_VALUE) { | |
53 return 0; | |
54 } | |
55 | |
56 int list_events = 0; | |
57 if (events & (kCreate | kMove | kDelete)) { | |
Søren Gjesse
2013/08/23 07:47:45
Maybe
list_events |= FILE_NOTIFY_CHANGE_FILE_NAME
| |
58 list_events |= FILE_NOTIFY_CHANGE_FILE_NAME | | |
59 FILE_NOTIFY_CHANGE_DIR_NAME; | |
60 } | |
61 if (events & kModifyContent) list_events |= FILE_NOTIFY_CHANGE_LAST_WRITE; | |
62 | |
63 return reinterpret_cast<intptr_t>( | |
64 new DirectoryWatchHandle(dir, list_events, recursive)); | |
65 } | |
66 | |
67 | |
68 bool FileSystemWatcher::RemovePath(intptr_t id, intptr_t path_id) { | |
69 return true; | |
70 } | |
71 | |
72 intptr_t FileSystemWatcher::ReadEvents(intptr_t id, | |
73 intptr_t path_id, | |
74 Event** events) { | |
75 const intptr_t kEventSize = sizeof(FILE_NOTIFY_INFORMATION); | |
76 DirectoryWatchHandle* dir = reinterpret_cast<DirectoryWatchHandle*>(path_id); | |
77 intptr_t available = dir->Available(); | |
78 intptr_t max_count = available / kEventSize + 1; | |
79 *events = new Event[max_count]; | |
80 uint8_t* buffer = new uint8_t[available]; | |
81 intptr_t bytes = dir->Read(buffer, available); | |
82 intptr_t offset = 0; | |
83 intptr_t i = 0; | |
84 while (offset < bytes) { | |
85 FILE_NOTIFY_INFORMATION* e = | |
86 reinterpret_cast<FILE_NOTIFY_INFORMATION*>(buffer + offset); | |
87 | |
88 Event* event = *events + i; | |
89 event->path_id = path_id; | |
90 event->link = 1; // Move events come in pairs. Just 'enable' by default. | |
91 event->event = 0; | |
92 if (e->Action == FILE_ACTION_ADDED) event->event |= kCreate; | |
93 if (e->Action == FILE_ACTION_REMOVED) event->event |= kDelete; | |
94 if (e->Action == FILE_ACTION_MODIFIED) event->event |= kModifyContent; | |
95 if (e->Action == FILE_ACTION_RENAMED_OLD_NAME) event->event |= kMove; | |
96 if (e->Action == FILE_ACTION_RENAMED_NEW_NAME) event->event |= kMove; | |
97 // Copy to include tailing \0. | |
98 intptr_t len = e->FileNameLength; | |
99 wchar_t* name = new wchar_t[len / 2 + 1]; | |
100 memcpy(name, e->FileName, len + 1); | |
101 name[len / 2] = 0; | |
102 event->filename = StringUtils::WideToUtf8(name); | |
103 delete[] name; | |
104 | |
105 i++; | |
106 if (e->NextEntryOffset == 0) break; | |
107 offset += e->NextEntryOffset; | |
108 } | |
109 delete[] buffer; | |
110 return i; | |
111 } | |
112 | |
113 } // namespace bin | |
114 } // namespace dart | |
115 | |
116 #endif // defined(TARGET_OS_WINDOWS) | |
OLD | NEW |