From e4a83654be2fd3e5d805372f28acc27b702996b5 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 9 Feb 2019 07:54:22 +0000 Subject: [PATCH] Fix for file stream size tracking (#1608) * Check current position against `size` in `FileStream::write()` and `FileStream::seek()` Fixes problem where `available()` reports negative value * Remove deprecated `FileStream::attach(const String&)` usage from samples --- Sming/SmingCore/Data/Stream/FileStream.cpp | 16 +++++++++++----- Sming/SmingCore/Data/Stream/FileStream.h | 2 +- Sming/SmingCore/Network/HttpClient.cpp | 2 +- samples/Basic_Serial/app/application.cpp | 2 +- samples/HttpClient/app/application.cpp | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Sming/SmingCore/Data/Stream/FileStream.cpp b/Sming/SmingCore/Data/Stream/FileStream.cpp index 5e2512dc47..053f3188b1 100644 --- a/Sming/SmingCore/Data/Stream/FileStream.cpp +++ b/Sming/SmingCore/Data/Stream/FileStream.cpp @@ -76,16 +76,19 @@ size_t FileStream::write(const uint8_t* buffer, size_t size) return 0; } - int pos = fileSeek(handle, 0, eSO_FileEnd); - if(!check(pos)) { + int writePos = fileSeek(handle, 0, eSO_FileEnd); + if(!check(writePos)) { return 0; } - this->pos = size_t(pos); + pos = size_t(writePos); int written = fileWrite(handle, buffer, size); if(check(written)) { - this->pos += size_t(written); + pos += size_t(written); + if(pos > this->size) { + this->size = pos; + } } return written; @@ -98,7 +101,10 @@ bool FileStream::seek(int len) return false; } - pos = newpos; + pos = size_t(newpos); + if(pos > size) { + size = pos; + } return true; } diff --git a/Sming/SmingCore/Data/Stream/FileStream.h b/Sming/SmingCore/Data/Stream/FileStream.h index 005c4b5526..bf23291b67 100644 --- a/Sming/SmingCore/Data/Stream/FileStream.h +++ b/Sming/SmingCore/Data/Stream/FileStream.h @@ -45,7 +45,7 @@ class FileStream : public ReadWriteStream void attach(file_t file, size_t size); /* @deprecated: use open() method */ - bool attach(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly) + bool attach(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly) __attribute__((deprecated)) { return open(fileName, openFlags); } diff --git a/Sming/SmingCore/Network/HttpClient.cpp b/Sming/SmingCore/Network/HttpClient.cpp index be15241c84..f3df14ff51 100644 --- a/Sming/SmingCore/Network/HttpClient.cpp +++ b/Sming/SmingCore/Network/HttpClient.cpp @@ -85,7 +85,7 @@ bool HttpClient::downloadFile(const String& url, const String& saveFileName, file = saveFileName; FileStream* fileStream = new FileStream(); - fileStream->attach(file, eFO_CreateNewAlways | eFO_WriteOnly); + fileStream->open(file, eFO_CreateNewAlways | eFO_WriteOnly); return send(request(url)->setResponseStream(fileStream)->setMethod(HTTP_GET)->onRequestComplete(requestComplete)); } diff --git a/samples/Basic_Serial/app/application.cpp b/samples/Basic_Serial/app/application.cpp index 7717750cbc..02096efb5f 100644 --- a/samples/Basic_Serial/app/application.cpp +++ b/samples/Basic_Serial/app/application.cpp @@ -201,7 +201,7 @@ void handleCommand(const String& command) if(command.equalsIgnoreCase(_F("cat"))) { String filename = F("Readme.md"); FileStream* fileStream = new FileStream; - if(fileStream && fileStream->attach(filename, eFO_ReadOnly)) { + if(fileStream && fileStream->open(filename, eFO_ReadOnly)) { Serial.printf(_F("Sending \"%s\" (%u bytes)\r\n"), filename.c_str(), fileStream->available()); auto demo = new SerialStreamTransmitDemo; demo->begin(Serial1); diff --git a/samples/HttpClient/app/application.cpp b/samples/HttpClient/app/application.cpp index c0b1723266..e9c1be5c8b 100644 --- a/samples/HttpClient/app/application.cpp +++ b/samples/HttpClient/app/application.cpp @@ -181,7 +181,7 @@ void connectOk(IPAddress ip, IPAddress mask, IPAddress gateway) // Or if you want to directly save the response body to a file then the following can be done // FileStream* responseBodyFile = new FileStream(); - // responseBodyFile->attach("file.name", eFO_CreateNewAlways | eFO_WriteOnly); + // responseBodyFile->open("file.name", eFO_CreateNewAlways | eFO_WriteOnly); // putRequest->setResponseStream(responseBodyFile); // << the complete body will be stored on your file system // see the implementation of `bool HttpClient::downloadFile(const String& url, const String& saveFileName, ...` for details.