1 /*
2 * Copyright (c) 2025 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 #include "access_ani.h"
17
18 #include "access_core.h"
19 #include "error_handler.h"
20 #include "filemgmt_libhilog.h"
21 #include "type_converter.h"
22
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27
28 static constexpr int EXIST_INDEX = 0;
29 static constexpr int WRITE_INDEX = 2;
30 static constexpr int READ_INDEX = 4;
31 static constexpr int READ_WRITE_INDEX = 6;
32 static constexpr int LOCAL_INDEX = 0;
33
ToAccessModeType(int32_t mode_index)34 AccessModeType ToAccessModeType(int32_t mode_index)
35 {
36 switch (mode_index) {
37 case EXIST_INDEX:
38 return AccessModeType::EXIST;
39 case WRITE_INDEX:
40 return AccessModeType::WRITE;
41 case READ_INDEX:
42 return AccessModeType::READ;
43 case READ_WRITE_INDEX:
44 return AccessModeType::READ_WRITE;
45 default:
46 return AccessModeType::ERROR;
47 }
48 }
49
OptToAccessModeType(const std::optional<int> & mode_index)50 std::optional<AccessModeType> OptToAccessModeType(const std::optional<int> &mode_index)
51 {
52 if (!mode_index.has_value()) {
53 return std::nullopt;
54 }
55 return ToAccessModeType(mode_index.value());
56 }
57
ToAccessFlagType(int32_t flag_index)58 AccessFlag ToAccessFlagType(int32_t flag_index)
59 {
60 switch (flag_index) {
61 case LOCAL_INDEX:
62 return AccessFlag::LOCAL_FLAG;
63 default:
64 return AccessFlag::DEFAULT_FLAG;
65 }
66 }
67
OptToAccessFlagType(const std::optional<int> & flag_index)68 std::optional<AccessFlag> OptToAccessFlagType(const std::optional<int> &flag_index)
69 {
70 if (!flag_index.has_value()) {
71 return std::nullopt;
72 }
73 return ToAccessFlagType(flag_index.value());
74 }
75
AccessSync3(ani_env * env,ani_class clazz,ani_string path,ani_enum_item mode,ani_enum_item flag)76 ani_boolean AccessAni::AccessSync3(
77 ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_enum_item mode, ani_enum_item flag)
78 {
79 ani_boolean ret = 0;
80 auto [succPath, pathStr] = TypeConverter::ToUTF8String(env, path);
81 if (!succPath) {
82 HILOGE("Invalid path");
83 ErrorHandler::Throw(env, EINVAL);
84 return ret;
85 }
86
87 auto [succMode, modeOp] = TypeConverter::EnumToInt32(env, mode);
88 if (!succMode) {
89 HILOGE("Invalid mode");
90 ErrorHandler::Throw(env, EINVAL);
91 return ret;
92 }
93 auto modeType = OptToAccessModeType(modeOp);
94
95 auto [succFlag, flagOpt] = TypeConverter::EnumToInt32(env, flag);
96 if (!succFlag) {
97 HILOGE("Invalid flag");
98 ErrorHandler::Throw(env, EINVAL);
99 return ret;
100 }
101 auto flagType = OptToAccessFlagType(flagOpt);
102
103 FsResult<bool> fsRet = FsResult<bool>::Error(UNKNOWN_ERR);
104 if (modeType != std::nullopt && flagType != std::nullopt) {
105 fsRet = AccessCore::DoAccess(pathStr, modeType.value(), flagType.value());
106 } else {
107 fsRet = AccessCore::DoAccess(pathStr, modeType);
108 }
109
110 if (!fsRet.IsSuccess()) {
111 HILOGE("DoAccess failed");
112 const auto &err = fsRet.GetError();
113 ErrorHandler::Throw(env, err);
114 return false;
115 }
116 return fsRet.GetData().value();
117 }
118
119 } // namespace ANI
120 } // namespace ModuleFileIO
121 } // namespace FileManagement
122 } // namespace OHOS