Merge branch 'master' into nbd

This commit is contained in:
folkert van heusden 2023-03-21 22:32:20 +01:00
commit cd55079217
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
7 changed files with 61 additions and 9 deletions

View file

@ -7,10 +7,9 @@
disk_backend_esp32::disk_backend_esp32(const std::string & filename) : disk_backend_esp32::disk_backend_esp32(const std::string & filename) :
filename(filename),
fh(new File32()) fh(new File32())
{ {
if (!fh->open(filename.c_str(), O_RDWR))
error_exit(true, "rk05: cannot open \"%s\"", filename.c_str());
} }
disk_backend_esp32::~disk_backend_esp32() disk_backend_esp32::~disk_backend_esp32()
@ -20,6 +19,17 @@ disk_backend_esp32::~disk_backend_esp32()
delete fh; delete fh;
} }
bool disk_backend_esp32::begin()
{
if (!fh->open(filename.c_str(), O_RDWR)) {
DOLOG(ll_error, true, "rk05: cannot open \"%s\"", filename.c_str());
return false;
}
return true;
}
bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const target) bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const target)
{ {
DOLOG(debug, false, "disk_backend_esp32::read: read %zu bytes from offset %zu", n, offset); DOLOG(debug, false, "disk_backend_esp32::read: read %zu bytes from offset %zu", n, offset);

View file

@ -7,12 +7,15 @@
class disk_backend_esp32 : public disk_backend class disk_backend_esp32 : public disk_backend
{ {
private: private:
const std::string filename;
File32 *const fh { nullptr }; File32 *const fh { nullptr };
public: public:
disk_backend_esp32(const std::string & filename); disk_backend_esp32(const std::string & filename);
virtual ~disk_backend_esp32(); virtual ~disk_backend_esp32();
bool begin();
bool read(const off_t offset, const size_t n, uint8_t *const target); bool read(const off_t offset, const size_t n, uint8_t *const target);
bool write(const off_t offset, const size_t n, const uint8_t *const from); bool write(const off_t offset, const size_t n, const uint8_t *const from);

View file

@ -147,11 +147,22 @@ std::pair<std::vector<disk_backend *>, std::vector<disk_backend *> > select_disk
if (fh.open(selected_file.c_str(), O_RDWR)) { if (fh.open(selected_file.c_str(), O_RDWR)) {
fh.close(); fh.close();
disk_backend *temp = new disk_backend_esp32(selected_file);
if (!temp->begin()) {
c->put_string("Cannot use: ");
c->put_string_lf(selected_file.c_str());
delete temp;
continue;
}
if (ch == '1') if (ch == '1')
return { { new disk_backend_esp32(selected_file) }, { } }; return { { temp }, { } };
if (ch == '2') if (ch == '2')
return { { }, { new disk_backend_esp32(selected_file) } }; return { { }, { temp } };
} }
c->put_string_lf("open failed"); c->put_string_lf("open failed");

View file

@ -10,6 +10,8 @@ public:
disk_backend(); disk_backend();
virtual ~disk_backend(); virtual ~disk_backend();
virtual bool begin() = 0;
virtual bool read(const off_t offset, const size_t n, uint8_t *const target) = 0; virtual bool read(const off_t offset, const size_t n, uint8_t *const target) = 0;
virtual bool write(const off_t offset, const size_t n, const uint8_t *const from) = 0; virtual bool write(const off_t offset, const size_t n, const uint8_t *const from) = 0;

View file

@ -1,4 +1,5 @@
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "disk_backend_file.h" #include "disk_backend_file.h"
@ -6,7 +7,7 @@
disk_backend_file::disk_backend_file(const std::string & filename) : disk_backend_file::disk_backend_file(const std::string & filename) :
fd(open(filename.c_str(), O_RDWR)) filename(filename)
{ {
} }
@ -15,6 +16,19 @@ disk_backend_file::~disk_backend_file()
close(fd); close(fd);
} }
bool disk_backend_file::begin()
{
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, const size_t n, uint8_t *const target) bool disk_backend_file::read(const off_t offset, const size_t n, uint8_t *const target)
{ {
DOLOG(debug, false, "disk_backend_file::read: read %zu bytes from offset %zu", n, offset); DOLOG(debug, false, "disk_backend_file::read: read %zu bytes from offset %zu", n, offset);

View file

@ -6,12 +6,16 @@
class disk_backend_file : public disk_backend class disk_backend_file : public disk_backend
{ {
private: private:
const int fd { -1 }; const std::string filename;
int fd { -1 };
public: public:
disk_backend_file(const std::string & filename); disk_backend_file(const std::string & filename);
virtual ~disk_backend_file(); virtual ~disk_backend_file();
bool begin();
bool read(const off_t offset, const size_t n, uint8_t *const target); bool read(const off_t offset, const size_t n, uint8_t *const target);
bool write(const off_t offset, const size_t n, const uint8_t *const from); bool write(const off_t offset, const size_t n, const uint8_t *const from);

View file

@ -82,6 +82,8 @@ int main(int argc, char *argv[])
std::string test; std::string test;
disk_backend *temp_d = nullptr;
int opt = -1; int opt = -1;
while((opt = getopt(argc, argv, "hm:T:r:R:p:ndtL:b:l:s:Q:")) != -1) while((opt = getopt(argc, argv, "hm:T:r:R:p:ndtL:b:l:s:Q:")) != -1)
{ {
@ -134,11 +136,17 @@ int main(int argc, char *argv[])
break; break;
case 'R': case 'R':
rk05_files.push_back(new disk_backend_file(optarg)); temp_d = new disk_backend_file(optarg);
if (!temp_d->begin())
error_exit(false, "Cannot use file \"%s\" for RK05", optarg);
rk05_files.push_back(temp_d);
break; break;
case 'r': case 'r':
rl02_files.push_back(new disk_backend_file(optarg)); temp_d = new disk_backend_file(optarg);
if (!temp_d->begin())
error_exit(false, "Cannot use file \"%s\" for RL02", optarg);
rl02_files.push_back(temp_d);
break; break;
case 'p': case 'p':