Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide better implementation for FileStream::id() #2705

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading