blob: 1bb6f4a37b16c4228e671414c14c56297a5778c9 [file] [log] [blame]
Andrew de los Reyes80061062010-02-04 14:25:00 -08001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_EXTENT_WRITER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_EXTENT_WRITER_H__
7
8#include <vector>
Chris Masone790e62e2010-08-12 10:41:18 -07009#include "base/logging.h"
Andrew de los Reyes80061062010-02-04 14:25:00 -080010#include "update_engine/update_metadata.pb.h"
11#include "update_engine/utils.h"
12
13// ExtentWriter is an abstract class which synchronously writes to a given
14// file descriptor at the extents given.
15
16namespace chromeos_update_engine {
17
Andrew de los Reyes80061062010-02-04 14:25:00 -080018class ExtentWriter {
19 public:
20 ExtentWriter() : end_called_(false) {}
21 virtual ~ExtentWriter() {
22 LOG_IF(ERROR, !end_called_) << "End() not called on ExtentWriter.";
23 }
24
25 // Returns true on success.
26 virtual bool Init(int fd,
27 const std::vector<Extent>& extents,
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070028 uint32_t block_size) = 0;
Andrew de los Reyes80061062010-02-04 14:25:00 -080029
30 // Returns true on success.
31 virtual bool Write(const void* bytes, size_t count) = 0;
32
33 // Should be called when all writing is complete. Returns true on success.
34 // The fd is not closed. Caller is responsible for closing it.
35 bool End() {
36 end_called_ = true;
37 return EndImpl();
38 }
39 virtual bool EndImpl() = 0;
40 private:
41 bool end_called_;
42};
43
44// DirectExtentWriter is probably the simplest ExtentWriter implementation.
45// It writes the data directly into the extents.
46
47class DirectExtentWriter : public ExtentWriter {
48 public:
49 DirectExtentWriter()
50 : fd_(-1),
51 block_size_(0),
52 extent_bytes_written_(0),
53 next_extent_index_(0) {}
54 ~DirectExtentWriter() {}
55
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070056 bool Init(int fd, const std::vector<Extent>& extents, uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080057 fd_ = fd;
58 block_size_ = block_size;
59 extents_ = extents;
60 return true;
61 }
62 bool Write(const void* bytes, size_t count);
63 bool EndImpl() {
64 return true;
65 }
66
67 private:
68 int fd_;
Chris Masone4dc2ada2010-09-23 12:43:03 -070069
Andrew de los Reyes80061062010-02-04 14:25:00 -080070 size_t block_size_;
71 // Bytes written into next_extent_index_ thus far
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070072 uint64_t extent_bytes_written_;
Andrew de los Reyes80061062010-02-04 14:25:00 -080073 std::vector<Extent> extents_;
74 // The next call to write should correspond to extents_[next_extent_index_]
75 std::vector<Extent>::size_type next_extent_index_;
76};
77
78// Takes an underlying ExtentWriter to which all operations are delegated.
79// When End() is called, ZeroPadExtentWriter ensures that the total number
80// of bytes written is a multiple of block_size_. If not, it writes zeros
81// to pad as needed.
82
83class ZeroPadExtentWriter : public ExtentWriter {
84 public:
85 ZeroPadExtentWriter(ExtentWriter* underlying_extent_writer)
86 : underlying_extent_writer_(underlying_extent_writer),
87 block_size_(0),
88 bytes_written_mod_block_size_(0) {}
89 ~ZeroPadExtentWriter() {}
90
Andrew de los Reyes08c4e272010-04-15 14:02:17 -070091 bool Init(int fd, const std::vector<Extent>& extents, uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080092 block_size_ = block_size;
93 return underlying_extent_writer_->Init(fd, extents, block_size);
94 }
95 bool Write(const void* bytes, size_t count) {
96 if (underlying_extent_writer_->Write(bytes, count)) {
97 bytes_written_mod_block_size_ += count;
98 bytes_written_mod_block_size_ %= block_size_;
99 return true;
100 }
101 return false;
102 }
103 bool EndImpl() {
104 if (bytes_written_mod_block_size_) {
105 const size_t write_size = block_size_ - bytes_written_mod_block_size_;
106 std::vector<char> zeros(write_size, 0);
107 TEST_AND_RETURN_FALSE(underlying_extent_writer_->Write(&zeros[0],
108 write_size));
109 }
110 return underlying_extent_writer_->End();
111 }
112
113 private:
114 ExtentWriter* underlying_extent_writer_; // The underlying ExtentWriter.
115 size_t block_size_;
116 size_t bytes_written_mod_block_size_;
117};
118
119} // namespace chromeos_update_engine
120
121#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_EXTENT_WRITER_H__