summaryrefslogtreecommitdiff
path: root/components/dfs/dfs_v2/include/dfs.h
blob: 582ec428aca3a3672ea539e238e11104d789abae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2005-02-22     Bernard      The first version.
 * 2023-05-05     Bernard      change to dfs v2.0
 */

#ifndef __DFS_H__
#define __DFS_H__

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "../../libc/compilers/common/include/dirent.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <rtatomic.h>
#include <rtdevice.h>

#ifndef ATTR_MODE_SET
#define ATTR_MODE_SET	(1 << 6)
#endif

#ifndef ATTR_ATIME_SET
#define ATTR_ATIME_SET	(1 << 7)
#endif

#ifndef ATTR_MTIME_SET
#define ATTR_MTIME_SET	(1 << 8)
#endif

#ifndef ATTR_UID_SET
#define ATTR_UID_SET	(1 << 9)
#endif

#ifndef ATTR_GID_SET
#define ATTR_GID_SET	(1 << 10)
#endif

#ifndef AT_SYMLINK_NOFOLLOW
#define AT_SYMLINK_NOFOLLOW 0x100
#endif

#ifndef UTIME_NOW
#define UTIME_NOW  0x3fffffff
#endif

#ifndef UTIME_OMIT
#define UTIME_OMIT 0x3ffffffe
#endif

#ifndef DFS_FD_MAX
#define DFS_FD_MAX              16
#endif

/*
 * skip stdin/stdout/stderr normally
 */
#ifndef DFS_STDIO_OFFSET
#define DFS_STDIO_OFFSET        3
#endif

#ifndef DFS_PATH_MAX
#define DFS_PATH_MAX            4096
#endif

#ifndef SECTOR_SIZE
#define SECTOR_SIZE             512
#endif

#define DFS_FS_FLAG_DEFAULT     0x00    /* default flag */
#define DFS_FS_FLAG_FULLPATH    0x01    /* set full path to underlaying file system */

/* File flags */
#define DFS_F_FREAD     0x01
#define DFS_F_FWRITE    0x02

#ifdef __cplusplus
extern "C" {
#endif

rt_inline int dfs_fflags(int oflags)
{
    int rw = oflags & O_ACCMODE;

    oflags &= ~O_ACCMODE;
    return (rw + 1) | oflags;
}

rt_inline int dfs_oflags(int fflags)
{
    int rw = fflags & (DFS_F_FREAD | DFS_F_FWRITE);

    fflags &= ~(DFS_F_FREAD | DFS_F_FWRITE);
    return (rw - 1) | fflags;
}

struct dfs_fdtable
{
    uint32_t maxfd;
    struct dfs_file **fds;
};

/* Initialization of dfs */
int dfs_init(void);

char *dfs_normalize_path(const char *directory, const char *filename);
const char *dfs_subdir(const char *directory, const char *filename);

rt_err_t dfs_lock(void);
void dfs_unlock(void);

rt_err_t dfs_file_lock(void);
void dfs_file_unlock(void);

int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, int fd_src);
int dfs_fdtable_drop_fd(struct dfs_fdtable *fdtab, int fd);

#ifdef DFS_USING_POSIX
/* FD APIs */
int fdt_fd_new(struct dfs_fdtable *fdt);
struct dfs_file *fdt_get_file(struct dfs_fdtable* fdt, int fd);
void fdt_fd_release(struct dfs_fdtable* fdt, int fd);
int fd_new(void);
int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file);
struct dfs_file *fd_get(int fd);
void fd_release(int fd);

void fd_init(struct dfs_file *fd);

struct dfs_fdtable *dfs_fdtable_get(void);
struct dfs_fdtable *dfs_fdtable_get_global(void);
int dfs_dup(int oldfd, int startfd);
#endif /* DFS_USING_POSIX */

struct dfs_file* dfs_file_create(void);
void dfs_file_destroy(struct dfs_file *file);

#ifdef __cplusplus
}
#endif

#endif