Skip to content

Commit

Permalink
Provide better implementation for FileStream::id() (#2705)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 authored Jan 11, 2024
1 parent 28c7dcd commit eba0793
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Sming/Core/Data/Stream/IFS/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include "FileStream.h"

#define ETAG_SIZE 16

namespace IFS
{
void FileStream::attach(FileHandle file, size_t size)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/HostTests/modules/Files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit eba0793

Please sign in to comment.