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