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.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 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#endif4041struct dirent {42char d_name[MAX_NAME_LEN]; /* filename */43uint8_t d_type;44};4546#endif // HAL_BOARD_CHIBIOS4748#include <fcntl.h>49#include <errno.h>50#include <unistd.h>5152#ifndef AP_FILESYSTEM_FORMAT_ENABLED53#define AP_FILESYSTEM_FORMAT_ENABLED 154#endif5556#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX || CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_QURT57#include "AP_Filesystem_posix.h"58#endif5960#if CONFIG_HAL_BOARD == HAL_BOARD_ESP3261#include "AP_Filesystem_ESP32.h"62#endif6364#include "AP_Filesystem_backend.h"6566class AP_Filesystem {67private:68struct DirHandle {69uint8_t fs_index;70void *dir;71};7273public:74AP_Filesystem() {}7576// functions that closely match the equivalent posix calls77int open(const char *fname, int flags, bool allow_absolute_paths = false);78int close(int fd);79int32_t read(int fd, void *buf, uint32_t count);80int32_t write(int fd, const void *buf, uint32_t count);81int fsync(int fd);82int32_t lseek(int fd, int32_t offset, int whence);83int stat(const char *pathname, struct stat *stbuf);8485// stat variant for scripting86typedef struct Stat {87uint32_t size;88int32_t mode;89uint32_t mtime;90uint32_t atime;91uint32_t ctime;92bool is_directory(void) const {93return (mode & S_IFMT) == S_IFDIR;94}95} stat_t;96bool stat(const char *pathname, stat_t &stbuf);9798int unlink(const char *pathname);99int mkdir(const char *pathname);100int rename(const char *oldpath, const char *newpath);101102DirHandle *opendir(const char *pathname);103struct dirent *readdir(DirHandle *dirp);104int closedir(DirHandle *dirp);105106// return free disk space in bytes, -1 on error107int64_t disk_free(const char *path);108109// return total disk space in bytes, -1 on error110int64_t disk_space(const char *path);111112// set modification time on a file113bool set_mtime(const char *filename, const uint32_t mtime_sec);114115// if filesystem is not running then try a remount. Return true if fs is mounted116bool retry_mount(void);117118// unmount filesystem for reboot119void unmount(void);120121// returns null-terminated string; cr or lf terminates line122bool fgets(char *buf, uint8_t buflen, int fd);123124// run crc32 over file with given name, returns true if successful125bool crc32(const char *fname, uint32_t& checksum) WARN_IF_UNUSED;126127// format filesystem. This is async, monitor get_format_status for progress128bool format(void);129130// retrieve status of format process:131AP_Filesystem_Backend::FormatStatus get_format_status() const;132133/*134Load a file's contents into memory. Returned object must be `delete`d to135free the data. The data is guaranteed to be null-terminated such that it136can be treated as a string.137*/138FileData *load_file(const char *filename);139140// get_singleton for scripting141static AP_Filesystem *get_singleton(void);142143private:144struct Backend {145const char *prefix;146AP_Filesystem_Backend &fs;147};148static const struct Backend backends[];149150/*151find backend by path152*/153const Backend &backend_by_path(const char *&path) const;154155/*156find backend by open fd157*/158const Backend &backend_by_fd(int &fd) const;159160// support for listing out virtual directory entries (e.g. @SYS161// then @MISSION)162struct {163uint8_t backend_ofs;164struct dirent de;165uint8_t d_off;166} virtual_dirent;167};168169namespace AP {170AP_Filesystem &FS();171};172173174175