14 using namespace Linux;
23 #define STORAGE_FILE SKETCHNAME ".stg" 27 static inline int is_dir(
const char *path)
31 if (
stat(path, &st) < 0) {
35 return S_ISDIR(st.st_mode);
38 static int mkdir_p(
const char *path,
int len, mode_t mode)
42 start = strndupa(path, len);
54 if (end == start + len) {
73 while (end > start && *end !=
'/') {
77 while (end > start && *(end - 1) ==
'/') {
82 while (end < start + len) {
83 if (
mkdir(start, mode) < 0 &&
errno != EEXIST) {
98 mkdir_p(dpath, strlen(dpath), 0777);
99 dfd =
open(dpath, O_RDONLY|O_CLOEXEC);
101 fprintf(stderr,
"Failed to open storage directory: %s (%m)\n", dpath);
105 unlinkat(dfd, dpath, 0);
106 int fd = openat(dfd,
STORAGE_FILE, O_RDWR|O_CREAT|O_CLOEXEC, 0666);
111 fprintf(stderr,
"Failed to create storage file %s/%s\n", dpath,
118 fprintf(stderr,
"Failed to set file size to %u kB (%m)\n",
151 int fd =
open(dpath, O_RDWR|O_CLOEXEC);
164 fd =
open(dpath, O_RDONLY|O_CLOEXEC);
186 uint16_t
end = loc + length;
196 if (loc >=
sizeof(
_buffer)-(n-1)) {
205 if (loc >=
sizeof(
_buffer)-(n-1)) {
208 if (memcmp(src, &
_buffer[loc], n) != 0) {
229 if (i == LINUX_STORAGE_NUM_LINES) {
233 uint32_t write_mask = (1U<<i);
235 for (n=1; (i+n) < LINUX_STORAGE_NUM_LINES &&
241 write_mask |= (1<<(n+i));
#define LINUX_STORAGE_MAX_WRITE
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
ssize_t write(int fd, const void *buf, size_t count)
POSIX Write count bytes from *buf to fileno fd.
void write_block(uint16_t dst, const void *src, size_t n)
#define HAL_BOARD_STORAGE_DIRECTORY
void fail(const char *why)
#define LINUX_STORAGE_LINE_SHIFT
int _storage_create(const char *dpath)
ssize_t read(int fd, void *buf, size_t count)
POSIX read count bytes from *buf to fileno fd.
#define LINUX_STORAGE_NUM_LINES
static const AP_HAL::HAL & hal
static int mkdir_p(const char *path, int len, mode_t mode)
int close(int fileno)
POSIX Close a file with fileno handel.
void read_block(void *dst, uint16_t src, size_t n)
virtual void _timer_tick(void) override
off_t lseek(int fileno, off_t position, int whence)
POSIX seek to file position.
int mkdir(const char *pathname, mode_t mode)
POSIX make a directory.
int ftruncate(int fd, off_t length)
POSIX truncate open file to length.
volatile bool _initialised
virtual const char * get_custom_storage_directory() const
static int is_dir(const char *path)
int stat(const char *name, struct stat *buf)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
void _mark_dirty(uint16_t loc, uint16_t length)
int errno
Note: fdevopen assigns stdin,stdout,stderr.
int fprintf(FILE *fp, const char *fmt,...)
fprintf character write function
volatile uint32_t _dirty_mask
uint8_t _buffer[LINUX_STORAGE_SIZE]
void panic(const char *errormsg,...) FMT_PRINTF(1