1 /*
2  * Copyright (c) 2024 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 OHOS_FILEMANAGEMENT_FILE_API_C_COMMON_ERROR_CODE_H
17 #define OHOS_FILEMANAGEMENT_FILE_API_C_COMMON_ERROR_CODE_H
18 
19 #include <errno.h>
20 #include <stdio.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif /* End of #ifdef __cplusplus */
25 
26 enum ErrorCode {
27     /** @error Permission verification failed. */
28     PERMISSION_ERROR = 201,
29     /** @error Invalid input parameter, pointer is null. */
30     PARAMETER_ERROR = 401,
31     /** @error Device not supported. */
32     DEVICE_NOT_SUPPORTED = 801,
33     /** @error Operation not permitted. */
34     E_PERM = 13900001,
35     /** @error No such file or directory. */
36     E_NOENT = 13900002,
37     /** @error Failed to apply for memory. */
38     E_NOMEM = 13900011,
39     /** @error Unknown error. */
40     UNKNOWN_ERROR = 13900042
41 };
42 
43 typedef struct {
44     int errNo;
45     int errorCode;
46 } Map;
47 
48 static Map g_map[] = {
49     { UNKNOWN_ERROR, UNKNOWN_ERROR },
50     { -PERMISSION_ERROR, PERMISSION_ERROR },
51     { -PARAMETER_ERROR, PARAMETER_ERROR },
52     { -DEVICE_NOT_SUPPORTED, DEVICE_NOT_SUPPORTED },
53     { -EPERM, E_PERM },
54     { -ENOENT, E_NOENT },
55     { -ENOMEM, E_NOMEM },
56 };
57 
GetErrorCode(int errNum)58 static int GetErrorCode(int errNum)
59 {
60     for (size_t i = 1; i < sizeof(g_map) / sizeof(g_map[0]); i++) {
61         if (g_map[i].errNo == errNum) {
62             return g_map[i].errorCode;
63         }
64     }
65     return g_map[0].errorCode;
66 }
67 #ifdef __cplusplus
68 }
69 #endif /* End of #ifdef __cplusplus */
70 #endif /* OHOS_FILEMANAGEMENT_FILE_API_C_COMMON_ERROR_CODE_H */
71