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