| Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "io_util.h" |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | status_t write_all(int fd, uint8_t const* buf, size_t size) |
| 22 | { |
| 23 | while (size > 0) { |
| 24 | ssize_t amt = ::write(fd, buf, size); |
| 25 | if (amt < 0) { |
| 26 | return -errno; |
| 27 | } |
| 28 | size -= amt; |
| 29 | buf += amt; |
| 30 | } |
| 31 | return NO_ERROR; |
| 32 | } |
| 33 | |
| 34 | Fpipe::Fpipe() {} |
| 35 | |
| 36 | Fpipe::~Fpipe() { close(); } |
| 37 | |
| 38 | bool Fpipe::close() { return !(::close(mFds[0]) || ::close(mFds[1])); } |
| 39 | |
| 40 | bool Fpipe::init() { return pipe(mFds) != -1; } |
| 41 | |
| 42 | int Fpipe::readFd() const { return mFds[0]; } |
| 43 | |
| 44 | int Fpipe::writeFd() const { return mFds[1]; } |