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