Skip to content

Commit 90fc0b9

Browse files
committed
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.
1 parent 61c9683 commit 90fc0b9

File tree

5 files changed

+113
-73
lines changed

5 files changed

+113
-73
lines changed

features/filesystem/Dir.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define DIR_H
1919

2020
#include "filesystem/FileSystem.h"
21+
#include "platform/DirHandle.h"
2122

2223
namespace mbed {
2324
/** \addtogroup filesystem */
@@ -26,7 +27,7 @@ namespace mbed {
2627

2728
/** Dir class
2829
*/
29-
class Dir {
30+
class Dir : public DirHandle {
3031
public:
3132
/** Create an uninitialized directory
3233
*

platform/DirHandle.h

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,92 @@
1818

1919
#include <stdint.h>
2020
#include "platform/platform.h"
21-
22-
#include "FileHandle.h"
21+
#include "platform/FileHandle.h"
2322

2423
namespace mbed {
2524
/** \addtogroup drivers */
2625
/** @{*/
2726

27+
2828
/** Represents a directory stream. Objects of this type are returned
29-
* by a FileSystemLike's opendir method. Implementations must define
30-
* at least closedir, readdir and rewinddir.
29+
* by an opendir function. The core functions are read and seek,
30+
* but only a subset needs to be provided.
3131
*
3232
* If a FileSystemLike class defines the opendir method, then the
3333
* directories of an object of that type can be accessed by
3434
* DIR *d = opendir("/example/directory") (or opendir("/example")
3535
* to open the root of the filesystem), and then using readdir(d) etc.
3636
*
37-
* The root directory is considered to contain all FileLike and
38-
* FileSystemLike objects, so the DIR* returned by opendir("/") will
37+
* The root directory is considered to contain all FileHandle and
38+
* FileSystem objects, so the DIR* returned by opendir("/") will
3939
* reflect this.
4040
*
41+
* @note to create a directory, @see Dir
4142
* @Note Synchronization level: Set by subclass
4243
*/
4344
class DirHandle {
4445
public:
45-
MBED_DEPRECATED_SINCE("mbed-os-5.4",
46-
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
47-
"Replaced by File")
48-
DirHandle() {}
46+
virtual ~DirHandle() {}
47+
48+
/** Read the next directory entry
49+
*
50+
* @param path The buffer to read the null terminated path name in to
51+
* @param ent The directory entry to fill out
52+
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
53+
*/
54+
virtual ssize_t read(struct dirent *ent) = 0;
55+
56+
/** Close a directory
57+
*
58+
* return 0 on success, negative error code on failure
59+
*/
60+
virtual int close() = 0;
61+
62+
/** Set the current position of the directory
63+
*
64+
* @param offset Offset of the location to seek to,
65+
* must be a value returned from tell
66+
*/
67+
virtual void seek(off_t offset) = 0;
68+
69+
/** Get the current position of the directory
70+
*
71+
* @return Position of the directory that can be passed to rewind
72+
*/
73+
virtual off_t tell() = 0;
74+
75+
/** Rewind the current position to the beginning of the directory
76+
*/
77+
virtual void rewind() = 0;
78+
79+
/** Get the sizeof the directory
80+
*
81+
* @return Number of files in the directory
82+
*/
83+
virtual size_t size()
84+
{
85+
off_t off = tell();
86+
size_t size = 0;
87+
struct dirent *ent = new struct dirent;
88+
89+
rewind();
90+
while (read(ent) > 0) {
91+
size += 1;
92+
}
93+
seek(off);
94+
95+
delete ent;
96+
return size;
97+
}
4998

5099
/** Closes the directory.
51100
*
52101
* @returns
53102
* 0 on success,
54103
* -1 on error.
55104
*/
56-
virtual int closedir()=0;
105+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close")
106+
virtual int closedir() { return close(); };
57107

58108
/** Return the directory entry at the current position, and
59109
* advances the position to the next entry.
@@ -63,51 +113,36 @@ class DirHandle {
63113
* directory entry at the current position, or NULL on reaching
64114
* end of directory or error.
65115
*/
66-
virtual struct dirent *readdir()=0;
116+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read")
117+
virtual struct dirent *readdir()
118+
{
119+
static struct dirent ent;
120+
return (read(&ent) > 0) ? &ent : NULL;
121+
}
67122

68123
/** Resets the position to the beginning of the directory.
69124
*/
70-
virtual void rewinddir()=0;
125+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind")
126+
virtual void rewinddir() { rewind(); }
71127

72128
/** Returns the current position of the DirHandle.
73129
*
74130
* @returns
75131
* the current position,
76132
* -1 on error.
77133
*/
78-
virtual off_t telldir() { return -1; }
134+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell")
135+
virtual off_t telldir() { return tell(); }
79136

80137
/** Sets the position of the DirHandle.
81138
*
82139
* @param location The location to seek to. Must be a value returned by telldir.
83140
*/
84-
virtual void seekdir(off_t location) { (void)location;}
85-
86-
virtual ~DirHandle() {}
87-
88-
protected:
89-
90-
/** Acquire exclusive access to this object.
91-
*/
92-
virtual void lock() {
93-
// Stub
94-
}
95-
96-
/** Release exclusive access to this object.
97-
*/
98-
virtual void unlock() {
99-
// Stub
100-
}
101-
102-
protected:
103-
/** Internal-only constructor to work around deprecated notices when not used
104-
*. due to nested deprecations and difficulty of compilers finding their way around
105-
* the class hierarchy
106-
*/
107-
friend class FileSystemLike;
108-
DirHandle(int) {}
141+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek")
142+
virtual void seekdir(off_t location) { seek(location); }
109143
};
110144

145+
111146
} // namespace mbed
112147

113148
#endif /* MBED_DIRHANDLE_H */

platform/FileHandle.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ class FileHandle {
5555
*/
5656
virtual ssize_t write(const void *buffer, size_t len) = 0;
5757

58+
/** Move the file position to a given offset from from a given location
59+
*
60+
* @param offset The offset from whence to move to
61+
* @param whence The start of where to seek
62+
* SEEK_SET to start from beginning of file,
63+
* SEEK_CUR to start from current position in file,
64+
* SEEK_END to start from end of file
65+
* @return The new offset of the file
66+
*/
67+
virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;
68+
5869
/** Close a file
5970
*
6071
* @return 0 on success, negative error code on failure
@@ -65,24 +76,19 @@ class FileHandle {
6576
*
6677
* @return 0 on success, negative error code on failure
6778
*/
68-
virtual int sync() = 0;
79+
virtual int sync()
80+
{
81+
return 0;
82+
}
6983

7084
/** Check if the file in an interactive terminal device
7185
*
7286
* @return True if the file is a terminal
7387
*/
74-
virtual int isatty() = 0;
75-
76-
/** Move the file position to a given offset from from a given location
77-
*
78-
* @param offset The offset from whence to move to
79-
* @param whence The start of where to seek
80-
* SEEK_SET to start from beginning of file,
81-
* SEEK_CUR to start from current position in file,
82-
* SEEK_END to start from end of file
83-
* @return The new offset of the file
84-
*/
85-
virtual off_t seek(off_t offset, int whence = SEEK_SET) = 0;
88+
virtual int isatty()
89+
{
90+
return false;
91+
}
8692

8793
/** Get the file position of the file
8894
*

platform/FileSystemLike.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,47 @@ class BaseDirHandle : public DirHandle {
2727
give unusual results from readdir.
2828
*/
2929
off_t n;
30-
struct dirent cur_entry;
3130

32-
BaseDirHandle() : DirHandle(0), n(0), cur_entry() {
31+
BaseDirHandle() : DirHandle(), n(0) {
3332
}
3433

35-
virtual int closedir() {
34+
virtual int close() {
3635
// No lock can be used in destructor
3736
delete this;
3837
return 0;
3938
}
4039

41-
virtual struct dirent *readdir() {
40+
virtual int read(struct dirent *ent) {
4241
lock();
4342
FileBase *ptr = FileBase::get(n);
4443
if (ptr == NULL) {
4544
unlock();
46-
return NULL;
45+
return -1;
4746
}
4847

4948
/* Increment n, so next readdir gets the next item */
5049
n++;
5150

5251
/* Setup cur entry and return a pointer to it */
53-
std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX);
52+
std::strncpy(ent->d_name, ptr->getName(), NAME_MAX);
5453
unlock();
55-
return &cur_entry;
54+
return 0;
5655
}
5756

58-
virtual off_t telldir() {
57+
virtual off_t tell() {
5958
lock();
6059
off_t offset = n;
6160
unlock();
6261
return offset;
6362
}
6463

65-
virtual void seekdir(off_t offset) {
64+
virtual void seek(off_t offset) {
6665
lock();
6766
n = offset;
6867
unlock();
6968
}
7069

71-
virtual void rewinddir() {
70+
virtual void rewind() {
7271
lock();
7372
n = 0;
7473
unlock();

platform/LocalFileSystem.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,43 +182,42 @@ void LocalFileHandle::unlock() {
182182
class LocalDirHandle : public DirHandle {
183183

184184
public:
185-
struct dirent cur_entry;
186185
XFINFO info;
187186

188-
LocalDirHandle() : cur_entry(), info() {
187+
LocalDirHandle() : info() {
189188
}
190189

191-
virtual int closedir() {
190+
virtual int close() {
192191
// No lock can be used in destructor
193192
delete this;
194193
return 0;
195194
}
196195

197-
virtual struct dirent *readdir() {
196+
virtual int read(struct dirent *ent) {
198197
lock();
199198
if (xffind("*", &info)!=0) {
200199
unlock();
201-
return NULL;
200+
return 0;
202201
}
203-
memcpy(cur_entry.d_name, info.name, sizeof(info.name));
202+
memcpy(ent->d_name, info.name, sizeof(info.name));
204203
unlock();
205-
return &cur_entry;
204+
return 1;
206205
}
207206

208-
virtual void rewinddir() {
207+
virtual void rewind() {
209208
lock();
210209
info.fileID = 0;
211210
unlock();
212211
}
213212

214-
virtual off_t telldir() {
213+
virtual off_t tell() {
215214
lock();
216215
int fileId = info.fileID;
217216
unlock();
218217
return fileId;
219218
}
220219

221-
virtual void seekdir(off_t offset) {
220+
virtual void seek(off_t offset) {
222221
lock();
223222
info.fileID = offset;
224223
unlock();

0 commit comments

Comments
 (0)