1// Copyright 2015 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_FILE_INTERFACE_H_
6#define _BSDIFF_FILE_INTERFACE_H_
7
8#include <sys/types.h>
9
10namespace bsdiff {
11
12class FileInterface {
13 public:
14  virtual ~FileInterface() = default;
15
16  // Reads synchronously from the current file position up to |count| bytes into
17  // the passed |buf| buffer. On success, stores in |bytes_read| how many bytes
18  // were actually read from the file. In case of error returns false. This
19  // method may read less than |count| bytes even if there's no error. If the
20  // end of file is reached, 0 bytes will be read and this methods returns true.
21  virtual bool Read(void* buf, size_t count, size_t* bytes_read) = 0;
22
23  // Writes synchronously up to |count| bytes from to passed |buf| buffer to
24  // the file. On success, stores in |bytes_written| how many bytes
25  // were actually written to the file. This method may write less than |count|
26  // bytes and return successfully, or even write 0 bytes if there's no more
27  // space left on the device. Returns whether the write succeeded.
28  virtual bool Write(const void* buf, size_t count, size_t* bytes_written) = 0;
29
30  // Change the current file position to |pos| bytes from the beginning of the
31  // file. Return whether the seek succeeded.
32  virtual bool Seek(off_t pos) = 0;
33
34  // Closes the file and flushes any cached data. Returns whether the close
35  // succeeded.
36  virtual bool Close() = 0;
37
38  // Compute the size of the file and store it in |size|. Returns whether it
39  // computed the size successfully.
40  virtual bool GetSize(uint64_t* size) = 0;
41
42 protected:
43  FileInterface() = default;
44};
45
46}  // namespace bsdiff
47
48#endif  // _BSDIFF_FILE_INTERFACE_H_
49