| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/edk/test/test_utils.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 #include <fcntl.h> | |
| 9 #include <io.h> | |
| 10 #include <string.h> | |
| 11 | |
| 12 namespace mojo { | |
| 13 namespace test { | |
| 14 | |
| 15 bool BlockingWrite(const embedder::PlatformHandle& handle, | |
| 16 const void* buffer, | |
| 17 size_t bytes_to_write, | |
| 18 size_t* bytes_written) { | |
| 19 OVERLAPPED overlapped = {0}; | |
| 20 DWORD bytes_written_dword = 0; | |
| 21 | |
| 22 if (!WriteFile(handle.handle, buffer, static_cast<DWORD>(bytes_to_write), | |
| 23 &bytes_written_dword, &overlapped)) { | |
| 24 if (GetLastError() != ERROR_IO_PENDING || | |
| 25 !GetOverlappedResult(handle.handle, &overlapped, &bytes_written_dword, | |
| 26 TRUE)) { | |
| 27 return false; | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 *bytes_written = bytes_written_dword; | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 bool BlockingRead(const embedder::PlatformHandle& handle, | |
| 36 void* buffer, | |
| 37 size_t buffer_size, | |
| 38 size_t* bytes_read) { | |
| 39 OVERLAPPED overlapped = {0}; | |
| 40 DWORD bytes_read_dword = 0; | |
| 41 | |
| 42 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), | |
| 43 &bytes_read_dword, &overlapped)) { | |
| 44 if (GetLastError() != ERROR_IO_PENDING || | |
| 45 !GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, | |
| 46 TRUE)) { | |
| 47 return false; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 *bytes_read = bytes_read_dword; | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 bool NonBlockingRead(const embedder::PlatformHandle& handle, | |
| 56 void* buffer, | |
| 57 size_t buffer_size, | |
| 58 size_t* bytes_read) { | |
| 59 OVERLAPPED overlapped = {0}; | |
| 60 DWORD bytes_read_dword = 0; | |
| 61 | |
| 62 if (!ReadFile(handle.handle, buffer, static_cast<DWORD>(buffer_size), | |
| 63 &bytes_read_dword, &overlapped)) { | |
| 64 if (GetLastError() != ERROR_IO_PENDING) | |
| 65 return false; | |
| 66 | |
| 67 CancelIo(handle.handle); | |
| 68 | |
| 69 if (!GetOverlappedResult(handle.handle, &overlapped, &bytes_read_dword, | |
| 70 TRUE)) { | |
| 71 *bytes_read = 0; | |
| 72 return true; | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 *bytes_read = bytes_read_dword; | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 embedder::ScopedPlatformHandle PlatformHandleFromFILE(util::ScopedFILE fp) { | |
| 81 CHECK(fp); | |
| 82 | |
| 83 HANDLE rv = INVALID_HANDLE_VALUE; | |
| 84 PCHECK(DuplicateHandle( | |
| 85 GetCurrentProcess(), | |
| 86 reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp.get()))), | |
| 87 GetCurrentProcess(), &rv, 0, TRUE, DUPLICATE_SAME_ACCESS)) | |
| 88 << "DuplicateHandle"; | |
| 89 return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv)); | |
| 90 } | |
| 91 | |
| 92 util::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, | |
| 93 const char* mode) { | |
| 94 CHECK(h.is_valid()); | |
| 95 // Microsoft's documentation for |_open_osfhandle()| only discusses these | |
| 96 // flags (and |_O_WTEXT|). Hmmm. | |
| 97 int flags = 0; | |
| 98 if (strchr(mode, 'a')) | |
| 99 flags |= _O_APPEND; | |
| 100 if (strchr(mode, 'r')) | |
| 101 flags |= _O_RDONLY; | |
| 102 if (strchr(mode, 't')) | |
| 103 flags |= _O_TEXT; | |
| 104 util::ScopedFILE rv(_fdopen( | |
| 105 _open_osfhandle(reinterpret_cast<intptr_t>(h.release().handle), flags), | |
| 106 mode)); | |
| 107 PCHECK(rv) << "_fdopen"; | |
| 108 return rv.Pass(); | |
| 109 } | |
| 110 | |
| 111 } // namespace test | |
| 112 } // namespace mojo | |
| OLD | NEW |