Skip to content

Commit

Permalink
FileSystem: Reverted deprecation of DirHandle
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
geky committed Mar 14, 2017
1 parent 61c9683 commit 90fc0b9
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 73 deletions.
3 changes: 2 additions & 1 deletion features/filesystem/Dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define DIR_H

#include "filesystem/FileSystem.h"
#include "platform/DirHandle.h"

namespace mbed {
/** \addtogroup filesystem */
Expand All @@ -26,7 +27,7 @@ namespace mbed {

/** Dir class
*/
class Dir {
class Dir : public DirHandle {
public:
/** Create an uninitialized directory
*
Expand Down
113 changes: 74 additions & 39 deletions platform/DirHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,92 @@

#include <stdint.h>
#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.
*
* @returns
* 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.
Expand All @@ -63,51 +113,36 @@ 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.
*
* @returns
* 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 */
Expand Down
32 changes: 19 additions & 13 deletions platform/FileHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down
19 changes: 9 additions & 10 deletions platform/FileSystemLike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 9 additions & 10 deletions platform/LocalFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 90fc0b9

Please sign in to comment.