OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Some common file utilities for plugin code. | 5 // Some common file utilities for plugin code. |
6 | 6 |
7 #include "ppapi/native_client/src/trusted/plugin/file_utils.h" | 7 #include "ppapi/native_client/src/trusted/plugin/file_utils.h" |
8 | 8 |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 | 12 |
13 #include <sys/stat.h> | 13 #include <sys/stat.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
15 | 15 |
16 #include "native_client/src/include/nacl_scoped_ptr.h" | 16 #include "native_client/src/include/nacl_scoped_ptr.h" |
17 #include "native_client/src/include/portability_io.h" | 17 #include "native_client/src/include/portability_io.h" |
18 #include "native_client/src/include/portability_string.h" | 18 #include "native_client/src/include/portability_string.h" |
| 19 #include "ppapi/native_client/src/trusted/plugin/utility.h" |
19 | 20 |
20 | 21 |
21 namespace plugin { | 22 namespace plugin { |
22 namespace file_utils { | 23 namespace file_utils { |
23 | 24 |
24 StatusCode SlurpFile(int32_t fd, | 25 StatusCode SlurpFile(int32_t fd, |
25 nacl::string& out_buf, | 26 nacl::string& out_buf, |
26 size_t max_size_to_read) { | 27 size_t max_size_to_read) { |
27 struct stat stat_buf; | 28 struct stat stat_buf; |
28 if (fstat(fd, &stat_buf) != 0) { | 29 if (fstat(fd, &stat_buf) != 0) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 total_bytes_read += bytes_this_read; | 66 total_bytes_read += bytes_this_read; |
66 bytes_to_read -= bytes_this_read; | 67 bytes_to_read -= bytes_this_read; |
67 } | 68 } |
68 | 69 |
69 fclose(input_file); | 70 fclose(input_file); |
70 buffer[total_bytes_read] = '\0'; | 71 buffer[total_bytes_read] = '\0'; |
71 out_buf = buffer.get(); | 72 out_buf = buffer.get(); |
72 return PLUGIN_FILE_SUCCESS; | 73 return PLUGIN_FILE_SUCCESS; |
73 } | 74 } |
74 | 75 |
| 76 // Converts a PP_FileHandle to a POSIX file descriptor. |
| 77 int32_t ConvertFileDescriptor(PP_FileHandle handle) { |
| 78 PLUGIN_PRINTF(("ConvertFileDescriptor, handle=%d\n", handle)); |
| 79 #if NACL_WINDOWS |
| 80 int32_t file_desc = NACL_NO_FILE_DESC; |
| 81 // On Windows, valid handles are 32 bit unsigned integers so this is safe. |
| 82 file_desc = reinterpret_cast<uintptr_t>(handle); |
| 83 // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. |
| 84 int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); |
| 85 if (posix_desc == -1) { |
| 86 // Close the Windows HANDLE if it can't be converted. |
| 87 CloseHandle(reinterpret_cast<HANDLE>(file_desc)); |
| 88 return -1; |
| 89 } |
| 90 return posix_desc; |
| 91 #else |
| 92 return handle; |
| 93 #endif |
| 94 } |
| 95 |
75 } // namespace file_utils | 96 } // namespace file_utils |
76 } // namespace plugin | 97 } // namespace plugin |
77 | |
OLD | NEW |