Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 5 | #include "bsdiff/buffer_file.h" |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 6 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 7 | #include "bsdiff/bspatch.h" |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 8 | |
| 9 | namespace bsdiff { |
| 10 | |
| 11 | BufferFile::BufferFile(std::unique_ptr<FileInterface> file, size_t size) |
| 12 | : file_(std::move(file)) { |
| 13 | buffer_.reserve(size); |
| 14 | } |
| 15 | |
| 16 | BufferFile::~BufferFile() { |
| 17 | Close(); |
| 18 | } |
| 19 | |
| 20 | bool BufferFile::Read(void* buf, size_t count, size_t* bytes_read) { |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | bool BufferFile::Write(const void* buf, size_t count, size_t* bytes_written) { |
| 25 | const uint8_t* data = static_cast<const uint8_t*>(buf); |
| 26 | buffer_.insert(buffer_.end(), data, data + count); |
| 27 | *bytes_written = count; |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | bool BufferFile::Seek(off_t pos) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | bool BufferFile::Close() { |
| 36 | if (!WriteAll(file_, buffer_.data(), buffer_.size())) |
| 37 | return false; |
| 38 | // Prevent writing |buffer_| to |file_| again if Close() is called more than |
| 39 | // once. |
| 40 | buffer_.clear(); |
| 41 | return file_->Close(); |
| 42 | } |
| 43 | |
| 44 | bool BufferFile::GetSize(uint64_t* size) { |
| 45 | *size = buffer_.size(); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | } // namespace bsdiff |