Skip to content

Commit 3a27568

Browse files
authored
Merge pull request ARMmbed#3773 from geky/fs-filesystem-simple-3
Filesystem: Restructure the filesystem api to be consistent with mbed OS
2 parents 262234d + 129bae4 commit 3a27568

33 files changed

+1957
-875
lines changed

drivers/DirHandle.h

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

1919
#include <stdint.h>
20-
21-
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
22-
# define NAME_MAX 255
23-
typedef int mode_t;
24-
25-
#else
26-
# include <sys/syslimits.h>
27-
#endif
20+
#include "platform/platform.h"
2821

2922
#include "FileHandle.h"
3023

31-
struct dirent {
32-
char d_name[NAME_MAX+1];
33-
uint8_t d_type;
34-
};
35-
36-
enum {
37-
DT_UNKNOWN, // The file type could not be determined.
38-
DT_FIFO, // This is a named pipe (FIFO).
39-
DT_CHR, // This is a character device.
40-
DT_DIR, // This is a directory.
41-
DT_BLK, // This is a block device.
42-
DT_REG, // This is a regular file.
43-
DT_LNK, // This is a symbolic link.
44-
DT_SOCK, // This is a UNIX domain socket.
45-
};
46-
4724
namespace mbed {
4825
/** \addtogroup drivers */
4926
/** @{*/
@@ -64,8 +41,12 @@ namespace mbed {
6441
* @Note Synchronization level: Set by subclass
6542
*/
6643
class DirHandle {
67-
6844
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+
6950
/** Closes the directory.
7051
*
7152
* @returns
@@ -117,22 +98,18 @@ class DirHandle {
11798
virtual void unlock() {
11899
// Stub
119100
}
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) {}
120109
};
121110

122111
} // namespace mbed
123112

124-
typedef mbed::DirHandle DIR;
125-
126-
extern "C" {
127-
DIR *opendir(const char*);
128-
struct dirent *readdir(DIR *);
129-
int closedir(DIR*);
130-
void rewinddir(DIR*);
131-
long telldir(DIR*);
132-
void seekdir(DIR*, long);
133-
int mkdir(const char *name, mode_t n);
134-
};
135-
136113
#endif /* MBED_DIRHANDLE_H */
137114

138115
/** @}*/

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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@
1919
typedef int FILEHANDLE;
2020

2121
#include <stdio.h>
22-
23-
#if defined(__ARMCC_VERSION) || defined(__ICCARM__)
24-
typedef int ssize_t;
25-
typedef long off_t;
26-
27-
#else
28-
# include <sys/types.h>
29-
#endif
22+
#include "platform/platform.h"
3023

3124
namespace mbed {
3225
/** \addtogroup drivers */
@@ -46,6 +39,11 @@ namespace mbed {
4639
class FileHandle {
4740

4841
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+
4947
/** Write the contents of a buffer to the file
5048
*
5149
* @param buffer the buffer to write from
@@ -120,7 +118,7 @@ class FileHandle {
120118
return res;
121119
}
122120

123-
virtual ~FileHandle();
121+
virtual ~FileHandle() {};
124122

125123
protected:
126124

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ namespace mbed {
2525
/** \addtogroup drivers */
2626
/** @{*/
2727

28+
class FileSystem;
29+
2830
class FilePath {
2931
public:
3032
FilePath(const char* file_path);
3133

3234
const char* fileName(void);
3335

3436
bool isFileSystem(void);
35-
FileSystemLike* fileSystem(void);
37+
FileSystem* fileSystem(void);
3638

3739
bool isFile(void);
3840
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

0 commit comments

Comments
 (0)