OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
7 | 7 |
| 8 #include "bin/fdutils.h" |
| 9 |
8 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
9 #include <fcntl.h> // NOLINT | 11 #include <fcntl.h> // NOLINT |
| 12 #include <sys/ioctl.h> // NOLINT |
10 #include <unistd.h> // NOLINT | 13 #include <unistd.h> // NOLINT |
11 #include <sys/ioctl.h> // NOLINT | |
12 | |
13 #include "bin/fdutils.h" | |
14 | 14 |
15 #include "platform/signal_blocker.h" | 15 #include "platform/signal_blocker.h" |
16 | 16 |
17 | |
18 namespace dart { | 17 namespace dart { |
19 namespace bin { | 18 namespace bin { |
20 | 19 |
21 bool FDUtils::SetCloseOnExec(intptr_t fd) { | 20 bool FDUtils::SetCloseOnExec(intptr_t fd) { |
22 intptr_t status; | 21 intptr_t status; |
23 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFD)); | 22 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFD)); |
24 if (status < 0) { | 23 if (status < 0) { |
25 return false; | 24 return false; |
26 } | 25 } |
27 status |= FD_CLOEXEC; | 26 status |= FD_CLOEXEC; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 return true; | 65 return true; |
67 } | 66 } |
68 | 67 |
69 | 68 |
70 intptr_t FDUtils::AvailableBytes(intptr_t fd) { | 69 intptr_t FDUtils::AvailableBytes(intptr_t fd) { |
71 int available; // ioctl for FIONREAD expects an 'int*' argument. | 70 int available; // ioctl for FIONREAD expects an 'int*' argument. |
72 int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); | 71 int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); |
73 if (result < 0) { | 72 if (result < 0) { |
74 return result; | 73 return result; |
75 } | 74 } |
76 #ifdef DEBUG | |
77 ASSERT(available >= 0); | 75 ASSERT(available >= 0); |
78 #endif | |
79 return static_cast<intptr_t>(available); | 76 return static_cast<intptr_t>(available); |
80 } | 77 } |
81 | 78 |
82 | 79 |
83 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { | 80 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { |
84 #ifdef DEBUG | 81 #ifdef DEBUG |
85 bool is_blocking = false; | 82 bool is_blocking = false; |
86 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); | 83 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
87 ASSERT(is_blocking); | 84 ASSERT(is_blocking); |
88 #endif | 85 #endif |
89 size_t remaining = count; | 86 size_t remaining = count; |
90 char* buffer_pos = reinterpret_cast<char*>(buffer); | 87 char* buffer_pos = reinterpret_cast<char*>(buffer); |
91 while (remaining > 0) { | 88 while (remaining > 0) { |
92 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer_pos, remaining)); | 89 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer_pos, remaining)); |
93 if (bytes_read == 0) { | 90 if (bytes_read == 0) { |
94 return count - remaining; | 91 return count - remaining; |
95 } else if (bytes_read == -1) { | 92 } else if (bytes_read == -1) { |
96 ASSERT(EAGAIN == EWOULDBLOCK); | 93 ASSERT(EAGAIN == EWOULDBLOCK); |
97 // Error code EWOULDBLOCK should only happen for non blocking | 94 // Error code EWOULDBLOCK should only happen for non blocking |
98 // file descriptors. | 95 // file descriptors. |
99 ASSERT(errno != EWOULDBLOCK); | 96 ASSERT(errno != EWOULDBLOCK); |
100 return -1; | 97 return -1; |
101 } else { | 98 } else { |
102 ASSERT((bytes_read > 0)); | 99 ASSERT(bytes_read > 0); |
103 remaining -= bytes_read; | 100 remaining -= bytes_read; |
104 buffer_pos += bytes_read; | 101 buffer_pos += bytes_read; |
105 } | 102 } |
106 } | 103 } |
107 return count; | 104 return count; |
108 } | 105 } |
109 | 106 |
110 | 107 |
111 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { | 108 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { |
112 #ifdef DEBUG | 109 #ifdef DEBUG |
(...skipping 20 matching lines...) Expand all Loading... |
133 buffer_pos += bytes_written; | 130 buffer_pos += bytes_written; |
134 } | 131 } |
135 } | 132 } |
136 return count; | 133 return count; |
137 } | 134 } |
138 | 135 |
139 } // namespace bin | 136 } // namespace bin |
140 } // namespace dart | 137 } // namespace dart |
141 | 138 |
142 #endif // defined(TARGET_OS_MACOS) | 139 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |