-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdisk_backend_file.cpp
More file actions
142 lines (115 loc) · 3.23 KB
/
disk_backend_file.cpp
File metadata and controls
142 lines (115 loc) · 3.23 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// (C) 2018-2026 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);
}
void disk_backend_file::show_state(console *const cnsl) const
{
cnsl->put_string_lf("identifier: " + get_identifier());
}
#if IS_POSIX
JsonDocument disk_backend_file::serialize()
{
JsonDocument j;
j["disk-backend-type"] = "file";
j["overlay"] = serialize_overlay();
j["filename"] = filename;
auto crc = crc_over_data();
if (crc.has_value())
j["crc32"] = crc.value();
return j;
}
disk_backend_file *disk_backend_file::deserialize(const JsonVariantConst j)
{
auto out = new disk_backend_file(j["filename"].as<std::string>());
if (j.containsKey("crc32")) {
auto crc = out->crc_over_data();
if (crc.has_value() == false || crc.value() != j["crc32"]) {
delete out;
DOLOG(log_ss::LS_DISK, "disk_backend_file::deserialize CRC32 mismatch; did the disk change outside this emulator?");
return nullptr;
}
}
// AFTER crc check
out->deserialize(j);
return out;
}
#endif
bool disk_backend_file::begin(const bool snapshots)
{
#if IS_POSIX
use_overlay = snapshots;
#endif
#if defined(_WIN32)
fd = open(filename.c_str(), O_RDWR | O_BINARY);
#else
fd = open(filename.c_str(), O_RDWR);
#endif
if (fd == -1) {
DOLOG(log_ss::LS_DISK, "disk_backend_file: cannot open \"%s\": %s", filename.c_str(), strerror(errno));
return false;
}
size = lseek(fd, 0, SEEK_END);
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)
{
DOLOG(log_ss::LS_DISK, "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 (ssize_t rc = ::read(fd, target, n); rc != ssize_t(n))
return false;
#else
ssize_t rc = pread(fd, target, n, offset);
if (rc != ssize_t(n)) {
DOLOG(log_ss::LS_DISK, "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)
{
DOLOG(log_ss::LS_DISK, "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(log_ss::LS_DISK, "disk_backend_file::write: write failure. expected %zu bytes, got %zd", n, rc);
return false;
}
return true;
#endif
}