Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp
9353 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
/*
16
ArduPilot filesystem interface for ROMFS
17
*/
18
19
#include "AP_Filesystem_config.h"
20
21
#if AP_FILESYSTEM_ROMFS_ENABLED
22
23
#include "AP_Filesystem.h"
24
#include "AP_Filesystem_ROMFS.h"
25
#include <AP_HAL/AP_HAL.h>
26
#include <AP_Math/AP_Math.h>
27
#include <AP_ROMFS/AP_ROMFS.h>
28
29
int AP_Filesystem_ROMFS::open(const char *fname, int flags, bool allow_absolute_paths)
30
{
31
if ((flags & O_ACCMODE) != O_RDONLY) {
32
errno = EROFS;
33
return -1;
34
}
35
36
WITH_SEMAPHORE(record_sem); // search for free file record
37
uint8_t idx;
38
for (idx=0; idx<max_open_file; idx++) {
39
if (file[idx].data == nullptr) {
40
break;
41
}
42
}
43
if (idx == max_open_file) {
44
errno = ENFILE;
45
return -1;
46
}
47
file[idx].data = AP_ROMFS::find_decompress(fname, file[idx].size);
48
if (file[idx].data == nullptr) {
49
errno = ENOENT;
50
return -1;
51
}
52
file[idx].ofs = 0;
53
return idx;
54
}
55
56
int AP_Filesystem_ROMFS::close(int fd)
57
{
58
if (fd < 0 || fd >= max_open_file || file[fd].data == nullptr) {
59
errno = EBADF;
60
return -1;
61
}
62
63
WITH_SEMAPHORE(record_sem); // release file record
64
AP_ROMFS::free(file[fd].data);
65
file[fd].data = nullptr;
66
return 0;
67
}
68
69
int32_t AP_Filesystem_ROMFS::read(int fd, void *buf, uint32_t count)
70
{
71
if (fd < 0 || fd >= max_open_file || file[fd].data == nullptr) {
72
errno = EBADF;
73
return -1;
74
}
75
count = MIN(file[fd].size - file[fd].ofs, count);
76
if (count == 0) {
77
return 0;
78
}
79
memcpy(buf, &file[fd].data[file[fd].ofs], count);
80
file[fd].ofs += count;
81
return count;
82
}
83
84
int32_t AP_Filesystem_ROMFS::write(int fd, const void *buf, uint32_t count)
85
{
86
errno = EROFS;
87
return -1;
88
}
89
90
int AP_Filesystem_ROMFS::fsync(int fd)
91
{
92
return 0;
93
}
94
95
int32_t AP_Filesystem_ROMFS::lseek(int fd, int32_t offset, int seek_from)
96
{
97
if (fd < 0 || fd >= max_open_file || file[fd].data == nullptr) {
98
errno = EBADF;
99
return -1;
100
}
101
switch (seek_from) {
102
case SEEK_SET:
103
if (offset < 0) {
104
errno = EINVAL;
105
return -1;
106
}
107
file[fd].ofs = MIN(file[fd].size, (uint32_t)offset);
108
break;
109
case SEEK_CUR:
110
file[fd].ofs = MIN(file[fd].size, offset+file[fd].ofs);
111
break;
112
case SEEK_END:
113
file[fd].ofs = file[fd].size;
114
break;
115
}
116
return file[fd].ofs;
117
}
118
119
int AP_Filesystem_ROMFS::stat(const char *name, struct stat *stbuf)
120
{
121
uint32_t size;
122
if (!AP_ROMFS::find_size(name, size)) {
123
errno = ENOENT;
124
return -1;
125
}
126
memset(stbuf, 0, sizeof(*stbuf));
127
stbuf->st_size = size;
128
return 0;
129
}
130
131
int AP_Filesystem_ROMFS::unlink(const char *pathname)
132
{
133
errno = EROFS;
134
return -1;
135
}
136
137
int AP_Filesystem_ROMFS::mkdir(const char *pathname)
138
{
139
errno = EROFS;
140
return -1;
141
}
142
143
void *AP_Filesystem_ROMFS::opendir(const char *pathname)
144
{
145
WITH_SEMAPHORE(record_sem); // search for free directory record
146
uint8_t idx;
147
for (idx=0; idx<max_open_dir; idx++) {
148
if (dir[idx].path == nullptr) {
149
break;
150
}
151
}
152
if (idx == max_open_dir) {
153
errno = ENFILE;
154
return nullptr;
155
}
156
dir[idx].ofs = 0;
157
dir[idx].path = strdup(pathname);
158
if (!dir[idx].path) {
159
return nullptr;
160
}
161
162
// Take a sneak peek and reset
163
const char *name = AP_ROMFS::dir_list(dir[idx].path, dir[idx].ofs);
164
dir[idx].ofs = 0;
165
if (!name) {
166
// Directory does not exist
167
return nullptr;
168
}
169
170
return (void*)&dir[idx];
171
}
172
173
struct dirent *AP_Filesystem_ROMFS::readdir(void *dirp)
174
{
175
uint32_t idx = ((rdir*)dirp) - &dir[0];
176
if (idx >= max_open_dir) {
177
errno = EBADF;
178
return nullptr;
179
}
180
const char *name = AP_ROMFS::dir_list(dir[idx].path, dir[idx].ofs);
181
if (!name) {
182
return nullptr;
183
}
184
const uint32_t plen = strlen(dir[idx].path);
185
if (plen > 0) {
186
// Offset to get just file/directory name
187
name += plen + 1;
188
}
189
190
// Copy full name
191
strncpy(dir[idx].de.d_name, name, sizeof(dir[idx].de.d_name));
192
193
const char* slash = strchr(name, '/');
194
if (slash == nullptr) {
195
// File
196
#if AP_FILESYSTEM_HAVE_DIRENT_DTYPE
197
dir[idx].de.d_type = DT_REG;
198
#endif
199
} else {
200
// Directory
201
#if AP_FILESYSTEM_HAVE_DIRENT_DTYPE
202
dir[idx].de.d_type = DT_DIR;
203
#endif
204
205
// Add null termination after directory name
206
const size_t index = slash - name;
207
dir[idx].de.d_name[index] = 0;
208
}
209
210
return &dir[idx].de;
211
}
212
213
int AP_Filesystem_ROMFS::closedir(void *dirp)
214
{
215
uint32_t idx = ((rdir *)dirp) - &dir[0];
216
if (idx >= max_open_dir) {
217
errno = EBADF;
218
return -1;
219
}
220
221
WITH_SEMAPHORE(record_sem); // release directory record
222
free(dir[idx].path);
223
dir[idx].path = nullptr;
224
return 0;
225
}
226
227
// return free disk space in bytes
228
int64_t AP_Filesystem_ROMFS::disk_free(const char *path)
229
{
230
return 0;
231
}
232
233
// return total disk space in bytes
234
int64_t AP_Filesystem_ROMFS::disk_space(const char *path)
235
{
236
return 0;
237
}
238
239
/*
240
set mtime on a file
241
*/
242
bool AP_Filesystem_ROMFS::set_mtime(const char *filename, const uint32_t mtime_sec)
243
{
244
return false;
245
}
246
247
/*
248
Load a file's contents into memory. Returned object must be `delete`d to free
249
the data. The data is guaranteed to be null-terminated such that it can be
250
treated as a string. Overridden in ROMFS to avoid taking twice the memory.
251
*/
252
FileData *AP_Filesystem_ROMFS::load_file(const char *filename)
253
{
254
FileData *fd = NEW_NOTHROW FileData(this);
255
if (!fd) {
256
return nullptr;
257
}
258
// AP_ROMFS adds the guaranteed termination so we don't have to.
259
fd->data = AP_ROMFS::find_decompress(filename, fd->length);
260
if (fd->data == nullptr) {
261
delete fd;
262
return nullptr;
263
}
264
return fd;
265
}
266
267
// unload data from load_file()
268
void AP_Filesystem_ROMFS::unload_file(FileData *fd)
269
{
270
AP_ROMFS::free(fd->data);
271
}
272
273
#endif // AP_FILESYSTEM_ROMFS_ENABLED
274
275