File: InMemoryFile.cpp

package info (click to toggle)
cryfs 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,412 kB
  • sloc: cpp: 150,187; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (56 lines) | stat: -rw-r--r-- 1,820 bytes parent folder | download | duplicates (4)
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
#include "InMemoryFile.h"

using cpputils::Data;

InMemoryFile::InMemoryFile(Data data): _data(std::move(data)) {
}

InMemoryFile::~InMemoryFile() {
}

fspp::num_bytes_t InMemoryFile::read(void *buf, fspp::num_bytes_t count, fspp::num_bytes_t offset) const {
  fspp::num_bytes_t realCount = std::min(count, fspp::num_bytes_t(_data.size()) - offset);
  std::memcpy(buf, _data.dataOffset(offset.value()), realCount.value());
  return realCount;
}

const void *InMemoryFile::data() const {
  return _data.data();
}

bool InMemoryFile::fileContentEquals(const Data &expected, fspp::num_bytes_t offset) const {
  return 0 == std::memcmp(expected.data(), _data.dataOffset(offset.value()), expected.size());
}

fspp::num_bytes_t InMemoryFile::size() const {
  return fspp::num_bytes_t(_data.size());
}

WriteableInMemoryFile::WriteableInMemoryFile(Data data): InMemoryFile(std::move(data)), _originalData(_data.copy()) {
}

void WriteableInMemoryFile::write(const void *buf, fspp::num_bytes_t count, fspp::num_bytes_t offset) {
  _extendFileSizeIfNecessary(count + offset);

  std::memcpy(_data.dataOffset(offset.value()), buf, count.value());
}

void WriteableInMemoryFile::_extendFileSizeIfNecessary(fspp::num_bytes_t size) {
  if (size > fspp::num_bytes_t(_data.size())) {
    _extendFileSize(size);
  }
}

void WriteableInMemoryFile::_extendFileSize(fspp::num_bytes_t size) {
  Data newfile(size.value());
  std::memcpy(newfile.data(), _data.data(), _data.size());
  _data = std::move(newfile);
}

bool WriteableInMemoryFile::sizeUnchanged() const {
  return _data.size() == _originalData.size();
}

bool WriteableInMemoryFile::regionUnchanged(fspp::num_bytes_t offset, fspp::num_bytes_t count) const {
  return 0 == std::memcmp(_data.dataOffset(offset.value()), _originalData.dataOffset(offset.value()), count.value());
}