blob: 2c15861b0901e9a49f094398a0d6ed2026fc5978 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Andrew de los Reyes80061062010-02-04 14:25:00 -080016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_
Andrew de los Reyes80061062010-02-04 14:25:00 -080019
Amin Hassanicd7edbe2017-09-18 17:05:02 -070020#include <memory>
21#include <utility>
Alex Deymo8427b4a2014-11-05 14:00:32 -080022
23#include <base/logging.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/secure_blob.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/utils.h"
27#include "update_engine/payload_consumer/file_descriptor.h"
Andrew de los Reyes80061062010-02-04 14:25:00 -080028#include "update_engine/update_metadata.pb.h"
Andrew de los Reyes80061062010-02-04 14:25:00 -080029
30// ExtentWriter is an abstract class which synchronously writes to a given
31// file descriptor at the extents given.
32
33namespace chromeos_update_engine {
34
Andrew de los Reyes80061062010-02-04 14:25:00 -080035class ExtentWriter {
36 public:
Alex Deymo05322872015-09-30 09:50:24 -070037 ExtentWriter() = default;
Andrew de los Reyes80061062010-02-04 14:25:00 -080038 virtual ~ExtentWriter() {
39 LOG_IF(ERROR, !end_called_) << "End() not called on ExtentWriter.";
40 }
41
42 // Returns true on success.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080043 virtual bool Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -070044 const google::protobuf::RepeatedPtrField<Extent>& extents,
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070045 uint32_t block_size) = 0;
Andrew de los Reyes80061062010-02-04 14:25:00 -080046
47 // Returns true on success.
48 virtual bool Write(const void* bytes, size_t count) = 0;
49
50 // Should be called when all writing is complete. Returns true on success.
51 // The fd is not closed. Caller is responsible for closing it.
52 bool End() {
53 end_called_ = true;
54 return EndImpl();
55 }
56 virtual bool EndImpl() = 0;
57 private:
Alex Deymo05322872015-09-30 09:50:24 -070058 bool end_called_{false};
Andrew de los Reyes80061062010-02-04 14:25:00 -080059};
60
61// DirectExtentWriter is probably the simplest ExtentWriter implementation.
62// It writes the data directly into the extents.
63
64class DirectExtentWriter : public ExtentWriter {
65 public:
Alex Deymo05322872015-09-30 09:50:24 -070066 DirectExtentWriter() = default;
67 ~DirectExtentWriter() override = default;
Andrew de los Reyes80061062010-02-04 14:25:00 -080068
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080069 bool Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -070070 const google::protobuf::RepeatedPtrField<Extent>& extents,
Alex Deymo05322872015-09-30 09:50:24 -070071 uint32_t block_size) override {
Andrew de los Reyes80061062010-02-04 14:25:00 -080072 fd_ = fd;
73 block_size_ = block_size;
74 extents_ = extents;
Amin Hassanicd7edbe2017-09-18 17:05:02 -070075 cur_extent_ = extents_.begin();
Andrew de los Reyes80061062010-02-04 14:25:00 -080076 return true;
77 }
Alex Deymo05322872015-09-30 09:50:24 -070078 bool Write(const void* bytes, size_t count) override;
79 bool EndImpl() override { return true; }
Andrew de los Reyes80061062010-02-04 14:25:00 -080080
81 private:
Alex Deymo05322872015-09-30 09:50:24 -070082 FileDescriptorPtr fd_{nullptr};
Chris Masone4dc2ada2010-09-23 12:43:03 -070083
Alex Deymo05322872015-09-30 09:50:24 -070084 size_t block_size_{0};
Amin Hassanicd7edbe2017-09-18 17:05:02 -070085 // Bytes written into |cur_extent_| thus far.
Alex Deymo05322872015-09-30 09:50:24 -070086 uint64_t extent_bytes_written_{0};
Amin Hassanicd7edbe2017-09-18 17:05:02 -070087 google::protobuf::RepeatedPtrField<Extent> extents_;
88 // The next call to write should correspond to |cur_extents_|.
89 google::protobuf::RepeatedPtrField<Extent>::iterator cur_extent_;
Andrew de los Reyes80061062010-02-04 14:25:00 -080090};
91
92// Takes an underlying ExtentWriter to which all operations are delegated.
93// When End() is called, ZeroPadExtentWriter ensures that the total number
94// of bytes written is a multiple of block_size_. If not, it writes zeros
95// to pad as needed.
96
97class ZeroPadExtentWriter : public ExtentWriter {
98 public:
Alex Deymo05322872015-09-30 09:50:24 -070099 explicit ZeroPadExtentWriter(
100 std::unique_ptr<ExtentWriter> underlying_extent_writer)
101 : underlying_extent_writer_(std::move(underlying_extent_writer)) {}
102 ~ZeroPadExtentWriter() override = default;
Andrew de los Reyes80061062010-02-04 14:25:00 -0800103
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800104 bool Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -0700105 const google::protobuf::RepeatedPtrField<Extent>& extents,
Alex Deymo05322872015-09-30 09:50:24 -0700106 uint32_t block_size) override {
Andrew de los Reyes80061062010-02-04 14:25:00 -0800107 block_size_ = block_size;
108 return underlying_extent_writer_->Init(fd, extents, block_size);
109 }
Alex Deymo05322872015-09-30 09:50:24 -0700110 bool Write(const void* bytes, size_t count) override {
Andrew de los Reyes80061062010-02-04 14:25:00 -0800111 if (underlying_extent_writer_->Write(bytes, count)) {
112 bytes_written_mod_block_size_ += count;
113 bytes_written_mod_block_size_ %= block_size_;
114 return true;
115 }
116 return false;
117 }
Alex Deymo05322872015-09-30 09:50:24 -0700118 bool EndImpl() override {
Andrew de los Reyes80061062010-02-04 14:25:00 -0800119 if (bytes_written_mod_block_size_) {
120 const size_t write_size = block_size_ - bytes_written_mod_block_size_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700121 brillo::Blob zeros(write_size, 0);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800122 TEST_AND_RETURN_FALSE(underlying_extent_writer_->Write(zeros.data(),
Andrew de los Reyes80061062010-02-04 14:25:00 -0800123 write_size));
124 }
125 return underlying_extent_writer_->End();
126 }
127
128 private:
Alex Deymo05322872015-09-30 09:50:24 -0700129 std::unique_ptr<ExtentWriter> underlying_extent_writer_;
130 size_t block_size_{0};
131 size_t bytes_written_mod_block_size_{0};
Andrew de los Reyes80061062010-02-04 14:25:00 -0800132};
133
134} // namespace chromeos_update_engine
135
Alex Deymo39910dc2015-11-09 17:04:30 -0800136#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_