Skip to content

Commit a67f098

Browse files
committed
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.
1 parent dd91b90 commit a67f098

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

platform/FileBase.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include <cstring>
17+
1618
#include "platform/FileBase.h"
1719
#include "platform/FileLike.h"
1820
#include "platform/FileHandle.h"
@@ -21,6 +23,7 @@ namespace mbed {
2123

2224
FileBase *FileBase::_head = NULL;
2325
SingletonPtr<PlatformMutex> FileBase::_mutex;
26+
FileBase *FileBase::_default = NULL;
2427

2528
FileBase::FileBase(const char *name, PathType t) : _next(NULL),
2629
_name(name),
@@ -52,26 +55,41 @@ FileBase::~FileBase()
5255
p->_next = _next;
5356
}
5457
}
58+
59+
if (_default == this) {
60+
_default == NULL;
61+
}
62+
5563
_mutex->unlock();
5664

5765
if (getPathType() == FilePathType) {
58-
extern void remove_filehandle(FileHandle * file);
59-
remove_filehandle(static_cast<FileHandle *>(static_cast<FileLike *>(this)));
66+
extern void remove_filehandle(FileHandle *file);
67+
remove_filehandle(static_cast<FileLike *>(this));
6068
}
6169
}
6270

71+
void FileBase::set_as_default()
72+
{
73+
_mutex->lock();
74+
_default = this;
75+
_mutex->unlock();
76+
}
77+
6378
FileBase *FileBase::lookup(const char *name, unsigned int len)
6479
{
6580
_mutex->lock();
6681
FileBase *p = _head;
6782
while (p != NULL) {
6883
/* Check that p->_name matches name and is the correct length */
69-
if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) {
84+
if (p->_name != NULL && len == std::strlen(p->_name) && std::memcmp(p->_name, name, len) == 0) {
7085
_mutex->unlock();
7186
return p;
7287
}
7388
p = p->_next;
7489
}
90+
if (len == (sizeof "default") - 1 && std::memcmp("default", name, len) == 0) {
91+
return _default;
92+
}
7593
_mutex->unlock();
7694
return NULL;
7795
}

platform/FileBase.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
typedef int FILEHANDLE;
2020

21-
#include <cstdio>
22-
#include <cstring>
23-
2421
#include "platform/platform.h"
2522
#include "platform/SingletonPtr.h"
2623
#include "platform/PlatformMutex.h"
@@ -55,9 +52,11 @@ class FileBase : private NonCopyable<FileBase> {
5552

5653
static FileBase *get(int n);
5754

58-
/* disallow copy constructor and assignment operators */
55+
void set_as_default();
56+
5957
private:
6058
static FileBase *_head;
59+
static FileBase *_default;
6160
static SingletonPtr<PlatformMutex> _mutex;
6261

6362
FileBase *_next;

0 commit comments

Comments
 (0)