Fix missing close files

Disable Debug
This commit is contained in:
Luc 2021-01-24 14:59:02 +01:00
parent 59e766b0b8
commit 868a9ba49c
4 changed files with 79 additions and 19 deletions

View File

@ -260,7 +260,7 @@
//DEBUG_OUTPUT_SERIAL2 3 //DEBUG_OUTPUT_SERIAL2 3
//DEBUG_OUTPUT_TELNET 4 //DEBUG_OUTPUT_TELNET 4
//DEBUG_OUTPUT_WEBSOCKET 5 //DEBUG_OUTPUT_WEBSOCKET 5
#define ESP_DEBUG_FEATURE DEBUG_OUTPUT_SERIAL0 //#define ESP_DEBUG_FEATURE DEBUG_OUTPUT_SERIAL0
#ifdef ESP_DEBUG_FEATURE #ifdef ESP_DEBUG_FEATURE
#define DEBUG_BAUDRATE 115200 #define DEBUG_BAUDRATE 115200

View File

@ -22,7 +22,7 @@
#define _VERSION_ESP3D_H #define _VERSION_ESP3D_H
//version and sources location //version and sources location
#define FW_VERSION "3.0.0.a85" #define FW_VERSION "3.0.0.a86"
#define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0"
#endif //_VERSION_ESP3D_H #endif //_VERSION_ESP3D_H

View File

