From eba0793f706faf9a4a2be70fc4cf01e77f1618b7 Mon Sep 17 00:00:00 2001 From: Mike Date: Thu, 11 Jan 2024 08:31:11 +0000 Subject: [PATCH] Provide better implementation for `FileStream::id()` (#2705) --- Sming/Core/Data/Stream/IFS/FileStream.cpp | 27 ++++++++++++++++++----- tests/HostTests/modules/Files.cpp | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Sming/Core/Data/Stream/IFS/FileStream.cpp b/Sming/Core/Data/Stream/IFS/FileStream.cpp index a046ad3ef3..9cdf0eb02c 100644 --- a/Sming/Core/Data/Stream/IFS/FileStream.cpp +++ b/Sming/Core/Data/Stream/IFS/FileStream.cpp @@ -10,8 +10,6 @@ #include "FileStream.h" -#define ETAG_SIZE 16 - namespace IFS { void FileStream::attach(FileHandle file, size_t size) @@ -172,10 +170,27 @@ String FileStream::id() const return nullptr; } - char buf[ETAG_SIZE]; - m_snprintf(buf, ETAG_SIZE, _F("00f-%x-%x0-%x"), stat.id, stat.size, stat.name.length); - - return String(buf); + /* + Jan 2024. How ETAGs are generated is not specified. Previous implementation was like this: + + m_snprintf(buf, ETAG_SIZE, _F("00f-%x-%x0-%x"), stat.id, stat.size, stat.name.length); + + Issues are: + - on some architectures compiler warns about differing types %x vs arguments + - name can change without length being affected; if name changes then file lookup would fail anyway + - modification timestamp is a good metric, should be incorporated + */ + String id; + id += "00f-"; + id += String(stat.id, HEX); + id += '-'; + id += String(stat.size, HEX); + id += '-'; + id += String(stat.mtime, HEX); + id += '-'; + id += String(stat.name.length, HEX); + + return id; } bool FileStream::truncate(size_t newSize) diff --git a/tests/HostTests/modules/Files.cpp b/tests/HostTests/modules/Files.cpp index 05eef51a4b..ed25524c7f 100644 --- a/tests/HostTests/modules/Files.cpp +++ b/tests/HostTests/modules/Files.cpp @@ -88,6 +88,7 @@ class FilesTest : public TestGroup { fileSetContent(testFileName, testContent); FileStream fs(testFileName); + Serial << testFileName << " ID: " << fs.id() << endl; res = fs.truncate(100); pos = fs.getPos(); size = fs.getSize();