mirror of https://github.com/ARMmbed/mbed-os.git
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.pull/3867/head
parent
61c9683644
commit
90fc0b9c47
|
@ -18,6 +18,7 @@
|
||||||
#define DIR_H
|
#define DIR_H
|
||||||
|
|
||||||
#include "filesystem/FileSystem.h"
|
#include "filesystem/FileSystem.h"
|
||||||
|
#include "platform/DirHandle.h"
|
||||||
|
|
||||||
namespace mbed {
|
namespace mbed {
|
||||||
/** \addtogroup filesystem */
|
/** \addtogroup filesystem */
|
||||||
|
@ -26,7 +27,7 @@ namespace mbed {
|
||||||
|
|
||||||
/** Dir class
|
/** Dir class
|
||||||
*/
|
*/
|
||||||
class Dir {
|
class Dir : public DirHandle {
|
||||||
public:
|
public:
|
||||||
/** Create an uninitialized directory
|
/** Create an uninitialized directory
|
||||||
*
|
*
|
||||||
|
|
|
@ -18,34 +18,83 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "platform/platform.h"
|
#include "platform/platform.h"
|
||||||
|
#include "platform/FileHandle.h"
|
||||||
#include "FileHandle.h"
|
|
||||||
|
|
||||||
namespace mbed {
|
namespace mbed {
|
||||||
/** \addtogroup drivers */
|
/** \addtogroup drivers */
|
||||||
/** @{*/
|
/** @{*/
|
||||||
|
|
||||||
|
|
||||||
/** Represents a directory stream. Objects of this type are returned
|
/** Represents a directory stream. Objects of this type are returned
|
||||||
* by a FileSystemLike's opendir method. Implementations must define
|
* by an opendir function. The core functions are read and seek,
|
||||||
* at least closedir, readdir and rewinddir.
|
* but only a subset needs to be provided.
|
||||||
*
|
*
|
||||||
* If a FileSystemLike class defines the opendir method, then the
|
* If a FileSystemLike class defines the opendir method, then the
|
||||||
* directories of an object of that type can be accessed by
|
* directories of an object of that type can be accessed by
|
||||||
* DIR *d = opendir("/example/directory") (or opendir("/example")
|
* DIR *d = opendir("/example/directory") (or opendir("/example")
|
||||||
* to open the root of the filesystem), and then using readdir(d) etc.
|
* to open the root of the filesystem), and then using readdir(d) etc.
|
||||||
*
|
*
|
||||||
* The root directory is considered to contain all FileLike and
|
* The root directory is considered to contain all FileHandle and
|
||||||
* FileSystemLike objects, so the DIR* returned by opendir("/") will
|
* FileSystem objects, so the DIR* returned by opendir("/") will
|
||||||
* reflect this.
|
* reflect this.
|
||||||
*
|
*
|
||||||
|
* @note to create a directory, @see Dir
|
||||||
* @Note Synchronization level: Set by subclass
|
* @Note Synchronization level: Set by subclass
|
||||||
*/
|
*/
|
||||||
class DirHandle {
|
class DirHandle {
|
||||||
public:
|
public:
|
||||||
MBED_DEPRECATED_SINCE("mbed-os-5.4",
|
virtual ~DirHandle() {}
|
||||||
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
|
|
||||||
"Replaced by File")
|
/** Read the next directory entry
|
||||||
DirHandle() {}
|
*
|
||||||
|
* @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.
|
/** Closes the directory.
|
||||||
*
|
*
|
||||||
|
@ -53,7 +102,8 @@ public:
|
||||||
* 0 on success,
|
* 0 on success,
|
||||||
* -1 on error.
|
* -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
|
/** Return the directory entry at the current position, and
|
||||||
* advances the position to the next entry.
|
* advances the position to the next entry.
|
||||||
|
@ -63,11 +113,17 @@ public:
|
||||||
* directory entry at the current position, or NULL on reaching
|
* directory entry at the current position, or NULL on reaching
|
||||||
* end of directory or error.
|
* 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.
|
/** 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.
|
/** Returns the current position of the DirHandle.
|
||||||
*
|
*
|
||||||
|
@ -75,39 +131,18 @@ public:
|
||||||
* the current position,
|
* the current position,
|
||||||
* -1 on error.
|
* -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.
|
/** Sets the position of the DirHandle.
|
||||||
*
|
*
|
||||||
* @param location The location to seek to. Must be a value returned by telldir.
|
* @param location The location to seek to. Must be a value returned by telldir.
|
||||||
*/
|
*/
|
||||||
virtual void seekdir(off_t location) { (void)location;}
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek")
|
||||||
|
virtual void seekdir(off_t location) { seek(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) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} // namespace mbed
|
} // namespace mbed
|
||||||
|
|
||||||
#endif /* MBED_DIRHANDLE_H */
|
#endif /* MBED_DIRHANDLE_H */
|
||||||
|
|
|
@ -55,24 +55,6 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual ssize_t write(const void *buffer, size_t len) = 0;
|
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
|
/** Move the file position to a given offset from from a given location
|
||||||
*
|
*
|
||||||
* @param offset The offset from whence to move to
|
* @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;
|
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
|
/** Get the file position of the file
|
||||||
*
|
*
|
||||||
* @note This is equivalent to seek(0, SEEK_CUR)
|
* @note This is equivalent to seek(0, SEEK_CUR)
|
||||||
|
|
|
@ -27,48 +27,47 @@ public:
|
||||||
give unusual results from readdir.
|
give unusual results from readdir.
|
||||||
*/
|
*/
|
||||||
off_t n;
|
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
|
// No lock can be used in destructor
|
||||||
delete this;
|
delete this;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual struct dirent *readdir() {
|
virtual int read(struct dirent *ent) {
|
||||||
lock();
|
lock();
|
||||||
FileBase *ptr = FileBase::get(n);
|
FileBase *ptr = FileBase::get(n);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
unlock();
|
unlock();
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Increment n, so next readdir gets the next item */
|
/* Increment n, so next readdir gets the next item */
|
||||||
n++;
|
n++;
|
||||||
|
|
||||||
/* Setup cur entry and return a pointer to it */
|
/* 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();
|
unlock();
|
||||||
return &cur_entry;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual off_t telldir() {
|
virtual off_t tell() {
|
||||||
lock();
|
lock();
|
||||||
off_t offset = n;
|
off_t offset = n;
|
||||||
unlock();
|
unlock();
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void seekdir(off_t offset) {
|
virtual void seek(off_t offset) {
|
||||||
lock();
|
lock();
|
||||||
n = offset;
|
n = offset;
|
||||||
unlock();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void rewinddir() {
|
virtual void rewind() {
|
||||||
lock();
|
lock();
|
||||||
n = 0;
|
n = 0;
|
||||||
unlock();
|
unlock();
|
||||||
|
|
|
@ -182,43 +182,42 @@ void LocalFileHandle::unlock() {
|
||||||
class LocalDirHandle : public DirHandle {
|
class LocalDirHandle : public DirHandle {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
struct dirent cur_entry;
|
|
||||||
XFINFO info;
|
XFINFO info;
|
||||||
|
|
||||||
LocalDirHandle() : cur_entry(), info() {
|
LocalDirHandle() : info() {
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual int closedir() {
|
virtual int close() {
|
||||||
// No lock can be used in destructor
|
// No lock can be used in destructor
|
||||||
delete this;
|
delete this;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual struct dirent *readdir() {
|
virtual int read(struct dirent *ent) {
|
||||||
lock();
|
lock();
|
||||||
if (xffind("*", &info)!=0) {
|
if (xffind("*", &info)!=0) {
|
||||||
unlock();
|
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();
|
unlock();
|
||||||
return &cur_entry;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void rewinddir() {
|
virtual void rewind() {
|
||||||
lock();
|
lock();
|
||||||
info.fileID = 0;
|
info.fileID = 0;
|
||||||
unlock();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual off_t telldir() {
|
virtual off_t tell() {
|
||||||
lock();
|
lock();
|
||||||
int fileId = info.fileID;
|
int fileId = info.fileID;
|
||||||
unlock();
|
unlock();
|
||||||
return fileId;
|
return fileId;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void seekdir(off_t offset) {
|
virtual void seek(off_t offset) {
|
||||||
lock();
|
lock();
|
||||||
info.fileID = offset;
|
info.fileID = offset;
|
||||||
unlock();
|
unlock();
|
||||||
|
|
Loading…
Reference in New Issue