Skip to content

Commit 77243ef

Browse files
committed
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.
1 parent cc58a7f commit 77243ef

File tree

9 files changed

+121
-201
lines changed

9 files changed

+121
-201
lines changed

drivers/FileBase.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include "drivers/FileBase.h"
1717
#include "drivers/FileLike.h"
18+
#include "drivers/FileHandle.h"
1819

1920
namespace mbed {
2021

@@ -52,8 +53,8 @@ FileBase::~FileBase() {
5253
_mutex->unlock();
5354

5455
if (getPathType() == FilePathType) {
55-
extern void remove_filehandle(FileLike *file);
56-
remove_filehandle(static_cast<FileLike*>(this));
56+
extern void remove_filehandle(FileHandle *file);
57+
remove_filehandle(static_cast<FileHandle*>(static_cast<FileLike*>(this)));
5758
}
5859
}
5960

drivers/FileHandle.h

Lines changed: 82 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,62 +25,97 @@ namespace mbed {
2525
/** \addtogroup drivers */
2626
/** @{*/
2727

28-
/** An OO equivalent of the internal FILEHANDLE variable
29-
* and associated _sys_* functions.
30-
*
31-
* FileHandle is an abstract class, needing at least sys_write and
32-
* sys_read to be implmented for a simple interactive device.
28+
29+
/** Class FileHandle
3330
*
34-
* No one ever directly tals to/instanciates a FileHandle - it gets
35-
* created by FileSystem, and wrapped up by stdio.
31+
* An abstract interface that represents operations on a file-like
32+
* object. The core functions are read, write, and seek, but only
33+
* a subset of these operations can be provided.
3634
*
37-
* @Note Synchronization level: Set by subclass
35+
* @note to create a file, @see File
36+
* @note Synchronization level: Set by subclass
3837
*/
3938
class FileHandle {
40-
4139
public:
42-
MBED_DEPRECATED_SINCE("mbed-os-5.4",
43-
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
44-
"Replaced by File")
45-
FileHandle() {}
40+
virtual ~FileHandle() {}
4641

47-
/** Write the contents of a buffer to the file
42+
/** Read the contents of a file into a buffer
4843
*
49-
* @param buffer the buffer to write from
50-
* @param length the number of characters to write
44+
* @param buffer The buffer to read in to
45+
* @param size The number of bytes to read
46+
* @return The number of bytes read, 0 at end of file, negative error on failure
47+
*/
48+
virtual ssize_t read(void *buffer, size_t len) = 0;
49+
50+
/** Write the contents of a buffer to a file
5151
*
52-
* @returns
53-
* The number of characters written (possibly 0) on success, -1 on error.
52+
* @param buffer The buffer to write from
53+
* @param size The number of bytes to write
54+
* @return The number of bytes written, negative error on failure
5455
*/
55-
virtual ssize_t write(const void* buffer, size_t length) = 0;
56+
virtual ssize_t write(const void *buffer, size_t len) = 0;
5657

57-
/** Close the file
58+
/** Close a file
5859
*
59-
* @returns
60-
* Zero on success, -1 on error.
60+
* @return 0 on success, negative error code on failure
6161
*/
6262
virtual int close() = 0;
6363

64-
/** Function read
65-
* Reads the contents of the file into a buffer
66-
*
67-
* @param buffer the buffer to read in to
68-
* @param length the number of characters to read
64+
/** Flush any buffers associated with the file
6965
*
70-
* @returns
71-
* The number of characters read (zero at end of file) on success, -1 on error.
66+
* @return 0 on success, negative error code on failure
7267
*/
73-
virtual ssize_t read(void* buffer, size_t length) = 0;
68+
virtual int sync() = 0;
7469

75-
/** Check if the handle is for a interactive terminal device.
76-
* If so, line buffered behaviour is used by default
70+
/** Check if the file in an interactive terminal device
7771
*
78-
* @returns
79-
* 1 if it is a terminal,
80-
* 0 otherwise
72+
* @return True if the file is a terminal
8173
*/
8274
virtual int isatty() = 0;
8375

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;
86+
87+
/** Get the file position of the file
88+
*
89+
* @note This is equivalent to seek(0, SEEK_CUR)
90+
*
91+
* @return The current offset in the file
92+
*/
93+
virtual off_t tell()
94+
{
95+
return seek(0, SEEK_CUR);
96+
}
97+
98+
/** Rewind the file position to the beginning of the file
99+
*
100+
* @note This is equivalent to seek(0, SEEK_SET)
101+
*/
102+
virtual void rewind()
103+
{
104+
seek(0, SEEK_SET);
105+
}
106+
107+
/** Get the size of the file
108+
*
109+
* @return Size of the file in bytes
110+
*/
111+
virtual size_t size()
112+
{
113+
off_t off = tell();
114+
size_t size = seek(0, SEEK_END);
115+
seek(off, SEEK_SET);
116+
return size;
117+
}
118+
84119
/** Move the file position to a given offset from a given location.
85120
*
86121
* @param offset The offset from whence to move to
@@ -91,7 +126,8 @@ class FileHandle {
91126
* new file position on success,
92127
* -1 on failure or unsupported
93128
*/
94-
virtual off_t lseek(off_t offset, int whence) = 0;
129+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::seek")
130+
virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); }
95131

96132
/** Flush any buffers associated with the FileHandle, ensuring it
97133
* is up to date on disk
@@ -100,43 +136,20 @@ class FileHandle {
100136
* 0 on success or un-needed,
101137
* -1 on error
102138
*/
103-
virtual int fsync() = 0;
104-
105-
virtual off_t flen() {
106-
lock();
107-
/* remember our current position */
108-
off_t pos = lseek(0, SEEK_CUR);
109-
if(pos == -1) {
110-
unlock();
111-
return -1;
112-
}
113-
/* seek to the end to get the file length */
114-
off_t res = lseek(0, SEEK_END);
115-
/* return to our old position */
116-
lseek(pos, SEEK_SET);
117-
unlock();
118-
return res;
119-
}
120-
121-
virtual ~FileHandle() {};
139+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::sync")
140+
virtual int fsync() { return sync(); }
122141

123-
protected:
124-
125-
/** Acquire exclusive access to this object.
126-
*/
127-
virtual void lock() {
128-
// Stub
129-
}
130-
131-
/** Release exclusive access to this object.
142+
/** Find the length of the file
143+
*
144+
* @returns
145+
* Length of the file
132146
*/
133-
virtual void unlock() {
134-
// Stub
135-
}
147+
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileHandle::size")
148+
virtual off_t flen() { return size(); }
136149
};
137150

