Make pread and DataView const.
GitOrigin-RevId: 4267ba93a1e83be55361c9f284f717151eb740cd
This commit is contained in:
parent
a4f98024a0
commit
ed33e23c40
@ -51,7 +51,7 @@ AesCbcState calc_aes_cbc_state_sha512(Slice seed) {
|
||||
}
|
||||
|
||||
template <class F>
|
||||
static Status data_view_for_each(DataView &data, F &&f) {
|
||||
static Status data_view_for_each(const DataView &data, F &&f) {
|
||||
const int64 step = 128 << 10;
|
||||
for (int64 i = 0, size = data.size(); i < size; i += step) {
|
||||
TRY_RESULT(bytes, data.pread(i, min(step, size - i)));
|
||||
@ -60,7 +60,7 @@ static Status data_view_for_each(DataView &data, F &&f) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Result<ValueHash> calc_value_hash(DataView &data_view) {
|
||||
Result<ValueHash> calc_value_hash(const DataView &data_view) {
|
||||
Sha256State state;
|
||||
sha256_init(&state);
|
||||
data_view_for_each(data_view, [&state](BufferSlice bytes) {
|
||||
@ -93,7 +93,7 @@ int64 FileDataView::size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
Result<BufferSlice> FileDataView::pread(int64 offset, int64 size) {
|
||||
Result<BufferSlice> FileDataView::pread(int64 offset, int64 size) const {
|
||||
auto slice = BufferSlice(narrow_cast<size_t>(size));
|
||||
TRY_RESULT(actual_size, fd_.pread(slice.as_slice(), offset));
|
||||
if (static_cast<int64>(actual_size) != size) {
|
||||
@ -109,7 +109,7 @@ int64 BufferSliceDataView::size() const {
|
||||
return narrow_cast<int64>(buffer_slice_.size());
|
||||
}
|
||||
|
||||
Result<BufferSlice> BufferSliceDataView::pread(int64 offset, int64 size) {
|
||||
Result<BufferSlice> BufferSliceDataView::pread(int64 offset, int64 size) const {
|
||||
auto end_offset = size + offset;
|
||||
if (this->size() < end_offset) {
|
||||
return Status::Error("Not enough data in BufferSlice");
|
||||
@ -117,20 +117,20 @@ Result<BufferSlice> BufferSliceDataView::pread(int64 offset, int64 size) {
|
||||
return BufferSlice(buffer_slice_.as_slice().substr(narrow_cast<size_t>(offset), narrow_cast<size_t>(size)));
|
||||
}
|
||||
|
||||
ConcatDataView::ConcatDataView(DataView &left, DataView &right) : left_(left), right_(right) {
|
||||
ConcatDataView::ConcatDataView(const DataView &left, const DataView &right) : left_(left), right_(right) {
|
||||
}
|
||||
|
||||
int64 ConcatDataView::size() const {
|
||||
return left_.size() + right_.size();
|
||||
}
|
||||
|
||||
Result<BufferSlice> ConcatDataView::pread(int64 offset, int64 size) {
|
||||
Result<BufferSlice> ConcatDataView::pread(int64 offset, int64 size) const {
|
||||
auto end_offset = size + offset;
|
||||
if (this->size() < end_offset) {
|
||||
return Status::Error("Not enough data in ConcatDataView");
|
||||
}
|
||||
|
||||
auto substr = [](DataView &slice, int64 offset, int64 size) -> Result<BufferSlice> {
|
||||
auto substr = [](const DataView &slice, int64 offset, int64 size) -> Result<BufferSlice> {
|
||||
auto l = max(int64{0}, offset);
|
||||
auto r = min(slice.size(), offset + size);
|
||||
if (l >= r) {
|
||||
@ -302,7 +302,7 @@ Result<ValueHash> Decryptor::finish() {
|
||||
return ValueHash{res};
|
||||
}
|
||||
|
||||
Encryptor::Encryptor(AesCbcState aes_cbc_state, DataView &data_view)
|
||||
Encryptor::Encryptor(AesCbcState aes_cbc_state, const DataView &data_view)
|
||||
: aes_cbc_state_(std::move(aes_cbc_state)), data_view_(data_view) {
|
||||
}
|
||||
|
||||
@ -310,7 +310,7 @@ int64 Encryptor::size() const {
|
||||
return data_view_.size();
|
||||
}
|
||||
|
||||
Result<BufferSlice> Encryptor::pread(int64 offset, int64 size) {
|
||||
Result<BufferSlice> Encryptor::pread(int64 offset, int64 size) const {
|
||||
if (offset != current_offset_) {
|
||||
return Status::Error("Arbitrary offset is not supported");
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ class DataView {
|
||||
DataView &operator=(DataView &&) = delete;
|
||||
|
||||
virtual int64 size() const = 0;
|
||||
virtual Result<BufferSlice> pread(int64 offset, int64 size) = 0;
|
||||
virtual Result<BufferSlice> pread(int64 offset, int64 size) const = 0;
|
||||
virtual ~DataView() = default;
|
||||
};
|
||||
|
||||
@ -79,7 +79,7 @@ class FileDataView : public DataView {
|
||||
FileDataView(FileFd &fd, int64 size);
|
||||
|
||||
int64 size() const override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) const override;
|
||||
|
||||
private:
|
||||
FileFd &fd_;
|
||||
@ -90,7 +90,7 @@ class BufferSliceDataView : public DataView {
|
||||
public:
|
||||
explicit BufferSliceDataView(BufferSlice buffer_slice);
|
||||
int64 size() const override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) const override;
|
||||
|
||||
private:
|
||||
BufferSlice buffer_slice_;
|
||||
@ -98,18 +98,18 @@ class BufferSliceDataView : public DataView {
|
||||
|
||||
class ConcatDataView : public DataView {
|
||||
public:
|
||||
ConcatDataView(DataView &left, DataView &right);
|
||||
ConcatDataView(const DataView &left, const DataView &right);
|
||||
int64 size() const override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) const override;
|
||||
|
||||
private:
|
||||
DataView &left_;
|
||||
DataView &right_;
|
||||
const DataView &left_;
|
||||
const DataView &right_;
|
||||
};
|
||||
|
||||
AesCbcState calc_aes_cbc_state_pbkdf2(Slice secret, Slice salt);
|
||||
AesCbcState calc_aes_cbc_state_sha512(Slice seed);
|
||||
Result<ValueHash> calc_value_hash(DataView &data_view);
|
||||
Result<ValueHash> calc_value_hash(const DataView &data_view);
|
||||
ValueHash calc_value_hash(Slice data);
|
||||
BufferSlice gen_random_prefix(int64 data_size);
|
||||
|
||||
@ -175,14 +175,14 @@ class Decryptor {
|
||||
// Encryption
|
||||
class Encryptor : public DataView {
|
||||
public:
|
||||
Encryptor(AesCbcState aes_cbc_state, DataView &data_view);
|
||||
Encryptor(AesCbcState aes_cbc_state, const DataView &data_view);
|
||||
int64 size() const override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) override;
|
||||
Result<BufferSlice> pread(int64 offset, int64 size) const override;
|
||||
|
||||
private:
|
||||
AesCbcState aes_cbc_state_;
|
||||
int64 current_offset_{0};
|
||||
DataView &data_view_;
|
||||
mutable AesCbcState aes_cbc_state_;
|
||||
mutable int64 current_offset_{0};
|
||||
const DataView &data_view_;
|
||||
};
|
||||
|
||||
// Main functions
|
||||
|
@ -259,7 +259,7 @@ Result<size_t> FileFd::pwrite(Slice slice, int64 offset) {
|
||||
return OS_ERROR(PSLICE() << "Pwrite to [fd = " << native_fd << "] at [offset = " << offset << "] has failed");
|
||||
}
|
||||
|
||||
Result<size_t> FileFd::pread(MutableSlice slice, int64 offset) {
|
||||
Result<size_t> FileFd::pread(MutableSlice slice, int64 offset) const {
|
||||
if (offset < 0) {
|
||||
return Status::Error("Offset must be non-negative");
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ class FileFd {
|
||||
Result<size_t> read(MutableSlice slice) TD_WARN_UNUSED_RESULT;
|
||||
|
||||
Result<size_t> pwrite(Slice slice, int64 offset) TD_WARN_UNUSED_RESULT;
|
||||
Result<size_t> pread(MutableSlice slice, int64 offset) TD_WARN_UNUSED_RESULT;
|
||||
Result<size_t> pread(MutableSlice slice, int64 offset) const TD_WARN_UNUSED_RESULT;
|
||||
|
||||
enum class LockFlags { Write, Read, Unlock };
|
||||
Status lock(LockFlags flags, int32 max_tries = 1) TD_WARN_UNUSED_RESULT;
|
||||
|
Reference in New Issue
Block a user