From 4a4448e7b09d9e61508724f038d68c48bf9f0bf8 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Tue, 21 Mar 2023 22:28:43 +0100 Subject: [PATCH] disk_backend::begin() --- ESP32/disk_backend_esp32.cpp | 14 ++++++++++++-- ESP32/disk_backend_esp32.h | 5 ++++- ESP32/main.ino | 15 +++++++++++++-- disk_backend.h | 2 ++ disk_backend_file.cpp | 16 +++++++++++++++- disk_backend_file.h | 6 +++++- main.cpp | 12 ++++++++++-- 7 files changed, 61 insertions(+), 9 deletions(-) diff --git a/ESP32/disk_backend_esp32.cpp b/ESP32/disk_backend_esp32.cpp index e0aa2fc..3934c10 100644 --- a/ESP32/disk_backend_esp32.cpp +++ b/ESP32/disk_backend_esp32.cpp @@ -7,10 +7,9 @@ disk_backend_esp32::disk_backend_esp32(const std::string & filename) : + filename(filename), 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() @@ -20,6 +19,17 @@ disk_backend_esp32::~disk_backend_esp32() 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) { DOLOG(debug, false, "disk_backend_esp32::read: read %zu bytes from offset %zu", n, offset); diff --git a/ESP32/disk_backend_esp32.h b/ESP32/disk_backend_esp32.h index a33136a..383eccf 100644 --- a/ESP32/disk_backend_esp32.h +++ b/ESP32/disk_backend_esp32.h @@ -7,12 +7,15 @@ class disk_backend_esp32 : public disk_backend { private: - File32 *const fh { nullptr }; + const std::string filename; + File32 *const fh { nullptr }; public: disk_backend_esp32(const std::string & filename); virtual ~disk_backend_esp32(); + bool begin(); + 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); diff --git a/ESP32/main.ino b/ESP32/main.ino index d0ea153..985ac0e 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -147,11 +147,22 @@ std::pair, std::vector > select_disk if (fh.open(selected_file.c_str(), O_RDWR)) { 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') - return { { new disk_backend_esp32(selected_file) }, { } }; + return { { temp }, { } }; if (ch == '2') - return { { }, { new disk_backend_esp32(selected_file) } }; + return { { }, { temp } }; } c->put_string_lf("open failed"); diff --git a/disk_backend.h b/disk_backend.h index 080c472..e0d44f2 100644 --- a/disk_backend.h +++ b/disk_backend.h @@ -10,6 +10,8 @@ public: 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 write(const off_t offset, const size_t n, const uint8_t *const from) = 0; diff --git a/disk_backend_file.cpp b/disk_backend_file.cpp index ed9a7db..ee33021 100644 --- a/disk_backend_file.cpp +++ b/disk_backend_file.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "disk_backend_file.h" @@ -6,7 +7,7 @@ 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); } +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) { DOLOG(debug, false, "disk_backend_file::read: read %zu bytes from offset %zu", n, offset); diff --git a/disk_backend_file.h b/disk_backend_file.h index 9f13227..bf1bd6d 100644 --- a/disk_backend_file.h +++ b/disk_backend_file.h @@ -6,12 +6,16 @@ class disk_backend_file : public disk_backend { private: - const int fd { -1 }; + const std::string filename; + + int fd { -1 }; public: disk_backend_file(const std::string & filename); virtual ~disk_backend_file(); + bool begin(); + 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); diff --git a/main.cpp b/main.cpp index 1c38638..8746e82 100644 --- a/main.cpp +++ b/main.cpp @@ -82,6 +82,8 @@ int main(int argc, char *argv[]) std::string test; + disk_backend *temp_d = nullptr; + int opt = -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; 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; 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; case 'p':