Skip to content

Commit eee515c

Browse files
committed
Filesystem: Created prototypical filesystem class
Intention is to make filesystem api and network stack api consistent as current designs diverge greatly. Attempted to change as little as possible outside of api structure.
1 parent 1c862b9 commit eee515c

File tree

6 files changed

+818
-0
lines changed

6 files changed

+818
-0
lines changed

features/filesystem/Dir.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015 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 "Dir.h"
18+
#include "mbed.h"
19+
20+
21+
Dir::Dir()
22+
: _fs(0), _dir(0)
23+
{
24+
}
25+
26+
Dir::Dir(FileSystem *fs, const char *path)
27+
: _fs(0), _dir(0)
28+
{
29+
open(fs, path);
30+
}
31+
32+
Dir::~Dir()
33+
{
34+
if (_fs) {
35+
close();
36+
}
37+
}
38+
39+
int Dir::open(FileSystem *fs, const char *path)
40+
{
41+
if (_fs) {
42+
return FS_ERROR_PARAMETER;
43+
}
44+
45+
_fs = fs;
46+
return _fs->dir_open(&_dir, path);
47+
}
48+
49+
int Dir::close()
50+
{
51+
if (!_fs) {
52+
return FS_ERROR_PARAMETER;
53+
}
54+
55+
int err = _fs->dir_close(_dir);
56+
_fs = 0;
57+
return err;
58+
}
59+
60+
ssize_t Dir::read(char *path, size_t len)
61+
{
62+
MBED_ASSERT(_fs);
63+
return _fs->dir_read(_dir, path, len);
64+
}
65+
66+
ssize_t Dir::read(char *path, size_t len, uint8_t *type)
67+
{
68+
MBED_ASSERT(_fs);
69+
return _fs->dir_read(_dir, path, len, type);
70+
}
71+
72+
void Dir::seek(off_t offset)
73+
{
74+
MBED_ASSERT(_fs);
75+
return _fs->dir_seek(_dir, offset);
76+
}
77+
78+
off_t Dir::tell()
79+
{
80+
MBED_ASSERT(_fs);
81+
return _fs->dir_tell(_dir);
82+
}
83+
84+
void Dir::rewind()
85+
{
86+
MBED_ASSERT(_fs);
87+
return _fs->dir_rewind(_dir);
88+
}
89+
90+
size_t Dir::size()
91+
{
92+
MBED_ASSERT(_fs);
93+
return _fs->dir_size(_dir);
94+
}

features/filesystem/Dir.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015 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+
#ifndef DIR_H
18+
#define DIR_H
19+
20+
#include "filesystem/FileSystem.h"
21+
22+
namespace mbed {
23+
/** \addtogroup filesystem */
24+
/** @{*/
25+
26+
27+
/** Dir class
28+
*/
29+
class Dir {
30+
public:
31+
/** Create an uninitialized directory
32+
*
33+
* Must call open to initialize the directory on a file system
34+
*/
35+
Dir();
36+
37+
/** Open a directory on a filesystem
38+
*
39+
* @param fs Filesystem as target for a directory
40+
* @param path Name of the directory to open
41+
*/
42+
Dir(FileSystem *fs, const char *path);
43+
44+
/** Destroy a file
45+
*
46+
* Closes file if the file is still open
47+
*/
48+
virtual ~Dir();
49+
50+
/** Open a directory on the filesystem
51+
*
52+
* @param fs Filesystem as target for a directory
53+
* @param path Name of the directory to open
54+
* @return 0 on success, negative error code on failure
55+
*/
56+
virtual int open(FileSystem *fs, const char *path);
57+
58+
/** Close a directory
59+
*
60+
* return 0 on success, negative error code on failure
61+
*/
62+
virtual int close();
63+
64+
/** Read the next directory entry
65+
*
66+
* @param path The buffer to read the null terminated path name in to
67+
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
68+
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
69+
*/
70+
virtual ssize_t read(char *path, size_t len);
71+
72+
/** Read the next directory entry
73+
*
74+
* @param dir Dir handle
75+
* @param path The buffer to read the null terminated path name in to
76+
* @param size The maximum number of bytes in the buffer, this is at most FS_NAME_MAX
77+
* @param type The type of the file, one of DT_DIR, DT_REG, etc...
78+
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
79+
*/
80+
virtual ssize_t read(char *path, size_t len, uint8_t *type);
81+
82+
/** Set the current position of the directory
83+
*
84+
* @param offset Offset of the location to seek to,
85+
* must be a value returned from tell
86+
*/
87+
virtual void seek(off_t offset);
88+
89+
/** Get the current position of the directory
90+
*
91+
* @return Position of the directory that can be passed to rewind
92+
*/
93+
virtual off_t tell();
94+
95+
/** Rewind the current position to the beginning of the directory
96+
*/
97+
virtual void rewind();
98+
99+
/** Get the sizeof the directory
100+
*
101+
* @return Number of files in the directory
102+
*/
103+
virtual size_t size();
104+
105+
private:
106+
FileSystem *_fs;
107+
fs_dir_t _dir;
108+
};
109+
110+
111+
/** @}*/
112+
} // namespace mbed
113+
114+
#endif

features/filesystem/File.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015 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 "File.h"
18+
#include "mbed.h"
19+
20+
21+
File::File()
22+
: _fs(0), _file(0)
23+
{
24+
}
25+
26+
File::File(FileSystem *fs, const char *path, int flags)
27+
: _fs(0), _file(0)
28+
{
29+
open(fs, path, flags);
30+
}
31+
32+
File::~File()
33+
{
34+
if (_fs) {
35+
close();
36+
}
37+
}
38+
39+
int File::open(FileSystem *fs, const char *path, int flags)
40+
{
41+
if (_fs) {
42+
return FS_ERROR_PARAMETER;
43+
}
44+
45+
_fs = fs;
46+
return _fs->file_open(&_file, path, flags);
47+
}
48+
49+
int File::close()
50+
{
51+
if (!_fs) {
52+
return FS_ERROR_PARAMETER;
53+
}
54+
55+
int err = _fs->file_close(_file);
56+
_fs = 0;
57+
return err;
58+
}
59+
60+
ssize_t File::read(void *buffer, size_t len)
61+
{
62+
MBED_ASSERT(_fs);
63+
return _fs->file_read(_file, buffer, len);
64+
}
65+
66+
ssize_t File::write(const void *buffer, size_t len)
67+
{
68+
MBED_ASSERT(_fs);
69+
return _fs->file_write(_file, buffer, len);
70+
}
71+
72+
int File::sync()
73+
{
74+
MBED_ASSERT(_fs);
75+
return _fs->file_sync(_file);
76+
}
77+
78+
bool File::isatty()
79+
{
80+
MBED_ASSERT(_fs);
81+
return _fs->file_isatty(_file);
82+
}
83+
84+
off_t File::seek(off_t offset, int whence)
85+
{
86+
MBED_ASSERT(_fs);
87+
return _fs->file_seek(_file, offset, whence);
88+
}
89+
90+
off_t File::tell()
91+
{
92+
MBED_ASSERT(_fs);
93+
return _fs->file_tell(_file);
94+
}
95+
96+
void File::rewind()
97+
{
98+
MBED_ASSERT(_fs);
99+
return _fs->file_rewind(_file);
100+
}
101+
102+
size_t File::size()
103+
{
104+
MBED_ASSERT(_fs);
105+
return _fs->file_size(_file);
106+
}
107+

0 commit comments

Comments
 (0)