1 //
2 // Copyright (C) 2009 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 UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_
18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_
19 
20 #include <memory>
21 #include <utility>
22 
23 #include <base/logging.h>
24 #include <brillo/secure_blob.h>
25 
26 #include "update_engine/common/utils.h"
27 #include "update_engine/payload_consumer/file_descriptor.h"
28 #include "update_engine/update_metadata.pb.h"
29 
30 // ExtentWriter is an abstract class which synchronously writes to a given
31 // file descriptor at the extents given.
32 
33 namespace chromeos_update_engine {
34 
35 class ExtentWriter {
36  public:
37   ExtentWriter() = default;
38   virtual ~ExtentWriter() = default;
39 
40   // Returns true on success.
41   virtual bool Init(const google::protobuf::RepeatedPtrField<Extent>& extents,
42                     uint32_t block_size) = 0;
43 
44   // Returns true on success.
45   virtual bool Write(const void* bytes, size_t count) = 0;
46 };
47 
48 // DirectExtentWriter is probably the simplest ExtentWriter implementation.
49 // It writes the data directly into the extents.
50 
51 class DirectExtentWriter : public ExtentWriter {
52  public:
DirectExtentWriter(FileDescriptorPtr fd)53   explicit DirectExtentWriter(FileDescriptorPtr fd) : fd_(fd) {}
54   ~DirectExtentWriter() override = default;
55 
Init(const google::protobuf::RepeatedPtrField<Extent> & extents,uint32_t block_size)56   bool Init(const google::protobuf::RepeatedPtrField<Extent>& extents,
57             uint32_t block_size) override {
58     block_size_ = block_size;
59     extents_ = extents;
60     cur_extent_ = extents_.begin();
61     return true;
62   }
63   bool Write(const void* bytes, size_t count) override;
64 
65  private:
66   FileDescriptorPtr fd_{nullptr};
67 
68   size_t block_size_{0};
69   // Bytes written into |cur_extent_| thus far.
70   uint64_t extent_bytes_written_{0};
71   google::protobuf::RepeatedPtrField<Extent> extents_;
72   // The next call to write should correspond to |cur_extents_|.
73   google::protobuf::RepeatedPtrField<Extent>::iterator cur_extent_;
74 };
75 
76 }  // namespace chromeos_update_engine
77 
78 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_
79