| 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef RUNTIME_BIN_FDUTILS_H_ |
| 6 | #define RUNTIME_BIN_FDUTILS_H_ |
| 7 | |
| 8 | #include "bin/builtin.h" |
| 9 | #include "platform/globals.h" |
| 10 | |
| 11 | namespace dart { |
| 12 | namespace bin { |
| 13 | |
| 14 | class FDUtils { |
| 15 | public: |
| 16 | static bool SetCloseOnExec(intptr_t fd); |
| 17 | |
| 18 | static bool SetNonBlocking(intptr_t fd); |
| 19 | static bool SetBlocking(intptr_t fd); |
| 20 | |
| 21 | // Checks whether the file descriptor is blocking. If the function |
| 22 | // returns true the value pointed to by is_blocking will be set to |
| 23 | // the blocking state of the file descriptor. If the function |
| 24 | // returns false the system call for checking the file descriptor |
| 25 | // failed and the value pointed to by is_blocking is not modified. |
| 26 | static bool IsBlocking(intptr_t fd, bool* is_blocking); |
| 27 | |
| 28 | static intptr_t AvailableBytes(intptr_t fd); |
| 29 | |
| 30 | // Reads the requested number of bytes from a file descriptor. This |
| 31 | // function will only return on short reads if an error occours in |
| 32 | // which case it returns -1 and errno is still valid. The file |
| 33 | // descriptor must be in blocking mode. |
| 34 | static ssize_t ReadFromBlocking(int fd, void* buffer, size_t count); |
| 35 | |
| 36 | // Writes the requested number of bytes to a file descriptor. This |
| 37 | // function will only return on short writes if an error occours in |
| 38 | // which case it returns -1 and errno is still valid. The file |
| 39 | // descriptor must be in blocking mode. |
| 40 | static ssize_t WriteToBlocking(int fd, const void* buffer, size_t count); |
| 41 | |
| 42 | // Closes fd without modifying errno. |
| 43 | static void SaveErrorAndClose(intptr_t fd); |
| 44 | |
| 45 | private: |
| 46 | DISALLOW_ALLOCATION(); |
| 47 | DISALLOW_IMPLICIT_CONSTRUCTORS(FDUtils); |
| 48 | }; |
| 49 | |
| 50 | } // namespace bin |
| 51 | } // namespace dart |
| 52 | |
| 53 | #endif // RUNTIME_BIN_FDUTILS_H_ |
| 54 | |