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