1*07fb1d06SElliott Hughes // Copyright 2017 The ChromiumOS Authors 2*07fb1d06SElliott Hughes // Use of this source code is governed by a BSD-style license that can be 3*07fb1d06SElliott Hughes // found in the LICENSE file. 4*07fb1d06SElliott Hughes 5*07fb1d06SElliott Hughes #ifndef SRC_EXTENT_STREAM_H_ 6*07fb1d06SElliott Hughes #define SRC_EXTENT_STREAM_H_ 7*07fb1d06SElliott Hughes 8*07fb1d06SElliott Hughes #include <memory> 9*07fb1d06SElliott Hughes #include <vector> 10*07fb1d06SElliott Hughes 11*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/common.h" 12*07fb1d06SElliott Hughes #include "puffin/src/include/puffin/stream.h" 13*07fb1d06SElliott Hughes 14*07fb1d06SElliott Hughes namespace puffin { 15*07fb1d06SElliott Hughes 16*07fb1d06SElliott Hughes // A stream object that allows reading and writing into disk extents. This is 17*07fb1d06SElliott Hughes // only used in main.cc for puffin binary to allow puffpatch on a actual rootfs 18*07fb1d06SElliott Hughes // and kernel images. 19*07fb1d06SElliott Hughes class ExtentStream : public StreamInterface { 20*07fb1d06SElliott Hughes public: 21*07fb1d06SElliott Hughes // Creates a stream only for writing. 22*07fb1d06SElliott Hughes static UniqueStreamPtr CreateForWrite(UniqueStreamPtr stream, 23*07fb1d06SElliott Hughes const std::vector<ByteExtent>& extents); 24*07fb1d06SElliott Hughes // Creates a stream only for reading. 25*07fb1d06SElliott Hughes static UniqueStreamPtr CreateForRead(UniqueStreamPtr stream, 26*07fb1d06SElliott Hughes const std::vector<ByteExtent>& extents); 27*07fb1d06SElliott Hughes ~ExtentStream() override = default; 28*07fb1d06SElliott Hughes 29*07fb1d06SElliott Hughes bool GetSize(uint64_t* size) const override; 30*07fb1d06SElliott Hughes bool GetOffset(uint64_t* offset) const override; 31*07fb1d06SElliott Hughes bool Seek(uint64_t offset) override; 32*07fb1d06SElliott Hughes bool Read(void* buffer, size_t length) override; 33*07fb1d06SElliott Hughes bool Write(const void* buffer, size_t length) override; 34*07fb1d06SElliott Hughes bool Close() override; 35*07fb1d06SElliott Hughes 36*07fb1d06SElliott Hughes private: 37*07fb1d06SElliott Hughes ExtentStream(UniqueStreamPtr stream, 38*07fb1d06SElliott Hughes const std::vector<ByteExtent>& extents, 39*07fb1d06SElliott Hughes bool is_for_write); 40*07fb1d06SElliott Hughes 41*07fb1d06SElliott Hughes // Since both read and write operations are very similar in this class, this 42*07fb1d06SElliott Hughes // function acts as a common operation that does both write and read based on 43*07fb1d06SElliott Hughes // the nullability of |read_buffer| or |write_buffer|. 44*07fb1d06SElliott Hughes bool DoReadOrWrite(void* read_buffer, 45*07fb1d06SElliott Hughes const void* write_buffer, 46*07fb1d06SElliott Hughes size_t length); 47*07fb1d06SElliott Hughes 48*07fb1d06SElliott Hughes // The underlying stream to read from and write into. 49*07fb1d06SElliott Hughes UniqueStreamPtr stream_; 50*07fb1d06SElliott Hughes 51*07fb1d06SElliott Hughes std::vector<ByteExtent> extents_; 52*07fb1d06SElliott Hughes 53*07fb1d06SElliott Hughes // The current |ByteExtent| that is being read from or write into. 54*07fb1d06SElliott Hughes std::vector<ByteExtent>::iterator cur_extent_; 55*07fb1d06SElliott Hughes 56*07fb1d06SElliott Hughes // The current offset in the current |ByteExtent| |cur_extent_|. 57*07fb1d06SElliott Hughes uint64_t cur_extent_offset_; 58*07fb1d06SElliott Hughes 59*07fb1d06SElliott Hughes // |True| if the stream is write only. |False| if the stream is read only. 60*07fb1d06SElliott Hughes bool is_for_write_; 61*07fb1d06SElliott Hughes 62*07fb1d06SElliott Hughes // The size of the stream. It is actually the cumulative size of all the bytes 63*07fb1d06SElliott Hughes // in |extents_|. 64*07fb1d06SElliott Hughes uint64_t size_; 65*07fb1d06SElliott Hughes 66*07fb1d06SElliott Hughes // The current offset. 67*07fb1d06SElliott Hughes uint64_t offset_; 68*07fb1d06SElliott Hughes 69*07fb1d06SElliott Hughes // Used for proper and faster seeking. 70*07fb1d06SElliott Hughes std::vector<uint64_t> extents_upper_bounds_; 71*07fb1d06SElliott Hughes 72*07fb1d06SElliott Hughes DISALLOW_COPY_AND_ASSIGN(ExtentStream); 73*07fb1d06SElliott Hughes }; 74*07fb1d06SElliott Hughes 75*07fb1d06SElliott Hughes } // namespace puffin 76*07fb1d06SElliott Hughes 77*07fb1d06SElliott Hughes #endif // SRC_EXTENT_STREAM_H_ 78