Add special handling for "/default" filesystem

Allow a FileBase (normally a FileSystemLike) to be set as the default,
so it can be looked up as "/default" as well as its actual name.
pull/7924/head
Kevin Bracey 2018-08-27 15:18:41 +03:00
parent dd91b90149
commit a67f09851b
2 changed files with 24 additions and 7 deletions

View File

@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstring>
#include "platform/FileBase.h"
#include "platform/FileLike.h"
#include "platform/FileHandle.h"
@ -21,6 +23,7 @@ namespace mbed {
FileBase *FileBase::_head = NULL;
SingletonPtr<PlatformMutex> FileBase::_mutex;
FileBase *FileBase::_default = NULL;
FileBase::FileBase(const char *name, PathType t) : _next(NULL),
_name(name),
@ -52,26 +55,41 @@ FileBase::~FileBase()
p->_next = _next;
}
}
if (_default == this) {
_default == NULL;
}
_mutex->unlock();
if (getPathType() == FilePathType) {
extern void remove_filehandle(FileHandle * file);
remove_filehandle(static_cast<FileHandle *>(static_cast<FileLike *>(this)));
extern void remove_filehandle(FileHandle *file);
remove_filehandle(static_cast<FileLike *>(this));
}
}
void FileBase::set_as_default()
{
_mutex->lock();
_default = this;
_mutex->unlock();
}
FileBase *FileBase::lookup(const char *name, unsigned int len)
{
_mutex->lock();
FileBase *p = _head;
while (p != NULL) {
/* Check that p->_name matches name and is the correct length */
if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) {
if (p->_name != NULL && len == std::strlen(p->_name) && std::memcmp(p->_name, name, len) == 0) {
_mutex->unlock();
return p;
}
p = p->_next;
}
if (len == (sizeof "default") - 1 && std::memcmp("default", name, len) == 0) {
return _default;
}
_mutex->unlock();
return NULL;
}

View File

@ -18,9 +18,6 @@
typedef int FILEHANDLE;
#include <cstdio>
#include <cstring>
#include "platform/platform.h"
#include "platform/SingletonPtr.h"
#include "platform/PlatformMutex.h"
@ -55,9 +52,11 @@ public:
static FileBase *get(int n);
/* disallow copy constructor and assignment operators */
void set_as_default();
private:
static FileBase *_head;
static FileBase *_default;
static SingletonPtr<PlatformMutex> _mutex;
FileBase *_next;