• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "move.h"
17 
18 #ifdef __MUSL__
19 #include <filesystem>
20 #else
21 #include <securec.h>
22 #include <sys/stat.h>
23 #endif
24 
25 #include <tuple>
26 #include <unistd.h>
27 #include "uv.h"
28 
29 #include "common_func.h"
30 #include "filemgmt_libhilog.h"
31 
32 namespace OHOS {
33 namespace FileManagement {
34 namespace ModuleFileIO {
35 using namespace std;
36 using namespace OHOS::FileManagement::LibN;
37 
38 #ifdef __MUSL__
CheckDir(const string & path)39 static bool CheckDir(const string &path)
40 {
41     if (!filesystem::is_directory(filesystem::status(path))) {
42         return false;
43     }
44     return true;
45 }
46 #else
CheckDir(const string & path)47 static bool CheckDir(const string &path)
48 {
49     struct stat fileInformation;
50     if (EOK == stat(path.c_str(), &fileInformation)) {
51         if (fileInformation.st_mode & S_IFDIR) {
52             return true;
53         }
54     } else {
55         HILOGE("Failed to stat file");
56     }
57     return false;
58 }
59 #endif
60 
ParseJsOperand(napi_env env,const NFuncArg & funcArg)61 static tuple<bool, unique_ptr<char[]>, unique_ptr<char[]>, int> ParseJsOperand(napi_env env, const NFuncArg& funcArg)
62 {
63     auto [resGetFirstArg, src, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
64     if (!resGetFirstArg || CheckDir(string(src.get()))) {
65         HILOGE("Invalid src");
66         return { false, nullptr, nullptr, 0 };
67     }
68     auto [resGetSecondArg, dest, unused] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
69     if (!resGetSecondArg || CheckDir(string(dest.get()))) {
70         HILOGE("Invalid dest");
71         return { false, nullptr, nullptr, 0 };
72     }
73     int mode = 0;
74     if (funcArg.GetArgc() >= NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)) {
75         bool resGetThirdArg = false;
76         tie(resGetThirdArg, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32();
77         if (!resGetThirdArg || (mode != MODE_FORCE_MOVE && mode != MODE_THROW_ERR)) {
78             HILOGE("Invalid mode");
79             return { false, nullptr, nullptr, 0 };
80         }
81     }
82     return { true, move(src), move(dest), mode };
83 }
84 
CopyAndDeleteFile(const string & src,const string & dest)85 static int CopyAndDeleteFile(const string &src, const string &dest)
86 {
87     int ret = 0;
88     uv_fs_t copyfile_req;
89     ret = uv_fs_copyfile(nullptr, &copyfile_req, src.c_str(), dest.c_str(), MODE_FORCE_MOVE, nullptr);
90     uv_fs_req_cleanup(&copyfile_req);
91     if (ret < 0) {
92         HILOGE("Failed to move file using copyfile interface.");
93         return errno;
94     }
95     uv_fs_t unlink_req;
96     ret = uv_fs_unlink(nullptr, &unlink_req, src.c_str(), nullptr);
97     if (ret < 0) {
98         HILOGE("Failed to unlink src file");
99         ret = uv_fs_unlink(nullptr, &unlink_req, dest.c_str(), nullptr);
100         if (ret < 0) {
101             HILOGE("Failed to unlink dest file");
102         }
103         uv_fs_req_cleanup(&unlink_req);
104         return errno;
105     }
106     uv_fs_req_cleanup(&unlink_req);
107     return ERRNO_NOERR;
108 }
109 
RenameFile(const string & src,const string & dest)110 static int RenameFile(const string &src, const string &dest)
111 {
112     int ret = 0;
113     uv_fs_t rename_req;
114     ret = uv_fs_rename(nullptr, &rename_req, src.c_str(), dest.c_str(), nullptr);
115     if (ret < 0 && errno == EXDEV) {
116         return CopyAndDeleteFile(src, dest);
117     }
118     if (ret < 0) {
119         HILOGE("Failed to move file using rename syscall.");
120         return errno;
121     }
122     return ERRNO_NOERR;
123 }
124 
MoveFile(const string & src,const string & dest,int mode)125 static int MoveFile(const string &src, const string &dest, int mode)
126 {
127     if (mode == MODE_THROW_ERR) {
128         uv_fs_t access_req;
129         int ret = uv_fs_access(nullptr, &access_req, dest.c_str(), 0, nullptr);
130         uv_fs_req_cleanup(&access_req);
131         if (ret == 0) {
132             HILOGE("Failed to move file due to existing destPath with MODE_THROW_ERR.");
133             return EEXIST;
134         }
135         if (ret < 0 && errno != ENOENT) {
136             HILOGE("Failed to access destPath with MODE_THROW_ERR.");
137             return errno;
138         }
139     }
140     return RenameFile(src, dest);
141 }
142 
Sync(napi_env env,napi_callback_info info)143 napi_value Move::Sync(napi_env env, napi_callback_info info)
144 {
145     NFuncArg funcArg(env, info);
146     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
147         HILOGE("Number of arguments unmatched");
148         NError(EINVAL).ThrowErr(env);
149         return nullptr;
150     }
151     auto [succ, src, dest, mode] = ParseJsOperand(env, funcArg);
152     if (!succ) {
153         NError(EINVAL).ThrowErr(env);
154         return nullptr;
155     }
156     int ret = MoveFile(string(src.get()), string(dest.get()), mode);
157     if (ret) {
158         NError(ret).ThrowErr(env);
159         return nullptr;
160     }
161     return NVal::CreateUndefined(env).val_;
162 }
163 
Async(napi_env env,napi_callback_info info)164 napi_value Move::Async(napi_env env, napi_callback_info info)
165 {
166     NFuncArg funcArg(env, info);
167     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) {
168         HILOGE("Number of arguments unmatched");
169         NError(EINVAL).ThrowErr(env);
170         return nullptr;
171     }
172     auto [succ, src, dest, mode] = ParseJsOperand(env, funcArg);
173     if (!succ) {
174         NError(EINVAL).ThrowErr(env);
175         return nullptr;
176     }
177 
178     auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode]() -> NError {
179         int ret = MoveFile(srcPath, destPath, mode);
180         if (ret) {
181             return NError(ret);
182         }
183         return NError(ERRNO_NOERR);
184     };
185 
186     auto cbComplCallback = [](napi_env env, NError err) -> NVal {
187         if (err) {
188             return { env, err.GetNapiErr(env) };
189         }
190         return { NVal::CreateUndefined(env) };
191     };
192 
193     NVal thisVar(env, funcArg.GetThisVar());
194     size_t argc = funcArg.GetArgc();
195     if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number))) {
196         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MOVE_NAME, cbExec, cbComplCallback).val_;
197     } else {
198         int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH);
199         NVal cb(env, funcArg[cbIdx]);
200         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MOVE_NAME, cbExec, cbComplCallback).val_;
201     }
202 }
203 } // namespace ModuleFileIO
204 } // namespace FileManagement
205 } // namespace OHOS