InputFile.cpp revision 5b948190e3b311d06526addbb8f0e77a76da2467
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
17#include "bcc/Support/InputFile.h"
18
19#include "bcc/Support/DebugHelper.h"
20
21using namespace bcc;
22
23InputFile::InputFile(const std::string &pFilename, unsigned pFlags)
24  : super(pFilename, pFlags) { }
25
26ssize_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.
33    ALOGW("InputFile::read: count = %zu, buffer = %p", count, pBuf);
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}
54