blob: 1744781546baaa8fd5cf0554a2499ac34c0c8394 [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
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_EXTENT_WRITER_H_
18#define UPDATE_ENGINE_EXTENT_WRITER_H_
Andrew de los Reyes80061062010-02-04 14:25:00 -080019
20#include <vector>
Alex Deymo8427b4a2014-11-05 14:00:32 -080021
22#include <base/logging.h>
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080023#include <chromeos/secure_blob.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080024
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080025#include "update_engine/file_descriptor.h"
Andrew de los Reyes80061062010-02-04 14:25:00 -080026#include "update_engine/update_metadata.pb.h"
27#include "update_engine/utils.h"
28
29// ExtentWriter is an abstract class which synchronously writes to a given
30// file descriptor at the extents given.
31
32namespace chromeos_update_engine {
33
Andrew de los Reyes80061062010-02-04 14:25:00 -080034class ExtentWriter {
35 public:
36 ExtentWriter() : end_called_(false) {}
37 virtual ~ExtentWriter() {
38 LOG_IF(ERROR, !end_called_) << "End() not called on ExtentWriter.";
39 }
40
41 // Returns true on success.
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080042 virtual bool Init(FileDescriptorPtr fd,
Andrew de los Reyes80061062010-02-04 14:25:00 -080043 const std::vector<Extent>& extents,
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070044 uint32_t block_size) = 0;
Andrew de los Reyes80061062010-02-04 14:25:00 -080045
46 // Returns true on success.
47 virtual bool Write(const void* bytes, size_t count) = 0;
48
49 // Should be called when all writing is complete. Returns true on success.
50 // The fd is not closed. Caller is responsible for closing it.
51 bool End() {
52 end_called_ = true;
53 return EndImpl();
54 }
55 virtual bool EndImpl() = 0;
56 private:
57 bool end_called_;
58};
59
60// DirectExtentWriter is probably the simplest ExtentWriter implementation.
61// It writes the data directly into the extents.
62
63class DirectExtentWriter : public ExtentWriter {
64 public:
65 DirectExtentWriter()
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080066 : fd_(nullptr),
Andrew de los Reyes80061062010-02-04 14:25:00 -080067 block_size_(0),
68 extent_bytes_written_(0),
69 next_extent_index_(0) {}
70 ~DirectExtentWriter() {}
71
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080072 bool Init(FileDescriptorPtr fd,
73 const std::vector<Extent>& extents,
74 uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080075 fd_ = fd;
76 block_size_ = block_size;
77 extents_ = extents;
78 return true;
79 }
80 bool Write(const void* bytes, size_t count);
81 bool EndImpl() {
82 return true;
83 }
84
85 private:
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080086 FileDescriptorPtr fd_;
Chris Masone4dc2ada2010-09-23 12:43:03 -070087
Andrew de los Reyes80061062010-02-04 14:25:00 -080088 size_t block_size_;
89 // Bytes written into next_extent_index_ thus far
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070090 uint64_t extent_bytes_written_;
Andrew de los Reyes80061062010-02-04 14:25:00 -080091 std::vector<Extent> extents_;
92 // The next call to write should correspond to extents_[next_extent_index_]
93 std::vector<Extent>::size_type next_extent_index_;
94};
95
96// Takes an underlying ExtentWriter to which all operations are delegated.
97// When End() is called, ZeroPadExtentWriter ensures that the total number
98// of bytes written is a multiple of block_size_. If not, it writes zeros
99// to pad as needed.
100
101class ZeroPadExtentWriter : public ExtentWriter {
102 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700103 explicit ZeroPadExtentWriter(ExtentWriter* underlying_extent_writer)
Andrew de los Reyes80061062010-02-04 14:25:00 -0800104 : underlying_extent_writer_(underlying_extent_writer),
105 block_size_(0),
106 bytes_written_mod_block_size_(0) {}
107 ~ZeroPadExtentWriter() {}
108
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800109 bool Init(FileDescriptorPtr fd,
110 const std::vector<Extent>& extents,
111 uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -0800112 block_size_ = block_size;
113 return underlying_extent_writer_->Init(fd, extents, block_size);
114 }
115 bool Write(const void* bytes, size_t count) {
116 if (underlying_extent_writer_->Write(bytes, count)) {
117 bytes_written_mod_block_size_ += count;
118 bytes_written_mod_block_size_ %= block_size_;
119 return true;
120 }
121 return false;
122 }
123 bool EndImpl() {
124 if (bytes_written_mod_block_size_) {
125 const size_t write_size = block_size_ - bytes_written_mod_block_size_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800126 chromeos::Blob zeros(write_size, 0);
127 TEST_AND_RETURN_FALSE(underlying_extent_writer_->Write(zeros.data(),
Andrew de los Reyes80061062010-02-04 14:25:00 -0800128 write_size));
129 }
130 return underlying_extent_writer_->End();
131 }
132
133 private:
134 ExtentWriter* underlying_extent_writer_; // The underlying ExtentWriter.
135 size_t block_size_;
136 size_t bytes_written_mod_block_size_;
137};
138
139} // namespace chromeos_update_engine
140
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700141#endif // UPDATE_ENGINE_EXTENT_WRITER_H_