| 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_ANDROID) | 6 #if defined(TARGET_OS_FUCHSIA) |
| 7 | 7 |
| 8 #include "bin/fdutils.h" | 8 #include "bin/fdutils.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| 11 #include <fcntl.h> // NOLINT | 11 #include <fcntl.h> // NOLINT |
| 12 #include <sys/ioctl.h> // NOLINT | 12 #include <sys/ioctl.h> // NOLINT |
| 13 #include <unistd.h> // NOLINT | 13 #include <unistd.h> // NOLINT |
| 14 | 14 |
| 15 #include "platform/signal_blocker.h" | 15 #include "platform/signal_blocker.h" |
| 16 | 16 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); | 64 status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFL)); |
| 65 if (status < 0) { | 65 if (status < 0) { |
| 66 return false; | 66 return false; |
| 67 } | 67 } |
| 68 *is_blocking = (status & O_NONBLOCK) == 0; | 68 *is_blocking = (status & O_NONBLOCK) == 0; |
| 69 return true; | 69 return true; |
| 70 } | 70 } |
| 71 | 71 |
| 72 | 72 |
| 73 intptr_t FDUtils::AvailableBytes(intptr_t fd) { | 73 intptr_t FDUtils::AvailableBytes(intptr_t fd) { |
| 74 // TODO(MG-364): Enable this code when it is supported. |
| 75 #if 0 |
| 74 int available; // ioctl for FIONREAD expects an 'int*' argument. | 76 int available; // ioctl for FIONREAD expects an 'int*' argument. |
| 75 int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); | 77 int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); |
| 76 if (result < 0) { | 78 if (result < 0) { |
| 77 return result; | 79 return result; |
| 78 } | 80 } |
| 79 ASSERT(available >= 0); | 81 ASSERT(available >= 0); |
| 80 return static_cast<intptr_t>(available); | 82 return static_cast<intptr_t>(available); |
| 83 #endif |
| 84 errno = ENOTSUP; |
| 85 return -1; |
| 81 } | 86 } |
| 82 | 87 |
| 83 | 88 |
| 84 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { | 89 ssize_t FDUtils::ReadFromBlocking(int fd, void* buffer, size_t count) { |
| 85 #ifdef DEBUG | 90 #ifdef DEBUG |
| 86 bool is_blocking = false; | 91 bool is_blocking = false; |
| 87 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); | 92 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 88 ASSERT(is_blocking); | 93 ASSERT(is_blocking); |
| 89 #endif | 94 #endif |
| 90 size_t remaining = count; | 95 size_t remaining = count; |
| 91 char* buffer_pos = reinterpret_cast<char*>(buffer); | 96 char* buffer_pos = reinterpret_cast<char*>(buffer); |
| 92 while (remaining > 0) { | 97 while (remaining > 0) { |
| 93 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer_pos, remaining)); | 98 ssize_t bytes_read = NO_RETRY_EXPECTED(read(fd, buffer_pos, remaining)); |
| 94 if (bytes_read == 0) { | 99 if (bytes_read == 0) { |
| 95 return count - remaining; | 100 return count - remaining; |
| 96 } else if (bytes_read == -1) { | 101 } else if (bytes_read == -1) { |
| 97 ASSERT(EAGAIN == EWOULDBLOCK); | 102 ASSERT(EAGAIN == EWOULDBLOCK); |
| 98 // Error code EWOULDBLOCK should only happen for non blocking | 103 // Error code EWOULDBLOCK should only happen for non blocking |
| 99 // file descriptors. | 104 // file descriptors. |
| 100 ASSERT(errno != EWOULDBLOCK); | 105 ASSERT(errno != EWOULDBLOCK); |
| 101 return -1; | 106 return -1; |
| 102 } else { | 107 } else { |
| 103 ASSERT(bytes_read > 0); | 108 ASSERT(bytes_read > 0); |
| 104 remaining -= bytes_read; | 109 remaining -= bytes_read; |
| 105 buffer_pos += bytes_read; | 110 buffer_pos += bytes_read; |
| 106 } | 111 } |
| 107 } | 112 } |
| 108 return count; | 113 return count; |
| 109 } | 114 } |
| 110 | 115 |
| 111 | 116 |
| 112 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { | 117 ssize_t FDUtils::WriteToBlocking(int fd, const void* buffer, size_t count) { |
| 113 #ifdef DEBUG | 118 #ifdef DEBUG |
| 114 bool is_blocking = false; | 119 bool is_blocking = false; |
| 115 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); | 120 ASSERT(FDUtils::IsBlocking(fd, &is_blocking)); |
| 116 ASSERT(is_blocking); | 121 ASSERT(is_blocking); |
| 117 #endif | 122 #endif |
| 118 size_t remaining = count; | 123 size_t remaining = count; |
| 119 char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer)); | 124 char* buffer_pos = const_cast<char*>(reinterpret_cast<const char*>(buffer)); |
| 120 while (remaining > 0) { | 125 while (remaining > 0) { |
| 121 ssize_t bytes_written = | 126 ssize_t bytes_written = NO_RETRY_EXPECTED(write(fd, buffer_pos, remaining)); |
| 122 TEMP_FAILURE_RETRY(write(fd, buffer_pos, remaining)); | |
| 123 if (bytes_written == 0) { | 127 if (bytes_written == 0) { |
| 124 return count - remaining; | 128 return count - remaining; |
| 125 } else if (bytes_written == -1) { | 129 } else if (bytes_written == -1) { |
| 126 ASSERT(EAGAIN == EWOULDBLOCK); | 130 ASSERT(EAGAIN == EWOULDBLOCK); |
| 127 // Error code EWOULDBLOCK should only happen for non blocking | 131 // Error code EWOULDBLOCK should only happen for non blocking |
| 128 // file descriptors. | 132 // file descriptors. |
| 129 ASSERT(errno != EWOULDBLOCK); | 133 ASSERT(errno != EWOULDBLOCK); |
| 130 return -1; | 134 return -1; |
| 131 } else { | 135 } else { |
| 132 ASSERT(bytes_written > 0); | 136 ASSERT(bytes_written > 0); |
| 133 remaining -= bytes_written; | 137 remaining -= bytes_written; |
| 134 buffer_pos += bytes_written; | 138 buffer_pos += bytes_written; |
| 135 } | 139 } |
| 136 } | 140 } |
| 137 return count; | 141 return count; |
| 138 } | 142 } |
| 139 | 143 |
| 144 |
| 145 void FDUtils::SaveErrorAndClose(intptr_t fd) { |
| 146 int err = errno; |
| 147 NO_RETRY_EXPECTED(close(fd)); |
| 148 errno = err; |
| 149 } |
| 150 |
| 140 } // namespace bin | 151 } // namespace bin |
| 141 } // namespace dart | 152 } // namespace dart |
| 142 | 153 |
| 143 #endif // defined(TARGET_OS_ANDROID) | 154 #endif // defined(TARGET_OS_FUCHSIA) |
| OLD | NEW |