1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CM_FILE_OPERATOR_H
17 #define CM_FILE_OPERATOR_H
18 
19 #include <sys/stat.h>
20 #include "cm_type.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define CM_MAX_FILE_NAME_LEN   512
27 #define CM_MAX_DIRENT_FILE_LEN 256
28 
29 struct CmFileDirentInfo {
30     char fileName[CM_MAX_DIRENT_FILE_LEN]; /* point to dirent->d_name */
31 };
32 
33 uint32_t CmFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len);
34 
35 int32_t CmFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len);
36 
37 int32_t CmUserBackupFileWrite(
38     const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len);
39 
40 int32_t CmFileRemove(const char *path, const char *fileName);
41 
42 uint32_t CmFileSize(const char *path, const char *fileName);
43 
44 int32_t CmIsFileExist(const char *path, const char *fileName);
45 
46 int32_t CmMakeDir(const char *path);
47 
48 /**
49  * @brief Creating a directory related to certificate backup (directory permissions can be specified)
50  *
51  * @param[in] path Directory absolute path
52  * @param[in] mode When not NULL, the permissions specified by *mode are used when creating directory
53  * @return int32_t Create result
54  * @retval CMR_OK Creating a directory succeeded
55  * @retval CMR_ERROR_ALREADY_EXISTS Directory already exists
56  * @retval CMR_ERROR_MAKE_DIR_FAIL Failed to create a directory
57  */
58 int32_t CmUserBakupMakeDir(const char *path, const mode_t *mode);
59 
60 void *CmOpenDir(const char *path);
61 
62 int32_t CmCloseDir(void *dirp);
63 
64 int32_t CmGetDirFile(void *dirp, struct CmFileDirentInfo *direntInfo);
65 
66 int32_t CmIsDirExist(const char *path);
67 
68 int32_t CmUserIdLayerGetFileCountAndNames(const char *path, struct CmBlob *fileNames,
69     const uint32_t arraySize, uint32_t *fileCount);
70 
71 int32_t CmUidLayerGetFileCountAndNames(const char *path, struct CmBlob *fileNames,
72     const uint32_t arraySize, uint32_t *fileCount);
73 
74 int32_t CmGetSubDir(void *dirp, struct CmFileDirentInfo *direntInfo);
75 
76 int32_t CmDirRemove(const char *path);
77 
78 #ifdef __cplusplus
79 }
80 #endif
81 
82 #endif /* CM_FILE_OPERATOR_H */
83 
84