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.
pull/4087/head
Christopher Haster 2017-03-30 14:40:36 -05:00 committed by Russ Butler
parent eed1cec5d8
commit 0fc5ce2b5e
5 changed files with 19 additions and 34 deletions

View File

@ -123,6 +123,7 @@ size_t FileSystem::dir_size(fs_dir_t dir)
return size; return size;
} }
// Internally used file wrapper that manages memory on close
template <typename F> template <typename F>
class Managed : public F { class Managed : public F {
public: public:

View File

@ -237,8 +237,6 @@ protected:
protected: protected:
// Hooks for FileSystemHandle // Hooks for FileSystemHandle
virtual int open(File *file, const char *path, int flags);
virtual int open(Dir *dir, const char *path);
virtual int open(FileHandle **file, const char *path, int flags); virtual int open(FileHandle **file, const char *path, int flags);
virtual int open(DirHandle **dir, const char *path); virtual int open(DirHandle **dir, const char *path);
}; };

View File

@ -21,7 +21,6 @@
#include "platform/FileSystemHandle.h" #include "platform/FileSystemHandle.h"
#include "platform/FileHandle.h" #include "platform/FileHandle.h"
#include "platform/DirHandle.h" #include "platform/DirHandle.h"
#include <errno.h>
namespace mbed { namespace mbed {
/** \addtogroup platform */ /** \addtogroup platform */
@ -43,26 +42,9 @@ public:
FileSystemLike(const char *name = NULL) : FileBase(name, FileSystemPathType) {} FileSystemLike(const char *name = NULL) : FileBase(name, FileSystemPathType) {}
virtual ~FileSystemLike() {} virtual ~FileSystemLike() {}
/** Open a file on the filesystem // Inherited functions with name conflicts
* using FileSystemHandle::open;
* @param file Destination for the handle to a newly created file using FileSystemHandle::open;
* @param path The name of the file to open
* @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
* bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
* @return 0 on success, negative error code on failure
*/
virtual int open(FileHandle **file, const char *filename, int flags) = 0;
/** Open a directory on the filesystem
*
* @param dir Destination for the handle to the directory
* @param path Name of the directory to open
* @return 0 on success, negative error code on failure
*/
virtual int open(DirHandle **dir, const char *path)
{
return -ENOSYS;
}
/** Open a file on the filesystem /** Open a file on the filesystem
* *
@ -74,7 +56,7 @@ public:
*/ */
MBED_DEPRECATED_SINCE("mbed-os-5.5", MBED_DEPRECATED_SINCE("mbed-os-5.5",
"Replaced by `int open(FileHandle **, ...)` for propagating error codes") "Replaced by `int open(FileHandle **, ...)` for propagating error codes")
virtual FileHandle *open(const char *path, int flags) FileHandle *open(const char *path, int flags)
{ {
FileHandle *file; FileHandle *file;
int err = open(&file, path, flags); int err = open(&file, path, flags);
@ -89,7 +71,7 @@ public:
*/ */
MBED_DEPRECATED_SINCE("mbed-os-5.5", MBED_DEPRECATED_SINCE("mbed-os-5.5",
"Replaced by `int open(DirHandle **, ...)` for propagating error codes") "Replaced by `int open(DirHandle **, ...)` for propagating error codes")
virtual DirHandle *opendir(const char *path) DirHandle *opendir(const char *path)
{ {
DirHandle *dir; DirHandle *dir;
int err = open(&dir, path); int err = open(&dir, path);

View File

@ -20,6 +20,7 @@
#include "platform/mbed_semihost_api.h" #include "platform/mbed_semihost_api.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
namespace mbed { namespace mbed {
@ -235,26 +236,28 @@ protected:
} }
}; };
FileHandle *LocalFileSystem::open(const char* name, int flags) { int LocalFileSystem::open(FileHandle **file, const char* name, int flags) {
// No global state modified so function is thread safe // No global state modified so function is thread safe
/* reject filenames with / in them */ /* reject filenames with / in them */
for (const char *tmp = name; *tmp; tmp++) { for (const char *tmp = name; *tmp; tmp++) {
if (*tmp == '/') { if (*tmp == '/') {
return NULL; return -EINVAL;
} }
} }
int openmode = posix_to_semihost_open_flags(flags); int openmode = posix_to_semihost_open_flags(flags);
if (openmode == OPEN_INVALID) { if (openmode == OPEN_INVALID) {
return NULL; return -EINVAL;
} }
FILEHANDLE fh = semihost_open(name, openmode); FILEHANDLE fh = semihost_open(name, openmode);
if (fh == -1) { if (fh == -1) {
return NULL; return -EIO;
} }
return new LocalFileHandle(fh);
*file = new LocalFileHandle(fh);
return 0;
} }
int LocalFileSystem::remove(const char *filename) { int LocalFileSystem::remove(const char *filename) {
@ -263,10 +266,11 @@ int LocalFileSystem::remove(const char *filename) {
return semihost_remove(filename); return semihost_remove(filename);
} }
DirHandle *LocalFileSystem::opendir(const char *name) { int LocalFileSystem::open(DirHandle **dir, const char *name) {
// No global state modified so function is thread safe // No global state modified so function is thread safe
return new LocalDirHandle(); *dir = new LocalDirHandle();
return 0;
} }
} // namespace mbed } // namespace mbed

View File

@ -106,9 +106,9 @@ public:
} }
virtual FileHandle *open(const char* name, int flags); virtual int open(FileHandle **file, const char *path, int flags);
virtual int open(DirHandle **dir, const char *name);
virtual int remove(const char *filename); virtual int remove(const char *filename);
virtual DirHandle *opendir(const char *name);
}; };
} // namespace mbed } // namespace mbed