Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Intel Corporation. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 5 | */ |
| 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 |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 17 | /* Create a fs_file_t type similar to FILE for familiarity */ |
| 18 | typedef struct _fs_file_object fs_file_t; |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 19 | |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 20 | /* Create a fs_dir_t type similar to DIR for familiarity */ |
| 21 | typedef struct _fs_dir_object fs_dir_t; |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 22 | |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 23 | enum fs_dir_entry_type { |
| 24 | FS_DIR_ENTRY_FILE, |
| 25 | FS_DIR_ENTRY_DIR |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 28 | /** |
| 29 | * @brief File System Functions |
| 30 | * @defgroup data_structures File System Data Structures |
| 31 | * @ingroup file_system |
| 32 | * @{ |
| 33 | */ |
| 34 | |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 35 | /** @var fs_file_t |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 36 | * @brief File object representing an open file |
| 37 | */ |
| 38 | |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 39 | /** @var fs_dir_t |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 40 | * @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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 50 | * - FS_DIR_ENTRY_FILE |
| 51 | * - FS_DIR_ENTRY_DIR |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 52 | * @param name Name of directory or file |
| 53 | * @param size Size of file. 0 if directory |
| 54 | */ |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 55 | struct fs_dirent { |
| 56 | enum fs_dir_entry_type type; |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 57 | char name[MAX_FILE_NAME + 1]; |
| 58 | size_t size; |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 61 | /** |
Ramesh Thomas | 524004d | 2016-09-01 17:11:16 -0700 | [diff] [blame] | 62 | * @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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 72 | struct fs_statvfs { |
Ramesh Thomas | d31b3fe | 2016-10-03 19:56:59 -0700 | [diff] [blame] | 73 | unsigned long f_bsize; |
| 74 | unsigned long f_frsize; |
| 75 | unsigned long f_blocks; |
| 76 | unsigned long f_bfree; |
Ramesh Thomas | 524004d | 2016-09-01 17:11:16 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /** |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 80 | * @} |
| 81 | */ |
| 82 | |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 83 | #ifndef FS_SEEK_SET |
| 84 | #define FS_SEEK_SET 0 /* Seek from beginning of file. */ |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 85 | #endif |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 86 | #ifndef FS_SEEK_CUR |
| 87 | #define FS_SEEK_CUR 1 /* Seek from current position. */ |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 88 | #endif |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 89 | #ifndef FS_SEEK_END |
| 90 | #define FS_SEEK_END 2 /* Seek from end of file. */ |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | /** |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 94 | * @brief File System APIs |
| 95 | * @defgroup file_system_api File System APIs |
| 96 | * @ingroup file_system |
| 97 | * @{ |
| 98 | */ |
| 99 | |
| 100 | /** |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 101 | * @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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 112 | int fs_open(fs_file_t *zfp, const char *file_name); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 113 | |
| 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 125 | int fs_close(fs_file_t *zfp); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 126 | |
| 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 | */ |
| 137 | int 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 153 | ssize_t fs_read(fs_file_t *zfp, void *ptr, size_t size); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 154 | |
| 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 174 | ssize_t fs_write(fs_file_t *zfp, const void *ptr, size_t size); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 175 | |
| 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 185 | * - FS_SEEK_SET = from beginning of file |
| 186 | * - FS_SEEK_CUR = from current position, |
| 187 | * - FS_SEEK_END = from end of file. |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 188 | * |
| 189 | * @retval 0 Success |
| 190 | * @retval -ERRNO errno code if error. |
| 191 | */ |
Johan Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 192 | int fs_seek(fs_file_t *zfp, off_t offset, int whence); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 193 | |
| 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 204 | off_t fs_tell(fs_file_t *zfp); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
Ramesh Thomas | 211db53 | 2016-08-26 17:56:07 -0700 | [diff] [blame] | 207 | * @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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 224 | int fs_truncate(fs_file_t *zfp, off_t length); |
Ramesh Thomas | 211db53 | 2016-08-26 17:56:07 -0700 | [diff] [blame] | 225 | |
| 226 | /** |
Ramesh Thomas | 97d8fd1 | 2016-09-01 23:15:25 -0700 | [diff] [blame] | 227 | * @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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 240 | int fs_sync(fs_file_t *zfp); |
Ramesh Thomas | 97d8fd1 | 2016-09-01 23:15:25 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 243 | * @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 | */ |
| 252 | int 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 265 | int fs_opendir(fs_dir_t *zdp, const char *path); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 266 | |
| 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 280 | int fs_readdir(fs_dir_t *zdp, struct fs_dirent *entry); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 281 | |
| 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 292 | int fs_closedir(fs_dir_t *zdp); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 293 | |
| 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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 306 | int fs_stat(const char *path, struct fs_dirent *entry); |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 307 | |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 308 | /** |
Ramesh Thomas | 524004d | 2016-09-01 17:11:16 -0700 | [diff] [blame] | 309 | * @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 Hedberg | b108d02 | 2016-10-30 08:57:35 +0200 | [diff] [blame] | 318 | int fs_statvfs(struct fs_statvfs *stat); |
Ramesh Thomas | 524004d | 2016-09-01 17:11:16 -0700 | [diff] [blame] | 319 | |
| 320 | /** |
Ramesh Thomas | 0e43969 | 2016-08-04 20:42:46 -0700 | [diff] [blame] | 321 | * @} |
| 322 | */ |
| 323 | |
Ramesh Thomas | c9ec4ee | 2016-07-22 17:48:59 -0700 | [diff] [blame] | 324 | #ifdef __cplusplus |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | #endif /* _FS_H_ */ |