151+
152+
/** @}*/
138153
} // namespace mbed
139154

140155
#endif
141-
142-
/** @}*/

drivers/FileLike.h

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "platform/mbed_toolchain.h"
2020
#include "drivers/FileBase.h"
21+
#include "drivers/FileHandle.h"
2122

2223
namespace mbed {
2324
/** \addtogroup drivers */
@@ -30,121 +31,14 @@ namespace mbed {
3031
*
3132
* @Note Synchronization level: Set by subclass
3233
*/
33-
class FileLike : public FileBase {
34+
class FileLike : public FileHandle, public FileBase {
3435
public:
3536
/** Constructor FileLike
3637
*
3738
* @param name The name to use to open the file.
3839
*/
3940
FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
4041
virtual ~FileLike() {}
41-
42-
/** Read the contents of a file into a buffer
43-
*
44-
* @param buffer The buffer to read in to
45-
* @param size The number of bytes to read
46-
* @return The number of bytes read, 0 at end of file, negative error on failure
47-
*/
48-
virtual ssize_t read(void *buffer, size_t len) = 0;
49-
50-
/** Write the contents of a buffer to a file
51-
*
52-
* @param buffer The buffer to write from
53-
* @param size The number of bytes to write
54-
* @return The number of bytes written, negative error on failure
55-
*/
56-
virtual ssize_t write(const void *buffer, size_t len) = 0;
57-
58-
/** Close a file
59-
*
60-
* @return 0 on success, negative error code on failure
61-
*/
62-
virtual int close() = 0;
63-
64-
/** Flush any buffers associated with the file
65-
*
66-
* @return 0 on success, negative error code on failure
67-
*/
68-
virtual int sync() = 0;
69-
70-
/** Check if the file in an interactive terminal device
71-
*
72-
* @return True if the file is a terminal
73-
*/
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;
86-
87-
/** Get the file position of the file
88-
*
89-
* @return The current offset in the file
90-
*/
91-
virtual off_t tell() = 0;
92-
93-
/** Rewind the file position to the beginning of the file
94-
*
95-
* @note This is equivalent to file_seek(file, 0, FS_SEEK_SET)
96-
*/
97-
virtual void rewind() = 0;
98-
99-
/** Get the size of the file
100-
*
101-
* @return Size of the file in bytes
102-
*/
103-
virtual size_t size() = 0;
104-
105-
/** Move the file position to a given offset from a given location.
106-
*
107-
* @param offset The offset from whence to move to
108-
* @param whence SEEK_SET for the start of the file, SEEK_CUR for the
109-
* current file position, or SEEK_END for the end of the file.
110-
*
111-
* @returns
112-
* new file position on success,
113-
* -1 on failure or unsupported
114-
*/
115-
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::seek")
116-
virtual off_t lseek(off_t offset, int whence) { return seek(offset, whence); }
117-
118-
/** Flush any buffers associated with the FileHandle, ensuring it
119-
* is up to date on disk
120-
*
121-
* @returns
122-
* 0 on success or un-needed,
123-
* -1 on error
124-
*/
125-
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::sync")
126-
virtual int fsync() { return sync(); }
127-
128-
/** Find the length of the file
129-
*
130-
* @returns
131-
* Length of the file
132-
*/
133-
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by FileLike::size")
134-
virtual off_t flen() { return size(); }
135-
136-
protected:
137-
/** Acquire exclusive access to this object.
138-
*/
139-
virtual void lock() {
140-
// Stub
141-
}
142-
143-
/** Release exclusive access to this object.
144-
*/
145-
virtual void unlock() {
146-
// Stub
147-
}
14842
};
14943

15044

drivers/LocalFileSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int LocalFileHandle::isatty() {
142142
return ret;
143143
}
144144

145-
off_t LocalFileHandle::lseek(off_t position, int whence) {
145+
off_t LocalFileHandle::seek(off_t position, int whence) {
146146
lock();
147147
if (whence == SEEK_CUR) {
148148
position += pos;
@@ -157,14 +157,14 @@ off_t LocalFileHandle::lseek(off_t position, int whence) {
157157
return position;
158158
}
159159

160-
int LocalFileHandle::fsync() {
160+
int LocalFileHandle::sync() {
161161
lock();
162162
int ret = semihost_ensure(_fh);
163163
unlock();
164164
return ret;
165165
}
166166

167-
off_t LocalFileHandle::flen() {
167+
size_t LocalFileHandle::size() {
168168
lock();
169169
off_t off = semihost_flen(_fh);
170170
unlock();

drivers/LocalFileSystem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class LocalFileHandle : public FileHandle {
4242

4343
virtual int isatty();
4444

45-
virtual off_t lseek(off_t position, int whence);
45+
virtual off_t seek(off_t position, int whence);
4646

47-
virtual int fsync();
47+
virtual int sync();
4848

49-
virtual off_t flen();
49+
virtual size_t size();
5050

5151
protected:
5252
virtual void lock();

0 commit comments

Comments
 (0)