From 0fc5ce2b5e6c73f9e9582d26f0ef4c2cb4607ad6 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 30 Mar 2017 14:40:36 -0500 Subject: [PATCH] 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. --- features/filesystem/FileSystem.cpp | 1 + features/filesystem/FileSystem.h | 2 -- platform/FileSystemLike.h | 28 +++++----------------------- platform/LocalFileSystem.cpp | 18 +++++++++++------- platform/LocalFileSystem.h | 4 ++-- 5 files changed, 19 insertions(+), 34 deletions(-) diff --git a/features/filesystem/FileSystem.cpp b/features/filesystem/FileSystem.cpp index 0f9eb40e08..dfae7a02e6 100644 --- a/features/filesystem/FileSystem.cpp +++ b/features/filesystem/FileSystem.cpp @@ -123,6 +123,7 @@ size_t FileSystem::dir_size(fs_dir_t dir) return size; } +// Internally used file wrapper that manages memory on close template class Managed : public F { public: diff --git a/features/filesystem/FileSystem.h b/features/filesystem/FileSystem.h index 31477b1741..932e797335 100644 --- a/features/filesystem/FileSystem.h +++ b/features/filesystem/FileSystem.h @@ -237,8 +237,6 @@ protected: protected: // 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(DirHandle **dir, const char *path); }; diff --git a/platform/FileSystemLike.h b/platform/FileSystemLike.h index 314b3379df..e2b18f8859 100644 --- a/platform/FileSystemLike.h +++ b/platform/FileSystemLike.h @@ -21,7 +21,6 @@ #include "platform/FileSystemHandle.h" #include "platform/FileHandle.h" #include "platform/DirHandle.h" -#include namespace mbed { /** \addtogroup platform */ @@ -43,26 +42,9 @@ public: FileSystemLike(const char *name = NULL) : FileBase(name, FileSystemPathType) {} virtual ~FileSystemLike() {} - /** Open a file on the filesystem - * - * @param file Destination for the handle to a newly created file - * @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; - } + // Inherited functions with name conflicts + using FileSystemHandle::open; + using FileSystemHandle::open; /** Open a file on the filesystem * @@ -74,7 +56,7 @@ public: */ MBED_DEPRECATED_SINCE("mbed-os-5.5", "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; int err = open(&file, path, flags); @@ -89,7 +71,7 @@ public: */ MBED_DEPRECATED_SINCE("mbed-os-5.5", "Replaced by `int open(DirHandle **, ...)` for propagating error codes") - virtual DirHandle *opendir(const char *path) + DirHandle *opendir(const char *path) { DirHandle *dir; int err = open(&dir, path); diff --git a/platform/LocalFileSystem.cpp b/platform/LocalFileSystem.cpp index 8b336fb5be..13daee31c2 100644 --- a/platform/LocalFileSystem.cpp +++ b/platform/LocalFileSystem.cpp @@ -20,6 +20,7 @@ #include "platform/mbed_semihost_api.h" #include #include +#include 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 /* reject filenames with / in them */ for (const char *tmp = name; *tmp; tmp++) { if (*tmp == '/') { - return NULL; + return -EINVAL; } } int openmode = posix_to_semihost_open_flags(flags); if (openmode == OPEN_INVALID) { - return NULL; + return -EINVAL; } FILEHANDLE fh = semihost_open(name, openmode); if (fh == -1) { - return NULL; + return -EIO; } - return new LocalFileHandle(fh); + + *file = new LocalFileHandle(fh); + return 0; } int LocalFileSystem::remove(const char *filename) { @@ -263,10 +266,11 @@ int LocalFileSystem::remove(const char *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 - return new LocalDirHandle(); + *dir = new LocalDirHandle(); + return 0; } } // namespace mbed diff --git a/platform/LocalFileSystem.h b/platform/LocalFileSystem.h index 5a6a6a4978..6ed24ff73c 100644 --- a/platform/LocalFileSystem.h +++ b/platform/LocalFileSystem.h @@ -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 DirHandle *opendir(const char *name); }; } // namespace mbed