Skip to content

Commit 5d6fc71

Browse files
gekyc1728p9
authored andcommitted
FileSystem: Reintegrated FileSystemLike interface
Required for other representations of FileSystems, ie LocalFileSystem Introduces FileSystemHandle for the same behaviour as FileHandle and DirHandle. Requires the following to hook into file/dir lookup: ``` int open(FileHandle **file, const char *filename, int flags) int open(DirHandle **dir, const char *path) ``` This hook is provided by the FileSystem class, so requires no changes from implementations.
1 parent ebeb776 commit 5d6fc71

File tree

11 files changed

+273
-261
lines changed

11 files changed

+273
-261
lines changed

features/filesystem/FileSystem.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
FileSystem::FileSystem(const char *name)
24-
: FileBase(name, FileSystemPathType)
24+
: FileSystemLike(name)
2525
{
2626
}
2727

@@ -109,3 +109,37 @@ size_t FileSystem::dir_size(fs_dir_t dir)
109109
return size;
110110
}
111111

112+
template <typename F>
113+
class Managed : public F {
114+
public:
115+
virtual int close() {
116+
int err = F::close();
117+
delete this;
118+
return err;
119+
}
120+
};
121+
122+
int FileSystem::open(FileHandle **file, const char *path, int flags)
123+
{
124+
File *f = new Managed<File>;
125+
int err = f->open(this, path, flags);
126+
if (err) {
127+
delete f;
128+
return err;
129+
}
130+
131+
*file = f;
132+
return 0;
133+
}
134+
135+
int FileSystem::open(DirHandle **dir, const char *path) {
136+
Dir *d = new Managed<Dir>;
137+
int err = d->open(this, path);
138+
if (err) {
139+
delete d;
140+
return err;
141+
}
142+
143+
*dir = d;
144+
return 0;
145+
}

features/filesystem/FileSystem.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "platform/platform.h"
2121

2222
#include "platform/FileBase.h"
23+
#include "platform/FileHandle.h"
24+
#include "platform/DirHandle.h"
25+
#include "platform/FileSystemLike.h"
2326
#include "BlockDevice.h"
2427