@ -53,6 +53,11 @@ size_t ESP_FileSystem::usedBytes()
return LITTLEFS.usedBytes(); return LITTLEFS.usedBytes();
} }
uint ESP_FileSystem::maxPathLength()
{
return 32;
}
bool ESP_FileSystem::rename(const char *oldpath, const char *newpath) bool ESP_FileSystem::rename(const char *oldpath, const char *newpath)
{ {
return LITTLEFS.rename(oldpath,newpath); return LITTLEFS.rename(oldpath,newpath);
@ -228,10 +233,18 @@ ESP_File::ESP_File(void* handle, bool isdir, bool iswritemode, const char * path
log_esp3d("name: %s", _name.c_str()); log_esp3d("name: %s", _name.c_str());
log_esp3d("filename: %s", _filename.c_str()); log_esp3d("filename: %s", _filename.c_str());
set = true; set = true;
} else {
log_esp3d("File %d busy", i);
log_esp3d(tFile_handle[i].name());
} }
} }
if(!set) { if(!set) {
log_esp3d("No handle available"); log_esp3d("No handle available");
#if defined(ESP_DEBUG_FEATURE)
for (uint8_t i=0; (i < ESP_MAX_OPENHANDLE) ; i++) {
log_esp3d(tFile_handle[i].name());
}
#endif
} }
} }
@ -254,6 +267,10 @@ void ESP_File::close()
_index = -1; _index = -1;
} }
} }
bool ESP_File::seek(uint32_t pos, uint8_t mode)
{
return tFile_handle[_index].seek(pos, (SeekMode)mode);
}
ESP_File ESP_File::openNextFile() ESP_File ESP_File::openNextFile()
{ {

View File

@ -313,6 +313,8 @@ bool ESPWebDAVCore::dirAction(const String& path,
log_esp3d("(file-OK)"); log_esp3d("(file-OK)");
} else { } else {
log_esp3d("(file-abort)"); log_esp3d("(file-abort)");
entry.close();
root.close();
return false; return false;
} }
} }
@ -332,6 +334,8 @@ bool ESPWebDAVCore::dirAction(const String& path,
log_esp3d("(dir-OK)"); log_esp3d("(dir-OK)");
} else { } else {
log_esp3d("(dir-abort)"); log_esp3d("(dir-abort)");
entry.close();
root.close();
return false; return false;
} }
} }
@ -417,72 +421,97 @@ void ESPWebDAVCore::handleRequest()
if (method.equals("PUT")) if (method.equals("PUT"))
// payload is managed // payload is managed
{ {
return handlePut(resource); handlePut(resource);
file.close();
return ;
} }
// swallow content // swallow content
if (!getPayload(payload)) { if (!getPayload(payload)) {
handleIssue(408, "Request Time-out"); handleIssue(408, "Request Time-out");
client->stop(); client->stop();
file.close();
return; return;
} }
// handle properties // handle properties
if (method.equals("PROPFIND")) { if (method.equals("PROPFIND")) {
return handleProp(resource, file); handleProp(resource, file);
file.close();
return;
} }
if (method.equals("GET")) { if (method.equals("GET")) {
return handleGet(resource, file, true); handleGet(resource, file, true);
file.close();
return ;
} }
if (method.equals("HEAD")) { if (method.equals("HEAD")) {
return handleGet(resource, file, false); handleGet(resource, file, false);
file.close();
return;
} }
// handle options // handle options
if (method.equals("OPTIONS")) { if (method.equals("OPTIONS")) {
return handleOptions(resource); handleOptions(resource);
file.close();
return ;
} }
#if WEBDAV_LOCK_SUPPORT #if WEBDAV_LOCK_SUPPORT
// handle file locks // handle file locks
if (method.equals("LOCK")) { if (method.equals("LOCK")) {
return handleLock(resource); handleLock(resource);
file.close();
return;
} }
if (method.equals("UNLOCK")) { if (method.equals("UNLOCK")) {
return handleUnlock(resource); handleUnlock(resource);
file.close();
return ;
} }
#endif #endif
if (method.equals("PROPPATCH")) { if (method.equals("PROPPATCH")) {
return handlePropPatch(resource, file); handlePropPatch(resource, file);
file.close();
return ;
} }
// directory creation // directory creation
if (method.equals("MKCOL")) { if (method.equals("MKCOL")) {
return handleDirectoryCreate(resource); handleDirectoryCreate(resource);
file.close();
return;
} }
// move a file or directory // move a file or directory
if (method.equals("MOVE")) { if (method.equals("MOVE")) {
return handleMove(resource, file); handleMove(resource, file);
file.close();
return;
} }
// delete a file or directory // delete a file or directory
if (method.equals("DELETE")) { if (method.equals("DELETE")) {
return handleDelete(resource); handleDelete(resource);
file.close();
return;
} }
// delete a file or directory // delete a file or directory
if (method.equals("COPY")) { if (method.equals("COPY")) {
return handleCopy(resource, file); handleCopy(resource, file);
file.close();
return;
} }
// if reached here, means its a unhandled // if reached here, means its a unhandled
handleIssue(404, "Not found"); handleIssue(404, "Not found");
file.close();
//return false; //return false;
} }
@ -926,6 +955,7 @@ void ESPWebDAVCore::handlePut(ResourceType resource)
auto numWrite = file.write(buf + written, numRead - written); auto numWrite = file.write(buf + written, numRead - written);
if (numWrite == 0 || (int)numWrite == -1) { if (numWrite == 0 || (int)numWrite == -1) {
log_esp3d("error: numread=%d write=%d written=%d", (int)numRead, (int)numWrite, (int)written); log_esp3d("error: numread=%d write=%d written=%d", (int)numRead, (int)numWrite, (int)written);
file.close();
return handleWriteError("Write data failed", file); return handleWriteError("Write data failed", file);
} }
written += numWrite; written += numWrite;
@ -943,12 +973,13 @@ void ESPWebDAVCore::handlePut(ResourceType resource)
// detect timeout condition // detect timeout condition
if (numRemaining) { if (numRemaining) {
file.close();
return handleWriteError("Timed out waiting for data", file); return handleWriteError("Timed out waiting for data", file);
} }
log_esp3d("File %d bytes stored in: %d sec",(contentLengthHeader - numRemaining), ((millis() - tStart) / 1000)); log_esp3d("File %d bytes stored in: %d sec",(contentLengthHeader - numRemaining), ((millis() - tStart) / 1000));
} }
file.close();
log_esp3d("file written ('%s': %d = %d bytes)", String(file.name()).c_str(), (int)contentLengthHeader, (int)file.size()); log_esp3d("file written ('%s': %d = %d bytes)", String(file.name()).c_str(), (int)contentLengthHeader, (int)file.size());
if (resource == RESOURCE_NONE) { if (resource == RESOURCE_NONE) {
@ -988,8 +1019,10 @@ void ESPWebDAVCore::handleDirectoryCreate(ResourceType resource)
if (parentLastIndex > 0) { if (parentLastIndex > 0) {
WebDavFile testParent = WebDavFS::open(uri.substring(0, parentLastIndex).c_str()); WebDavFile testParent = WebDavFS::open(uri.substring(0, parentLastIndex).c_str());
if (!testParent.isDirectory()) { if (!testParent.isDirectory()) {
testParent.close();
return handleIssue(409, "Conflict"); return handleIssue(409, "Conflict");
} }
testParent.close();
} }
if (!WebDavFS::mkdir(uri.c_str())) { if (!WebDavFS::mkdir(uri.c_str())) {
@ -1054,6 +1087,7 @@ void ESPWebDAVCore::handleMove(ResourceType resource, WebDavFile& src)
if (destFile) { if (destFile) {
if (overwrite.equalsIgnoreCase("F")) { if (overwrite.equalsIgnoreCase("F")) {
destFile.close();
return handleIssue(412, "Precondition Failed"); return handleIssue(412, "Precondition Failed");
} }
if (destFile.isDirectory()) { if (destFile.isDirectory()) {
@ -1165,8 +1199,7 @@ bool ESPWebDAVCore::copyFile(WebDavFile srcFile, const String& destName)
{ {
WebDavFile dest; WebDavFile dest;
if (overwrite.equalsIgnoreCase("F")) { if (overwrite.equalsIgnoreCase("F")) {
dest = WebDavFS::open(destName.c_str()); if (WebDavFS::exists(destName.c_str())) {
if (dest) {
log_esp3d("copy dest '%s' already exists and overwrite is false", destName.c_str()); log_esp3d("copy dest '%s' already exists and overwrite is false", destName.c_str());
handleIssue(412, "Precondition Failed"); handleIssue(412, "Precondition Failed");
return false; return false;
@ -1190,12 +1223,14 @@ bool ESPWebDAVCore::copyFile(WebDavFile srcFile, const String& destName)
if (!nb) { if (!nb) {
log_esp3d("copy: short read"); log_esp3d("copy: short read");
handleIssue(500, "Internal Server Error"); handleIssue(500, "Internal Server Error");
dest.close();
return false; return false;
} }
int wr = dest.write((const uint8_t*)cp, nb); int wr = dest.write((const uint8_t*)cp, nb);
if (wr != nb) { if (wr != nb) {
log_esp3d("copy: short write wr=%d != rd=%d", (int)wr, (int)nb); log_esp3d("copy: short write wr=%d != rd=%d", (int)wr, (int)nb);
handleIssue(500, "Internal Server Error"); handleIssue(500, "Internal Server Error");
dest.close();
return false; return false;
} }
} }
@ -1251,6 +1286,7 @@ void ESPWebDAVCore::handleCopy(ResourceType resource, WebDavFile& src)
stripName(destPath); stripName(destPath);
int code; int code;
if (/*(code = allowed(uri)) != 200 ||*/ (code = allowed(destParentPath)) != 200 || (code = allowed(destPath)) != 200) { if (/*(code = allowed(uri)) != 200 ||*/ (code = allowed(destParentPath)) != 200 || (code = allowed(destPath)) != 200) {
destParent.close();
return handleIssue(code, "Locked"); return handleIssue(code, "Locked");
} }
@ -1259,6 +1295,7 @@ void ESPWebDAVCore::handleCopy(ResourceType resource, WebDavFile& src)
log_esp3d("Source is directory"); log_esp3d("Source is directory");
if (!destParent.isDirectory()) { if (!destParent.isDirectory()) {
log_esp3d("'%s' is not a directory", destParentPath.c_str()); log_esp3d("'%s' is not a directory", destParentPath.c_str());
destParent.close();
return handleIssue(409, "Conflict"); return handleIssue(409, "Conflict");
} }
@ -1269,8 +1306,13 @@ void ESPWebDAVCore::handleCopy(ResourceType resource, WebDavFile& src)
destNameX += source.name(); destNameX += source.name();
stripName(destNameX); stripName(destNameX);
log_esp3d("COPY: '%s' -> '%s", source.name(), destNameX.c_str()); log_esp3d("COPY: '%s' -> '%s", source.name(), destNameX.c_str());
return copyFile(WebDavFS::open(source.name()), destNameX); WebDavFile orifile = WebDavFS::open(source.name());
bool res = copyFile(orifile, destNameX);
orifile.close();
return res;
//return copyFile(WebDavFS::open(source.name()), destNameX);
})) { })) {
destParent.close();
return; // handleIssue already called by failed copyFile() handleIssue(409, "Conflict"); return; // handleIssue already called by failed copyFile() handleIssue(409, "Conflict");
} }
} else { } else {
@ -1279,6 +1321,7 @@ void ESPWebDAVCore::handleCopy(ResourceType resource, WebDavFile& src)
// (COPY into non-existant collection '/litmus/nonesuch' succeeded) // (COPY into non-existant collection '/litmus/nonesuch' succeeded)
if (!destParent || !destParent.isDirectory()) { if (!destParent || !destParent.isDirectory()) {
log_esp3d("dest dir '%s' not existing", destParentPath.c_str()); log_esp3d("dest dir '%s' not existing", destParentPath.c_str());
destParent.close();
return handleIssue(409, "Conflict"); return handleIssue(409, "Conflict");
} }