InputFile.cpp revision c72c4ddfcd79c74f70713da91a69569451b5c19e
14f94c520f8d699a5973956a1716272146be17128Zonr Chang/* 24f94c520f8d699a5973956a1716272146be17128Zonr Chang * Copyright 2012, The Android Open Source Project 34f94c520f8d699a5973956a1716272146be17128Zonr Chang * 44f94c520f8d699a5973956a1716272146be17128Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License"); 54f94c520f8d699a5973956a1716272146be17128Zonr Chang * you may not use this file except in compliance with the License. 64f94c520f8d699a5973956a1716272146be17128Zonr Chang * You may obtain a copy of the License at 74f94c520f8d699a5973956a1716272146be17128Zonr Chang * 84f94c520f8d699a5973956a1716272146be17128Zonr Chang * http://www.apache.org/licenses/LICENSE-2.0 94f94c520f8d699a5973956a1716272146be17128Zonr Chang * 104f94c520f8d699a5973956a1716272146be17128Zonr Chang * Unless required by applicable law or agreed to in writing, software 114f94c520f8d699a5973956a1716272146be17128Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS, 124f94c520f8d699a5973956a1716272146be17128Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134f94c520f8d699a5973956a1716272146be17128Zonr Chang * See the License for the specific language governing permissions and 144f94c520f8d699a5973956a1716272146be17128Zonr Chang * limitations under the License. 154f94c520f8d699a5973956a1716272146be17128Zonr Chang */ 164f94c520f8d699a5973956a1716272146be17128Zonr Chang 17c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/Support/InputFile.h" 184f94c520f8d699a5973956a1716272146be17128Zonr Chang 19c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/Support/DebugHelper.h" 204f94c520f8d699a5973956a1716272146be17128Zonr Chang 214f94c520f8d699a5973956a1716272146be17128Zonr Changusing namespace bcc; 224f94c520f8d699a5973956a1716272146be17128Zonr Chang 234f94c520f8d699a5973956a1716272146be17128Zonr ChangInputFile::InputFile(const std::string &pFilename, unsigned pFlags) 244f94c520f8d699a5973956a1716272146be17128Zonr Chang : super(pFilename, pFlags) { } 254f94c520f8d699a5973956a1716272146be17128Zonr Chang 264f94c520f8d699a5973956a1716272146be17128Zonr Changssize_t InputFile::read(void *pBuf, size_t count) { 274f94c520f8d699a5973956a1716272146be17128Zonr Chang if ((mFD < 0) || hasError()) { 284f94c520f8d699a5973956a1716272146be17128Zonr Chang return -1; 294f94c520f8d699a5973956a1716272146be17128Zonr Chang } 304f94c520f8d699a5973956a1716272146be17128Zonr Chang 314f94c520f8d699a5973956a1716272146be17128Zonr Chang if ((count <= 0) || (pBuf == NULL)) { 324f94c520f8d699a5973956a1716272146be17128Zonr Chang // Keep safe and issue a warning. 33d14994d0c77dbd688d6771f5283554aa0ad55808Shih-wei Liao ALOGW("InputFile::read: count = %zu, buffer = %p", count, pBuf); 344f94c520f8d699a5973956a1716272146be17128Zonr Chang return 0; 354f94c520f8d699a5973956a1716272146be17128Zonr Chang } 364f94c520f8d699a5973956a1716272146be17128Zonr Chang 374f94c520f8d699a5973956a1716272146be17128Zonr Chang while (count > 0) { 384f94c520f8d699a5973956a1716272146be17128Zonr Chang ssize_t read_size = ::read(mFD, pBuf, count); 394f94c520f8d699a5973956a1716272146be17128Zonr Chang 404f94c520f8d699a5973956a1716272146be17128Zonr Chang if (read_size >= 0) { 414f94c520f8d699a5973956a1716272146be17128Zonr Chang return read_size; 424f94c520f8d699a5973956a1716272146be17128Zonr Chang } else if ((errno == EAGAIN) || (errno == EINTR)) { 434f94c520f8d699a5973956a1716272146be17128Zonr Chang // If the errno is EAGAIN or EINTR, then we try to read again. 444f94c520f8d699a5973956a1716272146be17128Zonr Chang // 454f94c520f8d699a5973956a1716272146be17128Zonr Chang // Fall-through 464f94c520f8d699a5973956a1716272146be17128Zonr Chang } else { 474f94c520f8d699a5973956a1716272146be17128Zonr Chang detectError(); 484f94c520f8d699a5973956a1716272146be17128Zonr Chang return -1; 494f94c520f8d699a5973956a1716272146be17128Zonr Chang } 504f94c520f8d699a5973956a1716272146be17128Zonr Chang } 514f94c520f8d699a5973956a1716272146be17128Zonr Chang // unreachable 524f94c520f8d699a5973956a1716272146be17128Zonr Chang return 0; 534f94c520f8d699a5973956a1716272146be17128Zonr Chang} 54