• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "create_randomaccessfile_ani.h"
17 
18 #include "ani_helper.h"
19 #include "ani_signature.h"
20 #include "create_randomaccessfile_core.h"
21 #include "error_handler.h"
22 #include "filemgmt_libhilog.h"
23 #include "type_converter.h"
24 
25 namespace OHOS {
26 namespace FileManagement {
27 namespace ModuleFileIO {
28 namespace ANI {
29 using namespace std;
30 using namespace OHOS::FileManagement::ModuleFileIO;
31 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
32 
Wrap(ani_env * env,const FsRandomAccessFile * rafFile)33 static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile)
34 {
35     if (rafFile == nullptr) {
36         HILOGE("FsRandomAccessFile pointer is null!");
37         return nullptr;
38     }
39 
40     auto classDesc = FS::RandomAccessFileInner::classDesc.c_str();
41     ani_class cls;
42     if (ANI_OK != env->FindClass(classDesc, &cls)) {
43         HILOGE("Cannot find class %s", classDesc);
44         return nullptr;
45     }
46 
47     auto ctorDesc = FS::RandomAccessFileInner::ctorDesc.c_str();
48     auto ctorSig = FS::RandomAccessFileInner::ctorSig.c_str();
49     ani_method ctor;
50     if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) {
51         HILOGE("Cannot find constructor method for class %s", classDesc);
52         return nullptr;
53     }
54 
55     ani_long ptr = static_cast<ani_long>(reinterpret_cast<std::uintptr_t>(rafFile));
56     ani_object obj;
57     if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) {
58         HILOGE("New %s obj Failed!", classDesc);
59         return nullptr;
60     }
61 
62     const auto &fdRet = rafFile->GetFD();
63     if (!fdRet.IsSuccess()) {
64         HILOGE("GetFD Failed!");
65         return nullptr;
66     }
67 
68     const auto &fd = fdRet.GetData().value();
69     if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", static_cast<double>(fd))) {
70         HILOGE("Set fd field value failed!");
71         return nullptr;
72     }
73 
74     const auto &fpRet = rafFile->GetFPointer();
75     if (!fpRet.IsSuccess()) {
76         HILOGE("GetFPointer Failed!");
77         return nullptr;
78     }
79 
80     const auto &fp = fpRet.GetData().value();
81     if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "filePointer", static_cast<double>(fp))) {
82         HILOGE("Set fp field value failed!");
83         return nullptr;
84     }
85     return obj;
86 }
87 
JudgeFile(ani_env * env,ani_object obj)88 static tuple<bool, bool> JudgeFile(ani_env *env, ani_object obj)
89 {
90     auto stringTypeDesc = BuiltInTypes::String::classDesc.c_str();
91     ani_class stringClass;
92     env->FindClass(stringTypeDesc, &stringClass);
93     ani_boolean isString = false;
94     env->Object_InstanceOf(obj, stringClass, &isString);
95     if (isString) {
96         return { true, true };
97     }
98 
99     auto fileClassDesc = FS::FileInner::classDesc.c_str();
100     ani_class fileClass;
101     env->FindClass(fileClassDesc, &fileClass);
102     ani_boolean isFile = false;
103     env->Object_InstanceOf(obj, fileClass, &isFile);
104     if (isFile) {
105         return { true, false };
106     }
107     HILOGE("Invalid file type");
108     return { false, false };
109 }
110 
ToRafOptions(ani_env * env,ani_object obj)111 static tuple<bool, optional<RandomAccessFileOptions>> ToRafOptions(ani_env *env, ani_object obj)
112 {
113     RandomAccessFileOptions options;
114     ani_boolean isUndefined;
115     env->Reference_IsUndefined(obj, &isUndefined);
116     if (isUndefined) {
117         return { true, nullopt };
118     }
119 
120     auto [succStart, start] = AniHelper::ParseInt64Option(env, obj, "start");
121     if (!succStart) {
122         HILOGE("Illegal option.start parameter");
123         return { false, nullopt };
124     }
125     options.start = start;
126 
127     auto [succEnd, end] = AniHelper::ParseInt64Option(env, obj, "end");
128     if (!succEnd) {
129         HILOGE("Illegal option.end parameter");
130         return { false, nullopt };
131     }
132     options.end = end;
133 
134     return { true, make_optional<RandomAccessFileOptions>(move(options)) };
135 }
136 
CreateRandomAccessFileByString(ani_env * env,ani_object file,ani_object mode,optional<RandomAccessFileOptions> op)137 static ani_object CreateRandomAccessFileByString(
138     ani_env *env, ani_object file, ani_object mode, optional<RandomAccessFileOptions> op)
139 {
140     auto [succPath, path] = TypeConverter::ToUTF8String(env, static_cast<ani_string>(file));
141     if (!succPath) {
142         HILOGE("Parse file path failed");
143         return nullptr;
144     }
145     auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode);
146     if (!succMode) {
147         HILOGE("Invalid mode");
148         ErrorHandler::Throw(env, EINVAL);
149         return nullptr;
150     }
151     FsResult<FsRandomAccessFile *> ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, modeOp, op);
152     if (!ret.IsSuccess()) {
153         HILOGE("CreateRandomAccessFile failed");
154         const auto &err = ret.GetError();
155         ErrorHandler::Throw(env, err);
156         return nullptr;
157     }
158     const FsRandomAccessFile *refFile = ret.GetData().value();
159     auto result = Wrap(env, move(refFile));
160     if (result == nullptr) {
161         delete refFile;
162         refFile = nullptr;
163         ErrorHandler::Throw(env, UNKNOWN_ERR);
164         return nullptr;
165     }
166     return result;
167 }
168 
CreateRandomAccessFileSync(ani_env * env,ani_class clazz,ani_object file,ani_object mode,ani_object options)169 ani_object CreateRandomAccessFileAni::CreateRandomAccessFileSync(
170     ani_env *env, [[maybe_unused]] ani_class clazz, ani_object file, ani_object mode, ani_object options)
171 {
172     auto [succOp, op] = ToRafOptions(env, options);
173     if (!succOp) {
174         HILOGE("Failed to resolve options!");
175         ErrorHandler::Throw(env, EINVAL);
176         return nullptr;
177     }
178 
179     auto [succ, isPath] = JudgeFile(env, file);
180     if (!succ) {
181         HILOGE("Judge file argument failed");
182         ErrorHandler::Throw(env, EINVAL);
183         return nullptr;
184     }
185 
186     if (isPath) {
187         return CreateRandomAccessFileByString(env, file, mode, op);
188     } else {
189         ani_double fdOp;
190         if (ANI_OK != env->Object_GetPropertyByName_Double(file, "fd", &fdOp)) {
191             HILOGE("Get fd in class file failed");
192             ErrorHandler::Throw(env, EINVAL);
193             return nullptr;
194         }
195         int32_t fd = static_cast<int32_t>(fdOp);
196         FsResult<FsRandomAccessFile *> ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, op);
197         if (!ret.IsSuccess()) {
198             HILOGE("CreateRandomAccessFile failed");
199             const auto &err = ret.GetError();
200             ErrorHandler::Throw(env, err);
201             return nullptr;
202         }
203         const FsRandomAccessFile *refFile = ret.GetData().value();
204         auto result = Wrap(env, move(refFile));
205         if (result == nullptr) {
206             delete refFile;
207             refFile = nullptr;
208             ErrorHandler::Throw(env, UNKNOWN_ERR);
209             return nullptr;
210         }
211         return result;
212     }
213 }
214 
215 } // namespace ANI
216 } // namespace ModuleFileIO
217 } // namespace FileManagement
218 } // namespace OHOS
219