HomeCore 0.1.3 (07b5544)
Bare-metal personal computer firmware for Cortex-M
Loading...
Searching...
No Matches
vfs.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 Grzegorz Grzęda
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31#ifndef HOME_CORE_VFS_H
32#define HOME_CORE_VFS_H
33/*---------------------------------------------------------------------------*/
34#if defined(__cplusplus)
35extern "C" {
36#endif
37/*---------------------------------------------------------------------------*/
38#include <stdint.h>
39#include <stddef.h>
40#include <stdbool.h>
41/*---------------------------------------------------------------------------*/
80/*---------------------------------------------------------------------------*/
85#define VFS_SNAPSHOT_CAPACITY 32
87#define VFS_PATH_CAPACITY 128
89/*---------------------------------------------------------------------------*/
94typedef struct vfs_node vfs_node_t;
95/*---------------------------------------------------------------------------*/
104typedef struct {
109 int (*open)(vfs_node_t *node, int flags);
111 int (*close)(vfs_node_t *node);
112
114 int (*read)(vfs_node_t *node, void *buf, unsigned len);
116 int (*write)(vfs_node_t *node, const void *buf, unsigned len);
117
119 int (*ioctl)(vfs_node_t *node, unsigned request, void *arg);
120
122 int (*lseek)(vfs_node_t *node, int offset, int whence);
123
135 int (*snapshot)(vfs_node_t *node, char *buf, unsigned capacity);
136} vfs_ops_t;
137/*---------------------------------------------------------------------------*/
145typedef struct vfs_node {
147 const char *name;
161 unsigned size;
162} vfs_node_t;
163/*---------------------------------------------------------------------------*/
170void vfs_init(void);
171/*---------------------------------------------------------------------------*/
183/*---------------------------------------------------------------------------*/
194vfs_node_t *vfs_find_node(const char *name);
196/*---------------------------------------------------------------------------*/
220int vfs_resolve_path(const char *base, const char *path, char result[VFS_PATH_CAPACITY]);
221/*---------------------------------------------------------------------------*/
236int vfs_mkdir(const char *path);
237/*---------------------------------------------------------------------------*/
251int vfs_rmdir(const char *path);
252/*---------------------------------------------------------------------------*/
264int vfs_unlink(const char *path);
265/*---------------------------------------------------------------------------*/
277typedef int (*vfs_directory_visitor_t)(const char *name, bool is_directory, void *context);
278/*---------------------------------------------------------------------------*/
295int vfs_list(const char *path, vfs_directory_visitor_t visitor, void *context);
297/*---------------------------------------------------------------------------*/
325int vfs_open(const char *name, int flags);
326/*---------------------------------------------------------------------------*/
337int vfs_close(int fd);
338/*---------------------------------------------------------------------------*/
355int vfs_read(int fd, void *buf, unsigned len);
356/*---------------------------------------------------------------------------*/
377int vfs_write(int fd, const void *buf, unsigned len);
378/*---------------------------------------------------------------------------*/
390int vfs_ioctl(int fd, unsigned request, void *arg);
391/*---------------------------------------------------------------------------*/
408int vfs_lseek(int fd, int offset, int whence);
409/*---------------------------------------------------------------------------*/
422/*---------------------------------------------------------------------------*/
424typedef struct {
430 unsigned size;
431} vfs_stat_t;
432/*---------------------------------------------------------------------------*/
446int vfs_stat(const char *path, vfs_stat_t *stat);
447/*---------------------------------------------------------------------------*/
457int vfs_fstat(int fd, vfs_stat_t *stat);
459/*---------------------------------------------------------------------------*/
475typedef struct {
477 int (*open)(void *fs, const char *path, int flags, void **file);
479 int (*close)(void *fs, void *file);
481 int (*read)(void *fs, void *file, void *buf, unsigned len);
483 int (*write)(void *fs, void *file, const void *buf, unsigned len);
485 int (*lseek)(void *fs, void *file, int offset, int whence);
487 int (*fstat)(void *fs, void *file, vfs_stat_t *stat);
489 int (*stat)(void *fs, const char *path, vfs_stat_t *stat);
491 int (*mkdir)(void *fs, const char *path);
493 int (*rmdir)(void *fs, const char *path);
495 int (*unlink)(void *fs, const char *path);
497 int (*list)(void *fs, const char *path, vfs_directory_visitor_t visitor, void *context);
499/*---------------------------------------------------------------------------*/
517int vfs_mount(const char *path, const vfs_fs_ops_t *ops, void *fs);
519/*---------------------------------------------------------------------------*/
521/*---------------------------------------------------------------------------*/
522#if defined(__cplusplus)
523}
524#endif
525/*---------------------------------------------------------------------------*/
526#endif // HOME_CORE_VFS_H
527/*---------------------------------------------------------------------------*/
int vfs_resolve_path(const char *base, const char *path, char result[VFS_PATH_CAPACITY])
Resolve a path against an explicit base directory.
struct vfs_node vfs_node_t
VFS node; see struct vfs_node.
Definition vfs.h:94
int vfs_stat(const char *path, vfs_stat_t *stat)
Report the type and size of a path.
int(* vfs_directory_visitor_t)(const char *name, bool is_directory, void *context)
Visitor called for each directory entry by vfs_list().
Definition vfs.h:277
void vfs_register_node(vfs_node_t *node)
Add a node to the namespace.
vfs_node_t * vfs_fd_node(int fd)
Get the node behind an open descriptor.
int vfs_fstat(int fd, vfs_stat_t *stat)
Report the type and size of an open descriptor.
int vfs_close(int fd)
Release a descriptor.
int vfs_unlink(const char *path)
Remove a file: a RAM file, whose contents are freed, or a file on a mounted filesystem.
#define VFS_PATH_CAPACITY
Path buffer size including the terminator; paths hold at most 127 bytes.
Definition vfs.h:87
int vfs_rmdir(const char *path)
Remove an empty directory created by vfs_mkdir().
int vfs_lseek(int fd, int offset, int whence)
Move a descriptor's position.
vfs_node_t * vfs_find_node(const char *name)
Look up a node by path.
int vfs_mkdir(const char *path)
Create an empty directory.
int vfs_mount(const char *path, const vfs_fs_ops_t *ops, void *fs)
Attach a filesystem at a new mount point.
int vfs_list(const char *path, vfs_directory_visitor_t visitor, void *context)
Visit the direct children of a directory.
int vfs_read(int fd, void *buf, unsigned len)
Read from a descriptor.
int vfs_write(int fd, const void *buf, unsigned len)
Write to a descriptor.
int vfs_open(const char *name, int flags)
Open a node and allocate the lowest free descriptor.
void vfs_init(void)
Obsolete initializer; currently does nothing.
int vfs_ioctl(int fd, unsigned request, void *arg)
Forward a control request to a device.
Operations a mounted filesystem provides; every one is required.
Definition vfs.h:475
int(* stat)(void *fs, const char *path, vfs_stat_t *stat)
Report a path's type and size; ENOENT if it does not exist.
Definition vfs.h:489
Named entry in the VFS namespace.
Definition vfs.h:145
void * driver_data
Driver-private state.
Definition vfs.h:151
bool is_owned
The VFS allocated the node and frees it on removal.
Definition vfs.h:159
const char * name
Canonical absolute path, such as /dev/uart0.
Definition vfs.h:147
vfs_ops_t ops
Device operations.
Definition vfs.h:149
vfs_node_t * next
Namespace list link, managed by the VFS.
Definition vfs.h:153
bool is_regular
The node is a RAM file managed by the VFS.
Definition vfs.h:157
unsigned size
RAM file length in bytes.
Definition vfs.h:161
bool is_directory
The node is a directory.
Definition vfs.h:155
Device operations.
Definition vfs.h:104
File type and size reported by vfs_stat() and vfs_fstat().
Definition vfs.h:424
bool is_directory
The path is a directory, including a mount point.
Definition vfs.h:426
bool is_regular
The path is a regular file: a RAM file or a file on a mounted filesystem.
Definition vfs.h:428
unsigned size
File length in bytes; 0 for directories and devices.
Definition vfs.h:430