Skip to content

Commit 0fc5ce2

Browse files
gekyc1728p9
authored andcommitted
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 eed1cec commit 0fc5ce2

File tree

5 files changed

+19
-34
lines changed

5 files changed

+19
-34
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: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "platform/FileSystemHandle.h"
2222
#include "platform/FileHandle.h"
2323
#include "platform/DirHandle.h"
24-
#include <errno.h>
2524

2625
namespace mbed {
2726
/** \addtogroup platform */
@@ -43,26 +42,9 @@ class FileSystemLike : public FileSystemHandle, public FileBase {
4342
FileSystemLike(const char *name = NULL) : FileBase(name, FileSystemPathType) {}
4443
virtual ~FileSystemLike() {}
4544

46-
/** Open a file on the filesystem
47-
*
48-
* @param file Destination for the handle to a newly created file
49-
* @param path The name of the file to open
50-
* @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
51-
* bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
52-
* @return 0 on success, negative error code on failure
53-
*/
54-
virtual int open(FileHandle **file, const char *filename, int flags) = 0;
55-
56-
/** Open a directory on the filesystem
57-
*
58-
* @param dir Destination for the handle to the directory
59-
* @param path Name of the directory to open
60-
* @return 0 on success, negative error code on failure
61-
*/
62-
virtual int open(DirHandle **dir, const char *path)
63-
{
64-
return -ENOSYS;
65-
}
45+
// Inherited functions with name conflicts
46+
using FileSystemHandle::open;
47+
using FileSystemHandle::open;
6648

6749
/** Open a file on the filesystem
6850
*
@@ -74,7 +56,7 @@ class FileSystemLike : public FileSystemHandle, public FileBase {
7456
*/
7557
MBED_DEPRECATED_SINCE("mbed-os-5.5",
7658
"Replaced by `int open(FileHandle **, ...)` for propagating error codes")
77-
virtual FileHandle *open(const char *path, int flags)
59+
FileHandle *open(const char *path, int flags)
7860
{
7961
FileHandle *file;
8062
int err = open(&file, path, flags);
@@ -89,7 +71,7 @@ class FileSystemLike : public FileSystemHandle, public FileBase {
8971
*/
9072
MBED_DEPRECATED_SINCE("mbed-os-5.5",
9173
"Replaced by `int open(DirHandle **, ...)` for propagating error codes")
92-
virtual DirHandle *opendir(const char *path)
74+
DirHandle *opendir(const char *path)
9375
{
9476
DirHandle *dir;
9577
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
@@ -106,9 +106,9 @@ class LocalFileSystem : public FileSystemLike {
106106

107107
}
108108

109-
virtual FileHandle *open(const char* name, int flags);
109+
virtual int open(FileHandle **file, const char *path, int flags);
110+
virtual int open(DirHandle **dir, const char *name);
110111
virtual int remove(const char *filename);
111-
virtual DirHandle *opendir(const char *name);
112112
};
113113

114114
} // namespace mbed

0 commit comments

Comments
 (0)