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

View File

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