Skip to content

Commit 2a6a18e

Browse files
committed
Filesystem: Integration with retarget code
1 parent 5b53aec commit 2a6a18e

22 files changed

+297
-163
lines changed

drivers/DirHandle.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define MBED_DIRHANDLE_H
1818

1919
#include <stdint.h>
20-
#include "retarget.h"
20+
#include "platform/platform.h"
2121

2222
#include "FileHandle.h"
2323

@@ -41,8 +41,12 @@ namespace mbed {
4141
* @Note Synchronization level: Set by subclass
4242
*/
4343
class DirHandle {
44-
4544
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() {}
49+
4650
/** Closes the directory.
4751
*
4852
* @returns
@@ -94,22 +98,18 @@ class DirHandle {
9498
virtual void unlock() {
9599
// Stub
96100
}
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) {}
97109
};
98110

99111
} // namespace mbed
100112

101-
typedef mbed::DirHandle DIR;
102-
103-
extern "C" {
104-
DIR *opendir(const char*);
105-
struct dirent *readdir(DIR *);
106-
int closedir(DIR*);
107-
void rewinddir(DIR*);
108-
long telldir(DIR*);
109-
void seekdir(DIR*, long);
110-
int mkdir(const char *name, mode_t n);
111-
};
112-
113113
#endif /* MBED_DIRHANDLE_H */
114114

115115
/** @}*/

drivers/FileBase.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
#include "drivers/FileBase.h"
17+
#include "drivers/FileLike.h"
1718

1819
namespace mbed {
1920

@@ -49,6 +50,11 @@ FileBase::~FileBase() {
4950
}
5051
}
5152
_mutex->unlock();
53+
54+
if (getPathType() == FilePathType) {
55+
extern void remove_filehandle(FileLike *file);
56+
remove_filehandle(static_cast<FileLike*>(this));
57+
}
5258
}
5359

5460
FileBase *FileBase::lookup(const char *name, unsigned int len) {

drivers/FileBase.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@ typedef int FILEHANDLE;
2121
#include <cstdio>
2222
#include <cstring>
2323

24-
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
25-
# define O_RDONLY 0
26-
# define O_WRONLY 1
27-
# define O_RDWR 2
28-
# define O_CREAT 0x0200
29-
# define O_TRUNC 0x0400
30-
# define O_APPEND 0x0008
31-
32-
# define NAME_MAX 255
33-
34-
typedef int mode_t;
35-
typedef int ssize_t;
36-
typedef long off_t;
37-
38-
#else
39-
# include <sys/fcntl.h>
40-
# include <sys/types.h>
41-
# include <sys/syslimits.h>
42-
#endif
43-
4424
#include "platform/platform.h"
4525
#include "platform/SingletonPtr.h"
4626
#include "platform/PlatformMutex.h"
@@ -57,7 +37,6 @@ typedef enum {
5737
class FileBase {
5838
public:
5939
FileBase(const char *name, PathType t);
60-
6140
virtual ~FileBase();
6241

6342
const char* getName(void);

drivers/FileHandle.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
typedef int FILEHANDLE;
2020

2121
#include <stdio.h>
22-
#include "retarget.h"
22+
#include "platform/platform.h"
2323

2424
namespace mbed {
2525
/** \addtogroup drivers */
@@ -39,6 +39,11 @@ namespace mbed {
3939
class FileHandle {
4040

4141
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() {}
46+
4247
/** Write the contents of a buffer to the file
4348
*
4449
* @param buffer the buffer to write from

drivers/FileLike.cpp

Lines changed: 0 additions & 28 deletions
This file was deleted.

drivers/FileLike.h

Lines changed: 115 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,139 @@
1616
#ifndef MBED_FILELIKE_H
1717
#define MBED_FILELIKE_H
1818

19+
#include "platform/toolchain.h"
1920
#include "drivers/FileBase.h"
20-
#include "drivers/FileHandle.h"
2121

2222
namespace mbed {
2323
/** \addtogroup drivers */
2424
/** @{*/
2525

26+
2627
/* Class FileLike
2728
* A file-like object is one that can be opened with fopen by
28-
* fopen("/name", mode). It is intersection of the classes Base and
29-
* FileHandle.
29+
* fopen("/name", mode).
3030
*
3131
* @Note Synchronization level: Set by subclass
3232
*/
33-
class FileLike : public FileHandle, public FileBase {
34-
33+
class FileLike : public FileBase {
3534
public:
36-
/* Constructor FileLike
35+
/** Constructor FileLike
36+
*
37+
* @param name The name to use to open the file.
38+
*/
39+
FileLike(const char *name = NULL) : FileBase(name, FilePathType) {}
40+
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
3751
*
38-
* Variables
39-
* name - The name to use to open the file.
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
4055
*/
41-
FileLike(const char *name);
56+
virtual ssize_t write(const void *buffer, size_t len) = 0;
4257

43-
virtual ~FileLike();
58+
/** Close a file
59+
*
60+
* @return 0 on success, negative error code on failure
61+
*/
62+
virtual int close() = 0;
4463

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+
}
45148
};
46149

150+
151+
/** @}*/
47152
} // namespace mbed
48153

49154
#endif
50-
51-
/** @}*/

drivers/FilePath.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ bool FilePath::isFileSystem(void) {
4949
return (fb->getPathType() == FileSystemPathType);
5050
}
5151

52-
FileSystemLike* FilePath::fileSystem(void) {
52+
FileSystem* FilePath::fileSystem(void) {
5353
if (isFileSystem()) {
54-
return (FileSystemLike*)fb;
54+
return (FileSystem*)fb;
5555
}
5656
return NULL;
5757
}

drivers/FilePath.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "drivers/FileSystemLike.h"
2222
#include "drivers/FileLike.h"
23+
#include "filesystem/FileSystem.h"
2324

2425
namespace mbed {
2526
/** \addtogroup drivers */
@@ -32,7 +33,7 @@ class FilePath {
3233
const char* fileName(void);
3334

3435
bool isFileSystem(void);
35-
FileSystemLike* fileSystem(void);
36+
FileSystem* fileSystem(void);
3637

3738
bool isFile(void);
3839
FileLike* file(void);

drivers/FileSystemLike.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class BaseDirHandle : public DirHandle {
2929
off_t n;
3030
struct dirent cur_entry;
3131

32-
BaseDirHandle() : n(0), cur_entry() {
32+
BaseDirHandle() : DirHandle(0), n(0), cur_entry() {
3333
}
3434

3535
virtual int closedir() {

drivers/FileSystemLike.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,16 @@ class FileSystemLike : public FileBase {
4141
*
4242
* @param name The name to use for the filesystem.
4343
*/
44+
MBED_DEPRECATED_SINCE("mbed-os-5.4",
45+
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
46+
"Replaced by FileSystem")
4447
FileSystemLike(const char *name);
4548

4649
virtual ~FileSystemLike();
4750

51+
MBED_DEPRECATED_SINCE("mbed-os-5.4",
52+
"The mbed 2 filesystem classes have been superseeded by the FileSystem api, "
53+
"Replaced by FileSystem")
4854
static DirHandle *opendir();
4955
friend class BaseDirHandle;
5056

drivers/Stream.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,26 @@ ssize_t Stream::read(void* buffer, size_t length) {
9494
return ptr - (const char*)buffer;
9595
}
9696

97-
off_t Stream::lseek(off_t offset, int whence) {
97+
off_t Stream::seek(off_t offset, int whence) {
9898
return 0;
9999
}
100100

101+
off_t Stream::tell() {
102+
return 0;
103+
}
104+
105+
void Stream::rewind() {
106+
}
107+
101108
int Stream::isatty() {
102109
return 0;
103110
}
104111

105-
int Stream::fsync() {
112+
int Stream::sync() {
106113
return 0;
107114
}
108115

109-
off_t Stream::flen() {
116+
size_t Stream::size() {
110117
return 0;
111118
}
112119

0 commit comments

Comments
 (0)