VSF Documented
esp_vfs.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18/*
19 * esp_vfs.h -- ESP-IDF VFS compatibility header (clean-room).
20 *
21 * POSIX filesystem API surface (open/close/read/write/lseek/stat/
22 * opendir/readdir/mkdir/unlink/rename/...) is provided natively by VSF's
23 * linux shell (shell/sys/linux) which wraps component/fs as synchronous
24 * POSIX calls. Users include <stdio.h> / <unistd.h> / <sys/stat.h> etc.
25 * and get matching behaviour.
26 *
27 * Runtime VFS driver registration (esp_vfs_register_fs / esp_vfs_register)
28 * maintains an internal registry. When active (VSF_USE_ESPIDF && USE_VFS),
29 * the open() syscall checks the registry for prefix matches before falling
30 * through to VSF's native VFS.
31 */
32
33#ifndef __VSF_ESPIDF_ESP_VFS_H__
34#define __VSF_ESPIDF_ESP_VFS_H__
35
36/*============================ INCLUDES ======================================*/
37
38#include "esp_err.h"
39#include "esp_vfs_ops.h"
40
41#include <stdint.h>
42#include <stddef.h>
43#include <stdbool.h>
44
45// POSIX surface
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <fcntl.h>
49#include <unistd.h>
50#include <dirent.h>
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/*============================ MACROS ========================================*/
57
58#ifndef ESP_VFS_PATH_MAX
59# define ESP_VFS_PATH_MAX 15
60#endif
61
62#define MAX_FDS FD_SETSIZE
63
64#define ESP_VFS_FLAG_DEFAULT (1 << 0)
65#define ESP_VFS_FLAG_CONTEXT_PTR (1 << 1)
66#define ESP_VFS_FLAG_READONLY_FS (1 << 2)
67#define ESP_VFS_FLAG_STATIC (1 << 3)
68
69/*============================ TYPES =========================================*/
70
71/*
72 * Legacy VFS definition structure (deprecated in favour of esp_vfs_fs_ops_t).
73 *
74 * Kept for backward compatibility with ESP-IDF code that initialises
75 * esp_vfs_t directly. New code should use esp_vfs_fs_ops_t (esp_vfs_ops.h).
76 */
77typedef struct {
78 int flags;
79 union {
80 ssize_t (*write_p)(void *ctx, int fd, const void *data, size_t size);
81 ssize_t (*write) ( int fd, const void *data, size_t size);
82 };
83 union {
84 off_t (*lseek_p)(void *ctx, int fd, off_t size, int mode);
85 off_t (*lseek) ( int fd, off_t size, int mode);
86 };
87 union {
88 ssize_t (*read_p) (void *ctx, int fd, void *dst, size_t size);
89 ssize_t (*read) ( int fd, void *dst, size_t size);
90 };
91 union {
92 ssize_t (*pread_p)(void *ctx, int fd, void *dst, size_t size, off_t offset);
93 ssize_t (*pread) ( int fd, void *dst, size_t size, off_t offset);
94 };
95 union {
96 ssize_t (*pwrite_p)(void *ctx, int fd, const void *src, size_t size, off_t offset);
97 ssize_t (*pwrite) ( int fd, const void *src, size_t size, off_t offset);
98 };
99 union {
100 int (*open_p) (void *ctx, const char *path, int flags, int mode);
101 int (*open) ( const char *path, int flags, int mode);
102 };
103 union {
104 int (*close_p)(void *ctx, int fd);
105 int (*close) ( int fd);
106 };
107 union {
108 int (*fstat_p)(void *ctx, int fd, struct stat *st);
109 int (*fstat) ( int fd, struct stat *st);
110 };
111 union {
112 int (*stat_p) (void *ctx, const char *path, struct stat *st);
113 int (*stat) ( const char *path, struct stat *st);
114 };
115 union {
116 int (*link_p) (void *ctx, const char *n1, const char *n2);
117 int (*link) ( const char *n1, const char *n2);
118 };
119 union {
120 int (*unlink_p)(void *ctx, const char *path);
121 int (*unlink) ( const char *path);
122 };
123 union {
124 int (*rename_p)(void *ctx, const char *src, const char *dst);
125 int (*rename) ( const char *src, const char *dst);
126 };
127 union {
128 DIR * (*opendir_p) (void *ctx, const char *name);
129 DIR * (*opendir) ( const char *name);
130 };
131 union {
132 struct dirent * (*readdir_p) (void *ctx, DIR *pdir);
133 struct dirent * (*readdir) ( DIR *pdir);
134 };
135 union {
136 int (*readdir_r_p)(void *ctx, DIR *pdir, struct dirent *entry,
137 struct dirent **out);
138 int (*readdir_r) ( DIR *pdir, struct dirent *entry,
139 struct dirent **out);
140 };
141 union {
142 long (*telldir_p)(void *ctx, DIR *pdir);
143 long (*telldir) ( DIR *pdir);
144 };
145 union {
146 void (*seekdir_p)(void *ctx, DIR *pdir, long offset);
147 void (*seekdir) ( DIR *pdir, long offset);
148 };
149 union {
150 int (*closedir_p)(void *ctx, DIR *pdir);
151 int (*closedir) ( DIR *pdir);
152 };
153 union {
154 int (*mkdir_p)(void *ctx, const char *name, mode_t mode);
155 int (*mkdir) ( const char *name, mode_t mode);
156 };
157 union {
158 int (*rmdir_p)(void *ctx, const char *name);
159 int (*rmdir) ( const char *name);
160 };
161 union {
162 int (*fcntl_p)(void *ctx, int fd, int cmd, int arg);
163 int (*fcntl) ( int fd, int cmd, int arg);
164 };
165 union {
166 int (*ioctl_p)(void *ctx, int fd, int cmd, va_list args);
167 int (*ioctl) ( int fd, int cmd, va_list args);
168 };
169 union {
170 int (*fsync_p)(void *ctx, int fd);
171 int (*fsync) ( int fd);
172 };
173 union {
174 int (*access_p)(void *ctx, const char *path, int amode);
175 int (*access) ( const char *path, int amode);
176 };
177 union {
178 int (*truncate_p) (void *ctx, const char *path, off_t length);
179 int (*truncate) ( const char *path, off_t length);
180 };
181 union {
182 int (*ftruncate_p)(void *ctx, int fd, off_t length);
183 int (*ftruncate) ( int fd, off_t length);
184 };
185 union {
186 int (*utime_p)(void *ctx, const char *path, const struct utimbuf *times);
187 int (*utime) ( const char *path, const struct utimbuf *times);
188 };
189} esp_vfs_t;
190
191/*============================ PROTOTYPES ====================================*/
192
193// ---- Registration / unregistration (old esp_vfs_t API) ----
194
195extern esp_err_t esp_vfs_register(const char *base_path,
196 const esp_vfs_t *vfs,
197 void *ctx);
198extern esp_err_t esp_vfs_unregister(const char *base_path);
199extern esp_err_t esp_vfs_register_fd_range(const esp_vfs_t *vfs, void *ctx,
200 int min_fd, int max_fd);
201
202// ---- Registration / unregistration (new esp_vfs_fs_ops_t API) ----
203// Declared in esp_vfs_ops.h:
204// esp_vfs_register_fs(), esp_vfs_register_fs_with_id(),
205// esp_vfs_unregister_fs(), esp_vfs_unregister_fs_with_id()
206
207// ---- VFS-ID-based registration ----
208
209extern esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx,
210 esp_vfs_id_t *vfs_id);
212extern esp_err_t esp_vfs_register_fd(esp_vfs_id_t vfs_id, int *fd);
214 int local_fd, bool permanent,
215 int *fd);
216extern esp_err_t esp_vfs_unregister_fd(esp_vfs_id_t vfs_id, int fd);
217
218// ---- POSIX syscall dispatch (used by newlib syscall table; thin wrappers) ----
219
220extern ssize_t esp_vfs_write(struct _reent *r, int fd,
221 const void *data, size_t size);
222extern off_t esp_vfs_lseek(struct _reent *r, int fd,
223 off_t size, int mode);
224extern ssize_t esp_vfs_read(struct _reent *r, int fd,
225 void *dst, size_t size);
226extern int esp_vfs_open(struct _reent *r, const char *path,
227 int flags, int mode);
228extern int esp_vfs_close(struct _reent *r, int fd);
229extern int esp_vfs_fstat(struct _reent *r, int fd, struct stat *st);
230extern int esp_vfs_stat(struct _reent *r, const char *path,
231 struct stat *st);
232extern int esp_vfs_link(struct _reent *r, const char *n1,
233 const char *n2);
234extern int esp_vfs_unlink(struct _reent *r, const char *path);
235extern int esp_vfs_rename(struct _reent *r, const char *src,
236 const char *dst);
237extern int esp_vfs_utime(const char *path,
238 const struct utimbuf *times);
239extern int esp_vfs_fsync(int fd);
240extern int esp_vfs_fcntl_r(struct _reent *r, int fd,
241 int cmd, int arg);
242extern int esp_vfs_ioctl(int fd, int cmd, ...);
243extern int esp_vfs_truncate(const char *path, off_t length);
244extern int esp_vfs_ftruncate(int fd, off_t length);
245extern int esp_vfs_access(const char *path, int amode);
246extern int esp_vfs_rmdir(const char *name);
247extern int esp_vfs_mkdir(const char *name, mode_t mode);
248extern DIR * esp_vfs_opendir(const char *name);
249extern int esp_vfs_closedir(DIR *pdir);
250extern int esp_vfs_readdir_r(DIR *pdir, struct dirent *entry,
251 struct dirent **out_dirent);
252extern struct dirent * esp_vfs_readdir(DIR *pdir);
253extern long esp_vfs_telldir(DIR *pdir);
254extern void esp_vfs_seekdir(DIR *pdir, long loc);
255extern void esp_vfs_rewinddir(DIR *pdir);
256
257// ---- select / pread / pwrite ----
258
259extern int esp_vfs_select(int nfds, fd_set *readfds,
260 fd_set *writefds, fd_set *errorfds,
261 struct timeval *timeout);
264 int *woken);
265extern ssize_t esp_vfs_pread(int fd, void *dst, size_t size,
266 off_t offset);
267extern ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size,
268 off_t offset);
269
270// ---- Debug ----
271
272extern void esp_vfs_dump_fds(FILE *fp);
273extern void esp_vfs_dump_registered_paths(FILE *fp);
274
275#ifdef __cplusplus
276}
277#endif
278
279#endif // __VSF_ESPIDF_ESP_VFS_H__
Definition vsf_linux_fs.h:115
#define readdir_r
Definition dirent.h:21
#define seekdir
Definition dirent.h:25
#define telldir
Definition dirent.h:24
#define closedir
Definition dirent.h:26
int esp_err_t
Definition esp_err.h:41
int esp_vfs_stat(struct _reent *r, const char *path, struct stat *st)
Definition esp_vfs_port.c:530
int esp_vfs_utime(const char *path, const struct utimbuf *times)
Definition esp_vfs_port.c:534
esp_err_t esp_vfs_unregister(const char *base_path)
Definition esp_vfs_port.c:513
ssize_t esp_vfs_pread(int fd, void *dst, size_t size, off_t offset)
Definition esp_vfs_port.c:553
int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
Definition esp_vfs_port.c:550
void esp_vfs_dump_registered_paths(FILE *fp)
Definition esp_vfs_port.c:556
void esp_vfs_dump_fds(FILE *fp)
Definition esp_vfs_port.c:555
ssize_t esp_vfs_write(struct _reent *r, int fd, const void *data, size_t size)
Definition esp_vfs_port.c:524
DIR * esp_vfs_opendir(const char *name)
Definition esp_vfs_port.c:543
off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode)
Definition esp_vfs_port.c:525
int esp_vfs_mkdir(const char *name, mode_t mode)
Definition esp_vfs_port.c:542
int esp_vfs_open(struct _reent *r, const char *path, int flags, int mode)
Definition esp_vfs_port.c:527
int esp_vfs_link(struct _reent *r, const char *n1, const char *n2)
Definition esp_vfs_port.c:531
void esp_vfs_select_triggered_isr(esp_vfs_select_sem_t sem, int *woken)
Definition esp_vfs_port.c:552
esp_err_t esp_vfs_register_fd_range(const esp_vfs_t *vfs, void *ctx, int min_fd, int max_fd)
Definition esp_vfs_port.c:514
int esp_vfs_rmdir(const char *name)
Definition esp_vfs_port.c:541
esp_err_t esp_vfs_register(const char *base_path, const esp_vfs_t *vfs, void *ctx)
Definition esp_vfs_port.c:495
int esp_vfs_fstat(struct _reent *r, int fd, struct stat *st)
Definition esp_vfs_port.c:529
int esp_vfs_ioctl(int fd, int cmd,...)
Definition esp_vfs_port.c:537
esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id)
Definition esp_vfs_port.c:517
ssize_t esp_vfs_read(struct _reent *r, int fd, void *dst, size_t size)
Definition esp_vfs_port.c:526
esp_err_t esp_vfs_unregister_fd(esp_vfs_id_t vfs_id, int fd)
Definition esp_vfs_port.c:521
ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size, off_t offset)
Definition esp_vfs_port.c:554
esp_err_t esp_vfs_unregister_with_id(esp_vfs_id_t vfs_id)
Definition esp_vfs_port.c:518
int esp_vfs_closedir(DIR *pdir)
Definition esp_vfs_port.c:544
long esp_vfs_telldir(DIR *pdir)
Definition esp_vfs_port.c:547
int esp_vfs_readdir_r(DIR *pdir, struct dirent *entry, struct dirent **out_dirent)
Definition esp_vfs_port.c:545
esp_err_t esp_vfs_register_fd_with_local_fd(esp_vfs_id_t vfs_id, int local_fd, bool permanent, int *fd)
Definition esp_vfs_port.c:520
int esp_vfs_rename(struct _reent *r, const char *src, const char *dst)
Definition esp_vfs_port.c:533
int esp_vfs_truncate(const char *path, off_t length)
Definition esp_vfs_port.c:538
struct dirent * esp_vfs_readdir(DIR *pdir)
Definition esp_vfs_port.c:546
int esp_vfs_ftruncate(int fd, off_t length)
Definition esp_vfs_port.c:539
void esp_vfs_select_triggered(esp_vfs_select_sem_t sem)
Definition esp_vfs_port.c:551
void esp_vfs_rewinddir(DIR *pdir)
Definition esp_vfs_port.c:549
int esp_vfs_fsync(int fd)
Definition esp_vfs_port.c:535
int esp_vfs_access(const char *path, int amode)
Definition esp_vfs_port.c:540
int esp_vfs_fcntl_r(struct _reent *r, int fd, int cmd, int arg)
Definition esp_vfs_port.c:536
int esp_vfs_unlink(struct _reent *r, const char *path)
Definition esp_vfs_port.c:532
int esp_vfs_close(struct _reent *r, int fd)
Definition esp_vfs_port.c:528
esp_err_t esp_vfs_register_fd(esp_vfs_id_t vfs_id, int *fd)
Definition esp_vfs_port.c:519
void esp_vfs_seekdir(DIR *pdir, long loc)
Definition esp_vfs_port.c:548
int esp_vfs_id_t
Definition esp_vfs_ops.h:54
#define open
Definition fcntl.h:20
#define fcntl
Definition fcntl.h:18
struct ieee80211_ext_chansw_ie data
Definition ieee80211.h:80
__le16 timeout
Definition ieee80211.h:94
unsigned int mode_t
Definition types.h:115
int ssize_t
Definition types.h:91
long off_t
Definition types.h:123
#define mkdir
Definition stat.h:31
#define fstat
Definition stat.h:24
Definition stdio.h:145
Definition reent.h:10
Definition dirent.h:35
Definition esp_vfs_ops.h:60
Definition esp_vfs.h:77
int flags
Definition esp_vfs.h:78
Definition select.h:42
Definition stat.h:107
Definition time.h:42
Definition utime.h:16
int ioctl(int fd, unsigned long request,...)
Definition vsf_linux_fs.c:2050
#define rename
Definition stdio.h:86
#define times
Definition times.h:18
#define link
Definition unistd.h:120
#define lseek
Definition unistd.h:124
#define unlink
Definition unistd.h:118
#define access
Definition unistd.h:117
#define pwrite
Definition unistd.h:130
#define pread
Definition unistd.h:129
#define truncate
Definition unistd.h:100
#define read
Definition unistd.h:125
#define rmdir
Definition unistd.h:122
#define ftruncate
Definition unistd.h:99
#define close
Definition unistd.h:123
#define write
Definition unistd.h:126
#define fsync
Definition unistd.h:136
#define utime(__file, __time)
Definition utime.h:22
uint64_t offset
Definition vsf_memfs.h:49
uint32_t size
Definition vsf_memfs.h:50
uint_fast8_t length
Definition vsf_pbuf.c:38
Generated from commit: vsfteam/vsf@c3767bf