SPIFlashFileSystem open file handle

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];
		}
	}
}

}

Is your open() method correct? Shouldn’t it return a handle, regardless of whether it finds it in the open file handles list, or not?
Maybe the else {} brackets are unnecessary.

you’re right. This was just an online written piece of code without any debugging or test…there’s still some error with it, currently debugging

this one seems to be doing all it should.
Any chance this might end up in the standard lib ? overhead is pretty small (just an array with pointers to the opened files).

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;
		}
		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];
 			}
 		}
 	}
}