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_file_ani.h"
17
18 #include "copy_file_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 using namespace std;
28 using namespace OHOS::FileManagement::ModuleFileIO;
29
CopyFileSync(ani_env * env,ani_class clazz,ani_object src,ani_object dest,ani_object mode)30 void CopyFileAni::CopyFileSync(
31 ani_env *env, [[maybe_unused]] ani_class clazz, ani_object src, ani_object dest, ani_object mode)
32 {
33 auto [succSrc, srcFile] = TypeConverter::ToFileInfo(env, src);
34 auto [succDest, destFile] = TypeConverter::ToFileInfo(env, dest);
35 if (!succSrc || !succDest) {
36 HILOGE("The first/second argument requires filepath/fd");
37 ErrorHandler::Throw(env, EINVAL);
38 return;
39 }
40
41 auto [succMode, optMode] = TypeConverter::ToOptionalInt32(env, mode);
42 if (!succMode) {
43 HILOGE("Failed to convert mode to int32");
44 ErrorHandler::Throw(env, EINVAL);
45 return;
46 }
47
48 auto ret = CopyFileCore::DoCopyFile(srcFile, destFile, optMode);
49 if (!ret.IsSuccess()) {
50 HILOGE("DoCopyFile failed!");
51 const FsError &err = ret.GetError();
52 ErrorHandler::Throw(env, err);
53 return;
54 }
55 }
56 } // namespace ANI
57 } // namespace ModuleFileIO
58 } // namespace FileManagement
59 } // namespace OHOS