Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012, 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 | |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame^] | 17 | #include "InputFile.h" |
Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame] | 18 | |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame^] | 19 | #include "DebugHelper.h" |
Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame] | 20 | |
| 21 | using namespace bcc; |
| 22 | |
| 23 | InputFile::InputFile(const std::string &pFilename, unsigned pFlags) |
| 24 | : super(pFilename, pFlags) { } |
| 25 | |
| 26 | ssize_t InputFile::read(void *pBuf, size_t count) { |
| 27 | if ((mFD < 0) || hasError()) { |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | if ((count <= 0) || (pBuf == NULL)) { |
| 32 | // Keep safe and issue a warning. |
Shih-wei Liao | cff7ac2 | 2012-04-25 05:54:25 -0700 | [diff] [blame] | 33 | ALOGW("InputFile::read: count = %zu, buffer = %p", count, pBuf); |
Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | while (count > 0) { |
| 38 | ssize_t read_size = ::read(mFD, pBuf, count); |
| 39 | |
| 40 | if (read_size >= 0) { |
| 41 | return read_size; |
| 42 | } else if ((errno == EAGAIN) || (errno == EINTR)) { |
| 43 | // If the errno is EAGAIN or EINTR, then we try to read again. |
| 44 | // |
| 45 | // Fall-through |
| 46 | } else { |
| 47 | detectError(); |
| 48 | return -1; |
| 49 | } |
| 50 | } |
| 51 | // unreachable |
| 52 | return 0; |
| 53 | } |