Skip to content

Commit 5b68689

Browse files
committed
LocalFileSystem: Moved away from deprecated open functions
The old open/opendir functions did not provide a route for errors and relied on implementations manually setting errno. Updated to return errors directly.
1 parent 9e8e0d8 commit 5b68689

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

features/filesystem/FileSystem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ size_t FileSystem::dir_size(fs_dir_t dir)
123123
return size;
124124
}
125125

126+
// Internally used file wrapper that manages memory on close
126127
template <typename F>
127128
class Managed : public F {
128129
public:

features/filesystem/FileSystem.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ class FileSystem : public FileSystemLike {
237237

238238
protected:
239239
// Hooks for FileSystemHandle
240-
virtual int open(File *file, const char *path, int flags);
241-
virtual int open(Dir *dir, const char *path);
242240
virtual int open(FileHandle **file, const char *path, int flags);
243241
virtual int open(DirHandle **dir, const char *path);
244242
};

platform/FileSystemLike.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class FileSystemLike : public FileSystemHandle, public FileBase {
7474
*/
7575
MBED_DEPRECATED_SINCE("mbed-os-5.5",
7676
"Replaced by `int open(FileHandle **, ...)` for propagating error codes")
77-
virtual FileHandle *open(const char *path, int flags)
77+
FileHandle *open(const char *path, int flags)
7878
{
7979
FileHandle *file;
8080
int err = open(&file, path, flags);
@@ -89,7 +89,7 @@ class FileSystemLike : public FileSystemHandle, public FileBase {
8989
*/
9090
MBED_DEPRECATED_SINCE("mbed-os-5.5",
9191
"Replaced by `int open(DirHandle **, ...)` for propagating error codes")
92-
virtual DirHandle *opendir(const char *path)
92+
DirHandle *opendir(const char *path)
9393
{
9494
DirHandle *dir;
9595
int err = open(&dir, path);

platform/LocalFileSystem.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "platform/mbed_semihost_api.h"
2121
#include <string.h>
2222
#include <stdio.h>
23+
#include <errno.h>
2324

2425
namespace mbed {
2526

@@ -235,26 +236,28 @@ class LocalDirHandle : public DirHandle {
235236
}
236237
};
237238

238-
FileHandle *LocalFileSystem::open(const char* name, int flags) {
239+
int LocalFileSystem::open(FileHandle **file, const char* name, int flags) {
239240
// No global state modified so function is thread safe
240241

241242
/* reject filenames with / in them */
242243
for (const char *tmp = name; *tmp; tmp++) {
243244
if (*tmp == '/') {
244-
return NULL;
245+
return -EINVAL;
245246
}
246247
}
247248

248249
int openmode = posix_to_semihost_open_flags(flags);
249250
if (openmode == OPEN_INVALID) {
250-
return NULL;
251+
return -EINVAL;
251252
}
252253

253254
FILEHANDLE fh = semihost_open(name, openmode);
254255
if (fh == -1) {
255-
return NULL;
256+
return -EIO;
256257
}
257-
return new LocalFileHandle(fh);
258+
259+
*file = new LocalFileHandle(fh);
260+
return 0;
258261
}
259262

260263
int LocalFileSystem::remove(const char *filename) {
@@ -263,10 +266,11 @@ int LocalFileSystem::remove(const char *filename) {
263266
return semihost_remove(filename);
264267
}
265268

266-
DirHandle *LocalFileSystem::opendir(const char *name) {
269+
int LocalFileSystem::open(DirHandle **dir, const char *name) {
267270
// No global state modified so function is thread safe
268271

269-
return new LocalDirHandle();
272+
*dir = new LocalDirHandle();
273+
return 0;
270274
}
271275

272276
} // namespace mbed

platform/LocalFileSystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ class LocalFileSystem : public FileSystemLike {
100100

101101
}
102102

103-
virtual FileHandle *open(const char* name, int flags);
103+
virtual int open(FileHandle **file, const char *path, int flags);
104+
virtual int open(DirHandle **dir, const char *name);
104105
virtual int remove(const char *filename);
105-
virtual DirHandle *opendir(const char *name);
106106
};
107107

108108
} // namespace mbed

0 commit comments

Comments
 (0)