62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
#pragma once
|
|
|
|
// In-memory stand-in for the nRF52 LittleFS, enough for ClientACL: whole-file
|
|
// write, read, exists, remove. Tests reach into `files` to tear or drop a write.
|
|
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
#define FILE_O_READ 0
|
|
#define FILE_O_WRITE 1
|
|
|
|
namespace Adafruit_LittleFS_Namespace {
|
|
|
|
class File {
|
|
std::map<std::string, std::string>* _files = nullptr;
|
|
std::string _name;
|
|
size_t _pos = 0;
|
|
bool _write = false;
|
|
|
|
public:
|
|
File() = default;
|
|
File(std::map<std::string, std::string>* files, const std::string& name, bool write)
|
|
: _files(files), _name(name), _write(write) {
|
|
if (write) (*_files)[name] = "";
|
|
}
|
|
explicit operator bool() const { return _files != nullptr; }
|
|
int read(uint8_t* buf, size_t len) {
|
|
const std::string& d = (*_files)[_name];
|
|
size_t n = std::min(len, d.size() - std::min(_pos, d.size()));
|
|
std::memcpy(buf, d.data() + _pos, n);
|
|
_pos += n;
|
|
return (int)n;
|
|
}
|
|
size_t write(const uint8_t* buf, size_t len) {
|
|
(*_files)[_name].append((const char*)buf, len);
|
|
return len;
|
|
}
|
|
void close() {}
|
|
};
|
|
|
|
} // namespace Adafruit_LittleFS_Namespace
|
|
|
|
class Adafruit_LittleFS {
|
|
public:
|
|
std::map<std::string, std::string> files;
|
|
int writes = 0;
|
|
|
|
bool exists(const char* name) { return files.count(name) > 0; }
|
|
bool remove(const char* name) { return files.erase(name) > 0; }
|
|
bool mkdir(const char*) { return true; }
|
|
Adafruit_LittleFS_Namespace::File open(const char* name, uint8_t mode = FILE_O_READ) {
|
|
if (mode == FILE_O_WRITE) {
|
|
writes++;
|
|
return Adafruit_LittleFS_Namespace::File(&files, name, true);
|
|
}
|
|
if (!exists(name)) return Adafruit_LittleFS_Namespace::File();
|
|
return Adafruit_LittleFS_Namespace::File(&files, name, false);
|
|
}
|
|
};
|