blob: 7ed9e1c8cb2d46f31f6b38af571feec4080ab970 [file] [log] [blame]
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -07001/*
2 * Copyright (c) 2016 Intel Corporation.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -07005 */
6
7#ifndef _FS_H_
8#define _FS_H_
9
10#include <sys/types.h>
11#include <fs/fs_interface.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
Johan Hedbergb108d022016-10-30 08:57:35 +020017/* Create a fs_file_t type similar to FILE for familiarity */
18typedef struct _fs_file_object fs_file_t;
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -070019
Johan Hedbergb108d022016-10-30 08:57:35 +020020/* Create a fs_dir_t type similar to DIR for familiarity */
21typedef struct _fs_dir_object fs_dir_t;
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -070022
Johan Hedbergb108d022016-10-30 08:57:35 +020023enum fs_dir_entry_type {
24 FS_DIR_ENTRY_FILE,
25 FS_DIR_ENTRY_DIR
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -070026};
27
Ramesh Thomas0e439692016-08-04 20:42:46 -070028/**
29 * @brief File System Functions
30 * @defgroup data_structures File System Data Structures
31 * @ingroup file_system
32 * @{
33 */
34
Johan Hedbergb108d022016-10-30 08:57:35 +020035/** @var fs_file_t
Ramesh Thomas0e439692016-08-04 20:42:46 -070036 * @brief File object representing an open file
37 */
38
Johan Hedbergb108d022016-10-30 08:57:35 +020039/** @var fs_dir_t
Ramesh Thomas0e439692016-08-04 20:42:46 -070040 * @brief Directory object representing an open directory
41 */
42
43/**
44 * @brief Structure to receive file or directory information
45 *
46 * Used in functions that reads the directory entries to get
47 * file or directory information.
48 *
49 * @param dir_entry_type Whether file or directory
Johan Hedbergb108d022016-10-30 08:57:35 +020050 * - FS_DIR_ENTRY_FILE
51 * - FS_DIR_ENTRY_DIR
Ramesh Thomas0e439692016-08-04 20:42:46 -070052 * @param name Name of directory or file
53 * @param size Size of file. 0 if directory
54 */
Johan Hedbergb108d022016-10-30 08:57:35 +020055struct fs_dirent {
56 enum fs_dir_entry_type type;
Ramesh Thomas0e439692016-08-04 20:42:46 -070057 char name[MAX_FILE_NAME + 1];
58 size_t size;
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -070059};
60
Ramesh Thomas0e439692016-08-04 20:42:46 -070061/**
Ramesh Thomas524004d2016-09-01 17:11:16 -070062 * @brief Structure to receive volume statistics
63 *
64 * Used to retrieve information about total and available space
65 * in the volume.
66 *
67 * @param f_bsize Optimal transfer block size
68 * @param f_frsize Allocation unit size
69 * @param f_blocks Size of FS in f_frsize units
70 * @param f_bfree Number of free blocks
71 */
Johan Hedbergb108d022016-10-30 08:57:35 +020072struct fs_statvfs {
Ramesh Thomasd31b3fe2016-10-03 19:56:59 -070073 unsigned long f_bsize;
74 unsigned long f_frsize;
75 unsigned long f_blocks;
76 unsigned long f_bfree;
Ramesh Thomas524004d2016-09-01 17:11:16 -070077};
78
79/**
Ramesh Thomas0e439692016-08-04 20:42:46 -070080 * @}
81 */
82
Johan Hedbergb108d022016-10-30 08:57:35 +020083#ifndef FS_SEEK_SET
84#define FS_SEEK_SET 0 /* Seek from beginning of file. */
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -070085#endif
Johan Hedbergb108d022016-10-30 08:57:35 +020086#ifndef FS_SEEK_CUR
87#define FS_SEEK_CUR 1 /* Seek from current position. */
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -070088#endif
Johan Hedbergb108d022016-10-30 08:57:35 +020089#ifndef FS_SEEK_END
90#define FS_SEEK_END 2 /* Seek from end of file. */
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -070091#endif
92
93/**
Ramesh Thomas0e439692016-08-04 20:42:46 -070094 * @brief File System APIs
95 * @defgroup file_system_api File System APIs
96 * @ingroup file_system
97 * @{
98 */
99
100/**
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700101 * @brief File open
102 *
103 * Opens an existing file or create a new one and associates
104 * a stream with it.
105 *
106 * @param zfp Pointer to file object
107 * @param file_name The name of file to open
108 *
109 * @retval 0 Success
110 * @retval -ERRNO errno code if error
111 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200112int fs_open(fs_file_t *zfp, const char *file_name);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700113
114/**
115 * @brief File close
116 *
117 * Flushes the associated stream and closes
118 * the file.
119 *
120 * @param zfp Pointer to the file object
121 *
122 * @retval 0 Success
123 * @retval -ERRNO errno code if error
124 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200125int fs_close(fs_file_t *zfp);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700126
127/**
128 * @brief File unlink
129 *
130 * Deletes the specified file or directory
131 *
132 * @param path Path to the file or directory to delete
133 *
134 * @retval 0 Success
135 * @retval -ERRNO errno code if error
136 */
137int fs_unlink(const char *path);
138
139/**
140 * @brief File read
141 *
142 * Reads items of data of size bytes long.
143 *
144 * @param zfp Pointer to the file object
145 * @param ptr Pointer to the data buffer
146 * @param size Number of bytes to be read
147 *
148 * @return Number of bytes read. On success, it will be equal to number of
149 * items requested to be read. Returns less than number of bytes
150 * requested if there are not enough bytes available in file. Will return
151 * -ERRNO code on error.
152 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200153ssize_t fs_read(fs_file_t *zfp, void *ptr, size_t size);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700154
155/**
156 * @brief File write
157 *
158 * Writes items of data of size bytes long.
159 *
160 * @param zfp Pointer to the file object
161 * @param ptr Pointer to the data buffer
162 * @param size Number of bytes to be write
163 *
164 * @return Number of bytes written. On success, it will be equal to the number
165 * of bytes requested to be written. Any other value, indicates an error. Will
166 * return -ERRNO code on error.
167 * In the case where -ERRNO is returned, the file pointer will not be
168 * advanced because it couldn't start the operation.
169 * In the case where it is able to write, but is not able to complete writing
170 * all of the requested number of bytes, then it is because the disk got full.
171 * In that case, it returns less number of bytes written than requested, but
172 * not a negative -ERRNO value as in regular error case.
173 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200174ssize_t fs_write(fs_file_t *zfp, const void *ptr, size_t size);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700175
176/**
177 * @brief File seek
178 *
179 * Moves the file position to a new location in the file. The offset is added
180 * to file position based on the 'whence' parameter.
181 *
182 * @param zfp Pointer to the file object
183 * @param offset Relative location to move the file pointer to
184 * @param whence Relative location from where offset is to be calculated.
Johan Hedbergb108d022016-10-30 08:57:35 +0200185 * - FS_SEEK_SET = from beginning of file
186 * - FS_SEEK_CUR = from current position,
187 * - FS_SEEK_END = from end of file.
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700188 *
189 * @retval 0 Success
190 * @retval -ERRNO errno code if error.
191 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200192int fs_seek(fs_file_t *zfp, off_t offset, int whence);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700193
194/**
195 * @brief Get current file position.
196 *
197 * Retrieves the current position in the file.
198 *
199 * @param zfp Pointer to the file object
200 *
201 * @retval position Current position in file
202 * Current revision does not validate the file object.
203 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200204off_t fs_tell(fs_file_t *zfp);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700205
206/**
Ramesh Thomas211db532016-08-26 17:56:07 -0700207 * @brief Change the size of an open file
208 *
209 * Truncates the file to the new length if it is shorter than the current
210 * size of the file. Expands the file if the new length is greater than the
211 * current size of the file. The expanded region would be filled with zeroes.
212 *
213 * @note In the case of expansion, if the volume got full during the
214 * expansion process, the function will expand to the maximum possible length
215 * and returns success. Caller should check if the expanded size matches the
216 * requested length.
217 *
218 * @param zfp Pointer to the file object
219 * @param length New size of the file in bytes
220 *
221 * @retval 0 Success
222 * @retval -ERRNO errno code if error
223 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200224int fs_truncate(fs_file_t *zfp, off_t length);
Ramesh Thomas211db532016-08-26 17:56:07 -0700225
226/**
Ramesh Thomas97d8fd12016-09-01 23:15:25 -0700227 * @brief Flushes any cached write of an open file
228 *
229 * This function can be used to flush the cache of an open file. This can
230 * be called to ensure data gets written to the storage media immediately.
231 * This may be done to avoid data loss if power is removed unexpectedly.
232 * Note that closing a file will cause caches to be flushed correctly so it
233 * need not be called if the file is being closed.
234 *
235 * @param zfp Pointer to the file object
236 *
237 * @retval 0 Success
238 * @retval -ERRNO errno code if error
239 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200240int fs_sync(fs_file_t *zfp);
Ramesh Thomas97d8fd12016-09-01 23:15:25 -0700241
242/**
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700243 * @brief Directory create
244 *
245 * Creates a new directory using specified path.
246 *
247 * @param path Path to the directory to create
248 *
249 * @retval 0 Success
250 * @retval -ERRNO errno code if error
251 */
252int fs_mkdir(const char *path);
253
254/**
255 * @brief Directory open
256 *
257 * Opens an existing directory specified by the path.
258 *
259 * @param zdp Pointer to the directory object
260 * @param path Path to the directory to open
261 *
262 * @retval 0 Success
263 * @retval -ERRNO errno code if error
264 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200265int fs_opendir(fs_dir_t *zdp, const char *path);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700266
267/**
268 * @brief Directory read entry
269 *
270 * Reads directory entries of a open directory
271 *
272 * @param zdp Pointer to the directory object
273 * @param entry Pointer to zfs_dirent structure to read the entry into
274 *
275 * @retval 0 Success
276 * @retval -ERRNO errno code if error
277 * @return In end-of-dir condition, this will return 0 and set
278 * entry->name[0] = 0
279 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200280int fs_readdir(fs_dir_t *zdp, struct fs_dirent *entry);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700281
282/**
283 * @brief Directory close
284 *
285 * Closes an open directory.
286 *
287 * @param zdp Pointer to the directory object
288 *
289 * @retval 0 Success
290 * @retval -ERRNO errno code if error
291 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200292int fs_closedir(fs_dir_t *zdp);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700293
294/**
295 * @brief File or directory status
296 *
297 * Checks the status of a file or directory specified by the path
298 *
299 * @param path Path to the file or directory
300 * @param entry Pointer to zfs_dirent structure to fill if file or directory
301 * exists.
302 *
303 * @retval 0 Success
304 * @retval -ERRNO errno code if error
305 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200306int fs_stat(const char *path, struct fs_dirent *entry);
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700307
Ramesh Thomas0e439692016-08-04 20:42:46 -0700308/**
Ramesh Thomas524004d2016-09-01 17:11:16 -0700309 * @brief Retrieves statistics of the file system volume
310 *
311 * Returns the total and available space in the file system volume.
312 *
313 * @param stat Pointer to zfs_statvfs structure to receive the fs statistics
314 *
315 * @retval 0 Success
316 * @retval -ERRNO errno code if error
317 */
Johan Hedbergb108d022016-10-30 08:57:35 +0200318int fs_statvfs(struct fs_statvfs *stat);
Ramesh Thomas524004d2016-09-01 17:11:16 -0700319
320/**
Ramesh Thomas0e439692016-08-04 20:42:46 -0700321 * @}
322 */
323
Ramesh Thomasc9ec4ee2016-07-22 17:48:59 -0700324#ifdef __cplusplus
325}
326#endif
327
328#endif /* _FS_H_ */