Repository navigation
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
179 lines (148 loc) · 3.27 KB
/
Copy pathFileSystem.cpp
File metadata and controls
179 lines (148 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FileSystem.h"
#include <errno.h>
namespace arduino {
FileSystem::FileSystem(const char *name)
{
}
int FileSystem::reformat(BlockDevice *bd)
{
return -ENOSYS;
}
int FileSystem::remove(const char *path)
{
return -ENOSYS;
}
int FileSystem::rename(const char *path, const char *newpath)
{
return -ENOSYS;
}
int FileSystem::stat(const char *path, struct stat *st)
{
return -ENOSYS;
}
int FileSystem::mkdir(const char *path, mode_t mode)
{
return -ENOSYS;
}
int FileSystem::statvfs(const char *path, struct statvfs *buf)
{
return -ENOSYS;
}
int FileSystem::file_sync(fs_file_t file)
{
return 0;
}
int FileSystem::file_isatty(fs_file_t file)
{
return false;
}
off_t FileSystem::file_tell(fs_file_t file)
{
return file_seek(file, 0, SEEK_CUR);
}
void FileSystem::file_rewind(fs_file_t file)
{
file_seek(file, 0, SEEK_SET);
}
off_t FileSystem::file_size(fs_file_t file)
{
off_t off = file_tell(file);
off_t size = file_seek(file, 0, SEEK_END);
file_seek(file, off, SEEK_SET);
return size;
}
int FileSystem::file_truncate(fs_file_t file, off_t length)
{
return -ENOSYS;
}
int FileSystem::dir_open(fs_dir_t *dir, const char *path)
{
return -ENOSYS;
}
int FileSystem::dir_close(fs_dir_t dir)
{
return -ENOSYS;
}
ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
{
return -ENOSYS;
}
void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
{
}
off_t FileSystem::dir_tell(fs_dir_t dir)
{
return 0;
}
void FileSystem::dir_rewind(fs_dir_t dir)
{
// Note, the may not satisfy rewind on all filesystems
dir_seek(dir, 0);
}
size_t FileSystem::dir_size(fs_dir_t dir)
{
off_t off = dir_tell(dir);
size_t size = 0;
struct dirent *ent = new struct dirent;
dir_rewind(dir);
while (true) {
int res = dir_read(dir, ent);
if (res <= 0) {
break;
}
size += 1;
}
dir_seek(dir, off);
delete ent;
return size;
}
// Internally used file wrapper that manages memory on close
template <typename F>
class Managed : public F {
public:
virtual int close()
{
int err = F::close();
delete this;
return err;
}
};
int FileSystem::open(File **file, const char *path, int flags)
{
File *f = new Managed<File>;
int err = f->open(this, path, flags);
if (err) {
delete f;
return err;
}
*file = f;
return 0;
}
int FileSystem::open(Dir **dir, const char *path)
{
Dir *d = new Managed<Dir>;
int err = d->open(this, path);
if (err) {
delete d;
return err;
}
*dir = d;
return 0;
}
} // namespace mbed