-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisk_backend_file.cpp
121 lines (93 loc) · 2.61 KB
/
disk_backend_file.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#include <cassert>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "disk_backend_file.h"
#include "gen.h"
#include "log.h"
disk_backend_file::disk_backend_file(const std::string & filename) :
filename(filename)
{
}
disk_backend_file::~disk_backend_file()
{
close(fd);
}
JsonDocument disk_backend_file::serialize() const
{
JsonDocument j;
j["disk-backend-type"] = "file";
j["overlay"] = serialize_overlay();
// TODO store checksum of backend
j["filename"] = filename;
return j;
}
disk_backend_file *disk_backend_file::deserialize(const JsonVariantConst j)
{
// TODO verify checksum of backend
// TODO overlay
return new disk_backend_file(j["filename"].as<std::string>());
}
bool disk_backend_file::begin(const bool snapshots)
{
#if IS_POSIX
use_overlay = snapshots;
#endif
fd = open(filename.c_str(), O_RDWR);
if (fd == -1) {
DOLOG(ll_error, true, "disk_backend_file: cannot open \"%s\": %s", filename.c_str(), strerror(errno));
return false;
}
return true;
}
bool disk_backend_file::read(const off_t offset_in, const size_t n, uint8_t *const target, const size_t sector_size)
{
TRACE("disk_backend_file::read: read %zu bytes from offset %zu", n, offset_in);
assert((offset_in % sector_size) == 0);
assert((n % sector_size) == 0);
for(off_t o=0; o<off_t(n); o+=sector_size) {
off_t offset = offset_in + o;
#if IS_POSIX
auto o_rc = get_from_overlay(offset, sector_size);
if (o_rc.has_value()) {
memcpy(&target[o], o_rc.value().data(), sector_size);
continue;
}
#endif
#if defined(_WIN32) // hope for the best
if (lseek(fd, offset, SEEK_SET) == -1)
return false;
if (::read(fd, target, n) != ssize_t(n))
return false;
#else
ssize_t rc = pread(fd, target, n, offset);
if (rc != ssize_t(n)) {
DOLOG(warning, false, "disk_backend_file::read: read failure. expected %zu bytes, got %zd", n, rc);
return false;
}
#endif
}
return true;
}
bool disk_backend_file::write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size)
{
TRACE("disk_backend_file::write: write %zu bytes to offset %zu", n, offset);
#if IS_POSIX
if (store_mem_range_in_overlay(offset, n, from, sector_size))
return true;
#endif
#if defined(_WIN32) // hope for the best
if (lseek(fd, offset, SEEK_SET) == -1)
return false;
return ::write(fd, from, n) == ssize_t(n);
#else
ssize_t rc = pwrite(fd, from, n, offset);
if (rc != ssize_t(n)) {
DOLOG(warning, false, "disk_backend_file::write: write failure. expected %zu bytes, got %zd", n, rc);
return false;
}
return true;
#endif
}