• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EXTENTS_FILE_H_
6 #define _BSDIFF_EXTENTS_FILE_H_
7 
8 #include <stdio.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "bsdiff/file_interface.h"
14 
15 /*
16  * Extent files.
17  *
18  * This modules provides a familiar interface for handling files through an
19  * indirection layer of extents, which are contiguous chunks of variable length
20  * at arbitrary offsets within a file.  Once an extent file handle is obtained,
21  * users may read, write and seek as they do with ordinary files, having the I/O
22  * with the underlying file done for them by the extent file implementation. The
23  * implementation supports "sparse extents", which are assumed to contain zeros
24  * but otherwise have no actual representation in the underlying file; these are
25  * denoted by negative offset values.
26  *
27  * Unlike ordinary files, the size of an extent file is fixed; it is not
28  * truncated on open, nor is writing past the extent span allowed. Also, writing
29  * to a sparse extent has no effect and will not raise an error.
30  */
31 
32 namespace bsdiff {
33 
34 /* An extent, defined by an offset and a length. */
35 struct ex_t {
36   off_t off;     // the extent offset; negative indicates a sparse extent.
37   uint64_t len;  // the extent length.
38 };
39 
40 class ExtentsFile : public FileInterface {
41  public:
42   // Creates an ExtentsFile based on the underlying |file| passed. The positions
43   // in the ExtentsFile will be linearly mapped to the extents provided in
44   // |extents|. The created ExtentsFile takes ownership of the |file| will close
45   // it on destruction.
46   ExtentsFile(std::unique_ptr<FileInterface> file,
47               const std::vector<ex_t>& extents);
48 
49   ~ExtentsFile() override;
50 
51   // FileInterface overrides.
52   bool Read(void* buf, size_t count, size_t* bytes_read) override;
53   bool Write(const void* buf, size_t count, size_t* bytes_written) override;
54   bool Seek(off_t pos) override;
55   bool Close() override;
56   bool GetSize(uint64_t* size) override;
57 
58  private:
59   void AdvancePos(uint64_t size);
60 
61   // Performs an I/O operation (either read or write). This template shares the
62   // code for both Read() and Write() implementations.
63   template <typename T>
64   bool IOOperation(bool (FileInterface::*io_op)(T*, size_t, size_t*),
65                    T* buf,
66                    size_t count,
67                    size_t* bytes_processed);
68 
69   // The underlying FileInterace instance.
70   std::unique_ptr<FileInterface> file_;
71 
72   // The list of extents mapping this instance to |file_|.
73   const std::vector<ex_t> extents_;
74 
75   // The accumulated length of the extents. The i-th element contains the sum of
76   // the length of all the extents from 0 up to but not including the i-th
77   // extent. This reduces the complexity for random-access Seek() calls.
78   std::vector<uint64_t> acc_len_;
79 
80   // Current extent index.
81   size_t curr_ex_idx_{0};
82 
83   // Current logical file position.
84   uint64_t curr_pos_{0};
85 
86   // Total length of all extents (constant).
87   uint64_t total_ex_len_{0};
88 };
89 
90 }  // namespace bsdiff
91 
92 #endif  // _BSDIFF_EXTENTS_FILE_H_
93