From 77243ef46ba150b0a43fcd8b8ce0be467160ad60 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 1 Mar 2017 16:23:03 -0600 Subject: [PATCH 1/4] Filesystem: Revert deprecation of FileHandle As identified by @hasnainvirk, @kjbracey-arm, the FileHandle and FileBase serve two separate functions and their integration is limiting for certain use cases. FileLike is actually the redundant class here, but the multiple inheritance it provides is used as a hack by the retargeting code to get at the FileHandle implementation bound to the FileBase name. It may make more sense for the FileBase to inherit from FileHandle, (with perhaps a different name), but rather than explore the possibility, this will just restore the previous hierarchy. --- drivers/FileBase.cpp | 5 +- drivers/FileHandle.h | 151 +++++++++++++++++++---------------- drivers/FileLike.h | 110 +------------------------ drivers/LocalFileSystem.cpp | 6 +- drivers/LocalFileSystem.h | 6 +- drivers/Stream.h | 12 +++ features/filesystem/File.cpp | 2 +- features/filesystem/File.h | 4 +- platform/mbed_retarget.cpp | 26 +++--- 9 files changed, 121 insertions(+), 201 deletions(-) diff --git a/drivers/FileBase.cpp b/drivers/FileBase.cpp index 862dc74b0d2..47e49e3f5c5 100644 --- a/drivers/FileBase.cpp +++ b/drivers/FileBase.cpp @@ -15,6 +15,7 @@ */ #include "drivers/FileBase.h" #include "drivers/FileLike.h" +#include "drivers/FileHandle.h" namespace mbed { @@ -52,8 +53,8 @@ FileBase::~FileBase() { _mutex->unlock(); if (getPathType() == FilePathType) { - extern void remove_filehandle(FileLike *file); - remove_filehandle(static_cast(this)); + extern void remove_filehandle(FileHandle *file); + remove_filehandle(static_cast(static_cast(this))); } } diff --git a/drivers/FileHandle.h b/drivers/FileHandle.h index 696c8365ed1..9f0858d35ba 100644 --- a/drivers/FileHandle.h +++ b/drivers/FileHandle.h @@ -25,62 +25,97 @@ namespace mbed { /** \addtogroup drivers */ /** @{*/ -/** An OO equivalent of the internal FILEHANDLE variable - * and associated _sys_* functions. - * - * FileHandle is an abstract class, needing at least sys_write and - * sys_read to be implmented for a simple interactive device. + +/** Class FileHandle * - * No one ever directly tals to/instanciates a FileHandle - it gets - * created by FileSystem, and wrapped up by stdio. + * An abstract interface that represents operations on a file-like + * object. The core functions are read, write, and seek, but only + * a subset of these operations can be provided. * - * @Note Synchronization level: Set by subclass + * @note to create a file, @see File + * @note Synchronization level: Set by subclass */ class FileHandle { - public: - MBED_DEPRECATED_SINCE("mbed-os-5.4", - "The mbed 2 filesystem classes have been superseeded by the FileSystem api, " - "Replaced by File") - FileHandle() {} + virtual ~FileHandle() {} - /** Write the contents of a buffer to the file + /** Read the contents of a file into a buffer * - * @param buffer the buffer to write from - * @param length the number of characters to write + * @param buffer The buffer to read in to + * @param size The number of bytes to read + * @return The number of bytes read, 0 at end of file, negative error on failure + */ + virtual ssize_t read(void *buffer, size_t len) = 0; + + /** Write the contents of a buffer to a file * - * @returns - * The number of characters written (possibly 0) on success, -1 on error. + * @param buffer The buffer to write from + * @param size The number of bytes to write + * @return The number of bytes written, negative error on failure */ - virtual ssize_t write(const void* buffer, size_t length) = 0; + virtual ssize_t write(const void *buffer, size_t len) = 0; - /** Close the file + /** Close a file * - * @returns - * Zero on success, -1 on error. + * @return 0 on success, negative error code on failure */ virtual int close() = 0; - /** Function read - * Reads the contents of the file into a buffer - * - * @param buffer the buffer to read in to - * @param length the number of characters to read + /** Flush any buffers associated with the file * - * @returns - * The number of characters read (zero at end of file) on success, -1 on error. + * @return 0 on success, negative error code on failure */ - virtual ssize_t read(void* buffer, size_t length) = 0; + virtual int sync() = 0; - /** Check if the handle is for a interactive terminal device. - * If so, line buffered behaviour is used by default + /** Check if the file in an interactive terminal device * - * @returns - * 1 if it is a terminal, - * 0 otherwise + * @return True if the file is a terminal */ virtual int isatty() = 0; + /** Move the file position to a given offset from from a given location + * + * @param offset The offset from whence to move to + * @param whence The start of where to seek + * SEEK_SET to start from beginning of file, + * SEEK_CUR to start from current position in file, + * SEEK_END to start from end of file + * @return The new offset of the file + */ + virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; + + /** Get the file position of the file + * + * @note This is equivalent to seek(0, SEEK_CUR) + * + * @return The current offset in the file + */ + virtual off_t tell() + { + return seek(0, SEEK_CUR); + } + + /** Rewind the file position to the beginning of the file + * + * @note This is equivalent to seek(0, SEEK_SET) + */ + virtual void rewind() + { + seek(0, SEEK_SET); + } + + /** Get the size of the file + * + * @return Size of the file in bytes + */ + virtual size_t size() + { + off_t off = tell(); + size_t size = seek(0, SEEK_END); + seek(off, SEEK_SET); + return size; + } + /** Move the file position to a given offset from a given location. * * @param offset The offset from whence to move to @@ -91,7 +126,8 @@ class FileHandle { * new file position on success, * -1 on failure or unsupported */ - virtual off_t lseek(off_t offset, int whence) = 0; + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek") + virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); } /** Flush any buffers associated with the FileHandle, ensuring it * is up to date on disk @@ -100,43 +136,20 @@ class FileHandle { * 0 on success or un-needed, * -1 on error */ - virtual int fsync() = 0; - - virtual off_t flen() { - lock(); - /* remember our current position */ - off_t pos = lseek(0, SEEK_CUR); - if(pos == -1) { - unlock(); - return -1; - } - /* seek to the end to get the file length */ - off_t res = lseek(0, SEEK_END); - /* return to our old position */ - lseek(pos, SEEK_SET); - unlock(); - return res; - } - - virtual ~FileHandle() {}; + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync") + virtual int fsync() { return sync(); } -protected: - - /** Acquire exclusive access to this object. - */ - virtual void lock() { - // Stub - } - - /** Release exclusive access to this object. + /** Find the length of the file + * + * @returns + * Length of the file */ - virtual void unlock() { - // Stub - } + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size") + virtual off_t flen() { return size(); } }; + +/** @}*/ } // namespace mbed #endif - -/** @}*/ diff --git a/drivers/FileLike.h b/drivers/FileLike.h index f213272021c..e9c8d8839f7 100644 --- a/drivers/FileLike.h +++ b/drivers/FileLike.h @@ -18,6 +18,7 @@ #include "platform/mbed_toolchain.h" #include "drivers/FileBase.h" +#include "drivers/FileHandle.h" namespace mbed { /** \addtogroup drivers */ @@ -30,7 +31,7 @@ namespace mbed { * * @Note Synchronization level: Set by subclass */ -class FileLike : public FileBase { +class FileLike : public FileHandle, public FileBase { public: /** Constructor FileLike * @@ -38,113 +39,6 @@ class FileLike : public FileBase { */ FileLike(const char *name = NULL) : FileBase(name, FilePathType) {} virtual ~FileLike() {} - - /** Read the contents of a file into a buffer - * - * @param buffer The buffer to read in to - * @param size The number of bytes to read - * @return The number of bytes read, 0 at end of file, negative error on failure - */ - virtual ssize_t read(void *buffer, size_t len) = 0; - - /** Write the contents of a buffer to a file - * - * @param buffer The buffer to write from - * @param size The number of bytes to write - * @return The number of bytes written, negative error on failure - */ - virtual ssize_t write(const void *buffer, size_t len) = 0; - - /** Close a file - * - * @return 0 on success, negative error code on failure - */ - virtual int close() = 0; - - /** Flush any buffers associated with the file - * - * @return 0 on success, negative error code on failure - */ - virtual int sync() = 0; - - /** Check if the file in an interactive terminal device - * - * @return True if the file is a terminal - */ - virtual int isatty() = 0; - - /** Move the file position to a given offset from from a given location - * - * @param offset The offset from whence to move to - * @param whence The start of where to seek - * SEEK_SET to start from beginning of file, - * SEEK_CUR to start from current position in file, - * SEEK_END to start from end of file - * @return The new offset of the file - */ - virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; - - /** Get the file position of the file - * - * @return The current offset in the file - */ - virtual off_t tell() = 0; - - /** Rewind the file position to the beginning of the file - * - * @note This is equivalent to file_seek(file, 0, FS_SEEK_SET) - */ - virtual void rewind() = 0; - - /** Get the size of the file - * - * @return Size of the file in bytes - */ - virtual size_t size() = 0; - - /** Move the file position to a given offset from a given location. - * - * @param offset The offset from whence to move to - * @param whence SEEK_SET for the start of the file, SEEK_CUR for the - * current file position, or SEEK_END for the end of the file. - * - * @returns - * new file position on success, - * -1 on failure or unsupported - */ - MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek") - virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); } - - /** Flush any buffers associated with the FileHandle, ensuring it - * is up to date on disk - * - * @returns - * 0 on success or un-needed, - * -1 on error - */ - MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync") - virtual int fsync() { return sync(); } - - /** Find the length of the file - * - * @returns - * Length of the file - */ - MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size") - virtual off_t flen() { return size(); } - -protected: - /** Acquire exclusive access to this object. - */ - virtual void lock() { - // Stub - } - - /** Release exclusive access to this object. - */ - virtual void unlock() { - // Stub - } }; diff --git a/drivers/LocalFileSystem.cpp b/drivers/LocalFileSystem.cpp index 0e11be4cd4b..037dcfac999 100644 --- a/drivers/LocalFileSystem.cpp +++ b/drivers/LocalFileSystem.cpp @@ -142,7 +142,7 @@ int LocalFileHandle::isatty() { return ret; } -off_t LocalFileHandle::lseek(off_t position, int whence) { +off_t LocalFileHandle::seek(off_t position, int whence) { lock(); if (whence == SEEK_CUR) { position += pos; @@ -157,14 +157,14 @@ off_t LocalFileHandle::lseek(off_t position, int whence) { return position; } -int LocalFileHandle::fsync() { +int LocalFileHandle::sync() { lock(); int ret = semihost_ensure(_fh); unlock(); return ret; } -off_t LocalFileHandle::flen() { +size_t LocalFileHandle::size() { lock(); off_t off = semihost_flen(_fh); unlock(); diff --git a/drivers/LocalFileSystem.h b/drivers/LocalFileSystem.h index c48c0f75b2f..904008ffb51 100644 --- a/drivers/LocalFileSystem.h +++ b/drivers/LocalFileSystem.h @@ -42,11 +42,11 @@ class LocalFileHandle : public FileHandle { virtual int isatty(); - virtual off_t lseek(off_t position, int whence); + virtual off_t seek(off_t position, int whence); - virtual int fsync(); + virtual int sync(); - virtual off_t flen(); + virtual size_t size(); protected: virtual void lock(); diff --git a/drivers/Stream.h b/drivers/Stream.h index 6f8292118a3..44bd55f7cab 100644 --- a/drivers/Stream.h +++ b/drivers/Stream.h @@ -66,6 +66,18 @@ class Stream : public FileLike { std::FILE *_file; + /** Acquire exclusive access to this object. + */ + virtual void lock() { + // Stub + } + + /** Release exclusive access to this object. + */ + virtual void unlock() { + // Stub + } + /* disallow copy constructor and assignment operators */ private: Stream(const Stream&); diff --git a/features/filesystem/File.cpp b/features/filesystem/File.cpp index 78cf92addf2..af8ffc85ff4 100644 --- a/features/filesystem/File.cpp +++ b/features/filesystem/File.cpp @@ -25,7 +25,7 @@ File::File() } File::File(FileSystem *fs, const char *path, int flags) - : FileLike(path), _fs(0), _file(0) + : _fs(0), _file(0) { open(fs, path, flags); } diff --git a/features/filesystem/File.h b/features/filesystem/File.h index 4a3da361f90..7bf1fe42aa3 100644 --- a/features/filesystem/File.h +++ b/features/filesystem/File.h @@ -18,7 +18,7 @@ #define FILE_H #include "filesystem/FileSystem.h" -#include "drivers/FileLike.h" +#include "drivers/FileHandle.h" namespace mbed { /** \addtogroup filesystem */ @@ -27,7 +27,7 @@ namespace mbed { /** File class */ -class File : public FileLike { +class File : public FileHandle { public: /** Create an uninitialized file * diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index bff1d59a699..ae769e1d45b 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -85,11 +85,11 @@ uint32_t mbed_heap_size = 0; * put it in a filehandles array and return the index into that array * (or rather index+3, as filehandles 0-2 are stdin/out/err). */ -static FileLike *filehandles[OPEN_MAX]; +static FileHandle *filehandles[OPEN_MAX]; static SingletonPtr filehandle_mutex; namespace mbed { -void remove_filehandle(FileLike *file) { +void remove_filehandle(FileHandle *file) { filehandle_mutex->lock(); /* Remove all open filehandles for this */ for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { @@ -233,16 +233,16 @@ extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) { filehandle_mutex->unlock(); return -1; } - filehandles[fh_i] = (FileLike*)FILE_HANDLE_RESERVED; + filehandles[fh_i] = (FileHandle*)FILE_HANDLE_RESERVED; filehandle_mutex->unlock(); - FileLike *res = NULL; + FileHandle *res = NULL; - /* FILENAME: ":0x12345678" describes a FileLike* */ + /* FILENAME: ":0x12345678" describes a FileHandle* */ if (name[0] == ':') { void *p; sscanf(name, ":%p", &p); - res = (FileLike*)p; + res = (FileHandle*)p; /* FILENAME: "/file_system/file_name" */ } else { @@ -295,7 +295,7 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh) { if (fh < 3) return 0; errno = EBADF; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; filehandles[fh-3] = NULL; if (fhc == NULL) return -1; @@ -335,7 +335,7 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign #endif n = length; } else { - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; n = fhc->write(buffer, length); @@ -387,7 +387,7 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int #endif n = 1; } else { - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; n = fhc->read(buffer, length); @@ -412,7 +412,7 @@ extern "C" int _isatty(FILEHANDLE fh) /* stdin, stdout and stderr should be tty */ if (fh < 3) return 1; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; int err = fhc->isatty(); @@ -436,7 +436,7 @@ int _lseek(FILEHANDLE fh, int offset, int whence) errno = EBADF; if (fh < 3) return 0; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; #if defined(__ARMCC_VERSION) @@ -451,7 +451,7 @@ extern "C" int PREFIX(_ensure)(FILEHANDLE fh) { errno = EBADF; if (fh < 3) return 0; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; int err = fhc->sync(); @@ -467,7 +467,7 @@ extern "C" long PREFIX(_flen)(FILEHANDLE fh) { errno = EBADF; if (fh < 3) return 0; - FileLike* fhc = filehandles[fh-3]; + FileHandle* fhc = filehandles[fh-3]; if (fhc == NULL) return -1; return fhc->size(); From 61c968364443f18c068e90b7136a3551484eeb44 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 7 Mar 2017 10:40:37 -0600 Subject: [PATCH 2/4] Filesystem: Moved retarget related file interfaces into platform --- features/filesystem/File.h | 2 +- features/filesystem/FileSystem.h | 2 +- mbed.h | 2 +- {drivers => platform}/DirHandle.h | 0 {drivers => platform}/FileBase.cpp | 6 +++--- {drivers => platform}/FileBase.h | 0 {drivers => platform}/FileHandle.h | 0 {drivers => platform}/FileLike.h | 4 ++-- {drivers => platform}/FilePath.cpp | 2 +- {drivers => platform}/FilePath.h | 4 ++-- {drivers => platform}/FileSystemLike.cpp | 2 +- {drivers => platform}/FileSystemLike.h | 6 +++--- {drivers => platform}/LocalFileSystem.cpp | 2 +- {drivers => platform}/LocalFileSystem.h | 2 +- {drivers => platform}/Stream.cpp | 2 +- {drivers => platform}/Stream.h | 4 ++-- platform/mbed_retarget.cpp | 2 +- 17 files changed, 21 insertions(+), 21 deletions(-) rename {drivers => platform}/DirHandle.h (100%) rename {drivers => platform}/FileBase.cpp (96%) rename {drivers => platform}/FileBase.h (100%) rename {drivers => platform}/FileHandle.h (100%) rename {drivers => platform}/FileLike.h (95%) rename {drivers => platform}/FilePath.cpp (98%) rename {drivers => platform}/FilePath.h (94%) rename {drivers => platform}/FileSystemLike.cpp (98%) rename {drivers => platform}/FileSystemLike.h (97%) rename {drivers => platform}/LocalFileSystem.cpp (99%) rename {drivers => platform}/LocalFileSystem.h (98%) rename {drivers => platform}/Stream.cpp (99%) rename {drivers => platform}/Stream.h (97%) diff --git a/features/filesystem/File.h b/features/filesystem/File.h index 7bf1fe42aa3..5043ba1b5f4 100644 --- a/features/filesystem/File.h +++ b/features/filesystem/File.h @@ -18,7 +18,7 @@ #define FILE_H #include "filesystem/FileSystem.h" -#include "drivers/FileHandle.h" +#include "platform/FileHandle.h" namespace mbed { /** \addtogroup filesystem */ diff --git a/features/filesystem/FileSystem.h b/features/filesystem/FileSystem.h index cfc84225d17..d5824b60b1e 100644 --- a/features/filesystem/FileSystem.h +++ b/features/filesystem/FileSystem.h @@ -19,7 +19,7 @@ #include "platform/platform.h" -#include "drivers/FileBase.h" +#include "platform/FileBase.h" #include "BlockDevice.h" namespace mbed { diff --git a/mbed.h b/mbed.h index 4ec380812a0..9b1b01e8d6a 100644 --- a/mbed.h +++ b/mbed.h @@ -94,7 +94,7 @@ #include "drivers/LowPowerTimeout.h" #include "drivers/LowPowerTicker.h" #include "drivers/LowPowerTimer.h" -#include "drivers/LocalFileSystem.h" +#include "platform/LocalFileSystem.h" #include "drivers/InterruptIn.h" #include "platform/mbed_wait_api.h" #include "hal/sleep_api.h" diff --git a/drivers/DirHandle.h b/platform/DirHandle.h similarity index 100% rename from drivers/DirHandle.h rename to platform/DirHandle.h diff --git a/drivers/FileBase.cpp b/platform/FileBase.cpp similarity index 96% rename from drivers/FileBase.cpp rename to platform/FileBase.cpp index 47e49e3f5c5..4458f584b48 100644 --- a/drivers/FileBase.cpp +++ b/platform/FileBase.cpp @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/FileBase.h" -#include "drivers/FileLike.h" -#include "drivers/FileHandle.h" +#include "platform/FileBase.h" +#include "platform/FileLike.h" +#include "platform/FileHandle.h" namespace mbed { diff --git a/drivers/FileBase.h b/platform/FileBase.h similarity index 100% rename from drivers/FileBase.h rename to platform/FileBase.h diff --git a/drivers/FileHandle.h b/platform/FileHandle.h similarity index 100% rename from drivers/FileHandle.h rename to platform/FileHandle.h diff --git a/drivers/FileLike.h b/platform/FileLike.h similarity index 95% rename from drivers/FileLike.h rename to platform/FileLike.h index e9c8d8839f7..a249b68c905 100644 --- a/drivers/FileLike.h +++ b/platform/FileLike.h @@ -17,8 +17,8 @@ #define MBED_FILELIKE_H #include "platform/mbed_toolchain.h" -#include "drivers/FileBase.h" -#include "drivers/FileHandle.h" +#include "platform/FileBase.h" +#include "platform/FileHandle.h" namespace mbed { /** \addtogroup drivers */ diff --git a/drivers/FilePath.cpp b/platform/FilePath.cpp similarity index 98% rename from drivers/FilePath.cpp rename to platform/FilePath.cpp index cdd64f5d9c6..e8733a33b4f 100644 --- a/drivers/FilePath.cpp +++ b/platform/FilePath.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/FilePath.h" +#include "platform/FilePath.h" namespace mbed { diff --git a/drivers/FilePath.h b/platform/FilePath.h similarity index 94% rename from drivers/FilePath.h rename to platform/FilePath.h index 7e1d1a58794..9daa1597c69 100644 --- a/drivers/FilePath.h +++ b/platform/FilePath.h @@ -18,8 +18,8 @@ #include "platform/platform.h" -#include "drivers/FileSystemLike.h" -#include "drivers/FileLike.h" +#include "platform/FileSystemLike.h" +#include "platform/FileLike.h" namespace mbed { /** \addtogroup drivers */ diff --git a/drivers/FileSystemLike.cpp b/platform/FileSystemLike.cpp similarity index 98% rename from drivers/FileSystemLike.cpp rename to platform/FileSystemLike.cpp index 167936af325..e7c6aafbb04 100644 --- a/drivers/FileSystemLike.cpp +++ b/platform/FileSystemLike.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/FileSystemLike.h" +#include "platform/FileSystemLike.h" namespace mbed { diff --git a/drivers/FileSystemLike.h b/platform/FileSystemLike.h similarity index 97% rename from drivers/FileSystemLike.h rename to platform/FileSystemLike.h index 47ece539d9a..b9949c2a878 100644 --- a/drivers/FileSystemLike.h +++ b/platform/FileSystemLike.h @@ -18,9 +18,9 @@ #include "platform/platform.h" -#include "drivers/FileBase.h" -#include "drivers/FileHandle.h" -#include "drivers/DirHandle.h" +#include "platform/FileBase.h" +#include "platform/FileHandle.h" +#include "platform/DirHandle.h" namespace mbed { /** \addtogroup drivers */ diff --git a/drivers/LocalFileSystem.cpp b/platform/LocalFileSystem.cpp similarity index 99% rename from drivers/LocalFileSystem.cpp rename to platform/LocalFileSystem.cpp index 037dcfac999..3b06c3c00b6 100644 --- a/drivers/LocalFileSystem.cpp +++ b/platform/LocalFileSystem.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/LocalFileSystem.h" +#include "platform/LocalFileSystem.h" #if DEVICE_LOCALFILESYSTEM diff --git a/drivers/LocalFileSystem.h b/platform/LocalFileSystem.h similarity index 98% rename from drivers/LocalFileSystem.h rename to platform/LocalFileSystem.h index 904008ffb51..526c2d1f223 100644 --- a/drivers/LocalFileSystem.h +++ b/platform/LocalFileSystem.h @@ -20,7 +20,7 @@ #if DEVICE_LOCALFILESYSTEM -#include "drivers/FileSystemLike.h" +#include "platform/FileSystemLike.h" #include "platform/PlatformMutex.h" namespace mbed { diff --git a/drivers/Stream.cpp b/platform/Stream.cpp similarity index 99% rename from drivers/Stream.cpp rename to platform/Stream.cpp index 9837c812971..76568712e78 100644 --- a/drivers/Stream.cpp +++ b/platform/Stream.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "drivers/Stream.h" +#include "platform/Stream.h" namespace mbed { diff --git a/drivers/Stream.h b/platform/Stream.h similarity index 97% rename from drivers/Stream.h rename to platform/Stream.h index 44bd55f7cab..cbb25a9c615 100644 --- a/drivers/Stream.h +++ b/platform/Stream.h @@ -17,8 +17,8 @@ #define MBED_STREAM_H #include "platform/platform.h" -#include "drivers/FileLike.h" -#include "drivers/FileHandle.h" +#include "platform/FileLike.h" +#include "platform/FileHandle.h" #include namespace mbed { diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index ae769e1d45b..3297fe1882b 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ #include "platform/platform.h" -#include "drivers/FilePath.h" +#include "platform/FilePath.h" #include "hal/serial_api.h" #include "platform/mbed_toolchain.h" #include "platform/mbed_semihost_api.h" From 90fc0b9c477ac52e8e14045f742bc4d931c0ee98 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 7 Mar 2017 11:11:20 -0600 Subject: [PATCH 3/4] FileSystem: Reverted deprecation of DirHandle Should follow same path as FileHandle, although this is less used and there is currently no route to introduce a hook for a customized DirHandle in retarget. --- features/filesystem/Dir.h | 3 +- platform/DirHandle.h | 113 +++++++++++++++++++++++------------ platform/FileHandle.h | 32 ++++++---- platform/FileSystemLike.cpp | 19 +++--- platform/LocalFileSystem.cpp | 19 +++--- 5 files changed, 113 insertions(+), 73 deletions(-) diff --git a/features/filesystem/Dir.h b/features/filesystem/Dir.h index b4dc4133d18..84b9194584e 100644 --- a/features/filesystem/Dir.h +++ b/features/filesystem/Dir.h @@ -18,6 +18,7 @@ #define DIR_H #include "filesystem/FileSystem.h" +#include "platform/DirHandle.h" namespace mbed { /** \addtogroup filesystem */ @@ -26,7 +27,7 @@ namespace mbed { /** Dir class */ -class Dir { +class Dir : public DirHandle { public: /** Create an uninitialized directory * diff --git a/platform/DirHandle.h b/platform/DirHandle.h index 6be86f969ad..7478b62b80e 100644 --- a/platform/DirHandle.h +++ b/platform/DirHandle.h @@ -18,34 +18,83 @@ #include #include "platform/platform.h" - -#include "FileHandle.h" +#include "platform/FileHandle.h" namespace mbed { /** \addtogroup drivers */ /** @{*/ + /** Represents a directory stream. Objects of this type are returned - * by a FileSystemLike's opendir method. Implementations must define - * at least closedir, readdir and rewinddir. + * by an opendir function. The core functions are read and seek, + * but only a subset needs to be provided. * * If a FileSystemLike class defines the opendir method, then the * directories of an object of that type can be accessed by * DIR *d = opendir("/example/directory") (or opendir("/example") * to open the root of the filesystem), and then using readdir(d) etc. * - * The root directory is considered to contain all FileLike and - * FileSystemLike objects, so the DIR* returned by opendir("/") will + * The root directory is considered to contain all FileHandle and + * FileSystem objects, so the DIR* returned by opendir("/") will * reflect this. * + * @note to create a directory, @see Dir * @Note Synchronization level: Set by subclass */ class DirHandle { public: - MBED_DEPRECATED_SINCE("mbed-os-5.4", - "The mbed 2 filesystem classes have been superseeded by the FileSystem api, " - "Replaced by File") - DirHandle() {} + virtual ~DirHandle() {} + + /** Read the next directory entry + * + * @param path The buffer to read the null terminated path name in to + * @param ent The directory entry to fill out + * @return 1 on reading a filename, 0 at end of directory, negative error on failure + */ + virtual ssize_t read(struct dirent *ent) = 0; + + /** Close a directory + * + * return 0 on success, negative error code on failure + */ + virtual int close() = 0; + + /** Set the current position of the directory + * + * @param offset Offset of the location to seek to, + * must be a value returned from tell + */ + virtual void seek(off_t offset) = 0; + + /** Get the current position of the directory + * + * @return Position of the directory that can be passed to rewind + */ + virtual off_t tell() = 0; + + /** Rewind the current position to the beginning of the directory + */ + virtual void rewind() = 0; + + /** Get the sizeof the directory + * + * @return Number of files in the directory + */ + virtual size_t size() + { + off_t off = tell(); + size_t size = 0; + struct dirent *ent = new struct dirent; + + rewind(); + while (read(ent) > 0) { + size += 1; + } + seek(off); + + delete ent; + return size; + } /** Closes the directory. * @@ -53,7 +102,8 @@ class DirHandle { * 0 on success, * -1 on error. */ - virtual int closedir()=0; + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close") + virtual int closedir() { return close(); }; /** Return the directory entry at the current position, and * advances the position to the next entry. @@ -63,11 +113,17 @@ class DirHandle { * directory entry at the current position, or NULL on reaching * end of directory or error. */ - virtual struct dirent *readdir()=0; + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read") + virtual struct dirent *readdir() + { + static struct dirent ent; + return (read(&ent) > 0) ? &ent : NULL; + } /** Resets the position to the beginning of the directory. */ - virtual void rewinddir()=0; + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind") + virtual void rewinddir() { rewind(); } /** Returns the current position of the DirHandle. * @@ -75,39 +131,18 @@ class DirHandle { * the current position, * -1 on error. */ - virtual off_t telldir() { return -1; } + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell") + virtual off_t telldir() { return tell(); } /** Sets the position of the DirHandle. * * @param location The location to seek to. Must be a value returned by telldir. */ - virtual void seekdir(off_t location) { (void)location;} - - virtual ~DirHandle() {} - -protected: - - /** Acquire exclusive access to this object. - */ - virtual void lock() { - // Stub - } - - /** Release exclusive access to this object. - */ - virtual void unlock() { - // Stub - } - -protected: - /** Internal-only constructor to work around deprecated notices when not used - *. due to nested deprecations and difficulty of compilers finding their way around - * the class hierarchy - */ - friend class FileSystemLike; - DirHandle(int) {} + MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek") + virtual void seekdir(off_t location) { seek(location); } }; + } // namespace mbed #endif /* MBED_DIRHANDLE_H */ diff --git a/platform/FileHandle.h b/platform/FileHandle.h index 9f0858d35ba..13947a936c6 100644 --- a/platform/FileHandle.h +++ b/platform/FileHandle.h @@ -55,6 +55,17 @@ class FileHandle { */ virtual ssize_t write(const void *buffer, size_t len) = 0; + /** Move the file position to a given offset from from a given location + * + * @param offset The offset from whence to move to + * @param whence The start of where to seek + * SEEK_SET to start from beginning of file, + * SEEK_CUR to start from current position in file, + * SEEK_END to start from end of file + * @return The new offset of the file + */ + virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; + /** Close a file * * @return 0 on success, negative error code on failure @@ -65,24 +76,19 @@ class FileHandle { * * @return 0 on success, negative error code on failure */ - virtual int sync() = 0; + virtual int sync() + { + return 0; + } /** Check if the file in an interactive terminal device * * @return True if the file is a terminal */ - virtual int isatty() = 0; - - /** Move the file position to a given offset from from a given location - * - * @param offset The offset from whence to move to - * @param whence The start of where to seek - * SEEK_SET to start from beginning of file, - * SEEK_CUR to start from current position in file, - * SEEK_END to start from end of file - * @return The new offset of the file - */ - virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0; + virtual int isatty() + { + return false; + } /** Get the file position of the file * diff --git a/platform/FileSystemLike.cpp b/platform/FileSystemLike.cpp index e7c6aafbb04..3831c22b46e 100644 --- a/platform/FileSystemLike.cpp +++ b/platform/FileSystemLike.cpp @@ -27,48 +27,47 @@ class BaseDirHandle : public DirHandle { give unusual results from readdir. */ off_t n; - struct dirent cur_entry; - BaseDirHandle() : DirHandle(0), n(0), cur_entry() { + BaseDirHandle() : DirHandle(), n(0) { } - virtual int closedir() { + virtual int close() { // No lock can be used in destructor delete this; return 0; } - virtual struct dirent *readdir() { + virtual int read(struct dirent *ent) { lock(); FileBase *ptr = FileBase::get(n); if (ptr == NULL) { unlock(); - return NULL; + return -1; } /* Increment n, so next readdir gets the next item */ n++; /* Setup cur entry and return a pointer to it */ - std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX); + std::strncpy(ent->d_name, ptr->getName(), NAME_MAX); unlock(); - return &cur_entry; + return 0; } - virtual off_t telldir() { + virtual off_t tell() { lock(); off_t offset = n; unlock(); return offset; } - virtual void seekdir(off_t offset) { + virtual void seek(off_t offset) { lock(); n = offset; unlock(); } - virtual void rewinddir() { + virtual void rewind() { lock(); n = 0; unlock(); diff --git a/platform/LocalFileSystem.cpp b/platform/LocalFileSystem.cpp index 3b06c3c00b6..6215fc432b0 100644 --- a/platform/LocalFileSystem.cpp +++ b/platform/LocalFileSystem.cpp @@ -182,43 +182,42 @@ void LocalFileHandle::unlock() { class LocalDirHandle : public DirHandle { public: - struct dirent cur_entry; XFINFO info; - LocalDirHandle() : cur_entry(), info() { + LocalDirHandle() : info() { } - virtual int closedir() { + virtual int close() { // No lock can be used in destructor delete this; return 0; } - virtual struct dirent *readdir() { + virtual int read(struct dirent *ent) { lock(); if (xffind("*", &info)!=0) { unlock(); - return NULL; + return 0; } - memcpy(cur_entry.d_name, info.name, sizeof(info.name)); + memcpy(ent->d_name, info.name, sizeof(info.name)); unlock(); - return &cur_entry; + return 1; } - virtual void rewinddir() { + virtual void rewind() { lock(); info.fileID = 0; unlock(); } - virtual off_t telldir() { + virtual off_t tell() { lock(); int fileId = info.fileID; unlock(); return fileId; } - virtual void seekdir(off_t offset) { + virtual void seek(off_t offset) { lock(); info.fileID = offset; unlock(); From 18bab4e024f70c2a3094f9fa102f372889717fc6 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 14 Mar 2017 11:04:22 -0500 Subject: [PATCH 4/4] Filesystem: Fixed typo in param naming --- features/filesystem/File.h | 4 ++-- features/filesystem/FileSystem.h | 4 ++-- platform/FileHandle.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/features/filesystem/File.h b/features/filesystem/File.h index 5043ba1b5f4..700c241634d 100644 --- a/features/filesystem/File.h +++ b/features/filesystem/File.h @@ -75,7 +75,7 @@ class File : public FileHandle { * @return The number of bytes read, 0 at end of file, negative error on failure */ - virtual ssize_t read(void *buffer, size_t len); + virtual ssize_t read(void *buffer, size_t size); /** Write the contents of a buffer to a file * @@ -83,7 +83,7 @@ class File : public FileHandle { * @param size The number of bytes to write * @return The number of bytes written, negative error on failure */ - virtual ssize_t write(const void *buffer, size_t len); + virtual ssize_t write(const void *buffer, size_t size); /** Flush any buffers associated with the file * diff --git a/features/filesystem/FileSystem.h b/features/filesystem/FileSystem.h index d5824b60b1e..ca7597282d1 100644 --- a/features/filesystem/FileSystem.h +++ b/features/filesystem/FileSystem.h @@ -118,7 +118,7 @@ class FileSystem : public FileBase { * @param size The number of bytes to read * @return The number of bytes read, 0 at end of file, negative error on failure */ - virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len) = 0; + virtual ssize_t file_read(fs_file_t file, void *buffer, size_t size) = 0; /** Write the contents of a buffer to a file * @@ -127,7 +127,7 @@ class FileSystem : public FileBase { * @param size The number of bytes to write * @return The number of bytes written, negative error on failure */ - virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len) = 0; + virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t size) = 0; /** Flush any buffers associated with the file * diff --git a/platform/FileHandle.h b/platform/FileHandle.h index 13947a936c6..e131599600a 100644 --- a/platform/FileHandle.h +++ b/platform/FileHandle.h @@ -45,7 +45,7 @@ class FileHandle { * @param size The number of bytes to read * @return The number of bytes read, 0 at end of file, negative error on failure */ - virtual ssize_t read(void *buffer, size_t len) = 0; + virtual ssize_t read(void *buffer, size_t size) = 0; /** Write the contents of a buffer to a file * @@ -53,7 +53,7 @@ class FileHandle { * @param size The number of bytes to write * @return The number of bytes written, negative error on failure */ - virtual ssize_t write(const void *buffer, size_t len) = 0; + virtual ssize_t write(const void *buffer, size_t size) = 0; /** Move the file position to a given offset from from a given location *