Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 1 | // 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 Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 9 | #include "base/logging.h" |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 10 | #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 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 18 | class 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 Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 28 | uint32_t block_size) = 0; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 29 | |
| 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 | |
| 47 | class 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 Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 56 | bool Init(int fd, const std::vector<Extent>& extents, uint32_t block_size) { |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 57 | 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 Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 69 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 70 | size_t block_size_; |
| 71 | // Bytes written into next_extent_index_ thus far |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 72 | uint64_t extent_bytes_written_; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 73 | 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 | |
| 83 | class 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 Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 91 | bool Init(int fd, const std::vector<Extent>& extents, uint32_t block_size) { |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 92 | 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__ |