• 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 "file_wrapper.h"
17 
18 #include "ani_signature.h"
19 #include "error_handler.h"
20 #include "filemgmt_libhilog.h"
21 #include "fs_file.h"
22 #include "type_converter.h"
23 
24 namespace OHOS {
25 namespace FileManagement {
26 namespace ModuleFileIO {
27 namespace ANI {
28 using namespace std;
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
31 
Unwrap(ani_env * env,ani_object object)32 FsFile *FileWrapper::Unwrap(ani_env *env, ani_object object)
33 {
34     ani_long nativePtr;
35     auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr);
36     if (ret != ANI_OK) {
37         HILOGE("Unwrap fsFile err: %{private}d", ret);
38         return nullptr;
39     }
40     uintptr_t ptrValue = static_cast<uintptr_t>(nativePtr);
41     FsFile *file = reinterpret_cast<FsFile *>(ptrValue);
42     return file;
43 }
44 
SetFileProperties(ani_env * env,ani_class cls,ani_object obj,const FsFile * file)45 static bool SetFileProperties(ani_env *env, ani_class cls, ani_object obj, const FsFile *file)
46 {
47     const auto &fdRet = file->GetFD();
48     if (!fdRet.IsSuccess()) {
49         HILOGE("GetFD Failed!");
50         return false;
51     }
52 
53     const auto &fd = fdRet.GetData().value();
54     if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", static_cast<double>(fd))) {
55         HILOGE("Set fd field value failed!");
56         return false;
57     }
58 
59     const auto &pathRet = file->GetPath();
60     if (!pathRet.IsSuccess()) {
61         HILOGE("GetPath Failed!");
62         return false;
63     }
64 
65     const auto &path = pathRet.GetData().value();
66     if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "path", path)) {
67         HILOGE("Set path field value failed!");
68         return false;
69     }
70 
71     const auto &nameRet = file->GetName();
72     if (!pathRet.IsSuccess()) {
73         HILOGE("GetPath Failed!");
74         return false;
75     }
76 
77     const auto &name = nameRet.GetData().value();
78     if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "name", name)) {
79         HILOGE("Set name field value failed!");
80         return false;
81     }
82     return true;
83 }
84 
Wrap(ani_env * env,const FsFile * file)85 ani_object FileWrapper::Wrap(ani_env *env, const FsFile *file)
86 {
87     if (file == nullptr) {
88         HILOGE("FsFile pointer is null!");
89         return nullptr;
90     }
91 
92     auto classDesc = FS::FileInner::classDesc.c_str();
93     ani_class cls;
94     if (ANI_OK != env->FindClass(classDesc, &cls)) {
95         HILOGE("Cannot find class %s", classDesc);
96         return nullptr;
97     }
98 
99     auto ctorDesc = FS::FileInner::ctorDesc.c_str();
100     auto ctorSig = FS::FileInner::ctorSig.c_str();
101     ani_method ctor;
102     if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) {
103         HILOGE("Cannot find constructor method for class %s", classDesc);
104         return nullptr;
105     }
106 
107     ani_long ptr = static_cast<ani_long>(reinterpret_cast<std::uintptr_t>(file));
108     ani_object obj;
109     if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) {
110         HILOGE("New %s obj Failed!", classDesc);
111         return nullptr;
112     }
113 
114     if (!SetFileProperties(env, cls, obj, file)) {
115         return nullptr;
116     }
117 
118     return obj;
119 }
120 
121 } // namespace ANI
122 } // namespace ModuleFileIO
123 } // namespace FileManagement
124 } // namespace OHOS