Chromium Code Reviews| Index: runtime/bin/file_system_watcher_win.cc |
| diff --git a/runtime/bin/file_system_watcher_win.cc b/runtime/bin/file_system_watcher_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5aaf90b4e01bdf72d98532ac979f2b75eeb4307a |
| --- /dev/null |
| +++ b/runtime/bin/file_system_watcher_win.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#include "platform/globals.h" |
| +#if defined(TARGET_OS_WINDOWS) |
| + |
| +#include "bin/file_system_watcher.h" |
| +#include "bin/eventhandler.h" |
| + |
| +#include <WinIoCtl.h> // NOLINT |
| + |
| +#include "bin/builtin.h" |
| +#include "bin/log.h" |
| +#include "bin/utils.h" |
| + |
| + |
| +namespace dart { |
| +namespace bin { |
| + |
| +intptr_t FileSystemWatcher::Init() { |
| + return 0; |
| +} |
| + |
| + |
| +void FileSystemWatcher::Stop(intptr_t id) { |
| + // Nothing to do. |
| +} |
| + |
| + |
| +intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) { |
| + return path_id; |
| +} |
| + |
| + |
| +intptr_t FileSystemWatcher::AddPath(intptr_t id, |
| + const char* path, |
| + int events, |
| + bool recursive) { |
| + const wchar_t* name = StringUtils::Utf8ToWide(path); |
| + HANDLE dir = CreateFileW(name, |
| + FILE_LIST_DIRECTORY, |
| + FILE_SHARE_READ | |
| + FILE_SHARE_WRITE | |
| + FILE_SHARE_DELETE, |
| + NULL, |
| + OPEN_EXISTING, |
| + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, |
| + NULL); |
| + free(const_cast<wchar_t*>(name)); |
| + |
| + if (dir == INVALID_HANDLE_VALUE) { |
| + return 0; |
| + } |
| + |
| + int list_events = 0; |
| + if (events & (kCreate | kMove | kDelete)) { |
|
Søren Gjesse
2013/08/23 07:47:45
Maybe
list_events |= FILE_NOTIFY_CHANGE_FILE_NAME
|
| + list_events |= FILE_NOTIFY_CHANGE_FILE_NAME | |
| + FILE_NOTIFY_CHANGE_DIR_NAME; |
| + } |
| + if (events & kModifyContent) list_events |= FILE_NOTIFY_CHANGE_LAST_WRITE; |
| + |
| + return reinterpret_cast<intptr_t>( |
| + new DirectoryWatchHandle(dir, list_events, recursive)); |
| +} |
| + |
| + |
| +bool FileSystemWatcher::RemovePath(intptr_t id, intptr_t path_id) { |
| + return true; |
| +} |
| + |
| +intptr_t FileSystemWatcher::ReadEvents(intptr_t id, |
| + intptr_t path_id, |
| + Event** events) { |
| + const intptr_t kEventSize = sizeof(FILE_NOTIFY_INFORMATION); |
| + DirectoryWatchHandle* dir = reinterpret_cast<DirectoryWatchHandle*>(path_id); |
| + intptr_t available = dir->Available(); |
| + intptr_t max_count = available / kEventSize + 1; |
| + *events = new Event[max_count]; |
| + uint8_t* buffer = new uint8_t[available]; |
| + intptr_t bytes = dir->Read(buffer, available); |
| + intptr_t offset = 0; |
| + intptr_t i = 0; |
| + while (offset < bytes) { |
| + FILE_NOTIFY_INFORMATION* e = |
| + reinterpret_cast<FILE_NOTIFY_INFORMATION*>(buffer + offset); |
| + |
| + Event* event = *events + i; |
| + event->path_id = path_id; |
| + event->link = 1; // Move events come in pairs. Just 'enable' by default. |
| + event->event = 0; |
| + if (e->Action == FILE_ACTION_ADDED) event->event |= kCreate; |
| + if (e->Action == FILE_ACTION_REMOVED) event->event |= kDelete; |
| + if (e->Action == FILE_ACTION_MODIFIED) event->event |= kModifyContent; |
| + if (e->Action == FILE_ACTION_RENAMED_OLD_NAME) event->event |= kMove; |
| + if (e->Action == FILE_ACTION_RENAMED_NEW_NAME) event->event |= kMove; |
| + // Copy to include tailing \0. |
| + intptr_t len = e->FileNameLength; |
| + wchar_t* name = new wchar_t[len / 2 + 1]; |
| + memcpy(name, e->FileName, len + 1); |
| + name[len / 2] = 0; |
| + event->filename = StringUtils::WideToUtf8(name); |
| + delete[] name; |
| + |
| + i++; |
| + if (e->NextEntryOffset == 0) break; |
| + offset += e->NextEntryOffset; |
| + } |
| + delete[] buffer; |
| + return i; |
| +} |
| + |
| +} // namespace bin |
| +} // namespace dart |
| + |
| +#endif // defined(TARGET_OS_WINDOWS) |