grblHAL core  20240704
vfs.h
Go to the documentation of this file.
1 /*
2  vfs.h - An embedded CNC Controller with rs274/ngc (g-code) support
3 
4  Virtual File System handler
5 
6  Part of grblHAL
7 
8  Copyright (c) 2022-2024 Terje Io
9 
10  Grbl is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  Grbl is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #ifndef INCLUDE_VFS_H
25 #define INCLUDE_VFS_H
26 
27 #define GRBL_VFS
28 
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 
35 #define vfs_load_plugin(x)
36 
37 #ifndef bcopy
38 #define bcopy(src, dest, len) memmove(dest, src, len)
39 #endif
40 
41 #if !(defined(__time_t_defined) || defined(_TIME_H_) || defined(__MSP432P401R__) || defined(PART_TM4C123GH6PM))
42 typedef struct {
43  short date;
44  short time;
45 } time_t;
46 
47 struct tm {
48  int tm_year;
49  int tm_mon;
50  int tm_mday;
51  int tm_hour;
52  int tm_min;
53  int tm_sec;
54 };
55 #endif // __time_t_defined
56 
57 typedef union
58 {
59  uint8_t mode;
60  struct {
61  uint8_t read_only :1,
62  hidden :1,
63  system :1,
64  unused :1,
66  archive :1;
67  };
69 
70 typedef struct {
71  size_t st_size;
73 #ifdef ESP_PLATFORM // some versions of ESP-IDF/Compiler combos are fcked up
74  time_t st_mtim;
75 #else
77 #endif
78 } vfs_stat_t;
79 
80 typedef struct {
81  const void *fs;
82  size_t size;
83  bool update;
84  uint8_t handle __attribute__ ((aligned (4))); // first byte of file handle structure
85 } vfs_file_t;
86 
87 struct vfs_dir;
88 typedef struct vfs_dir vfs_dir_t;
89 
90 typedef struct {
91  char name[255];
92  size_t size;
94 } vfs_dirent_t;
95 
96 typedef struct {
97  uint64_t size;
98  uint64_t used;
99 } vfs_free_t;
100 
101 typedef struct {
102  const char *name;
103  size_t size;
104  const uint8_t data[];
106 
107 typedef vfs_file_t *(*vfs_open_ptr)(const char *filename, const char *mode);
108 typedef char *(*vfs_getcwd_ptr)(char *buf, size_t size);
109 typedef size_t (*vfs_read_ptr)(void *buffer, size_t size, size_t count, vfs_file_t *file);
110 typedef size_t (*vfs_write_ptr)(const void *buffer, size_t size, size_t count, vfs_file_t *file);
111 typedef void (*vfs_close_ptr)(vfs_file_t *file);
112 typedef size_t (*vfs_ftell_ptr)(vfs_file_t *file);
113 typedef int (*vfs_fseek_ptr)(vfs_file_t *file, size_t offset);
114 typedef bool (*vfs_eof_ptr)(vfs_file_t *file);
115 typedef int (*vfs_rename_ptr)(const char *from, const char *to);
116 typedef int (*vfs_unlink_ptr)(const char *filename);
117 
118 typedef int (*vfs_mkdir_ptr)(const char *path);
119 typedef int (*vfs_chdir_ptr)(const char *path);
120 typedef int (*vfs_rmdir_ptr)(const char *path);
121 typedef vfs_dir_t *(*vfs_opendir_ptr)(const char *path);
122 typedef char *(*vfs_readdir_ptr)(vfs_dir_t *dir, vfs_dirent_t *dirent);
123 typedef void (*vfs_closedir_ptr)(vfs_dir_t *dir);
124 typedef int (*vfs_stat_ptr)(const char *filename, vfs_stat_t *st);
125 typedef int (*vfs_utime_ptr)(const char *filename, struct tm *modified);
126 
127 typedef bool (*vfs_getfree_ptr)(vfs_free_t *free);
128 typedef int (*vfs_format_ptr)(void);
129 
130 typedef struct
131 {
132  const char *fs_name;
133  bool removable;
154 } vfs_t;
155 
156 typedef void (*on_vfs_changed_ptr)(const vfs_t *fs);
157 typedef void (*on_vfs_mount_ptr)(const char *path, const vfs_t *fs);
158 typedef void (*on_vfs_unmount_ptr)(const char *path);
159 
160 typedef struct {
164 } vfs_events_t;
165 
166 typedef struct vfs_mount
167 {
168  char path[64];
169  const vfs_t *vfs;
171  struct vfs_mount *next;
173 
174 typedef struct vfs_mount_ll_entry
175 {
179 
180 typedef struct {
182 } vfs_drives_t;
183 
184 typedef struct {
185  const char *name;
186  const char *path;
187  bool removable;
189  const void *fs;
190 } vfs_drive_t;
191 
192 struct vfs_dir {
193  const void *fs;
195  uint8_t handle __attribute__ ((aligned (4))); // must be last!
196 };
197 
198 extern int vfs_errno;
199 extern vfs_events_t vfs;
200 
201 char *vfs_fixpath (char *path);
202 
203 bool vfs_mount (const char *path, const vfs_t *fs, vfs_st_mode_t mode);
204 bool vfs_unmount (const char *path);
205 vfs_file_t *vfs_open (const char *filename, const char *mode);
206 void vfs_close (vfs_file_t *file);
207 size_t vfs_read (void *buffer, size_t size, size_t count, vfs_file_t *file);
208 size_t vfs_write (const void *buffer, size_t size, size_t count, vfs_file_t *file);
209 int vfs_puts (const char *s, vfs_file_t *file);
210 size_t vfs_tell (vfs_file_t *file);
211 int vfs_seek (vfs_file_t *file, size_t offset);
212 bool vfs_eof (vfs_file_t *file);
213 int vfs_rename (const char *from, const char *to);
214 int vfs_unlink (const char *filename);
215 int vfs_mkdir (const char *path);
216 int vfs_rmdir (const char *path);
217 int vfs_chdir (const char *path);
218 vfs_dir_t *vfs_opendir (const char *path);
220 void vfs_closedir (vfs_dir_t *dir);
221 char *vfs_getcwd (char *buf, size_t len);
222 int vfs_stat (const char *filename, vfs_stat_t *st);
223 int vfs_utime (const char *filename, struct tm *modified);
224 vfs_free_t *vfs_fgetfree (const char *path);
225 
227 vfs_drive_t *vfs_drives_read (vfs_drives_t *handle, bool add_hidden);
228 void vfs_drives_close (vfs_drives_t *handle);
230 int vfs_drive_format (vfs_drive_t *drive);
231 vfs_drive_t *vfs_get_drive (const char *path);
232 
233 #endif // INCLUDE_VFS_H
Definition: vfs.h:101
size_t size
Definition: vfs.h:103
const char * name
Definition: vfs.h:102
Definition: vfs.h:42
short time
Definition: vfs.h:44
short date
Definition: vfs.h:43
Definition: vfs.h:47
int tm_mday
Definition: vfs.h:50
int tm_sec
Definition: vfs.h:53
int tm_hour
Definition: vfs.h:51
int tm_min
Definition: vfs.h:52
int tm_year
Definition: vfs.h:48
int tm_mon
Definition: vfs.h:49
Definition: vfs.h:192
const void * fs
Definition: vfs.h:193
vfs_mount_ll_entry_t * mounts
Definition: vfs.h:194
uint8_t handle __attribute__((aligned(4)))
Definition: vfs.h:90
vfs_st_mode_t st_mode
Definition: vfs.h:93
size_t size
Definition: vfs.h:92
Definition: vfs.h:184
bool removable
Definition: vfs.h:187
const void * fs
Definition: vfs.h:189
const char * path
Definition: vfs.h:186
const char * name
Definition: vfs.h:185
vfs_st_mode_t mode
Definition: vfs.h:188
Definition: vfs.h:180
vfs_mount_t * mount
Definition: vfs.h:181
Definition: vfs.h:160
on_vfs_unmount_ptr on_unmount
Called when a file system is unmounted.
Definition: vfs.h:162
on_vfs_changed_ptr on_fs_changed
Called when file system content is changed.
Definition: vfs.h:163
on_vfs_mount_ptr on_mount
Called when a file system is mounted.
Definition: vfs.h:161
Definition: vfs.h:80
const void * fs
Definition: vfs.h:81
size_t size
Definition: vfs.h:82
bool update
Definition: vfs.h:83
uint8_t handle __attribute__((aligned(4)))
Definition: vfs.h:96
uint64_t used
Definition: vfs.h:98
uint64_t size
Definition: vfs.h:97
Definition: vfs.h:175
struct vfs_mount_ll_entry * next
Definition: vfs.h:177
vfs_mount_t * mount
Definition: vfs.h:176
Definition: vfs.h:167
struct vfs_mount * next
Definition: vfs.h:171
const vfs_t * vfs
Definition: vfs.h:169
vfs_st_mode_t mode
Definition: vfs.h:170
char path[64]
Definition: vfs.h:168
Definition: vfs.h:70
vfs_st_mode_t st_mode
Definition: vfs.h:72
size_t st_size
Definition: vfs.h:71
time_t st_mtime
Definition: vfs.h:76
Definition: vfs.h:131
vfs_open_ptr fopen
Definition: vfs.h:134
vfs_getfree_ptr fgetfree
Definition: vfs.h:152
bool removable
Definition: vfs.h:133
vfs_format_ptr format
Definition: vfs.h:153
vfs_chdir_ptr fchdir
Definition: vfs.h:144
vfs_write_ptr fwrite
Definition: vfs.h:137
vfs_stat_ptr fstat
Definition: vfs.h:149
vfs_eof_ptr feof
Definition: vfs.h:140
vfs_mkdir_ptr fmkdir
Definition: vfs.h:143
const char * fs_name
Definition: vfs.h:132
vfs_close_ptr fclose
Definition: vfs.h:135
vfs_getcwd_ptr fgetcwd
Definition: vfs.h:151
vfs_fseek_ptr fseek
Definition: vfs.h:139
vfs_readdir_ptr readdir
Definition: vfs.h:147
vfs_unlink_ptr funlink
Definition: vfs.h:142
vfs_rename_ptr frename
Definition: vfs.h:141
vfs_opendir_ptr fopendir
Definition: vfs.h:146
vfs_read_ptr fread
Definition: vfs.h:136
vfs_ftell_ptr ftell
Definition: vfs.h:138
vfs_utime_ptr futime
Definition: vfs.h:150
vfs_rmdir_ptr frmdir
Definition: vfs.h:145
vfs_closedir_ptr fclosedir
Definition: vfs.h:148
Definition: vfs.h:58
uint8_t directory
Definition: vfs.h:65
uint8_t system
Definition: vfs.h:63
uint8_t mode
Definition: vfs.h:59
uint8_t hidden
Definition: vfs.h:62
uint8_t read_only
Definition: vfs.h:61
uint8_t archive
Definition: vfs.h:66
uint8_t unused
Definition: vfs.h:64
vfs_free_t * vfs_drive_getfree(vfs_drive_t *drive)
Definition: vfs.c:621
struct vfs_mount vfs_mount_t
void(* vfs_closedir_ptr)(vfs_dir_t *dir)
Definition: vfs.h:123
int vfs_rename(const char *from, const char *to)
Definition: vfs.c:269
void(* on_vfs_unmount_ptr)(const char *path)
Definition: vfs.h:158
size_t(* vfs_read_ptr)(void *buffer, size_t size, size_t count, vfs_file_t *file)
Definition: vfs.h:109
vfs_free_t * vfs_fgetfree(const char *path)
Definition: vfs.c:480
int(* vfs_rename_ptr)(const char *from, const char *to)
Definition: vfs.h:115
int(* vfs_mkdir_ptr)(const char *path)
Definition: vfs.h:118
vfs_dir_t *(* vfs_opendir_ptr)(const char *path)
Definition: vfs.h:121
int vfs_puts(const char *s, vfs_file_t *file)
Definition: vfs.c:236
void vfs_closedir(vfs_dir_t *dir)
Definition: vfs.c:417
bool vfs_eof(vfs_file_t *file)
Definition: vfs.c:262
vfs_dir_t * vfs_opendir(const char *path)
Definition: vfs.c:361
vfs_drive_t * vfs_drives_read(vfs_drives_t *handle, bool add_hidden)
Definition: vfs.c:591
size_t vfs_read(void *buffer, size_t size, size_t count, vfs_file_t *file)
Definition: vfs.c:222
char * vfs_fixpath(char *path)
Definition: vfs.c:137
bool(* vfs_getfree_ptr)(vfs_free_t *free)
Definition: vfs.h:127
void(* on_vfs_mount_ptr)(const char *path, const vfs_t *fs)
Definition: vfs.h:157
vfs_drive_t * vfs_get_drive(const char *path)
Definition: vfs.c:553
int(* vfs_unlink_ptr)(const char *filename)
Definition: vfs.h:116
char *(* vfs_readdir_ptr)(vfs_dir_t *dir, vfs_dirent_t *dirent)
Definition: vfs.h:122
int vfs_rmdir(const char *path)
Definition: vfs.c:309
size_t(* vfs_ftell_ptr)(vfs_file_t *file)
Definition: vfs.h:112
int vfs_stat(const char *filename, vfs_stat_t *st)
Definition: vfs.c:445
int vfs_mkdir(const char *path)
Definition: vfs.c:297
vfs_drives_t * vfs_drives_open(void)
Definition: vfs.c:567
int vfs_drive_format(vfs_drive_t *drive)
Definition: vfs.c:630
struct vfs_mount_ll_entry vfs_mount_ll_entry_t
bool(* vfs_eof_ptr)(vfs_file_t *file)
Definition: vfs.h:114
int vfs_seek(vfs_file_t *file, size_t offset)
Definition: vfs.c:255
int(* vfs_rmdir_ptr)(const char *path)
Definition: vfs.h:120
char *(* vfs_getcwd_ptr)(char *buf, size_t size)
Definition: vfs.h:108
int(* vfs_chdir_ptr)(const char *path)
Definition: vfs.h:119
bool vfs_mount(const char *path, const vfs_t *fs, vfs_st_mode_t mode)
Definition: vfs.c:492
int(* vfs_stat_ptr)(const char *filename, vfs_stat_t *st)
Definition: vfs.h:124
void vfs_close(vfs_file_t *file)
Definition: vfs.c:212
int(* vfs_fseek_ptr)(vfs_file_t *file, size_t offset)
Definition: vfs.h:113
vfs_events_t vfs
Definition: vfs.c:134
void(* vfs_close_ptr)(vfs_file_t *file)
Definition: vfs.h:111
char * vfs_getcwd(char *buf, size_t len)
Definition: vfs.c:430
size_t(* vfs_write_ptr)(const void *buffer, size_t size, size_t count, vfs_file_t *file)
Definition: vfs.h:110
int vfs_utime(const char *filename, struct tm *modified)
Definition: vfs.c:473
int vfs_chdir(const char *path)
Definition: vfs.c:321
bool vfs_unmount(const char *path)
Definition: vfs.c:523
vfs_file_t *(* vfs_open_ptr)(const char *filename, const char *mode)
Definition: vfs.h:107
int vfs_errno
Definition: vfs.c:133
vfs_file_t * vfs_open(const char *filename, const char *mode)
Definition: vfs.c:199
void(* on_vfs_changed_ptr)(const vfs_t *fs)
Definition: vfs.h:156
void vfs_drives_close(vfs_drives_t *handle)
Definition: vfs.c:616
int(* vfs_utime_ptr)(const char *filename, struct tm *modified)
Definition: vfs.h:125
int(* vfs_format_ptr)(void)
Definition: vfs.h:128
int vfs_unlink(const char *filename)
Definition: vfs.c:285
vfs_dirent_t * vfs_readdir(vfs_dir_t *dir)
Definition: vfs.c:391
size_t vfs_write(const void *buffer, size_t size, size_t count, vfs_file_t *file)
Definition: vfs.c:229
size_t vfs_tell(vfs_file_t *file)
Definition: vfs.c:248