In an attempt to facilitate the use of the SPIFlashFileSystem lib, we wanted to make a small abstraction layer that allows our developers to perform File operations without being worried about file existence, opened state and without the need to keep track of the file handles (instances of SPIFlashFileSystem.File) that were obtained when opening a file. When using the open() method, a reference to this instance is returned, but it is nowhere stored in memory (_openfiles only stores an id into the fat).
After some trying it seems quite impossible to retrieve the already existing instance of SPIFlashFileSystem.File associated with an opened file (besides enumerating getroottable() and comparing all objects found with an instance that you need). Recreating a new instance based on the info that is stored in various internal arrays is possible, but then you lose all valuable data such as read- and write pointers. Doing this (having multiple instances of SPIFlashFileSystem.File pointing to the same file) would actually most likely corrupt the file pretty quickly.
For now we’ll create our own inherited SPIFlashFileSystem class with an override for open() that actually stores these instance references. WOuld be nicer thoough to get that directly in the lib.
class ExtSPIFlashFileSystem extends SPIFlashFileSystem
{
_openFileHandles = null;
constructor(start = null, end = null, flash = null)
{
_openFileHandles = {};
base.constructor(start, end , flash );
}
function open(fname, mode)
{
if (!(fname in _openFileHandles))
{
local handle = base.open(fname,mode);
_openFileHandles[fname] <- handle;
}
else
{
return _openFileHandles[fname];
}
}
function getFileHandle(fname)
{
if (fname in _openFileHandles)
return _openFileHandles[fname];
return null;
}
function _close(fileId, fileIdx, dirty)
{
base._close(fileId, fileIdx, dirty);
foreach(file in getFileList())
{
if (file.id == fileId)
{
delete _openFileHandles[file.fname];
}
}
}
}