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 "open_ani.h"
17
18 #include "ani_helper.h"
19 #include "error_handler.h"
20 #include "file_wrapper.h"
21 #include "filemgmt_libhilog.h"
22 #include "open_core.h"
23 #include "type_converter.h"
24
25 namespace OHOS {
26 namespace FileManagement {
27 namespace ModuleFileIO {
28 namespace ANI {
29 using namespace OHOS::FileManagement::ModuleFileIO;
30
OpenSync(ani_env * env,ani_class clazz,ani_string path,ani_object mode)31 ani_object OpenAni::OpenSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_object mode)
32 {
33 #ifdef FILE_API_TRACE
34 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
35 #endif
36
37 auto [succPath, filePath] = TypeConverter::ToUTF8String(env, path);
38 if (!succPath) {
39 HILOGE("Invalid path");
40 ErrorHandler::Throw(env, EINVAL);
41 return nullptr;
42 }
43
44 auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode);
45 if (!succMode) {
46 HILOGE("Invalid mode");
47 ErrorHandler::Throw(env, EINVAL);
48 return nullptr;
49 }
50
51 FsResult<FsFile *> ret = OpenCore::DoOpen(filePath, modeOp);
52 if (!ret.IsSuccess()) {
53 HILOGE("Open failed");
54 const auto &err = ret.GetError();
55 ErrorHandler::Throw(env, err);
56 return nullptr;
57 }
58
59 const FsFile *file = ret.GetData().value();
60 auto result = FileWrapper::Wrap(env, move(file));
61 if (result == nullptr) {
62 delete file;
63 file = nullptr;
64 ErrorHandler::Throw(env, UNKNOWN_ERR);
65 return nullptr;
66 }
67 return result;
68 }
69 } // namespace ANI
70 } // namespace ModuleFileIO
71 } // namespace FileManagement
72 } // namespace OHOS