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
5 #include "bsdiff/buffer_file.h"
6
7 #include "bsdiff/bspatch.h"
8
9 namespace bsdiff {
10
BufferFile(std::unique_ptr<FileInterface> file,size_t size)11 BufferFile::BufferFile(std::unique_ptr<FileInterface> file, size_t size)
12 : file_(std::move(file)) {
13 buffer_.reserve(size);
14 }
15
~BufferFile()16 BufferFile::~BufferFile() {
17 Close();
18 }
19
Read(void * buf,size_t count,size_t * bytes_read)20 bool BufferFile::Read(void* buf, size_t count, size_t* bytes_read) {
21 return false;
22 }
23
Write(const void * buf,size_t count,size_t * bytes_written)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
Seek(off_t pos)31 bool BufferFile::Seek(off_t pos) {
32 return false;
33 }
34
Close()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
GetSize(uint64_t * size)44 bool BufferFile::GetSize(uint64_t* size) {
45 *size = buffer_.size();
46 return true;
47 }
48
49 } // namespace bsdiff
50