Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Filesystem/AP_Filesystem.h
9526 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. This offsets a minimal subset of
17
full functionality offered by posix type interfaces, meeting the
18
needs of ArduPilot
19
*/
20
#pragma once
21
22
#include <stdint.h>
23
#include <AP_HAL/AP_HAL_Boards.h>
24
25
#include "AP_Filesystem_config.h"
26
27
#ifndef MAX_NAME_LEN
28
#define MAX_NAME_LEN 255
29
#endif
30
31
#if (CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS) || (CONFIG_HAL_BOARD == HAL_BOARD_ESP32)
32
#define DT_REG 0
33
#define DT_DIR 1
34
#define DT_LNK 10
35
#endif
36
37
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
38
#if AP_FILESYSTEM_FATFS_ENABLED
39
#include "AP_Filesystem_FATFS.h"
40
#endif
41
#if AP_FILESYSTEM_LITTLEFS_ENABLED
42
#include "AP_Filesystem_FlashMemory_LittleFS.h"
43
#endif
44
45
struct dirent {
46
char d_name[MAX_NAME_LEN]; /* filename */
47
uint8_t d_type;
48
};
49
50
#endif // HAL_BOARD_CHIBIOS
51
52
#include <fcntl.h>
53
#include <errno.h>
54
#include <unistd.h>
55
56
#ifndef AP_FILESYSTEM_FORMAT_ENABLED
57
#define AP_FILESYSTEM_FORMAT_ENABLED 1
58
#endif
59
60
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX || CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_QURT
61
#include "AP_Filesystem_posix.h"
62
#if AP_FILESYSTEM_LITTLEFS_ENABLED
63
#include "AP_Filesystem_FlashMemory_LittleFS.h"
64
#endif
65
#endif
66
67
#if CONFIG_HAL_BOARD == HAL_BOARD_ESP32
68
#include "AP_Filesystem_ESP32.h"
69
#endif
70
71
#include "AP_Filesystem_backend.h"
72
73
class AP_Filesystem {
74
private:
75
struct DirHandle {
76
uint8_t fs_index;
77
void *dir;
78
};
79
80
public:
81
AP_Filesystem() {}
82
83
// functions that closely match the equivalent posix calls
84
int open(const char *fname, int flags, bool allow_absolute_paths = false);
85
int close(int fd);
86
int32_t read(int fd, void *buf, uint32_t count);
87
int32_t write(int fd, const void *buf, uint32_t count);
88
int fsync(int fd);
89
int32_t lseek(int fd, int32_t offset, int whence);
90
int stat(const char *pathname, struct stat *stbuf);
91
92
// stat variant for scripting
93
typedef struct Stat {
94
uint32_t size;
95
int32_t mode;
96
uint32_t mtime;
97
uint32_t atime;
98
uint32_t ctime;
99
bool is_directory(void) const {
100
return (mode & S_IFMT) == S_IFDIR;
101
}
102
} stat_t;
103
bool stat(const char *pathname, stat_t &stbuf);
104
105
int unlink(const char *pathname);
106
int mkdir(const char *pathname);
107
int rename(const char *oldpath, const char *newpath);
108
109
DirHandle *opendir(const char *pathname);
110
struct dirent *readdir(DirHandle *dirp);
111
int closedir(DirHandle *dirp);
112
113
// return number of bytes that should be written before fsync for optimal
114
// streaming performance/robustness. if zero, any number can be written.
115
uint32_t bytes_until_fsync(int fd);
116
117
// return free disk space in bytes, -1 on error
118
int64_t disk_free(const char *path);
119
120
// return total disk space in bytes, -1 on error
121
int64_t disk_space(const char *path);
122
123
// set modification time on a file
124
bool set_mtime(const char *filename, const uint32_t mtime_sec);
125
126
// if filesystem is not running then try a remount. Return true if fs is mounted
127
bool retry_mount(void);
128
129
// unmount filesystem for reboot
130
void unmount(void);
131
132
// returns null-terminated string; cr or lf terminates line
133
bool fgets(char *buf, uint8_t buflen, int fd);
134
135
// run crc32 over file with given name, returns true if successful
136
bool crc32(const char *fname, uint32_t& checksum) WARN_IF_UNUSED;
137
138
// format filesystem. This is async, monitor get_format_status for progress
139
bool format(void);
140
141
// retrieve status of format process:
142
AP_Filesystem_Backend::FormatStatus get_format_status() const;
143
144
/*
145
Load a file's contents into memory. Returned object must be `delete`d to
146
free the data. The data is guaranteed to be null-terminated such that it
147
can be treated as a string.
148
*/
149
FileData *load_file(const char *filename);
150
151
// get_singleton for scripting
152
static AP_Filesystem *get_singleton(void);
153
154
private:
155
struct Backend {
156
const char *prefix;
157
AP_Filesystem_Backend &fs;
158
};
159
static const struct Backend backends[];
160
161
/*
162
find backend by path
163
*/
164
const Backend &backend_by_path(const char *&path) const;
165
166
/*
167
find backend by open fd
168
*/
169
const Backend &backend_by_fd(int &fd) const;
170
171
// support for listing out virtual directory entries (e.g. @SYS
172
// then @MISSION)
173
struct {
174
uint8_t backend_ofs;
175
struct dirent de;
176
uint8_t d_off;
177
} virtual_dirent;
178
};
179
180
namespace AP {
181
AP_Filesystem &FS();
182
};
183
184
185