2528
namespace mbed {
@@ -31,15 +34,19 @@ namespace mbed {
3134
typedef void *fs_file_t;
3235
typedef void *fs_dir_t;
3336

34-
/** A filesystem-like object is one that can be used to open files
35-
* though it by fopen("/name/filename", flags)
37+
// Predeclared classes
38+
class Dir;
39+
class File;
40+
41+
/** A filesystem object provides filesystem operations and file operations
42+
* for the File and Dir classes on a block device.
3643
*
37-
* Implementations must define at least open (the default definitions
38-
* of the rest of the functions just return error values).
44+
* Implementations must provide at minimum file operations and mount
45+
* operations for block devices.
3946
*
40-
* @Note Synchronization level: Set by subclass
47+
* @Note Synchronization level: Set by subclass
4148
*/
42-
class FileSystem : public FileBase {
49+
class FileSystem : public FileSystemLike {
4350
public:
4451
/** FileSystem lifetime
4552
*/
@@ -227,6 +234,13 @@ class FileSystem : public FileBase {
227234
* @return Number of files in the directory
228235
*/
229236
virtual size_t dir_size(fs_dir_t dir);
237+
238+
protected:
239+
// Hooks for FileSystemHandle
240+
virtual int open(File *file, const char *path, int flags);
241+
virtual int open(Dir *dir, const char *path);
242+
virtual int open(FileHandle **file, const char *path, int flags);
243+
virtual int open(DirHandle **dir, const char *path);
230244
};
231245

232246

mbed.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
#include "platform/mbed_rtc_time.h"
8787
#include "platform/mbed_poll.h"
8888
#include "platform/ATCmdParser.h"
89+
#include "platform/FileSystemHandle.h"
90+
#include "platform/FileHandle.h"
91+
#include "platform/DirHandle.h"
8992

9093
// mbed Non-hardware components
9194
#include "platform/Callback.h"

platform/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-
FileSystem* FilePath::fileSystem(void) {
52+
FileSystemLike* FilePath::fileSystem(void) {
5353
if (isFileSystem()) {
54-
return (FileSystem*)fb;
54+
return static_cast<FileSystemLike*>(fb);
5555
}
5656
return NULL;
5757
}

platform/FilePath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class FilePath {
3737
const char* fileName(void);
3838

3939
bool isFileSystem(void);
40-
FileSystem* fileSystem(void);
40+
FileSystemLike* fileSystem(void);
4141

4242
bool isFile(void);
4343
FileLike* file(void);

platform/FileSystemHandle.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mbed.h"
18+
#include "FileSystemHandle.h"
19+
#include <errno.h>
20+
21+
int FileSystemHandle::open(DirHandle **dir, const char *path)
22+
{
23+
return -ENOSYS;
24+
}
25+
26+
int FileSystemHandle::remove(const char *path)
27+
{
28+
return -ENOSYS;
29+
}
30+
31+
int FileSystemHandle::rename(const char *path, const char *newpath)
32+
{
33+
return -ENOSYS;
34+
}
35+
36+
int FileSystemHandle::stat(const char *path, struct stat *st)
37+
{
38+
return -ENOSYS;
39+
}
40+
41+
int FileSystemHandle::mkdir(const char *path, mode_t mode)
42+
{
43+
return -ENOSYS;
44+
}

platform/FileSystemHandle.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_FILESYSTEMHANDLE_H
17+
#define MBED_FILESYSTEMHANDLE_H
18+
19+
#include "platform/platform.h"
20+
21+
#include "platform/FileBase.h"
22+
#include "platform/FileHandle.h"
23+
#include "platform/DirHandle.h"
24+
25+
namespace mbed {
26+
/** \addtogroup drivers */
27+
/** @{*/
28+
29+
30+
/** A filesystem-like object is one that can be used to open file-like
31+
* objects though it by fopen("/name/filename", mode)
32+
*
33+
* Implementations must define at least open (the default definitions
34+
* of the rest of the functions just return error values).
35+
*
36+
* @Note Synchronization level: Set by subclass
37+
*/
38+
class FileSystemHandle {
39+
public:
40+
/** FileSystemHandle lifetime
41+
*/
42+
virtual ~FileSystemHandle() {}
43+
44+
/** Open a file on the filesystem
45+
*
46+
* @param file Destination for the handle to a newly created file
47+
* @param path The name of the file to open
48+
* @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
49+
* bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
50+
* @return 0 on success, negative error code on failure
51+
*/
52+
virtual int open(FileHandle **file, const char *filename, int flags) = 0;
53+
54+
/** Open a directory on the filesystem
55+
*
56+
* @param dir Destination for the handle to the directory
57+
* @param path Name of the directory to open
58+
* @return 0 on success, negative error code on failure
59+
*/
60+
virtual int open(DirHandle **dir, const char *path);
61+
62+
/** Remove a file from the filesystem.
63+
*
64+
* @param path The name of the file to remove.
65+
* @return 0 on success, negative error code on failure
66+
*/
67+
virtual int remove(const char *path);
68+
69+
/** Rename a file in the filesystem.
70+
*
71+
* @param path The name of the file to rename.
72+
* @param newpath The name to rename it to
73+
* @return 0 on success, negative error code on failure
74+
*/
75+
virtual int rename(const char *path, const char *newpath);
76+
77+
/** Store information about the file in a stat structure
78+
*
79+
* @param path The name of the file to find information about
80+
* @param st The stat buffer to write to
81+
* @return 0 on success, negative error code on failure
82+
*/
83+
virtual int stat(const char *path, struct stat *st);
84+
85+
/** Create a directory in the filesystem.
86+
*
87+
* @param path The name of the directory to create.
88+
* @param mode The permissions with which to create the directory
89+
* @return 0 on success, negative error code on failure
90+
*/
91+
virtual int mkdir(const char *path, mode_t mode);
92+
};
93+
94+
95+
} // namespace mbed
96+
97+
#endif
98+
99+
/** @}*/

platform/FileSystemLike.cpp

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

0 commit comments

Comments
 (0)