2019-08-01 02:07:26 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
ArduPilot filesystem interface. This offsets a minimal subset of
|
|
|
|
full functionality offered by posix type interfaces, meeting the
|
|
|
|
needs of ArduPilot
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <AP_HAL/AP_HAL_Boards.h>
|
|
|
|
|
2022-09-14 07:06:39 -03:00
|
|
|
#include "AP_Filesystem_config.h"
|
2019-08-01 02:07:26 -03:00
|
|
|
|
2022-02-20 23:46:19 -04:00
|
|
|
#ifndef MAX_NAME_LEN
|
|
|
|
#define MAX_NAME_LEN 255
|
|
|
|
#endif
|
|
|
|
|
2022-11-27 19:24:50 -04:00
|
|
|
#if (CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS) || (CONFIG_HAL_BOARD == HAL_BOARD_ESP32)
|
|
|
|
#define DT_REG 0
|
|
|
|
#define DT_DIR 1
|
|
|
|
#define DT_LNK 10
|
|
|
|
#endif
|
|
|
|
|
2019-08-01 02:07:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
2023-05-14 22:44:22 -03:00
|
|
|
#if AP_FILESYSTEM_FATFS_ENABLED
|
2019-08-01 02:07:26 -03:00
|
|
|
#include "AP_Filesystem_FATFS.h"
|
|
|
|
#endif
|
2022-02-20 23:46:19 -04:00
|
|
|
|
2020-03-29 22:53:29 -03:00
|
|
|
struct dirent {
|
|
|
|
char d_name[MAX_NAME_LEN]; /* filename */
|
|
|
|
uint8_t d_type;
|
|
|
|
};
|
2022-02-20 23:46:19 -04:00
|
|
|
|
|
|
|
#endif // HAL_BOARD_CHIBIOS
|
|
|
|
|
2020-03-29 22:53:29 -03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2021-11-07 04:30:47 -04:00
|
|
|
|
|
|
|
#ifndef AP_FILESYSTEM_FORMAT_ENABLED
|
2023-06-03 04:25:53 -03:00
|
|
|
#define AP_FILESYSTEM_FORMAT_ENABLED 1
|
2021-11-07 04:30:47 -04:00
|
|
|
#endif
|
|
|
|
|
2024-07-05 20:17:46 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX || CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_QURT
|
2019-08-01 02:07:26 -03:00
|
|
|
#include "AP_Filesystem_posix.h"
|
|
|
|
#endif
|
|
|
|
|
2021-10-27 05:06:35 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_ESP32
|
|
|
|
#include "AP_Filesystem_ESP32.h"
|
|
|
|
#endif
|
|
|
|
|
2020-03-29 22:53:29 -03:00
|
|
|
#include "AP_Filesystem_backend.h"
|
|
|
|
|
2019-08-01 02:07:26 -03:00
|
|
|
class AP_Filesystem {
|
2020-03-11 03:48:09 -03:00
|
|
|
private:
|
|
|
|
struct DirHandle {
|
|
|
|
uint8_t fs_index;
|
|
|
|
void *dir;
|
|
|
|
};
|
2019-08-01 02:07:26 -03:00
|
|
|
|
|
|
|
public:
|
|
|
|
AP_Filesystem() {}
|
|
|
|
|
|
|
|
// functions that closely match the equivalent posix calls
|
2022-03-14 11:33:32 -03:00
|
|
|
int open(const char *fname, int flags, bool allow_absolute_paths = false);
|
2019-08-01 02:07:26 -03:00
|
|
|
int close(int fd);
|
2020-03-29 22:53:29 -03:00
|
|
|
int32_t read(int fd, void *buf, uint32_t count);
|
|
|
|
int32_t write(int fd, const void *buf, uint32_t count);
|
2019-08-01 02:07:26 -03:00
|
|
|
int fsync(int fd);
|
2020-03-29 22:53:29 -03:00
|
|
|
int32_t lseek(int fd, int32_t offset, int whence);
|
2019-08-01 02:07:26 -03:00
|
|
|
int stat(const char *pathname, struct stat *stbuf);
|
2023-12-07 20:19:07 -04:00
|
|
|
|
|
|
|
// stat variant for scripting
|
2024-02-19 14:41:27 -04:00
|
|
|
typedef struct Stat {
|
2023-12-07 20:19:07 -04:00
|
|
|
uint32_t size;
|
|
|
|
int32_t mode;
|
|
|
|
uint32_t mtime;
|
|
|
|
uint32_t atime;
|
|
|
|
uint32_t ctime;
|
|
|
|
bool is_directory(void) const {
|
|
|
|
return (mode & S_IFMT) == S_IFDIR;
|
|
|
|
}
|
|
|
|
} stat_t;
|
|
|
|
bool stat(const char *pathname, stat_t &stbuf);
|
|
|
|
|
2019-08-01 02:07:26 -03:00
|
|
|
int unlink(const char *pathname);
|
|
|
|
int mkdir(const char *pathname);
|
2023-02-21 17:23:28 -04:00
|
|
|
int rename(const char *oldpath, const char *newpath);
|
2020-03-11 03:48:09 -03:00
|
|
|
|
|
|
|
DirHandle *opendir(const char *pathname);
|
|
|
|
struct dirent *readdir(DirHandle *dirp);
|
|
|
|
int closedir(DirHandle *dirp);
|
2019-08-01 02:07:26 -03:00
|
|
|
|
|
|
|
// return free disk space in bytes, -1 on error
|
|
|
|
int64_t disk_free(const char *path);
|
|
|
|
|
|
|
|
// return total disk space in bytes, -1 on error
|
|
|
|
int64_t disk_space(const char *path);
|
2019-08-26 02:55:28 -03:00
|
|
|
|
|
|
|
// set modification time on a file
|
2020-03-29 22:53:29 -03:00
|
|
|
bool set_mtime(const char *filename, const uint32_t mtime_sec);
|
2020-03-11 03:48:09 -03:00
|
|
|
|
2020-10-23 22:24:06 -03:00
|
|
|
// if filesystem is not running then try a remount. Return true if fs is mounted
|
|
|
|
bool retry_mount(void);
|
|
|
|
|
|
|
|
// unmount filesystem for reboot
|
|
|
|
void unmount(void);
|
2020-11-12 20:02:21 -04:00
|
|
|
|
2021-09-29 00:01:26 -03:00
|
|
|
// returns null-terminated string; cr or lf terminates line
|
|
|
|
bool fgets(char *buf, uint8_t buflen, int fd);
|
|
|
|
|
2023-11-25 13:16:59 -04:00
|
|
|
// run crc32 over file with given name, returns true if successful
|
|
|
|
bool crc32(const char *fname, uint32_t& checksum) WARN_IF_UNUSED;
|
|
|
|
|
2022-12-08 02:47:18 -04:00
|
|
|
// format filesystem. This is async, monitor get_format_status for progress
|
2021-11-07 04:30:47 -04:00
|
|
|
bool format(void);
|
2022-12-08 02:47:18 -04:00
|
|
|
|
|
|
|
// retrieve status of format process:
|
|
|
|
AP_Filesystem_Backend::FormatStatus get_format_status() const;
|
|
|
|
|
2020-11-12 20:02:21 -04:00
|
|
|
/*
|
2024-02-23 18:09:37 -04:00
|
|
|
Load a file's contents into memory. Returned object must be `delete`d to
|
|
|
|
free the data. The data is guaranteed to be null-terminated such that it
|
|
|
|
can be treated as a string.
|
2020-11-12 20:02:21 -04:00
|
|
|
*/
|
|
|
|
FileData *load_file(const char *filename);
|
2023-12-07 20:19:07 -04:00
|
|
|
|
|
|
|
// get_singleton for scripting
|
|
|
|
static AP_Filesystem *get_singleton(void);
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
private:
|
|
|
|
struct Backend {
|
|
|
|
const char *prefix;
|
|
|
|
AP_Filesystem_Backend &fs;
|
|
|
|
};
|
|
|
|
static const struct Backend backends[];
|
|
|
|
|
|
|
|
/*
|
|
|
|
find backend by path
|
|
|
|
*/
|
|
|
|
const Backend &backend_by_path(const char *&path) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
find backend by open fd
|
|
|
|
*/
|
|
|
|
const Backend &backend_by_fd(int &fd) const;
|
2022-10-16 20:25:30 -03:00
|
|
|
|
|
|
|
// support for listing out virtual directory entries (e.g. @SYS
|
|
|
|
// then @MISSION)
|
|
|
|
struct {
|
|
|
|
uint8_t backend_ofs;
|
|
|
|
struct dirent de;
|
|
|
|
uint8_t d_off;
|
|
|
|
} virtual_dirent;
|
2019-08-01 02:07:26 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
AP_Filesystem &FS();
|
|
|
|
};
|
2020-03-29 22:53:29 -03:00
|
|
|
|