blob: 9e53561ea4d0e012e1141a34865bdd54a1d12c7d [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;
Sen Jiang5e1af982018-11-01 15:01:45 -070038 virtual ~ExtentWriter() = default;
Andrew de los Reyes80061062010-02-04 14:25:00 -080039
40 // Returns true on success.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080041 virtual bool Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -070042 const google::protobuf::RepeatedPtrField<Extent>& extents,
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070043 uint32_t block_size) = 0;
Andrew de los Reyes80061062010-02-04 14:25:00 -080044
45 // Returns true on success.
46 virtual bool Write(const void* bytes, size_t count) = 0;
Andrew de los Reyes80061062010-02-04 14:25:00 -080047};
48
49// DirectExtentWriter is probably the simplest ExtentWriter implementation.
50// It writes the data directly into the extents.
51
52class DirectExtentWriter : public ExtentWriter {
53 public:
Alex Deymo05322872015-09-30 09:50:24 -070054 DirectExtentWriter() = default;
55 ~DirectExtentWriter() override = default;
Andrew de los Reyes80061062010-02-04 14:25:00 -080056
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080057 bool Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -070058 const google::protobuf::RepeatedPtrField<Extent>& extents,
Alex Deymo05322872015-09-30 09:50:24 -070059 uint32_t block_size) override {
Andrew de los Reyes80061062010-02-04 14:25:00 -080060 fd_ = fd;
61 block_size_ = block_size;
62 extents_ = extents;
Amin Hassanicd7edbe2017-09-18 17:05:02 -070063 cur_extent_ = extents_.begin();
Andrew de los Reyes80061062010-02-04 14:25:00 -080064 return true;
65 }
Alex Deymo05322872015-09-30 09:50:24 -070066 bool Write(const void* bytes, size_t count) override;
Andrew de los Reyes80061062010-02-04 14:25:00 -080067
68 private:
Alex Deymo05322872015-09-30 09:50:24 -070069 FileDescriptorPtr fd_{nullptr};
Chris Masone4dc2ada2010-09-23 12:43:03 -070070
Alex Deymo05322872015-09-30 09:50:24 -070071 size_t block_size_{0};
Amin Hassanicd7edbe2017-09-18 17:05:02 -070072 // Bytes written into |cur_extent_| thus far.
Alex Deymo05322872015-09-30 09:50:24 -070073 uint64_t extent_bytes_written_{0};
Amin Hassanicd7edbe2017-09-18 17:05:02 -070074 google::protobuf::RepeatedPtrField<Extent> extents_;
75 // The next call to write should correspond to |cur_extents_|.
76 google::protobuf::RepeatedPtrField<Extent>::iterator cur_extent_;
Andrew de los Reyes80061062010-02-04 14:25:00 -080077};
78
Andrew de los Reyes80061062010-02-04 14:25:00 -080079} // namespace chromeos_update_engine
80
Alex Deymo39910dc2015-11-09 17:04:30 -080081#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_