diff --git a/features/filesystem/Dir.h b/features/filesystem/Dir.h index b4dc4133d1..84b9194584 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 6be86f969a..7478b62b80 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 @@ public: * 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 @@ public: * 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 @@ public: * 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 9f0858d35b..13947a936c 100644 --- a/platform/FileHandle.h +++ b/platform/FileHandle.h @@ -55,24 +55,6 @@ public: */ 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 @@ -84,6 +66,30 @@ public: */ virtual off_t seek(off_t offset, int whence = SEEK_SET) = 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() + { + return 0; + } + + /** Check if the file in an interactive terminal device + * + * @return True if the file is a terminal + */ + virtual int isatty() + { + return false; + } + /** Get the file position of the file * * @note This is equivalent to seek(0, SEEK_CUR) diff --git a/platform/FileSystemLike.cpp b/platform/FileSystemLike.cpp index e7c6aafbb0..3831c22b46 100644 --- a/platform/FileSystemLike.cpp +++ b/platform/FileSystemLike.cpp @@ -27,48 +27,47 @@ public: 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 3b06c3c00b..6215fc432b 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();