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