Skip to content

Commit 0da63ee

Browse files
authored
Merge pull request #4087 from geky/localfs-fslike-fix
LocalFileSystem: Repair the FileSystemLike hooks
2 parents 273f653 + 0fc5ce2 commit 0da63ee

13 files changed

+295
-284
lines changed

features/filesystem/FileSystem.cpp

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/* mbed Microcontroller Library
32
* Copyright (c) 2006-2013 ARM Limited
43
*
@@ -21,8 +20,28 @@
2120

2221

2322
FileSystem::FileSystem(const char *name)
24-
: FileBase(name, FileSystemPathType)
23+
: FileSystemLike(name)
24+
{
25+
}
26+
27+
int FileSystem::remove(const char *path)
28+
{
29+
return -ENOSYS;
30+
}
31+
32+
int FileSystem::rename(const char *path, const char *newpath)
33+
{
34+
return -ENOSYS;
35+
}
36+
37+
int FileSystem::stat(const char *path, struct stat *st)
2538
{
39+
return -ENOSYS;
40+
}
41+
42+
int FileSystem::mkdir(const char *path, mode_t mode)
43+
{
44+
return -ENOSYS;
2645
}
2746

2847
int FileSystem::file_sync(fs_file_t file)
@@ -53,11 +72,6 @@ off_t FileSystem::file_size(fs_file_t file)
5372
return size;
5473
}
5574

56-
int FileSystem::mkdir(const char *path, mode_t mode)
57-
{
58-
return -ENOSYS;
59-
}
60-
6175
int FileSystem::dir_open(fs_dir_t *dir, const char *path)
6276
{
6377
return -ENOSYS;
@@ -109,3 +123,38 @@ size_t FileSystem::dir_size(fs_dir_t dir)
109123
return size;
110124
}
111125

126+
// Internally used file wrapper that manages memory on close
127+
template <typename F>
128+
class Managed : public F {
129+
public:
130+
virtual int close() {
131+
int err = F::close();
132+
delete this;
133+
return err;
134+
}
135+
};
136+
137+
int FileSystem::open(FileHandle **file, const char *path, int flags)
138+
{
139+
File *f = new Managed<File>;
140+
int err = f->open(this, path, flags);
141+
if (err) {
142+
delete f;
143+
return err;
144+
}
145+
146+
*file = f;
147+
return 0;
148+
}
149+
150+
int FileSystem::open(DirHandle **dir, const char *path) {
151+
Dir *d = new Managed<Dir>;
152+
int err = d->open(this, path);
153+
if (err) {
154+
delete d;
155+
return err;
156+
}
157+
158+
*dir = d;
159+
return 0;
160+
}

features/filesystem/FileSystem.h

Lines changed: 21 additions & 9 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
*/
@@ -64,23 +71,23 @@ class FileSystem : public FileBase {
6471
* @param path The name of the file to remove.
6572
* @return 0 on success, negative error code on failure
6673
*/
67-
virtual int remove(const char *path) = 0;
74+
virtual int remove(const char *path);
6875

6976
/** Rename a file in the filesystem.
7077
*
7178
* @param path The name of the file to rename.
7279
* @param newpath The name to rename it to
7380
* @return 0 on success, negative error code on failure
7481
*/
75-
virtual int rename(const char *path, const char *newpath) = 0;
82+
virtual int rename(const char *path, const char *newpath);
7683

7784
/** Store information about the file in a stat structure
7885
*
7986
* @param path The name of the file to find information about
8087
* @param st The stat buffer to write to
8188
* @return 0 on success, negative error code on failure
8289
*/
83-
virtual int stat(const char *path, struct stat *st) = 0;
90+
virtual int stat(const char *path, struct stat *st);
8491

8592
/** Create a directory in the filesystem.
8693
*
@@ -227,6 +234,11 @@ 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(FileHandle **file, const char *path, int flags);
241+
virtual int open(DirHandle **dir, const char *path);
230242
};
231243

232244

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+
/** @}*/

0 commit comments

Comments
 (0)