2021-07-24 03:44:00 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "arch.h"
|
2021-12-21 18:16:12 +00:00
|
|
|
#include "config.h"
|
2021-07-24 03:44:00 +01:00
|
|
|
|
2021-07-28 21:11:07 +01:00
|
|
|
enum { MG_FS_READ = 1, MG_FS_WRITE = 2, MG_FS_DIR = 4 };
|
|
|
|
|
|
|
|
// Filesystem API functions
|
2021-09-15 07:43:48 +01:00
|
|
|
// stat() returns MG_FS_* flags and populates file size and modification time
|
|
|
|
// list() calls fn() for every directory entry, allowing to list a directory
|
2021-07-28 21:11:07 +01:00
|
|
|
struct mg_fs {
|
2021-07-29 14:21:20 +01:00
|
|
|
int (*stat)(const char *path, size_t *size, time_t *mtime);
|
2021-07-28 21:11:07 +01:00
|
|
|
void (*list)(const char *path, void (*fn)(const char *, void *), void *);
|
2022-01-17 14:42:41 +00:00
|
|
|
void *(*open)(const char *path, int flags); // Open file
|
|
|
|
void (*close)(void *fd); // Close file
|
2021-09-15 07:43:48 +01:00
|
|
|
size_t (*read)(void *fd, void *buf, size_t len); // Read file
|
|
|
|
size_t (*write)(void *fd, const void *buf, size_t len); // Write file
|
|
|
|
size_t (*seek)(void *fd, size_t offset); // Set file position
|
2022-01-18 17:11:02 +00:00
|
|
|
bool (*rename)(const char *from, const char *to); // Rename
|
|
|
|
bool (*remove)(const char *path); // Delete file
|
2022-01-19 08:43:34 +00:00
|
|
|
bool (*mkd)(const char *path); // Create directory
|
2021-07-28 21:11:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// File descriptor
|
|
|
|
struct mg_fd {
|
|
|
|
void *fd;
|
|
|
|
struct mg_fs *fs;
|
|
|
|
};
|
2022-01-18 17:11:02 +00:00
|
|
|
|
2022-01-17 14:42:41 +00:00
|
|
|
struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags);
|
|
|
|
void mg_fs_close(struct mg_fd *fd);
|
2022-01-18 17:11:02 +00:00
|
|
|
char *mg_file_read(struct mg_fs *fs, const char *path, size_t *size);
|
|
|
|
bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t);
|
|
|
|
bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);
|
2021-07-28 21:11:07 +01:00
|
|
|
|
|
|
|
extern struct mg_fs mg_fs_posix; // POSIX open/close/read/write/seek
|
|
|
|
extern struct mg_fs mg_fs_packed; // Packed FS, see examples/complete
|
2022-01-18 07:44:30 +00:00
|
|
|
extern struct mg_fs mg_fs_fat; // FAT FS
|