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::WatchPath(const char* path, |
| 22 int events, |
| 23 bool recursive) { |
| 24 const wchar_t* name = StringUtils::Utf8ToWide(path); |
| 25 HANDLE dir = CreateFileW(name, |
| 26 FILE_LIST_DIRECTORY, |
| 27 FILE_SHARE_READ | |
| 28 FILE_SHARE_WRITE | |
| 29 FILE_SHARE_DELETE, |
| 30 NULL, |
| 31 OPEN_EXISTING, |
| 32 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, |
| 33 NULL); |
| 34 free(const_cast<wchar_t*>(name)); |
| 35 |
| 36 if (dir == INVALID_HANDLE_VALUE) { |
| 37 return -1; |
| 38 } |
| 39 |
| 40 int list_events = 0; |
| 41 if (events & (kCreate | kMove | kDelete)) { |
| 42 list_events |= FILE_NOTIFY_CHANGE_FILE_NAME | |
| 43 FILE_NOTIFY_CHANGE_DIR_NAME; |
| 44 } |
| 45 if (events & kModifyContent) list_events |= FILE_NOTIFY_CHANGE_LAST_WRITE; |
| 46 |
| 47 return reinterpret_cast<intptr_t>( |
| 48 new DirectoryWatchHandle(dir, list_events, recursive)); |
| 49 } |
| 50 |
| 51 |
| 52 void FileSystemWatcher::UnwatchPath(intptr_t id) { |
| 53 // Nothing to do. |
| 54 } |
| 55 |
| 56 |
| 57 intptr_t FileSystemWatcher::GetSocketId(intptr_t id) { |
| 58 return id; |
| 59 } |
| 60 |
| 61 |
| 62 Dart_Handle FileSystemWatcher::ReadEvents(intptr_t id) { |
| 63 const intptr_t kEventSize = sizeof(FILE_NOTIFY_INFORMATION); |
| 64 DirectoryWatchHandle* dir = reinterpret_cast<DirectoryWatchHandle*>(id); |
| 65 intptr_t available = dir->Available(); |
| 66 intptr_t max_count = available / kEventSize + 1; |
| 67 Dart_Handle events = Dart_NewList(max_count); |
| 68 uint8_t* buffer = new uint8_t[available]; |
| 69 intptr_t bytes = dir->Read(buffer, available); |
| 70 intptr_t offset = 0; |
| 71 intptr_t i = 0; |
| 72 while (offset < bytes) { |
| 73 FILE_NOTIFY_INFORMATION* e = |
| 74 reinterpret_cast<FILE_NOTIFY_INFORMATION*>(buffer + offset); |
| 75 |
| 76 Dart_Handle event = Dart_NewList(3); |
| 77 int mask = 0; |
| 78 if (e->Action == FILE_ACTION_ADDED) mask |= kCreate; |
| 79 if (e->Action == FILE_ACTION_REMOVED) mask |= kDelete; |
| 80 if (e->Action == FILE_ACTION_MODIFIED) mask |= kModifyContent; |
| 81 if (e->Action == FILE_ACTION_RENAMED_OLD_NAME) mask |= kMove; |
| 82 if (e->Action == FILE_ACTION_RENAMED_NEW_NAME) mask |= kMove; |
| 83 Dart_ListSetAt(event, 0, Dart_NewInteger(mask)); |
| 84 // Move events come in pairs. Just 'enable' by default. |
| 85 Dart_ListSetAt(event, 1, Dart_NewInteger(1)); |
| 86 Dart_ListSetAt(event, 2, Dart_NewStringFromUTF16( |
| 87 reinterpret_cast<uint16_t*>(e->FileName), e->FileNameLength / 2)); |
| 88 |
| 89 Dart_ListSetAt(events, i, event); |
| 90 i++; |
| 91 if (e->NextEntryOffset == 0) break; |
| 92 offset += e->NextEntryOffset; |
| 93 } |
| 94 delete[] buffer; |
| 95 return events; |
| 96 } |
| 97 |
| 98 } // namespace bin |
| 99 } // namespace dart |
| 100 |
| 101 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |