Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
fs.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation.
3 * Copyright (c) 2020-2021 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 <sys/dlist.h>
14#include <fs/fs_interface.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
25struct fs_file_system_t;
26
32};
33
48enum {
51
54
57};
58
60#define FS_MOUNT_FLAG_NO_FORMAT BIT(0)
62#define FS_MOUNT_FLAG_READ_ONLY BIT(1)
68#define FS_MOUNT_FLAG_AUTOMOUNT BIT(2)
69
70
83struct fs_mount_t {
85 int type;
86 const char *mnt_point;
87 void *fs_data;
89 /* fields filled by file system core */
90 size_t mountp_len;
91 const struct fs_file_system_t *fs;
93};
94
107struct fs_dirent {
110 size_t size;
111};
112
125 unsigned long f_bsize;
126 unsigned long f_frsize;
127 unsigned long f_blocks;
128 unsigned long f_bfree;
129};
130
131
137#define FS_O_READ 0x01
139#define FS_O_WRITE 0x02
141#define FS_O_RDWR (FS_O_READ | FS_O_WRITE)
143#define FS_O_MODE_MASK 0x03
144
146#define FS_O_CREATE 0x10
148#define FS_O_APPEND 0x20
150#define FS_O_FLAGS_MASK 0x30
151
153#define FS_O_MASK (FS_O_MODE_MASK | FS_O_FLAGS_MASK)
162#ifndef FS_SEEK_SET
164#define FS_SEEK_SET 0
165#endif
166#ifndef FS_SEEK_CUR
168#define FS_SEEK_CUR 1
169#endif
170#ifndef FS_SEEK_END
172#define FS_SEEK_END 2
173#endif
178/*
179 * @brief Get the common mount flags for an fstab entry.
180
181 * @param node_id the node identifier for a child entry in a
182 * zephyr,fstab node.
183 * @return a value suitable for initializing an fs_mount_t flags
184 * member.
185 */
186#define FSTAB_ENTRY_DT_MOUNT_FLAGS(node_id) \
187 ((DT_PROP(node_id, automount) ? FS_MOUNT_FLAG_AUTOMOUNT : 0) \
188 | (DT_PROP(node_id, read_only) ? FS_MOUNT_FLAG_READ_ONLY : 0) \
189 | (DT_PROP(node_id, no_format) ? FS_MOUNT_FLAG_NO_FORMAT : 0))
190
195#define FS_FSTAB_ENTRY(node_id) _CONCAT(z_fsmp_, node_id)
196
203#define FS_FSTAB_DECLARE_ENTRY(node_id) \
204 extern struct fs_mount_t FS_FSTAB_ENTRY(node_id)
205
215static inline void fs_file_t_init(struct fs_file_t *zfp)
216{
217 *zfp = (struct fs_file_t){ 0 };
218}
219
229static inline void fs_dir_t_init(struct fs_dir_t *zdp)
230{
231 *zdp = (struct fs_dir_t){ 0 };
232}
233
263int fs_open(struct fs_file_t *zfp, const char *file_name, fs_mode_t flags);
264
275int fs_close(struct fs_file_t *zfp);
276
290int fs_unlink(const char *path);
291
313int fs_rename(const char *from, const char *to);
314
329ssize_t fs_read(struct fs_file_t *zfp, void *ptr, size_t size);
330
349ssize_t fs_write(struct fs_file_t *zfp, const void *ptr, size_t size);
350
368int fs_seek(struct fs_file_t *zfp, off_t offset, int whence);
369
383off_t fs_tell(struct fs_file_t *zfp);
384
404int fs_truncate(struct fs_file_t *zfp, off_t length);
405
420int fs_sync(struct fs_file_t *zfp);
421
433int fs_mkdir(const char *path);
434
446int fs_opendir(struct fs_dir_t *zdp, const char *path);
447
465int fs_readdir(struct fs_dir_t *zdp, struct fs_dirent *entry);
466
477int fs_closedir(struct fs_dir_t *zdp);
478
504int fs_mount(struct fs_mount_t *mp);
505
520int fs_unmount(struct fs_mount_t *mp);
521
537int fs_readmount(int *index, const char **name);
538
552int fs_stat(const char *path, struct fs_dirent *entry);
553
567int fs_statvfs(const char *path, struct fs_statvfs *stat);
568
580int fs_register(int type, const struct fs_file_system_t *fs);
581
593int fs_unregister(int type, const struct fs_file_system_t *fs);
594
600#ifdef __cplusplus
601}
602#endif
603
604#endif /* ZEPHYR_INCLUDE_FS_FS_H_ */
ZTEST_BMEM int index[(3)]
Definition: main.c:32
Doubly-linked list implementation.
uint8_t fs_mode_t
Definition: fs_interface.h:36
#define MAX_FILE_NAME
Definition: fs_interface.h:30
struct _dnode sys_dnode_t
Definition: dlist.h:49
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.
off_t fs_tell(struct fs_file_t *zfp)
Get current file position.
fs_dir_entry_type
Definition: fs.h:27
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:229
static void fs_file_t_init(struct fs_file_t *zfp)
Initialize fs_file_t object.
Definition: fs.h:215
int fs_truncate(struct fs_file_t *zfp, off_t length)
Truncate or extend an open file to a given size.
@ FS_LITTLEFS
Definition: fs.h:53
@ FS_TYPE_EXTERNAL_BASE
Definition: fs.h:56
@ FS_FATFS
Definition: fs.h:50
@ FS_DIR_ENTRY_DIR
Definition: fs.h:31
@ FS_DIR_ENTRY_FILE
Definition: fs.h:29
flags
Definition: http_parser.h:131
__SIZE_TYPE__ ssize_t
Definition: types.h:28
void * ptr
Definition: printk.c:79
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
Directory object representing an open directory.
Definition: fs_interface.h:67
Structure to receive file or directory information.
Definition: fs.h:107
enum fs_dir_entry_type type
Definition: fs.h:108
size_t size
Definition: fs.h:110
char name[MAX_FILE_NAME+1]
Definition: fs.h:109
File System interface structure.
Definition: fs_sys.h:42
File object representing an open file.
Definition: fs_interface.h:53
File system mount info structure.
Definition: fs.h:83
const struct fs_file_system_t * fs
Definition: fs.h:91
const char * mnt_point
Definition: fs.h:86
void * storage_dev
Definition: fs.h:88
void * fs_data
Definition: fs.h:87
int type
Definition: fs.h:85
size_t mountp_len
Definition: fs.h:90
uint8_t flags
Definition: fs.h:92
sys_dnode_t node
Definition: fs.h:84
Structure to receive volume statistics.
Definition: fs.h:124
unsigned long f_bsize
Definition: fs.h:125
unsigned long f_frsize
Definition: fs.h:126
unsigned long f_blocks
Definition: fs.h:127
unsigned long f_bfree
Definition: fs.h:128
Definition: stat.h:39