CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp
Views: 1798
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
uint8_t idx;
36
for (idx=0; idx<max_open_file; idx++) {
37
if (file[idx].data == nullptr) {
38
break;
39
}
40
}
41
if (idx == max_open_file) {
42
errno = ENFILE;
43
return -1;
44
}
45
if (file[idx].data != nullptr) {
46
errno = EBUSY;
47
return -1;
48
}
49
file[idx].data = AP_ROMFS::find_decompress(fname, file[idx].size);
50
if (file[idx].data == nullptr) {
51
errno = ENOENT;
52
return -1;
53
}
54
file[idx].ofs = 0;
55
return idx;
56
}
57
58
int AP_Filesystem_ROMFS::close(int fd)
59
{
60
if (fd < 0 || fd >= max_open_file || file[fd].data == nullptr) {
61
errno = EBADF;
62
return -1;
63
}
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
uint8_t idx;
146
for (idx=0; idx<max_open_dir; idx++) {
147
if (dir[idx].path == nullptr) {
148
break;
149
}
150
}
151
if (idx == max_open_dir) {
152
errno = ENFILE;
153
return nullptr;
154
}
155
dir[idx].ofs = 0;
156
dir[idx].path = strdup(pathname);
157
if (!dir[idx].path) {
158
return nullptr;
159
}
160
161
// Take a sneak peek and reset
162
const char *name = AP_ROMFS::dir_list(dir[idx].path, dir[idx].ofs);
163
dir[idx].ofs = 0;
164
if (!name) {
165
// Directory does not exist
166
return nullptr;
167
}
168
169
return (void*)&dir[idx];
170
}
171
172
struct dirent *AP_Filesystem_ROMFS::readdir(void *dirp)
173
{
174
uint32_t idx = ((rdir*)dirp) - &dir[0];
175
if (idx >= max_open_dir) {
176
errno = EBADF;
177
return nullptr;
178
}
179
const char *name = AP_ROMFS::dir_list(dir[idx].path, dir[idx].ofs);
180
if (!name) {
181
return nullptr;
182
}
183
const uint32_t plen = strlen(dir[idx].path);
184
if (plen > 0) {
185
// Offset to get just file/directory name
186
name += plen + 1;
187
}
188
189
// Copy full name
190
strncpy(dir[idx].de.d_name, name, sizeof(dir[idx].de.d_name));
191
192
const char* slash = strchr(name, '/');
193
if (slash == nullptr) {
194
// File
195
#if AP_FILESYSTEM_HAVE_DIRENT_DTYPE
196
dir[idx].de.d_type = DT_REG;
197
#endif
198
} else {
199
// Directory
200
#if AP_FILESYSTEM_HAVE_DIRENT_DTYPE
201
dir[idx].de.d_type = DT_DIR;
202
#endif
203
204
// Add null termination after directory name
205
const size_t index = slash - name;
206
dir[idx].de.d_name[index] = 0;
207
}
208
209
return &dir[idx].de;
210
}
211
212
int AP_Filesystem_ROMFS::closedir(void *dirp)
213
{
214
uint32_t idx = ((rdir *)dirp) - &dir[0];
215
if (idx >= max_open_dir) {
216
errno = EBADF;
217
return -1;
218
}
219
free(dir[idx].path);
220
dir[idx].path = nullptr;
221
return 0;
222
}
223
224
// return free disk space in bytes
225
int64_t AP_Filesystem_ROMFS::disk_free(const char *path)
226
{
227
return 0;
228
}
229
230
// return total disk space in bytes
231
int64_t AP_Filesystem_ROMFS::disk_space(const char *path)
232
{
233
return 0;
234
}
235
236
/*
237
set mtime on a file
238
*/
239
bool AP_Filesystem_ROMFS::set_mtime(const char *filename, const uint32_t mtime_sec)
240
{
241
return false;
242
}
243
244
/*
245
Load a file's contents into memory. Returned object must be `delete`d to free
246
the data. The data is guaranteed to be null-terminated such that it can be
247
treated as a string. Overridden in ROMFS to avoid taking twice the memory.
248
*/
249
FileData *AP_Filesystem_ROMFS::load_file(const char *filename)
250
{
251
FileData *fd = NEW_NOTHROW FileData(this);
252
if (!fd) {
253
return nullptr;
254
}
255
// AP_ROMFS adds the guaranteed termination so we don't have to.
256
fd->data = AP_ROMFS::find_decompress(filename, fd->length);
257
if (fd->data == nullptr) {
258
delete fd;
259
return nullptr;
260
}
261
return fd;
262
}
263
264
// unload data from load_file()
265
void AP_Filesystem_ROMFS::unload_file(FileData *fd)
266
{
267
AP_ROMFS::free(fd->data);
268
}
269
270
#endif // AP_FILESYSTEM_ROMFS_ENABLED
271
272