1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef RAMFS_API_H
6 #define RAMFS_API_H
7 
8 #include "ramfs_err.h"
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 typedef struct {
15     uint32_t st_mode;
16     uint32_t st_size;
17     uint8_t  is_dir;
18 } ramfs_stat_t;
19 
20 typedef enum {
21     RAMFS_MODE_WR = 0x01,
22     RAMFS_MODE_RD = 0x02,
23 } ramfs_mode_e;
24 
25 /**
26  * @brief Initialize ramfs module
27  *
28  * @return None
29  */
30 void ramfs_init(void);
31 
32 /**
33  * @brief Deinitialize ramfs module
34  *
35  * @return None
36  */
37 void ramfs_deinit(void);
38 
39 /**
40  * @brief Give the state of the ramfs
41  *
42  * @return 0 if the ramfs is initialized and can be used else false
43  */
44 int32_t ramfs_ready(void);
45 
46 /**
47  * @brief Open a file in ramfs
48  *
49  * @param[out] fp   pointer to a ramfs file object
50  * @param[in]  fn   name of the file. There are no directories. e.g. "a.txt"
51  * @param[in]  mode file open mode (e.g. RAMFS_MODE_WR | RAMFS_MODE_RD)
52  *
53  * @return 0 on success, otherwise will be failed
54  */
55 int32_t ramfs_open(void *fp, const char* fn, uint32_t mode);
56 
57 /**
58  * @brief Create a file with a constant data
59  *
60  * @param[in] fn   name of the file (directories are not supported)
61  * @param[in] data pointer to a constant data
62  * @param[in] len  length of the constant data
63  *
64  * @return 0 on success, otherwise will be failed
65  */
66 int32_t ramfs_create_const(const char *fn, const void *data, uint32_t len);
67 
68 /**
69  * @brief Close an opened file
70  *
71  * @param[in] fp pointer to a ramfs file object
72  *
73  * @return 0 on success, otherwise will be failed
74  */
75 int32_t ramfs_close(void *fp);
76 
77 /**
78  * @brief Remove a file, the file should not be opened
79  *
80  * @param[in] fn pointer to a ramfs file object
81  *
82  * @return 0 on success, otherwise will be failed
83  */
84 int32_t ramfs_remove(const char *fn);
85 
86 /**
87  * @brief Remove a file, the file should not be opened
88  *
89  * @param[in] old pointer to old name
90  * @param[in] new pointer to new name
91  *
92  * @return 0 on success, otherwise will be failed
93  */
94 int32_t ramfs_rename(const char *old, const char *new);
95 
96 /**
97  * @brief Read data from an opened file
98  *
99  * @param[in]  fp  pointer to a ramfs file object
100  * @param[out] buf poiner to a memory block where to store the read data
101  * @param[in]  btr number of bytes to read
102  * @param[out] br  the real number of read bytes(Byte Read)
103  *
104  * @return 0 on success, otherwise will be failed
105  */
106 int32_t ramfs_read(void *fp, void *buf, uint32_t btr, uint32_t *br);
107 
108 /**
109  * @brief Write data to an opened file
110  *
111  * @param[in]  fp  pointer to a ramfs file object
112  * @param[in]  buf pointer to a memory block which content will be written
113  * @param[in]  btw number of bytes to write
114  * @param[out] bw  the real number to write bytes(Byte Written)
115  *
116  * @return 0 on success, otherwise will be failed
117  */
118 int32_t ramfs_write(void *fp, const void *buf, uint32_t btw, uint32_t *bw);
119 
120 /**
121  * @brief Set the read/write pointer. Also expand the file size if necessary.
122  *
123  * @param[in] fp  pointer to a ramfs file object (opened with ramfs_open)
124  * @param[in] pos the new position of read/write pointer
125  *
126  * @return 0 on success, otherwise will be failed
127  */
128 int32_t ramfs_seek(void *fp, uint32_t pos);
129 
130 /**
131  * @brief Give the position of the read/write pointer
132  *
133  * @param[in]  fp  pointer to a ramfs file object (opened with ramfs_open)
134  * @param[out] pos pointer to store the result
135  *
136  * @return 0 on success, otherwise will be failed
137  */
138 int32_t ramfs_tell(void *fp, uint32_t *pos);
139 
140 /**
141  * @brief Truncate the file size to the current position of the read/write pointer
142  *
143  * @param[in] fp pointer to a ramfs file object (opened with ramfs_open)
144  *
145  * @return 0 on success, otherwise will be failed
146  */
147 int32_t ramfs_trunc(void *fp);
148 
149 /**
150  * @brief Give the size of the file in bytes
151  *
152  * @param[in]  fp   pointer to a ramfs file object
153  * @param[out] size pointer to store the size
154  *
155  * @return 0 on success, otherwise will be failed
156  */
157 int32_t ramfs_size(void *fp, uint32_t *size);
158 
159 /**
160  * @brief Get access information
161  *
162  * @param[in] path the path of the file
163  * @param[in] mode the information to get
164  *
165  * @return 0 on success, otherwise will be failed
166  */
167 int32_t ramfs_access(const char *path, int32_t mode);
168 
169 /**
170  * @brief Create a directory
171  *
172  * @param[in] path the path of the directory
173  *
174  * @return 0 on success, otherwise will be failed
175  */
176 int32_t ramfs_mkdir(const char *path);
177 
178 /**
179  * @brief Open a directory in ramfs
180  *
181  * @param[out] dp   pointer to a ramfs directory object
182  * @param[in]  path the path of the directory
183  *
184  * @return 0 on success, otherwise will be failed
185  */
186 int32_t ramfs_opendir(void *dp, const char *path);
187 
188 /**
189  * @brief Read the next file name under the directory
190  *
191  * @param[in]  dp pointer to a ramfs directory object
192  * @param[out] fn pointer to buffer to store the file name
193  *
194  * @return 0 on success, otherwise will be failed
195  */
196 int32_t ramfs_readdir(void *dp, char *fn);
197 
198 /**
199  * @brief Close the directory
200  *
201  * @param[in] dp pointer to a ramfs directory object
202  *
203  * @return 0 on success, otherwise will be failed
204  */
205 int32_t ramfs_closedir(void *dp);
206 
207 /**
208  * @brief Remove a directory
209  *
210  * @param[in]  path the path of the directory
211  *
212  * @return 0 on success, otherwise will be failed
213  */
214 int32_t ramfs_rmdir(const char *path);
215 
216 /**
217  * @brief Get file info
218  *
219  * @param[in]  path the path of the file to find info about
220  * @param[out] st   the stat buffer to write to
221  *
222  * @return 0 on success, otherwise will be failed
223  */
224 int32_t ramfs_stat(const char *path, ramfs_stat_t *st);
225 
226 /**
227  * @brief Get path info
228  *
229  * @param[in] name the kind of path conf to get
230  *
231  * @return value of path info
232  */
233 int32_t ramfs_pathconf(int32_t name);
234 
235 /**
236  * @brief link path2 to path1
237  *
238  * @param[in] path1 the path to be linked
239  * @param[in] path2 the path to link
240  *
241  * @return 0 on success, negative error on failure
242  *
243  */
244 int ramfs_link(const char *path1, const char *path2);
245 
246 /**
247  * @brief Remove a file from the filesystem
248  *
249  * @param[in] path the path of the file to remove
250  *
251  * @return 0 on success, negative error on failure
252  *
253  */
254 int ramfs_unlink(const char *path);
255 
256 #ifdef __cplusplus
257 }
258 #endif
259 
260 #endif /* RAMFS_API_H */
261 
262