Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_ |
| 18 | #define ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_ |
| 19 | |
| 20 | #include "output_stream.h" |
| 21 | |
| 22 | #include "base/logging.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | // OutputStream wrapper that delays reporting an error until Flush(). |
| 27 | class ErrorDelayingOutputStream FINAL : public OutputStream { |
| 28 | public: |
| 29 | explicit ErrorDelayingOutputStream(OutputStream* output) |
| 30 | : OutputStream(output->GetLocation()), |
| 31 | output_(output), |
| 32 | output_good_(true), |
| 33 | output_offset_(0) { } |
| 34 | |
| 35 | // This function always succeeds to simplify code. |
| 36 | // Use Good() to check the actual status of the output stream. |
| 37 | bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE { |
| 38 | if (output_good_) { |
| 39 | if (!output_->WriteFully(buffer, byte_count)) { |
| 40 | PLOG(ERROR) << "Failed to write " << byte_count |
| 41 | << " bytes to " << GetLocation() << " at offset " << output_offset_; |
| 42 | output_good_ = false; |
| 43 | } |
| 44 | } |
| 45 | output_offset_ += byte_count; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | // This function always succeeds to simplify code. |
| 50 | // Use Good() to check the actual status of the output stream. |
| 51 | off_t Seek(off_t offset, Whence whence) OVERRIDE { |
| 52 | // We keep shadow copy of the offset so that we return |
| 53 | // the expected value even if the output stream failed. |
| 54 | off_t new_offset; |
| 55 | switch (whence) { |
| 56 | case kSeekSet: |
| 57 | new_offset = offset; |
| 58 | break; |
| 59 | case kSeekCurrent: |
| 60 | new_offset = output_offset_ + offset; |
| 61 | break; |
| 62 | default: |
| 63 | LOG(FATAL) << "Unsupported seek type: " << whence; |
| 64 | UNREACHABLE(); |
| 65 | } |
| 66 | if (output_good_) { |
| 67 | off_t actual_offset = output_->Seek(offset, whence); |
| 68 | if (actual_offset == static_cast<off_t>(-1)) { |
| 69 | PLOG(ERROR) << "Failed to seek in " << GetLocation() << ". Offset=" << offset |
| 70 | << " whence=" << whence << " new_offset=" << new_offset; |
| 71 | output_good_ = false; |
| 72 | } |
| 73 | DCHECK_EQ(actual_offset, new_offset); |
| 74 | } |
| 75 | output_offset_ = new_offset; |
| 76 | return new_offset; |
| 77 | } |
| 78 | |
| 79 | // Flush the output and return whether all operations have succeeded. |
| 80 | // Do nothing if we already have a pending error. |
| 81 | bool Flush() OVERRIDE { |
| 82 | if (output_good_) { |
| 83 | output_good_ = output_->Flush(); |
| 84 | } |
| 85 | return output_good_; |
| 86 | } |
| 87 | |
| 88 | // Check (without flushing) whether all operations have succeeded so far. |
| 89 | bool Good() const { |
| 90 | return output_good_; |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | OutputStream* output_; |
| 95 | bool output_good_; // True if all writes to output succeeded. |
| 96 | off_t output_offset_; // Keep track of the current position in the stream. |
| 97 | }; |
| 98 | |
| 99 | } // namespace art |
| 100 | |
| 101 | #endif // ART_COMPILER_LINKER_ERROR_DELAYING_OUTPUT_STREAM_H_ |