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/core/test/test_utils.h"
6
7 #include <fcntl.h>
8 #include <io.h>
9 #include <stddef.h>
10 #include <string.h>
11 #include <windows.h>
12
13 namespace mojo {
14 namespace core {
15 namespace test {
16
PlatformHandleFromFILE(base::ScopedFILE fp)17 PlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) {
18 CHECK(fp);
19
20 HANDLE rv = INVALID_HANDLE_VALUE;
21 PCHECK(DuplicateHandle(
22 GetCurrentProcess(),
23 reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fp.get()))),
24 GetCurrentProcess(), &rv, 0, TRUE, DUPLICATE_SAME_ACCESS))
25 << "DuplicateHandle";
26 return PlatformHandle(base::win::ScopedHandle(rv));
27 }
28
FILEFromPlatformHandle(PlatformHandle h,const char * mode)29 base::ScopedFILE FILEFromPlatformHandle(PlatformHandle h, const char* mode) {
30 CHECK(h.is_valid());
31 // Microsoft's documentation for |_open_osfhandle()| only discusses these
32 // flags (and |_O_WTEXT|). Hmmm.
33 int flags = 0;
34 if (strchr(mode, 'a'))
35 flags |= _O_APPEND;
36 if (strchr(mode, 'r'))
37 flags |= _O_RDONLY;
38 if (strchr(mode, 't'))
39 flags |= _O_TEXT;
40 base::ScopedFILE rv(_fdopen(
41 _open_osfhandle(reinterpret_cast<intptr_t>(h.ReleaseHandle()), flags),
42 mode));
43 PCHECK(rv) << "_fdopen";
44 return rv;
45 }
46
47 } // namespace test
48 } // namespace core
49 } // namespace mojo
50