Line data Source code
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 24546 : FileData(void *_backend) : 34 24546 : 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 0 : virtual int open(const char *fname, int flags, bool allow_absolute_paths = false) { 47 0 : return -1; 48 : } 49 0 : virtual int close(int fd) { return -1; } 50 0 : virtual int32_t read(int fd, void *buf, uint32_t count) { return -1; } 51 0 : virtual int32_t write(int fd, const void *buf, uint32_t count) { return -1; } 52 0 : virtual int fsync(int fd) { return 0; } 53 0 : virtual int32_t lseek(int fd, int32_t offset, int whence) { return -1; } 54 0 : virtual int stat(const char *pathname, struct stat *stbuf) { return -1; } 55 0 : virtual int unlink(const char *pathname) { return -1; } 56 0 : virtual int mkdir(const char *pathname) { return -1; } 57 2 : virtual void *opendir(const char *pathname) { return nullptr; } 58 0 : virtual struct dirent *readdir(void *dirp) { return nullptr; } 59 0 : virtual int closedir(void *dirp) { return -1; } 60 0 : virtual int rename(const char *oldpath, const char *newpath) { return -1; } 61 : 62 : // return number of bytes that should be written before fsync for optimal 63 : // streaming performance/robustness. if zero, any number can be written. 64 1026028 : virtual uint32_t bytes_until_fsync(int fd) { return 0; } 65 : 66 : // return free disk space in bytes, -1 on error 67 0 : virtual int64_t disk_free(const char *path) { return 0; } 68 : 69 : // return total disk space in bytes, -1 on error 70 0 : virtual int64_t disk_space(const char *path) { return 0; } 71 : 72 : // set modification time on a file 73 0 : virtual bool set_mtime(const char *filename, const uint32_t mtime_sec) { return false; } 74 : 75 : // retry mount of filesystem if needed 76 0 : virtual bool retry_mount(void) { return true; } 77 : 78 : // unmount filesystem for reboot 79 0 : virtual void unmount(void) {} 80 : 81 : enum class FormatStatus { 82 : NOT_STARTED, 83 : PENDING, 84 : IN_PROGRESS, 85 : SUCCESS, 86 : FAILURE, 87 : }; 88 : 89 : // format sdcard. This is async, monitor get_format_status for progress 90 0 : virtual bool format(void) { return false; } 91 0 : virtual AP_Filesystem_Backend::FormatStatus get_format_status() const { return FormatStatus::NOT_STARTED; } 92 : 93 : /* 94 : Load a file's contents into memory. Returned object must be `delete`d to 95 : free the data. The data is guaranteed to be null-terminated such that it 96 : can be treated as a string. 97 : */ 98 : virtual FileData *load_file(const char *filename); 99 : 100 : // unload data from load_file() 101 : virtual void unload_file(FileData *fd); 102 : 103 : protected: 104 : // return true if file operations are allowed 105 : bool file_op_allowed(void) const; 106 : }; 107 : 108 : 109 : #if CONFIG_HAL_BOARD == HAL_BOARD_SITL 110 : #define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control); return retfail; } } while(0) 111 : #else 112 : #define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { return retfail; } } while(0) 113 : #endif