ardupilot/libraries/AP_Filesystem/AP_Filesystem.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

164 lines
4.3 KiB
C
Raw Normal View History

/*
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>
#include "AP_Filesystem_config.h"
#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
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
#if AP_FILESYSTEM_FATFS_ENABLED
#include "AP_Filesystem_FATFS.h"
#endif
struct dirent {
char d_name[MAX_NAME_LEN]; /* filename */
uint8_t d_type;
};
#endif // HAL_BOARD_CHIBIOS
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#ifndef AP_FILESYSTEM_FORMAT_ENABLED
#define AP_FILESYSTEM_FORMAT_ENABLED 1
#endif
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX || CONFIG_HAL_BOARD == HAL_BOARD_SITL
#include "AP_Filesystem_posix.h"
#endif
#if CONFIG_HAL_BOARD == HAL_BOARD_ESP32
#include "AP_Filesystem_ESP32.h"
#endif
#include "AP_Filesystem_backend.h"
class AP_Filesystem {
private:
struct DirHandle {
uint8_t fs_index;
void *dir;
};
public:
AP_Filesystem() {}
// functions that closely match the equivalent posix calls
int open(const char *fname, int flags, bool allow_absolute_paths = false);
int close(int fd);
int32_t read(int fd, void *buf, uint32_t count);
int32_t write(int fd, const void *buf, uint32_t count);
int fsync(int fd);
int32_t lseek(int fd, int32_t offset, int whence);
int stat(const char *pathname, struct stat *stbuf);
// stat variant for scripting
typedef struct {
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);
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);
DirHandle *opendir(const char *pathname);
struct dirent *readdir(DirHandle *dirp);
int closedir(DirHandle *dirp);
// 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
bool set_mtime(const char *filename, const uint32_t mtime_sec);
// 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);
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;
// format filesystem. This is async, monitor get_format_status for progress
bool format(void);
// retrieve status of format process:
AP_Filesystem_Backend::FormatStatus get_format_status() const;
/*
load a full file. Use delete to free the data
*/
FileData *load_file(const char *filename);
// get_singleton for scripting
static AP_Filesystem *get_singleton(void);
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;
};
namespace AP {
AP_Filesystem &FS();
};