1 /* 2 * @file hi_fs.h 3 * 4 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @defgroup iot_fs Filesystem 20 * @ingroup osa 21 */ 22 #ifndef __HI_FS_H__ 23 #define __HI_FS_H__ 24 25 #include <hi_types.h> 26 27 typedef struct { 28 hi_u32 size; 29 hi_u32 next_offset; 30 hi_char name[1]; 31 }hi_file_list; 32 33 34 #define HI_FS_SEEK_SET (0) /**< set file offset to offset */ 35 #define HI_FS_SEEK_CUR (1) /**< set file offset to current plus offset */ 36 #define HI_FS_SEEK_END (2) /**< set file offset to EOF plus offset */ 37 38 #define HI_FS_O_RDONLY 00 39 #define HI_FS_O_WRONLY 01 40 #define HI_FS_O_RDWR 02 41 42 #define HI_FS_O_CREAT 0100 43 #define HI_FS_O_EXCL 0200 44 #define HI_FS_O_TRUNC 01000 45 #define HI_FS_O_APPEND 02000 46 #define HI_FS_O_PATH 010000000 47 48 49 #define HI_FS_O_SEARCH HI_FS_O_PATH 50 #define HI_FS_O_ACCMODE (HI_FS_O_RDONLY | HI_FS_O_WRONLY | HI_FS_O_RDWR | HI_FS_O_SEARCH) 51 52 /** 53 * @ingroup iot_fs 54 * @brief Get filesystem error code.CNcomment:获取错误码。CNend 55 * 56 * @par 描述: 57 * @li Returns the filesystem's most recent error code value.CNcomment:返回文件系统最近的错误代码值。CNend 58 * 59 * @attention None 60 * @param None 61 * 62 * @retval filesystem error code.CNcomment:文件系统错误码。CNend 63 * @par 依赖: 64 * @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 65 * @see hi_get_fs_error 66 */ 67 hi_s32 hi_get_fs_error(hi_void); 68 69 /** 70 * @ingroup iot_fs 71 * @brief Set filesystem check and repair.CNcomment:设置文件系统加载时是否自动检查并修复。CNend 72 * 73 * @par 描述: 74 * @li Set filesystem check and repair, default True. 75 CNcomment:设置文件系统加载时是否自动检查并修复,默认自动检查并修复。CNend 76 * 77 * @attention set before fs init.CNcomment:在文件系统初始化之前调用。CNend 78 * @param check [IN] type #hi_bool,whether check and repair when filesystem mount. 79 CNcomment:是否自动检查并修复。CNend 80 * 81 * @retval None 82 * @par 依赖: 83 * @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 84 * @see hi_get_fs_check 85 */ 86 hi_void hi_set_fs_check(hi_bool check); 87 88 /** 89 * @ingroup iot_fs 90 * @brief Get filesystem check and repair.CNcomment:获取文件系统加载时是否自动检查并修复。CNend 91 * 92 * @par 描述: 93 * @li Get filesystem check and repair, default True. 94 CNcomment:获取文件系统加载时是否自动检查并修复,默认自动检查并修复。CNend 95 * 96 * @attention None 97 * @param None 98 * 99 * @retval #HI_TRUE auto check and repair when filesystem mount. 100 * @retval #HI_FALSE not check and repair when filesystem mount. 101 * @par 依赖: 102 * @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 103 * @see hi_set_fs_check 104 */ 105 hi_bool hi_get_fs_check(hi_void); 106 107 /** 108 * @ingroup iot_fs 109 * @brief Initialize virtual filesystem.CNcomment:初始化虚拟文件系统。CNend 110 * 111 * @par 描述: 112 * @li Initialize virtual file system configuration related parameters. 113 CNcomment:初始化虚拟文件系统配置相关参数。CNend 114 * @li Mount partition to virtual file system work buffer.CNcomment:挂载分区到虚拟文件系统工作缓冲区。CNend 115 * 116 * @attention None 117 * @param None 118 * 119 * @retval None 120 * @par 依赖: 121 * @li hi_fs.h:describes the file system APIs.CNcomment:文件用于描述文件系统相关接口。CNend 122 * @see hi_fs_init 123 */ 124 hi_void hi_fs_init(hi_void); 125 126 /** 127 * @ingroup iot_fs 128 * @brief Open or create a file.CNcomment:打开或创建文件。 CNend 129 * 130 * @par 描述: 131 * Open or create a file.CNcomment:打开或创建指定的文件。CNend 132 * 133 * @attention The path length is less than 31 bytes, excluding the terminator. 134 Maximum support for opening 32 files at the same time. 135 136 * @param path [IN] type #const hi_char*,file name CNcomment:要打开或创建的目标文件。CNend 137 * 138 * @param flags [IN] type #hi_u32,The flag combination is as follows: 139 * HI_FS_O_RDONLY:Open file as read-only.CNcomment:以只读的方式打开文件。CNend 140 * HI_FS_O_WRONLY:Open the file in write-only mode.CNcomment:以只写的方式打开文件。CNend 141 * HI_FS_O_RDWR:Open file in read-write mode.CNcomment:以读写的方式打开文件。CNend 142 * HI_FS_O_CREAT:If the file you want to open does not exist, the file will be created automatically. 143 CNcomment:如果要打开的文件不存在则自动建立该文件。CNend 144 * HI_FS_O_EXCL:If HI_FS_O_CREAT is also set, this command will check to see if the file exists. 145 If the file does not exist, create the file, otherwise it will cause a file error to open. 146 CNcomment:如果HI_FS_O_CREAT也被设置,此指令会去检查文件是否存在。 147 文件若不存在则建立该文件,否则将导致打开文件错误。CNend 148 * HI_FS_O_TRUNC:If the file exists and is opened in write mode, this flag will make the file length 149 clear to 0, and the data originally stored in the file will disappear. 150 CNcomment:若文件存在并且以可写的模式打开时,此标志会令文件长度清为0,而原来存于该文件的数据也会消失。CNend 151 * HI_FS_O_APPEND:When reading and writing a file, it will start moving from the end of the file, that is, 152 the written data will be added to the file in an additional way. 153 CNcomment:当读写文件时会从文件尾开始移动,也就是所写入的数据会以附加的方式加入到文件后面。CNend 154 * CNcomment:打开文件时,可以传入多个参数选项。CNend 155 * 156 * @retval #>0 Success. Return file descriptor 157 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 158 * @par 依赖: 159 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 160 * @see hi_open 161 */ 162 hi_s32 hi_open(const hi_char* path, hi_u32 flags); 163 164 /** 165 * @ingroup iot_fs 166 * @brief Close an open file.CNcomment:关闭已经打开的文件。 CNend 167 * 168 * @par 描述: 169 * Close an open file.CNcomment:关闭一个已经打开的文件。CNend 170 * 171 * @attention None 172 * @param fd [IN] type #hi_s32,file descriptor CNcomment:需要关闭的文件描述符。CNend 173 * 174 * 175 * @retval #0 Success. 176 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 177 * @par 依赖: 178 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 179 * @see hi_open 180 */ 181 hi_s32 hi_close(hi_s32 fd); 182 183 /** 184 * @ingroup iot_fs 185 * @brief Read file data of the specified size:从文件中,读取指定大小的文件数据。CNend 186 * 187 * @par 描述: 188 * Read file data of the specified size:从文件中,读取指定大小的文件数据。CNend 189 * 190 * @attention None 191 * @param fd [IN] type #hi_s32,file descriptor CNcomment:需要读文件的文件描述符。CNend 192 * @param buf [OUT] type #hi_char*,save read data buffer CNcomment:保存读数据缓冲区。CNend 193 * @param len [IN] type #hi_u32,The number of bytes requested to be read 194 CNcomment:需要读取数据的长度。CNend 195 * 196 * 197 * @retval #>=0 Success. Returns the number of bytes read, if it returns 0, it means that the end of the file 198 has been reached or there is no data to be read. 199 CNcomment:返回读取的字节数,如果返回0,表示已到达文件尾或无可读取的数据。CNend 200 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 201 * @par 依赖: 202 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 203 * @see hi_open 204 */ 205 hi_s32 hi_read(hi_s32 fd, hi_char* buf, hi_u32 len); 206 207 /** 208 * @ingroup iot_fs 209 * @brief Write file data of the specified size:向文件中,写入指定大小的文件数据。CNend 210 * 211 * @par 描述: 212 * Write file data of the specified size:向文件中,写入指定大小的文件数据。CNend 213 * 214 * @attention None 215 * @param fd [IN] type #hi_s32,file descriptor CNcomment:需要写文件的文件描述符。CNend 216 * @param buf [IN] type #hi_char*,Store data that needs to be written to a file. 217 CNcomment:存放需要写入文件的数据。CNend 218 * @param len [IN] type #hi_u32,The number of bytes requested to be write CNcomment:需要写入数据的长度。CNend 219 * 220 * 221 * @retval #>=0 Success. Returns the number of bytes actually written, if it returns 0, it means nothing to do. 222 reached or there is no data to be read. 223 CNcomment:返回实际写入的字节数,如果返回0,表示什么也没有做。CNend 224 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 225 * @par 依赖: 226 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 227 * @see hi_open 228 */ 229 hi_s32 hi_write(hi_s32 fd, const hi_char* buf, hi_u32 len); 230 231 /** 232 * @ingroup iot_fs 233 * @brief Delete a file.CNcomment:删除指定的文件。 CNend 234 * 235 * @par 描述: 236 * Delete a file.CNcomment:删除指定的文件。CNend 237 * 238 * @attention If the number of file descriptors currently open has reached the upper limit (32), 239 * then one of the file descriptors must be closed, otherwise the file will not be deleted. 240 * CNcomment:如果当前已经打开的文件描述符数已达上限(32个), 241 * 那么必须要关闭其中一个文件描述符,否则文件将不能被删除。CNend 242 * @attention The path length is less than 31 bytes, excluding the terminator. 243 * 244 * @param path [IN] type #const hi_char*,file name CNcomment:要删除的目标文件。CNend 245 * 246 * 247 * @retval #0 Success. 248 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 249 * @par 依赖: 250 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 251 * @see hi_open 252 */ 253 hi_s32 hi_unlink(const hi_char *path); 254 255 /** 256 * @ingroup iot_fs 257 * @brief Relocate read/write file offsets.CNcomment:重新定位文件的读/写偏移量。 CNend 258 * 259 * @par 描述: 260 * Relocate read/write file offsets.CNcomment:重新定位文件的读/写偏移量。 CNend 261 * 262 * @attention whence is one of the following: 263 SEEK_SET Point the read and write position to the file header and then increase the offset amount. 264 CNcomment:将读写位置指向文件头后再增加offset个位移量。CNend 265 SEEK_CUR Increase the offset by the current read and write position. 266 CNcomment:以目前的读写位置往后增加offset个位移量。 CNend 267 SEEK_END Point the read and write position to the end of the file and then increase the offset amount, 268 only support offset values can only be negative. 269 CNcomment:将读写位置指向文件尾后再增加offset个位移量,只支持offset的值为负数。CNend 270 When the whennce value is SEEK_CUR or SEEK_END, the parameter offet allows 271 the occurrence of a negative value. 272 CNcomment:当whence 值为SEEK_CUR 或SEEK_END时,参数offet允许负值的出现。CNend 273 274 * @param fd [IN] type #hi_s32,file descriptor CNcomment:需要重新定位读/写偏移量的文件描述符。CNend 275 * @param offs [IN] type #hi_s32,Move the number of displacements of the read/write position 276 * according to the parameter whence CNcomment:根据参数whence来移动读写位置的位移数。CNend 277 * @param whence [IN] type #hi_u32,Read / write file offset. CNcomment:读/写文件偏移。CNend 278 * 279 * @retval #>=0 Success. Returns the current read and write position, 280 * which is how many bytes from the beginning of the file 281 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 282 * @par 依赖: 283 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 284 * @see hi_open 285 */ 286 hi_s32 hi_lseek(hi_s32 fd, hi_s32 offs, hi_u32 whence); 287 288 /** 289 * @ingroup iot_fs 290 * @brief Relocate Enumerate all files in the directory, the file system does not support multi-level directories. 291 * CNcomment:枚举目录下的所有文件,文件系统不支持多级目录。 CNend 292 * 293 * @par 描述: 294 * Relocate Enumerate all files in the directory, the file system does not support multi-level directories. 295 * CNcomment:枚举目录下的所有文件,文件系统不支持多级目录。CNend 296 * 297 * @attention 298 * 299 * @param buf [OUT] type #hi_char**,Buf stores information about all files and is released by the user. 300 * CNcomment:buf中存放所有文件的信息,由用户释放。CNend 301 * 302 * @retval #>=0 Success. 303 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 304 * @par 依赖: 305 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 306 * @see hi_open 307 */ 308 hi_s32 hi_enum_file(hi_char** buf); 309 310 /** 311 * @ingroup iot_fs 312 * @brief Relocate Get file size. CNcomment:获取指定文件的大小。 CNend 313 * 314 * @par 描述: 315 * Relocate Get file size. CNcomment:获取指定文件的大小。CNend 316 * 317 * @attention 318 * 319 * @param file_name [IN] type #const hi_char*,file name CNcomment:文件名。CNend 320 * @param file_size [OUT] type #hi_u32*,file size. CNcomment:返回文件的大小。CNend 321 * 322 * @retval #>=0 Success. 323 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 324 * @par 依赖: 325 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 326 * @see hi_open 327 */ 328 hi_s32 hi_stat(const hi_char *file_name, hi_u32* file_size); 329 /** 330 * @ingroup iot_fs 331 * @brief Relocate Get file size. CNcomment:获取指定文件的大小。 CNend 332 * 333 * @par 描述: 334 * Relocate Get file size. CNcomment:获取指定文件的大小。CNend 335 * 336 * @attention 337 * 338 * @param fd [IN] type #hi_s32,file descriptor CNcomment:文件描述符。CNend 339 * @param file_size [OUT] type #hi_u32*,file size. CNcomment:返回文件的大小。CNend 340 * 341 * @retval #>=0 Success. 342 * @retval #-1 Failure. For details,Get error code by hi_get_fs_error. 343 * @par 依赖: 344 @li hi_fs.h:describes the filesystem APIs.CNcomment:文件用于描述文件系统相关接口。CNend 345 * @see hi_open 346 */ 347 hi_s32 hi_fstat(hi_s32 fd, hi_u32 *file_size); 348 #endif 349 350