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_backend.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 backend interface.
17
*/
18
#pragma once
19
20
#include <stdint.h>
21
#include <AP_HAL/AP_HAL_Boards.h>
22
23
#include "AP_Filesystem_config.h"
24
25
#include <AP_InternalError/AP_InternalError.h>
26
27
// returned structure from a load_file() call
28
class FileData {
29
public:
30
uint32_t length;
31
const uint8_t *data;
32
33
FileData(void *_backend) :
34
backend(_backend) {}
35
36
// destructor to free data
37
~FileData();
38
private:
39
const void *backend;
40
};
41
42
class AP_Filesystem_Backend {
43
44
public:
45
// functions that closely match the equivalent posix calls
46
virtual int open(const char *fname, int flags, bool allow_absolute_paths = false) {
47
return -1;
48
}
49
virtual int close(int fd) { return -1; }
50
virtual int32_t read(int fd, void *buf, uint32_t count) { return -1; }
51
virtual int32_t write(int fd, const void *buf, uint32_t count) { return -1; }
52
virtual int fsync(int fd) { return 0; }
53
virtual int32_t lseek(int fd, int32_t offset, int whence) { return -1; }
54
virtual int stat(const char *pathname, struct stat *stbuf) { return -1; }
55
virtual int unlink(const char *pathname) { return -1; }
56
virtual int mkdir(const char *pathname) { return -1; }
57
virtual void *opendir(const char *pathname) { return nullptr; }
58
virtual struct dirent *readdir(void *dirp) { return nullptr; }
59
virtual int closedir(void *dirp) { return -1; }
60
virtual int rename(const char *oldpath, const char *newpath) { return -1; }
61
62
// return free disk space in bytes, -1 on error
63
virtual int64_t disk_free(const char *path) { return 0; }
64
65
// return total disk space in bytes, -1 on error
66
virtual int64_t disk_space(const char *path) { return 0; }
67
68
// set modification time on a file
69
virtual bool set_mtime(const char *filename, const uint32_t mtime_sec) { return false; }
70
71
// retry mount of filesystem if needed
72
virtual bool retry_mount(void) { return true; }
73
74
// unmount filesystem for reboot
75
virtual void unmount(void) {}
76
77
enum class FormatStatus {
78
NOT_STARTED,
79
PENDING,
80
IN_PROGRESS,
81
SUCCESS,
82
FAILURE,
83
};
84
85
// format sdcard. This is async, monitor get_format_status for progress
86
virtual bool format(void) { return false; }
87
virtual AP_Filesystem_Backend::FormatStatus get_format_status() const { return FormatStatus::NOT_STARTED; }
88
89
/*
90
Load a file's contents into memory. Returned object must be `delete`d to
91
free the data. The data is guaranteed to be null-terminated such that it
92
can be treated as a string.
93
*/
94
virtual FileData *load_file(const char *filename);
95
96
// unload data from load_file()
97
virtual void unload_file(FileData *fd);
98
99
protected:
100
// return true if file operations are allowed
101
bool file_op_allowed(void) const;
102
};
103
104
105
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
106
#define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control); return retfail; } } while(0)
107
#else
108
#define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { return retfail; } } while(0)
109
#endif
110
111