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 | |
| 5 | #ifndef _BSDIFF_SINK_FILE_H_ |
| 6 | #define _BSDIFF_SINK_FILE_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <functional> |
| 11 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 12 | #include "bsdiff/file_interface.h" |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 13 | |
| 14 | using sink_func = std::function<size_t(const uint8_t*, size_t)>; |
| 15 | |
| 16 | namespace bsdiff { |
| 17 | |
| 18 | class SinkFile : public FileInterface { |
| 19 | public: |
| 20 | // Creates a SinkFile based on the underlying |sink| function passed. |
| 21 | // The SinkFile will call |sink| function upon write. |
| 22 | // Read, Seek and GetSize are not supported. |
| 23 | explicit SinkFile(const sink_func& sink); |
| 24 | |
| 25 | ~SinkFile() = default; |
| 26 | |
| 27 | // FileInterface overrides. |
| 28 | bool Read(void* buf, size_t count, size_t* bytes_read) override; |
| 29 | bool Write(const void* buf, size_t count, size_t* bytes_written) override; |
| 30 | bool Seek(off_t pos) override; |
| 31 | bool Close() override; |
| 32 | bool GetSize(uint64_t* size) override; |
| 33 | |
| 34 | private: |
| 35 | // The sink() function used to write data. |
Amin Hassani | 8a183d3 | 2018-07-02 11:37:53 -0700 | [diff] [blame] | 36 | const sink_func sink_; |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | } // namespace bsdiff |
| 40 | |
| 41 | #endif // _BSDIFF_SINK_FILE_H_ |