1*a3a45f30SXin Li // Copyright 2015 The Chromium OS Authors. All rights reserved.
2*a3a45f30SXin Li // Use of this source code is governed by a BSD-style license that can be
3*a3a45f30SXin Li // found in the LICENSE file.
4*a3a45f30SXin Li
5*a3a45f30SXin Li #include "bsdiff/extents_file.h"
6*a3a45f30SXin Li
7*a3a45f30SXin Li #include <string.h>
8*a3a45f30SXin Li
9*a3a45f30SXin Li #include <algorithm>
10*a3a45f30SXin Li
11*a3a45f30SXin Li // Extent files implementation extending FileInterface.
12*a3a45f30SXin Li //
13*a3a45f30SXin Li // This class allows to map linear positions in a file to a list of regions in
14*a3a45f30SXin Li // another file. All the reads and writes are unbuffered, passed directly to the
15*a3a45f30SXin Li // underlying file. Seeking is done in O(log(N)), where N is the number of
16*a3a45f30SXin Li // extents in the file, but sequential reads jump to the next extent in O(1).
17*a3a45f30SXin Li
18*a3a45f30SXin Li namespace bsdiff {
19*a3a45f30SXin Li
ExtentsFile(std::unique_ptr<FileInterface> file,const std::vector<ex_t> & extents)20*a3a45f30SXin Li ExtentsFile::ExtentsFile(std::unique_ptr<FileInterface> file,
21*a3a45f30SXin Li const std::vector<ex_t>& extents)
22*a3a45f30SXin Li : file_(std::move(file)), extents_(extents) {
23*a3a45f30SXin Li acc_len_.reserve(extents.size());
24*a3a45f30SXin Li for (const ex_t& extent : extents) {
25*a3a45f30SXin Li acc_len_.push_back(total_ex_len_);
26*a3a45f30SXin Li total_ex_len_ += extent.len;
27*a3a45f30SXin Li }
28*a3a45f30SXin Li }
29*a3a45f30SXin Li
~ExtentsFile()30*a3a45f30SXin Li ExtentsFile::~ExtentsFile() {
31*a3a45f30SXin Li Close();
32*a3a45f30SXin Li }
33*a3a45f30SXin Li
Read(void * buf,size_t count,size_t * bytes_read)34*a3a45f30SXin Li bool ExtentsFile::Read(void* buf, size_t count, size_t* bytes_read) {
35*a3a45f30SXin Li return IOOperation(&FileInterface::Read, buf, count, bytes_read);
36*a3a45f30SXin Li }
37*a3a45f30SXin Li
38*a3a45f30SXin Li
Write(const void * buf,size_t count,size_t * bytes_written)39*a3a45f30SXin Li bool ExtentsFile::Write(const void* buf, size_t count, size_t* bytes_written) {
40*a3a45f30SXin Li return IOOperation(&FileInterface::Write, buf, count, bytes_written);
41*a3a45f30SXin Li }
42*a3a45f30SXin Li
Seek(off_t pos)43*a3a45f30SXin Li bool ExtentsFile::Seek(off_t pos) {
44*a3a45f30SXin Li if (pos < 0 || static_cast<uint64_t>(pos) > total_ex_len_)
45*a3a45f30SXin Li return false;
46*a3a45f30SXin Li if (acc_len_.empty())
47*a3a45f30SXin Li return true;
48*a3a45f30SXin Li // Note that the first element of acc_len_ is always 0, and pos is at least 0,
49*a3a45f30SXin Li // so the upper_bound will never return acc_len_.begin().
50*a3a45f30SXin Li curr_pos_ = pos;
51*a3a45f30SXin Li curr_ex_idx_ = std::upper_bound(acc_len_.begin(), acc_len_.end(), pos) -
52*a3a45f30SXin Li acc_len_.begin();
53*a3a45f30SXin Li // We handle the corner case where |pos| is the size of all the extents by
54*a3a45f30SXin Li // leaving the value of curr_ex_idx_ the same way AdvancePos(0) would leave it
55*a3a45f30SXin Li // after the seek.
56*a3a45f30SXin Li if (curr_pos_ < total_ex_len_)
57*a3a45f30SXin Li curr_ex_idx_--;
58*a3a45f30SXin Li return true;
59*a3a45f30SXin Li }
60*a3a45f30SXin Li
Close()61*a3a45f30SXin Li bool ExtentsFile::Close() {
62*a3a45f30SXin Li return file_->Close();
63*a3a45f30SXin Li }
64*a3a45f30SXin Li
GetSize(uint64_t * size)65*a3a45f30SXin Li bool ExtentsFile::GetSize(uint64_t* size) {
66*a3a45f30SXin Li *size = total_ex_len_;
67*a3a45f30SXin Li return true;
68*a3a45f30SXin Li }
69*a3a45f30SXin Li
AdvancePos(uint64_t size)70*a3a45f30SXin Li void ExtentsFile::AdvancePos(uint64_t size) {
71*a3a45f30SXin Li curr_pos_ += size;
72*a3a45f30SXin Li for (; curr_ex_idx_ < extents_.size(); curr_ex_idx_++) {
73*a3a45f30SXin Li if (curr_pos_ < acc_len_[curr_ex_idx_] + extents_[curr_ex_idx_].len)
74*a3a45f30SXin Li return;
75*a3a45f30SXin Li }
76*a3a45f30SXin Li }
77*a3a45f30SXin Li
78*a3a45f30SXin Li template <typename T>
IOOperation(bool (FileInterface::* io_op)(T *,size_t,size_t *),T * buf,size_t count,size_t * bytes_processed)79*a3a45f30SXin Li bool ExtentsFile::IOOperation(bool (FileInterface::*io_op)(T*, size_t, size_t*),
80*a3a45f30SXin Li T* buf,
81*a3a45f30SXin Li size_t count,
82*a3a45f30SXin Li size_t* bytes_processed) {
83*a3a45f30SXin Li bool result = true;
84*a3a45f30SXin Li size_t processed = 0;
85*a3a45f30SXin Li AdvancePos(0);
86*a3a45f30SXin Li while (count > 0 && curr_ex_idx_ < extents_.size()) {
87*a3a45f30SXin Li const ex_t& ex = extents_[curr_ex_idx_];
88*a3a45f30SXin Li off_t curr_ex_off = curr_pos_ - acc_len_[curr_ex_idx_];
89*a3a45f30SXin Li size_t chunk_size =
90*a3a45f30SXin Li std::min(static_cast<uint64_t>(count), ex.len - curr_ex_off);
91*a3a45f30SXin Li size_t chunk_processed = 0;
92*a3a45f30SXin Li if (ex.off < 0) {
93*a3a45f30SXin Li chunk_processed = chunk_size;
94*a3a45f30SXin Li } else {
95*a3a45f30SXin Li if (!file_->Seek(ex.off + curr_ex_off) ||
96*a3a45f30SXin Li !(file_.get()->*io_op)(buf, chunk_size, &chunk_processed)) {
97*a3a45f30SXin Li processed += chunk_processed;
98*a3a45f30SXin Li result = processed > 0;
99*a3a45f30SXin Li break;
100*a3a45f30SXin Li }
101*a3a45f30SXin Li }
102*a3a45f30SXin Li processed += chunk_processed;
103*a3a45f30SXin Li count -= chunk_processed;
104*a3a45f30SXin Li // T can be either const void* or void*. We const_cast it back to void* and
105*a3a45f30SXin Li // then char* to do the arithmetic operation, but |buf| will continue to be
106*a3a45f30SXin Li // const void* if it was defined that way.
107*a3a45f30SXin Li buf = static_cast<char*>(const_cast<void*>(buf)) + chunk_processed;
108*a3a45f30SXin Li AdvancePos(chunk_processed);
109*a3a45f30SXin Li if (!chunk_processed)
110*a3a45f30SXin Li break;
111*a3a45f30SXin Li }
112*a3a45f30SXin Li *bytes_processed = processed;
113*a3a45f30SXin Li return result;
114*a3a45f30SXin Li }
115*a3a45f30SXin Li
116*a3a45f30SXin Li } // namespace bsdiff
117