10e98b08fSopenharmony_ci/*
20e98b08fSopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd.
30e98b08fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40e98b08fSopenharmony_ci * you may not use this file except in compliance with the License.
50e98b08fSopenharmony_ci * You may obtain a copy of the License at
60e98b08fSopenharmony_ci *
70e98b08fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80e98b08fSopenharmony_ci *
90e98b08fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100e98b08fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110e98b08fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120e98b08fSopenharmony_ci * See the License for the specific language governing permissions and
130e98b08fSopenharmony_ci * limitations under the License.
140e98b08fSopenharmony_ci */
150e98b08fSopenharmony_ci
160e98b08fSopenharmony_ci/**
170e98b08fSopenharmony_ci * @addtogroup utils_file
180e98b08fSopenharmony_ci * @{
190e98b08fSopenharmony_ci *
200e98b08fSopenharmony_ci * @brief Performs operations on a file.
210e98b08fSopenharmony_ci *
220e98b08fSopenharmony_ci * This module allows you to performs file operations such as to open, close,
230e98b08fSopenharmony_ci * read, and write a file, and to obtain the file size.
240e98b08fSopenharmony_ci * The filing system varies according to the hardware platform.
250e98b08fSopenharmony_ci * The following limitations are imposed on a platform that
260e98b08fSopenharmony_ci * uses the Serial Peripheral Interface Flash Filing System (SPIFFS):
270e98b08fSopenharmony_ci * <ul>
280e98b08fSopenharmony_ci *    <li>Multi-level directories are not supported.</li>
290e98b08fSopenharmony_ci *    <li>A file name can have a maximum length of 32 bytes (including the end-of-text character in the string).</li>
300e98b08fSopenharmony_ci *    <li>A maximum of 32 files can be opened at the same time.</li>
310e98b08fSopenharmony_ci * </ul>
320e98b08fSopenharmony_ci *
330e98b08fSopenharmony_ci * @since 1.0
340e98b08fSopenharmony_ci * @version 1.0
350e98b08fSopenharmony_ci */
360e98b08fSopenharmony_ci
370e98b08fSopenharmony_ci/**
380e98b08fSopenharmony_ci * @file utils_file.h
390e98b08fSopenharmony_ci *
400e98b08fSopenharmony_ci * @brief Performs operations on a file, including to open, close, write, read, and delete a file.
410e98b08fSopenharmony_ci *
420e98b08fSopenharmony_ci * @since 1.0
430e98b08fSopenharmony_ci * @version 1.0
440e98b08fSopenharmony_ci */
450e98b08fSopenharmony_ci
460e98b08fSopenharmony_ci#ifndef FILE_SYSTEM_API_H
470e98b08fSopenharmony_ci#define FILE_SYSTEM_API_H
480e98b08fSopenharmony_ci
490e98b08fSopenharmony_ci#ifdef __cplusplus
500e98b08fSopenharmony_ci#if __cplusplus
510e98b08fSopenharmony_ciextern "C" {
520e98b08fSopenharmony_ci#endif
530e98b08fSopenharmony_ci#endif /* __cplusplus */
540e98b08fSopenharmony_ci
550e98b08fSopenharmony_ci/**
560e98b08fSopenharmony_ci * @brief Defines the offset position used by {@link UtilsFileSeek}
570e98b08fSopenharmony_ci * in a file to start offsetting from the file header.
580e98b08fSopenharmony_ci *
590e98b08fSopenharmony_ci */
600e98b08fSopenharmony_ci#ifndef SEEK_SET_FS
610e98b08fSopenharmony_ci#define SEEK_SET_FS  0
620e98b08fSopenharmony_ci#endif
630e98b08fSopenharmony_ci
640e98b08fSopenharmony_ci/**
650e98b08fSopenharmony_ci * @brief Defines the offset position used by {@link UtilsFileSeek}
660e98b08fSopenharmony_ci * in a file to start offsetting from the current read and write position.
670e98b08fSopenharmony_ci *
680e98b08fSopenharmony_ci */
690e98b08fSopenharmony_ci#ifndef SEEK_CUR_FS
700e98b08fSopenharmony_ci#define SEEK_CUR_FS  1
710e98b08fSopenharmony_ci#endif
720e98b08fSopenharmony_ci
730e98b08fSopenharmony_ci/**
740e98b08fSopenharmony_ci * @brief Defines the offset position used by {@link UtilsFileSeek}
750e98b08fSopenharmony_ci * in a file to start offsetting from the end of the file.
760e98b08fSopenharmony_ci *
770e98b08fSopenharmony_ci */
780e98b08fSopenharmony_ci#ifndef SEEK_END_FS
790e98b08fSopenharmony_ci#define SEEK_END_FS  2
800e98b08fSopenharmony_ci#endif
810e98b08fSopenharmony_ci
820e98b08fSopenharmony_ci/**
830e98b08fSopenharmony_ci * @brief Defines a flag used by{@link UtilsFileOpen} to open a file in read-only mode.
840e98b08fSopenharmony_ci *
850e98b08fSopenharmony_ci */
860e98b08fSopenharmony_ci#ifndef O_RDONLY_FS
870e98b08fSopenharmony_ci#define O_RDONLY_FS  00
880e98b08fSopenharmony_ci#endif
890e98b08fSopenharmony_ci
900e98b08fSopenharmony_ci/**
910e98b08fSopenharmony_ci * @brief Defines a flag used by {@link UtilsFileOpen} to open a file in write-only mode.
920e98b08fSopenharmony_ci *
930e98b08fSopenharmony_ci */
940e98b08fSopenharmony_ci#ifndef O_WRONLY_FS
950e98b08fSopenharmony_ci#define O_WRONLY_FS  01
960e98b08fSopenharmony_ci#endif
970e98b08fSopenharmony_ci
980e98b08fSopenharmony_ci/**
990e98b08fSopenharmony_ci * @brief Defines a flag used by {@link UtilsFileOpen} to open a file in read-and-write mode.
1000e98b08fSopenharmony_ci *
1010e98b08fSopenharmony_ci */
1020e98b08fSopenharmony_ci#ifndef O_RDWR_FS
1030e98b08fSopenharmony_ci#define O_RDWR_FS    02
1040e98b08fSopenharmony_ci#endif
1050e98b08fSopenharmony_ci
1060e98b08fSopenharmony_ci/**
1070e98b08fSopenharmony_ci * @brief Defines a flag used by {@link UtilsFileOpen} to create a file when the file to open does not exist.
1080e98b08fSopenharmony_ci *
1090e98b08fSopenharmony_ci */
1100e98b08fSopenharmony_ci#ifndef O_CREAT_FS
1110e98b08fSopenharmony_ci#define O_CREAT_FS   0100
1120e98b08fSopenharmony_ci#endif
1130e98b08fSopenharmony_ci
1140e98b08fSopenharmony_ci/**
1150e98b08fSopenharmony_ci * @brief Defines a flag used by {@link UtilsFileOpen} to check whether the file to open exists
1160e98b08fSopenharmony_ci * when {@link O_CREAT_FS} is already set.
1170e98b08fSopenharmony_ci *
1180e98b08fSopenharmony_ci * If the file does not exist, a new file will be created. If the file exists, the file cannot be opened.
1190e98b08fSopenharmony_ci *
1200e98b08fSopenharmony_ci */
1210e98b08fSopenharmony_ci#ifndef O_EXCL_FS
1220e98b08fSopenharmony_ci#define O_EXCL_FS    0200
1230e98b08fSopenharmony_ci#endif
1240e98b08fSopenharmony_ci
1250e98b08fSopenharmony_ci/**
1260e98b08fSopenharmony_ci * @brief Defines a flag used by {@link UtilsFileOpen} to clear the file content
1270e98b08fSopenharmony_ci * if the file exists and can be opened in write mode.
1280e98b08fSopenharmony_ci *
1290e98b08fSopenharmony_ci */
1300e98b08fSopenharmony_ci#ifndef O_TRUNC_FS
1310e98b08fSopenharmony_ci#define O_TRUNC_FS   01000
1320e98b08fSopenharmony_ci#endif
1330e98b08fSopenharmony_ci
1340e98b08fSopenharmony_ci/**
1350e98b08fSopenharmony_ci * @brief Defines a flag used by {@link UtilsFileOpen} to start reading or writing from the end of a file.
1360e98b08fSopenharmony_ci *
1370e98b08fSopenharmony_ci */
1380e98b08fSopenharmony_ci#ifndef O_APPEND_FS
1390e98b08fSopenharmony_ci#define O_APPEND_FS  02000
1400e98b08fSopenharmony_ci#endif
1410e98b08fSopenharmony_ci
1420e98b08fSopenharmony_ci/**
1430e98b08fSopenharmony_ci * @brief Opens or creates a file.
1440e98b08fSopenharmony_ci *
1450e98b08fSopenharmony_ci * @param path Indicates the file to open or create.
1460e98b08fSopenharmony_ci * @param oflag Indicates the mode of opening a file. The following modes are supported.
1470e98b08fSopenharmony_ci * oflag | Description
1480e98b08fSopenharmony_ci * ------------|------------------------------------------------
1490e98b08fSopenharmony_ci * O_RDONLY_FS | For details, see {@link O_RDONLY_FS}.
1500e98b08fSopenharmony_ci * O_WRONLY_FS | For details, see {@link O_WRONLY_FS}.
1510e98b08fSopenharmony_ci * O_RDWR_FS | For details, see {@link O_RDWR_FS}.
1520e98b08fSopenharmony_ci * O_CREAT_FS | For details, see {@link O_CREAT_FS}.
1530e98b08fSopenharmony_ci * O_EXCL_FS | For details, see {@link O_EXCL_FS}.
1540e98b08fSopenharmony_ci * O_TRUNC_FS | For details, see {@link O_TRUNC_FS}.
1550e98b08fSopenharmony_ci * O_APPEND_FS | For details, see {@link O_APPEND_FS}.
1560e98b08fSopenharmony_ci * These modes can be used together, with each of them identified by "or".
1570e98b08fSopenharmony_ci * @param mode Used for function compatibility. This parameter does not take effect in any scenario.
1580e98b08fSopenharmony_ci * @return Returns the file descriptor if the file is opened or created; returns <b>-1</b> otherwise.
1590e98b08fSopenharmony_ci * @since 1.0
1600e98b08fSopenharmony_ci * @version 1.0
1610e98b08fSopenharmony_ci */
1620e98b08fSopenharmony_ciint UtilsFileOpen(const char* path, int oflag, int mode);
1630e98b08fSopenharmony_ci
1640e98b08fSopenharmony_ci/**
1650e98b08fSopenharmony_ci * @brief Closes a file with the specified file descriptor.
1660e98b08fSopenharmony_ci *
1670e98b08fSopenharmony_ci * @param fd Indicates the file descriptor of the file to close.
1680e98b08fSopenharmony_ci * @return Returns <b>0</b> if the file is closed; returns <b>-1</b> otherwise.
1690e98b08fSopenharmony_ci * @since 1.0
1700e98b08fSopenharmony_ci * @version 1.0
1710e98b08fSopenharmony_ci */
1720e98b08fSopenharmony_ciint UtilsFileClose(int fd);
1730e98b08fSopenharmony_ci
1740e98b08fSopenharmony_ci/**
1750e98b08fSopenharmony_ci * @brief Reads a specified length of data from a file with the specified file descriptor
1760e98b08fSopenharmony_ci * and writes the data into the buffer.
1770e98b08fSopenharmony_ci *
1780e98b08fSopenharmony_ci * @param fd Indicates the file descriptor of the file to read.
1790e98b08fSopenharmony_ci * @param buf Indicates the buffer that stores the read data. This is an output parameter.
1800e98b08fSopenharmony_ci * @param len Indicates the length of the data to read.
1810e98b08fSopenharmony_ci * @return Returns the number of bytes of the data if the data is read; returns <b>-1</b> otherwise.
1820e98b08fSopenharmony_ci * @since 1.0
1830e98b08fSopenharmony_ci * @version 1.0
1840e98b08fSopenharmony_ci */
1850e98b08fSopenharmony_ciint UtilsFileRead(int fd, char* buf, unsigned int len);
1860e98b08fSopenharmony_ci
1870e98b08fSopenharmony_ci/**
1880e98b08fSopenharmony_ci * @brief Writes a specified length of data into a file with the specified file descriptor.
1890e98b08fSopenharmony_ci *
1900e98b08fSopenharmony_ci * @param fd Indicates the file descriptor of the file where to write the data.
1910e98b08fSopenharmony_ci * @param buf Indicates the data to write.
1920e98b08fSopenharmony_ci * @param len Indicates the length of the data to write.
1930e98b08fSopenharmony_ci * @return Returns the number of bytes of the data if the data is written; returns <b>-1</b> otherwise.
1940e98b08fSopenharmony_ci * @since 1.0
1950e98b08fSopenharmony_ci * @version 1.0
1960e98b08fSopenharmony_ci */
1970e98b08fSopenharmony_ciint UtilsFileWrite(int fd, const char* buf, unsigned int len);
1980e98b08fSopenharmony_ci
1990e98b08fSopenharmony_ci/**
2000e98b08fSopenharmony_ci * @brief Deletes a specified file.
2010e98b08fSopenharmony_ci *
2020e98b08fSopenharmony_ci * @param path Indicates the file to delete.
2030e98b08fSopenharmony_ci * @attention If the number of opened files reaches the upper limit (32), close any of them first.
2040e98b08fSopenharmony_ci * Otherwise, the file cannot be deleted.
2050e98b08fSopenharmony_ci * @return Returns <b>0</b> if the file is deleted; returns <b>-1</b> otherwise.
2060e98b08fSopenharmony_ci * @since 1.0
2070e98b08fSopenharmony_ci * @version 1.0
2080e98b08fSopenharmony_ci */
2090e98b08fSopenharmony_ciint UtilsFileDelete(const char* path);
2100e98b08fSopenharmony_ci
2110e98b08fSopenharmony_ci/**
2120e98b08fSopenharmony_ci * @brief Obtains the file size.
2130e98b08fSopenharmony_ci *
2140e98b08fSopenharmony_ci * @param path Indicates the file name.
2150e98b08fSopenharmony_ci * @param fileSize Indicates the file size. This is an output parameter.
2160e98b08fSopenharmony_ci * @return Returns <b>0</b> if the file size is obtained; returns <b>-1</b> otherwise.
2170e98b08fSopenharmony_ci * @since 1.0
2180e98b08fSopenharmony_ci * @version 1.0
2190e98b08fSopenharmony_ci */
2200e98b08fSopenharmony_ciint UtilsFileStat(const char* path, unsigned int* fileSize);
2210e98b08fSopenharmony_ci
2220e98b08fSopenharmony_ci/**
2230e98b08fSopenharmony_ci * @brief Adjusts the read and write position offset in a file.
2240e98b08fSopenharmony_ci *
2250e98b08fSopenharmony_ci * @param fd Indicates the file descriptor of the file where the read and write position offset needs adjustment.
2260e98b08fSopenharmony_ci * @param offset Indicates the offset of the read and write position based on the <b>whence</b> parameter.
2270e98b08fSopenharmony_ci * The value can be negative if the value of <b>whence</b> is <b>SEEK_CUR_FS</b> or <b>SEEK_END_FS</b>.
2280e98b08fSopenharmony_ci * @param whence Indicates the start position of the offset. The following start positions are supported.
2290e98b08fSopenharmony_ci * whence | Description
2300e98b08fSopenharmony_ci * ------------|------------------------------------------------
2310e98b08fSopenharmony_ci * SEEK_SET_FS | Adjusts the read and write position to the file header.
2320e98b08fSopenharmony_ci *      ^      | Then adds the offset after the read and write position.
2330e98b08fSopenharmony_ci * SEEK_CUR_FS | Adds the offset after the current read and write position.
2340e98b08fSopenharmony_ci * SEEK_END_FS | Adjusts the read and write position to the end of the file.
2350e98b08fSopenharmony_ci *      ^      | Then adds the offset after the read and write position.
2360e98b08fSopenharmony_ci *
2370e98b08fSopenharmony_ci * @return Returns the current read and write position if the operation is successful; returns <b>-1</b> otherwise.
2380e98b08fSopenharmony_ci * @since 1.0
2390e98b08fSopenharmony_ci * @version 1.0
2400e98b08fSopenharmony_ci */
2410e98b08fSopenharmony_ciint UtilsFileSeek(int fd, int offset, unsigned int whence);
2420e98b08fSopenharmony_ci
2430e98b08fSopenharmony_ci/**
2440e98b08fSopenharmony_ci * @brief Copies the source file to a target file.
2450e98b08fSopenharmony_ci *
2460e98b08fSopenharmony_ci * @param src Indicates the source file to copy.
2470e98b08fSopenharmony_ci * @param dest Indicates the target file.
2480e98b08fSopenharmony_ci * @attention If the number of opened files reaches the upper limit (32), close any two files first.
2490e98b08fSopenharmony_ci * Otherwise, the file cannot be copied.
2500e98b08fSopenharmony_ci * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
2510e98b08fSopenharmony_ci * @since 1.0
2520e98b08fSopenharmony_ci * @version 1.0
2530e98b08fSopenharmony_ci */
2540e98b08fSopenharmony_ciint UtilsFileCopy(const char* src, const char* dest);
2550e98b08fSopenharmony_ci
2560e98b08fSopenharmony_ci/**
2570e98b08fSopenharmony_ci * @brief Moves the source file into a target file.
2580e98b08fSopenharmony_ci *
2590e98b08fSopenharmony_ci * @param src Indicates the source file.
2600e98b08fSopenharmony_ci * @param dest Indicates the target file.
2610e98b08fSopenharmony_ci * @attention If the number of opened files reaches the upper limit (32), close any two files first.
2620e98b08fSopenharmony_ci * Otherwise, the file cannot be moved.
2630e98b08fSopenharmony_ci * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
2640e98b08fSopenharmony_ci * @since 1.0
2650e98b08fSopenharmony_ci * @version 1.0
2660e98b08fSopenharmony_ci */
2670e98b08fSopenharmony_ciint UtilsFileMove(const char* src, const char* dest);
2680e98b08fSopenharmony_ci
2690e98b08fSopenharmony_ci#ifdef __cplusplus
2700e98b08fSopenharmony_ci#if __cplusplus
2710e98b08fSopenharmony_ci}
2720e98b08fSopenharmony_ci#endif
2730e98b08fSopenharmony_ci#endif /* __cplusplus */
2740e98b08fSopenharmony_ci
2750e98b08fSopenharmony_ci#endif  // FILE_SYSTEM_API_H
2760e98b08fSopenharmony_ci/** @} */