• 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 "copy_ani.h"
17 
18 #include "ani_helper.h"
19 #include "ani_signature.h"
20 #include "copy_core.h"
21 #include "error_handler.h"
22 #include "file_utils.h"
23 #include "filemgmt_libhilog.h"
24 #include "fs_task_signal.h"
25 #include "progress_listener_ani.h"
26 #include "task_signal_listener_ani.h"
27 #include "task_signal_wrapper.h"
28 #include "type_converter.h"
29 
30 namespace OHOS {
31 namespace FileManagement {
32 namespace ModuleFileIO {
33 namespace ANI {
34 using namespace std;
35 using namespace OHOS::FileManagement::ModuleFileIO;
36 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
37 
ParseListenerFromOptionArg(ani_env * env,const ani_object & options,CopyOptions & opts)38 static bool ParseListenerFromOptionArg(ani_env *env, const ani_object &options, CopyOptions &opts)
39 {
40     ani_ref prog;
41     if (ANI_OK != env->Object_GetPropertyByName_Ref(options, "progressListener", &prog)) {
42         HILOGE("Illegal options.progressListener type");
43         return false;
44     }
45 
46     ani_boolean isUndefined = true;
47     env->Reference_IsUndefined(prog, &isUndefined);
48     if (isUndefined) {
49         return true;
50     }
51 
52     ani_ref cbRef;
53     if (ANI_OK != env->GlobalReference_Create(prog, &cbRef)) {
54         HILOGE("Failed to create reference");
55         return false;
56     }
57 
58     ani_vm *vm = nullptr;
59     env->GetVM(&vm);
60     auto listener = CreateSharedPtr<ProgressListenerAni>(vm, cbRef);
61     if (listener == nullptr) {
62         HILOGE("Failed to request heap memory.");
63         return false;
64     }
65 
66     opts.progressListener = move(listener);
67     return true;
68 }
69 
ParseCopySignalFromOptionArg(ani_env * env,const ani_object & options,CopyOptions & opts)70 static bool ParseCopySignalFromOptionArg(ani_env *env, const ani_object &options, CopyOptions &opts)
71 {
72     ani_ref prog;
73     if (ANI_OK != env->Object_GetPropertyByName_Ref(options, "copySignal", &prog)) {
74         HILOGE("Illegal options.CopySignal type");
75         return false;
76     }
77 
78     ani_boolean isUndefined = true;
79     env->Reference_IsUndefined(prog, &isUndefined);
80     if (isUndefined) {
81         return true;
82     }
83 
84     FsTaskSignal *copySignal = TaskSignalWrapper::Unwrap(env, static_cast<ani_object>(prog));
85     if (copySignal != nullptr) {
86         opts.copySignal = copySignal;
87     }
88 
89     return true;
90 }
91 
ParseOptions(ani_env * env,ani_object & options)92 static tuple<bool, optional<CopyOptions>> ParseOptions(ani_env *env, ani_object &options)
93 {
94     ani_boolean isUndefined;
95     env->Reference_IsUndefined(options, &isUndefined);
96     if (isUndefined) {
97         return { true, nullopt };
98     }
99 
100     CopyOptions opts;
101     auto succ = ParseListenerFromOptionArg(env, options, opts);
102     if (!succ) {
103         return { false, nullopt };
104     }
105 
106     succ = ParseCopySignalFromOptionArg(env, options, opts);
107     if (!succ) {
108         return { false, nullopt };
109     }
110 
111     return { true, make_optional(move(opts)) };
112 }
113 
CopySync(ani_env * env,ani_class clazz,ani_string srcUri,ani_string destUri,ani_object options)114 void CopyAni::CopySync(
115     ani_env *env, [[maybe_unused]] ani_class clazz, ani_string srcUri, ani_string destUri, ani_object options)
116 {
117     auto [succSrc, src] = TypeConverter::ToUTF8String(env, srcUri);
118     auto [succDest, dest] = TypeConverter::ToUTF8String(env, destUri);
119     auto [succOpts, opts] = ParseOptions(env, options);
120 
121     if (!succSrc) {
122         HILOGE("The first argument requires uri");
123         ErrorHandler::Throw(env, E_PARAMS);
124         return;
125     }
126     if (!succDest) {
127         HILOGE("The second argument requires uri");
128         ErrorHandler::Throw(env, E_PARAMS);
129         return;
130     }
131     if (!succOpts) {
132         HILOGE("The third argument requires listener function");
133         ErrorHandler::Throw(env, E_PARAMS);
134         return;
135     }
136 
137     auto ret = CopyCore::DoCopy(src, dest, opts);
138     if (!ret.IsSuccess()) {
139         HILOGE("DoCopy failed!");
140         const FsError &err = ret.GetError();
141         ErrorHandler::Throw(env, err);
142         return;
143     }
144 }
145 } // namespace ANI
146 } // namespace ModuleFileIO
147 } // namespace FileManagement
148 } // namespace OHOS