Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
fs.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation.
3 * Copyright (c) 2020-2024 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_FS_FS_H_
9#define ZEPHYR_INCLUDE_FS_FS_H_
10
11#include <sys/types.h>
12
13#include <zephyr/sys/dlist.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
28struct fs_file_system_t;
29
39
54enum {
57
60
63
66
69
72
75};
76
78#define FS_MOUNT_FLAG_NO_FORMAT BIT(0)
80#define FS_MOUNT_FLAG_READ_ONLY BIT(1)
86#define FS_MOUNT_FLAG_AUTOMOUNT BIT(2)
95#define FS_MOUNT_FLAG_USE_DISK_ACCESS BIT(3)
96
104 int type;
106 const char *mnt_point;
108 void *fs_data;
111 /* The following fields are filled by file system core */
115 const struct fs_file_system_t *fs;
118};
119
126struct fs_dirent {
134 size_t size;
135};
136
145 unsigned long f_bsize;
147 unsigned long f_frsize;
149 unsigned long f_blocks;
151 unsigned long f_bfree;
152};
153
154
160#define FS_O_READ 0x01
162#define FS_O_WRITE 0x02
164#define FS_O_RDWR (FS_O_READ | FS_O_WRITE)
166#define FS_O_MODE_MASK 0x03
167
169#define FS_O_CREATE 0x10
171#define FS_O_APPEND 0x20
173#define FS_O_TRUNC 0x40
175#define FS_O_FLAGS_MASK 0x70
176
177
179#define FS_O_MASK (FS_O_MODE_MASK | FS_O_FLAGS_MASK)
183
188#ifndef FS_SEEK_SET
190#define FS_SEEK_SET 0
191#endif
192#ifndef FS_SEEK_CUR
194#define FS_SEEK_CUR 1
195#endif
196#ifndef FS_SEEK_END
198#define FS_SEEK_END 2
199#endif
203
212#define FSTAB_ENTRY_DT_MOUNT_FLAGS(node_id) \
213 ((DT_PROP(node_id, automount) ? FS_MOUNT_FLAG_AUTOMOUNT : 0) \
214 | (DT_PROP(node_id, read_only) ? FS_MOUNT_FLAG_READ_ONLY : 0) \
215 | (DT_PROP(node_id, no_format) ? FS_MOUNT_FLAG_NO_FORMAT : 0) \
216 | (DT_PROP(node_id, disk_access) ? FS_MOUNT_FLAG_USE_DISK_ACCESS : 0))
217
224#define FSTAB_ENTRY_DT_MOUNT_POINT(node_id) \
225 DT_PROP(node_id, mount_point)
226
233#define FSTAB_ENTRY_DT_INST_MOUNT_POINT(inst) \
234 DT_INST_PROP(inst, mount_point)
235
242#define FS_FSTAB_ENTRY(node_id) _CONCAT(z_fsmp_, node_id)
243
252#define FS_FSTAB_DECLARE_ENTRY(node_id) \
253 extern struct fs_mount_t FS_FSTAB_ENTRY(node_id)
254
264static inline void fs_file_t_init(struct fs_file_t *zfp)
265{
266 zfp->filep = NULL;
267 zfp->mp = NULL;
268 zfp->flags = 0;
269}
270
280static inline void fs_dir_t_init(struct fs_dir_t *zdp)
281{
282 zdp->dirp = NULL;
283 zdp->mp = NULL;
284}
285
321int fs_open(struct fs_file_t *zfp, const char *file_name, fs_mode_t flags);
322
334int fs_close(struct fs_file_t *zfp);
335
350int fs_unlink(const char *path);
351
377int fs_rename(const char *from, const char *to);
378
395ssize_t fs_read(struct fs_file_t *zfp, void *ptr, size_t size);
396
416ssize_t fs_write(struct fs_file_t *zfp, const void *ptr, size_t size);
417
436int fs_seek(struct fs_file_t *zfp, off_t offset, int whence);
437
453
474int fs_truncate(struct fs_file_t *zfp, off_t length);
475
492int fs_sync(struct fs_file_t *zfp);
493
508int fs_mkdir(const char *path);
509
524int fs_opendir(struct fs_dir_t *zdp, const char *path);
525
545int fs_readdir(struct fs_dir_t *zdp, struct fs_dirent *entry);
546
558int fs_closedir(struct fs_dir_t *zdp);
559
587int fs_mount(struct fs_mount_t *mp);
588
603int fs_unmount(struct fs_mount_t *mp);
604
620int fs_readmount(int *index, const char **name);
621
638int fs_stat(const char *path, struct fs_dirent *entry);
639
654int fs_statvfs(const char *path, struct fs_statvfs *stat);
655
667int fs_mkfs(int fs_type, uintptr_t dev_id, void *cfg, int flags);
668
684int fs_register(int type, const struct fs_file_system_t *fs);
685
697int fs_unregister(int type, const struct fs_file_system_t *fs);
698
712int fs_gc(struct fs_mount_t *mp);
713
717
718
719#ifdef __cplusplus
720}
721#endif
722
723#endif /* ZEPHYR_INCLUDE_FS_FS_H_ */
Header file for the doubly-linked list API.
uint8_t fs_mode_t
Definition fs_interface.h:70
#define MAX_FILE_NAME
Definition fs_interface.h:63
struct _dnode sys_dnode_t
Doubly-linked list node structure.
Definition dlist.h:57
int fs_opendir(struct fs_dir_t *zdp, const char *path)
Directory open.
int fs_unregister(int type, const struct fs_file_system_t *fs)
Unregister a file system.
int fs_statvfs(const char *path, struct fs_statvfs *stat)
Retrieves statistics of the file system volume.
int fs_mkdir(const char *path)
Directory create.
int fs_rename(const char *from, const char *to)
Rename file or directory.
int fs_mount(struct fs_mount_t *mp)
Mount filesystem.
int fs_close(struct fs_file_t *zfp)
Close file.
int fs_gc(struct fs_mount_t *mp)
Attempt to proactively clean file system.
off_t fs_tell(struct fs_file_t *zfp)
Get current file position.
fs_dir_entry_type
Enumeration for directory entry types.
Definition fs.h:33
int fs_mkfs(int fs_type, uintptr_t dev_id, void *cfg, int flags)
Create fresh file system.
int fs_seek(struct fs_file_t *zfp, off_t offset, int whence)
Seek file.
int fs_stat(const char *path, struct fs_dirent *entry)
File or directory status.
int fs_register(int type, const struct fs_file_system_t *fs)
Register a file system.
int fs_open(struct fs_file_t *zfp, const char *file_name, fs_mode_t flags)
Open or create file.
ssize_t fs_write(struct fs_file_t *zfp, const void *ptr, size_t size)
Write file.
int fs_closedir(struct fs_dir_t *zdp)
Directory close.
int fs_readmount(int *index, const char **name)
Get path of mount point at index.
int fs_readdir(struct fs_dir_t *zdp, struct fs_dirent *entry)
Directory read entry.
int fs_unlink(const char *path)
Unlink file.
ssize_t fs_read(struct fs_file_t *zfp, void *ptr, size_t size)
Read file.
int fs_unmount(struct fs_mount_t *mp)
Unmount filesystem.
int fs_sync(struct fs_file_t *zfp)
Flush cached write data buffers of an open file.
static void fs_dir_t_init(struct fs_dir_t *zdp)
Initialize fs_dir_t object.
Definition fs.h:280
static void fs_file_t_init(struct fs_file_t *zfp)
Initialize fs_file_t object.
Definition fs.h:264
int fs_truncate(struct fs_file_t *zfp, off_t length)
Truncate or extend an open file to a given size.
@ FS_EXT2
Identifier for in-tree Ext2 file system.
Definition fs.h:62
@ FS_LITTLEFS
Identifier for in-tree LittleFS file system.
Definition fs.h:59
@ FS_RPMSGFS
Identifier for in-tree RpMsgFS file system.
Definition fs.h:68
@ FS_VIRTIOFS
Identifier for in-tree Virtiofs file system.
Definition fs.h:65
@ FS_TYPE_EXTERNAL_BASE
Base identifier for external file systems.
Definition fs.h:74
@ FS_NATIVE_MOUNT
Identifier for in-tree Native Simulator mount file system.
Definition fs.h:71
@ FS_FATFS
Identifier for in-tree FatFS file system.
Definition fs.h:56
@ FS_DIR_ENTRY_DIR
Identifier for directory entry.
Definition fs.h:37
@ FS_DIR_ENTRY_FILE
Identifier for file entry.
Definition fs.h:35
__SIZE_TYPE__ ssize_t
Definition types.h:28
__INTPTR_TYPE__ off_t
Definition types.h:36
flags
Definition parser.h:97
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
Directory object representing an open directory.
Definition fs_interface.h:98
const struct fs_mount_t * mp
Pointer to mount point structure.
Definition fs_interface.h:102
void * dirp
Pointer to directory object structure.
Definition fs_interface.h:100
Structure to receive file or directory information.
Definition fs.h:126
enum fs_dir_entry_type type
File/directory type (FS_DIR_ENTRY_FILE or FS_DIR_ENTRY_DIR).
Definition fs.h:130
size_t size
Size of file (0 if directory).
Definition fs.h:134
char name[MAX_FILE_NAME+1]
Name of file or directory.
Definition fs.h:132
File System interface structure.
Definition fs_sys.h:22
File object representing an open file.
Definition fs_interface.h:84
fs_mode_t flags
Open/create flags.
Definition fs_interface.h:90
void * filep
Pointer to file object structure.
Definition fs_interface.h:86
const struct fs_mount_t * mp
Pointer to mount point structure.
Definition fs_interface.h:88
File system mount info structure.
Definition fs.h:100
const struct fs_file_system_t * fs
Pointer to File system interface of the mount point.
Definition fs.h:115
const char * mnt_point
Mount point directory name (ex: "/fatfs").
Definition fs.h:106
void * storage_dev
Pointer to backend storage device.
Definition fs.h:110
void * fs_data
Pointer to file system specific data.
Definition fs.h:108
int type
File system type.
Definition fs.h:104
size_t mountp_len
Length of Mount point string.
Definition fs.h:113
uint8_t flags
Mount flags.
Definition fs.h:117
sys_dnode_t node
Entry for the fs_mount_list list.
Definition fs.h:102
Structure to receive volume statistics.
Definition fs.h:143
unsigned long f_bsize
Optimal transfer block size.
Definition fs.h:145
unsigned long f_frsize
Allocation unit size.
Definition fs.h:147
unsigned long f_blocks
Size of FS in f_frsize units.
Definition fs.h:149
unsigned long f_bfree
Number of free blocks.
Definition fs.h:151
Definition stat.h:57