Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_backend.h
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14/*15ArduPilot filesystem backend interface.16*/17#pragma once1819#include <stdint.h>20#include <AP_HAL/AP_HAL_Boards.h>2122#include "AP_Filesystem_config.h"2324#include <AP_InternalError/AP_InternalError.h>2526// returned structure from a load_file() call27class FileData {28public:29uint32_t length;30const uint8_t *data;3132FileData(void *_backend) :33backend(_backend) {}3435// destructor to free data36~FileData();37private:38const void *backend;39};4041class AP_Filesystem_Backend {4243public:44// functions that closely match the equivalent posix calls45virtual int open(const char *fname, int flags, bool allow_absolute_paths = false) {46return -1;47}48virtual int close(int fd) { return -1; }49virtual int32_t read(int fd, void *buf, uint32_t count) { return -1; }50virtual int32_t write(int fd, const void *buf, uint32_t count) { return -1; }51virtual int fsync(int fd) { return 0; }52virtual int32_t lseek(int fd, int32_t offset, int whence) { return -1; }53virtual int stat(const char *pathname, struct stat *stbuf) { return -1; }54virtual int unlink(const char *pathname) { return -1; }55virtual int mkdir(const char *pathname) { return -1; }56virtual void *opendir(const char *pathname) { return nullptr; }57virtual struct dirent *readdir(void *dirp) { return nullptr; }58virtual int closedir(void *dirp) { return -1; }59virtual int rename(const char *oldpath, const char *newpath) { return -1; }6061// return free disk space in bytes, -1 on error62virtual int64_t disk_free(const char *path) { return 0; }6364// return total disk space in bytes, -1 on error65virtual int64_t disk_space(const char *path) { return 0; }6667// set modification time on a file68virtual bool set_mtime(const char *filename, const uint32_t mtime_sec) { return false; }6970// retry mount of filesystem if needed71virtual bool retry_mount(void) { return true; }7273// unmount filesystem for reboot74virtual void unmount(void) {}7576enum class FormatStatus {77NOT_STARTED,78PENDING,79IN_PROGRESS,80SUCCESS,81FAILURE,82};8384// format sdcard. This is async, monitor get_format_status for progress85virtual bool format(void) { return false; }86virtual AP_Filesystem_Backend::FormatStatus get_format_status() const { return FormatStatus::NOT_STARTED; }8788/*89Load a file's contents into memory. Returned object must be `delete`d to90free the data. The data is guaranteed to be null-terminated such that it91can be treated as a string.92*/93virtual FileData *load_file(const char *filename);9495// unload data from load_file()96virtual void unload_file(FileData *fd);9798protected:99// return true if file operations are allowed100bool file_op_allowed(void) const;101};102103104#if CONFIG_HAL_BOARD == HAL_BOARD_SITL105#define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control); return retfail; } } while(0)106#else107#define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { return retfail; } } while(0)108#endif109110111