-
David Gauchard authoredDavid Gauchard authored
fs.cpp 2.24 KiB
#include <ESP8266WebServer.h>
#include <ESPWebDAV.h>
#include <LittleFS.h>
#include <StreamString.h>
#include <interrupts.h> // InterruptLock
#include "fs.h"
#include "syringe.h"
using InterruptLock = esp8266::InterruptLock;
extern Syringe syringe;
ESPWebDAVCore* dav = nullptr;
ESP8266WebServer* http = nullptr;
FS* filesystem = nullptr;
const char* fsMsgHeader = nullptr;
bool fs_init (const char* msgHeader, Print* out)
{
fsMsgHeader = msgHeader;
filesystem = &LittleFS;
if (!filesystem->begin() && (!filesystem->format() || !filesystem->begin()))
{
out->printf("%sFS: mount/format/mount: failed\n", fsMsgHeader);
return false;
}
http = new (std::nothrow) ESP8266WebServer(HTTPORT);
if (!http)
{
out->printf("#%sFS: http server start: failed\n", fsMsgHeader);
return false;
}
dav = new (std::nothrow) ESPWebDAVCore;
if (!dav)
{
out->printf("%sFS: webdav allocation: failed\n", fsMsgHeader);
delete http;
return false;
}
dav->begin(filesystem);
http->addHook(hookWebDAVForWebserver(DAVROOT, *dav));
return true;
}
void ls (Print* out)
{
if (dav)
{
out->printf("%sLS:\n", fsMsgHeader);
{ InterruptLock lock; dav->dir("/", out); }
}
}
void cat (const char* filename, Stream* out)
{
if (filesystem)
{
File f = ({ InterruptLock lock; filesystem->open(filename, "r"); });
if (!f)
out->printf("%scat: '%s' not found\n", fsMsgHeader, filename);
else
{
out->printf("%s>>>>FILE name:'%s' fullname:'%s' size=%zd\n", fsMsgHeader, f.name(), f.fullName(), f.size());
{ InterruptLock lock; f.sendSize(out, f.size()); }
out->printf("%s<<<<FILE\n", fsMsgHeader);
{ InterruptLock lock; f.close(); }
}
}
}
void testw (const char* filename, Stream* out)
{
if (filesystem)
{
File f = ({ InterruptLock lock; filesystem->open(filename, "w"); });
if (!f)
out->printf("%stest: '%s' can't write\n", fsMsgHeader, filename);
else
{
StreamString blah = "blah";
{ InterruptLock lock; blah.sendAll(f); }
{ InterruptLock lock; f.close(); }
}
}
}