Path: blob/master/libraries/AP_Filesystem/AP_Filesystem.h
9526 views
/*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 interface. This offsets a minimal subset of16full functionality offered by posix type interfaces, meeting the17needs of ArduPilot18*/19#pragma once2021#include <stdint.h>22#include <AP_HAL/AP_HAL_Boards.h>2324#include "AP_Filesystem_config.h"2526#ifndef MAX_NAME_LEN27#define MAX_NAME_LEN 25528#endif2930#if (CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS) || (CONFIG_HAL_BOARD == HAL_BOARD_ESP32)31#define DT_REG 032#define DT_DIR 133#define DT_LNK 1034#endif3536#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS37#if AP_FILESYSTEM_FATFS_ENABLED38#include "AP_Filesystem_FATFS.h"39#endif40#if AP_FILESYSTEM_LITTLEFS_ENABLED41#include "AP_Filesystem_FlashMemory_LittleFS.h"42#endif4344struct dirent {45char d_name[MAX_NAME_LEN]; /* filename */46uint8_t d_type;47};4849#endif // HAL_BOARD_CHIBIOS5051#include <fcntl.h>52#include <errno.h>53#include <unistd.h>5455#ifndef AP_FILESYSTEM_FORMAT_ENABLED56#define AP_FILESYSTEM_FORMAT_ENABLED 157#endif5859#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX || CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_QURT60#include "AP_Filesystem_posix.h"61#if AP_FILESYSTEM_LITTLEFS_ENABLED62#include "AP_Filesystem_FlashMemory_LittleFS.h"63#endif64#endif6566#if CONFIG_HAL_BOARD == HAL_BOARD_ESP3267#include "AP_Filesystem_ESP32.h"68#endif6970#include "AP_Filesystem_backend.h"7172class AP_Filesystem {73private:74struct DirHandle {75uint8_t fs_index;76void *dir;77};7879public:80AP_Filesystem() {}8182// functions that closely match the equivalent posix calls83int open(const char *fname, int flags, bool allow_absolute_paths = false);84int close(int fd);85int32_t read(int fd, void *buf, uint32_t count);86int32_t write(int fd, const void *buf, uint32_t count);87int fsync(int fd);88int32_t lseek(int fd, int32_t offset, int whence);89int stat(const char *pathname, struct stat *stbuf);9091// stat variant for scripting92typedef struct Stat {93uint32_t size;94int32_t mode;95uint32_t mtime;96uint32_t atime;97uint32_t ctime;98bool is_directory(void) const {99return (mode & S_IFMT) == S_IFDIR;100}101} stat_t;102bool stat(const char *pathname, stat_t &stbuf);103104int unlink(const char *pathname);105int mkdir(const char *pathname);106int rename(const char *oldpath, const char *newpath);107108DirHandle *opendir(const char *pathname);109struct dirent *readdir(DirHandle *dirp);110int closedir(DirHandle *dirp);111112// return number of bytes that should be written before fsync for optimal113// streaming performance/robustness. if zero, any number can be written.114uint32_t bytes_until_fsync(int fd);115116// return free disk space in bytes, -1 on error117int64_t disk_free(const char *path);118119// return total disk space in bytes, -1 on error120int64_t disk_space(const char *path);121122// set modification time on a file123bool set_mtime(const char *filename, const uint32_t mtime_sec);124125// if filesystem is not running then try a remount. Return true if fs is mounted126bool retry_mount(void);127128// unmount filesystem for reboot129void unmount(void);130131// returns null-terminated string; cr or lf terminates line132bool fgets(char *buf, uint8_t buflen, int fd);133134// run crc32 over file with given name, returns true if successful135bool crc32(const char *fname, uint32_t& checksum) WARN_IF_UNUSED;136137// format filesystem. This is async, monitor get_format_status for progress138bool format(void);139140// retrieve status of format process:141AP_Filesystem_Backend::FormatStatus get_format_status() const;142143/*144Load a file's contents into memory. Returned object must be `delete`d to145free the data. The data is guaranteed to be null-terminated such that it146can be treated as a string.147*/148FileData *load_file(const char *filename);149150// get_singleton for scripting151static AP_Filesystem *get_singleton(void);152153private:154struct Backend {155const char *prefix;156AP_Filesystem_Backend &fs;157};158static const struct Backend backends[];159160/*161find backend by path162*/163const Backend &backend_by_path(const char *&path) const;164165/*166find backend by open fd167*/168const Backend &backend_by_fd(int &fd) const;169170// support for listing out virtual directory entries (e.g. @SYS171// then @MISSION)172struct {173uint8_t backend_ofs;174struct dirent de;175uint8_t d_off;176} virtual_dirent;177};178179namespace AP {180AP_Filesystem &FS();181};182183184185