From f276d094aedbc5142cf09ea592efde0817beb3b7 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Mon, 4 Dec 2023 19:01:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6=E5=8F=8A?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E6=8B=B7=E8=B4=9D=EF=BC=8C=E4=B8=8D=E5=9C=A8?= =?UTF-8?q?=E4=B8=BB=E7=BA=BF=E7=A8=8B=E5=81=9A=E8=80=97=E6=97=B6=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=AD=89=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../kits/js/src/mod_fs/properties/copy.cpp | 131 +++++++++--------- .../kits/js/src/mod_fs/properties/copy.h | 3 +- 2 files changed, 64 insertions(+), 70 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.cpp b/interfaces/kits/js/src/mod_fs/properties/copy.cpp index 3960fea51..f52e535de 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -73,7 +74,6 @@ tuple Copy::GetListenerFromOptionArg(napi_env env, const NFuncArg &f { if (funcArg.GetArgc() >= NARG_CNT::THREE) { NVal op(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("progressListener") && !op.GetProp("progressListener").TypeIs(napi_undefined)) { if (!op.GetProp("progressListener").TypeIs(napi_function)) { HILOGE("Illegal options.progressListener type"); @@ -93,8 +93,7 @@ bool Copy::IsRemoteUri(const std::string &uri) bool Copy::IsDirectory(const std::string &path) { - struct stat buf { - }; + struct stat buf {}; int ret = stat(path.c_str(), &buf); if (ret == -1) { HILOGE("stat failed, errno is %{public}d, path is %{public}s", errno, path.c_str()); @@ -105,8 +104,7 @@ bool Copy::IsDirectory(const std::string &path) bool Copy::IsFile(const std::string &path) { - struct stat buf { - }; + struct stat buf {}; int ret = stat(path.c_str(), &buf); if (ret == -1) { HILOGI("stat failed, errno is %{public}d, ", errno); @@ -117,8 +115,7 @@ bool Copy::IsFile(const std::string &path) tuple Copy::GetFileSize(const std::string &path) { - struct stat buf { - }; + struct stat buf {}; int ret = stat(path.c_str(), &buf); if (ret == -1) { HILOGI("Stat failed."); @@ -127,23 +124,14 @@ tuple Copy::GetFileSize(const std::string &path) return { ERRNO_NOERR, buf.st_size }; } -bool Copy::CheckValidParam(const std::string &srcUri, const std::string &destUri) +void Copy::CheckOrCreateDestPath(const std::string &destPath) { - auto srcPath = ConvertUriToPath(srcUri); - auto destPath = ConvertUriToPath(destUri); if (!filesystem::exists(destPath)) { HILOGE("destPath not exist, destPath = %{public}s", destPath.c_str()); ofstream out; out.open(destPath.c_str()); out.close(); } - if (IsDirectory(srcPath)) { - return IsDirectory(destPath); - } - if (IsFile(srcPath)) { - return IsFile(destPath); - } - return false; } int Copy::CopyFile(const string &src, const string &dest) @@ -236,6 +224,9 @@ uint64_t Copy::GetDirSize(std::shared_ptr infos, std::string path) long int size = 0; for (int i = 0; i < num; i++) { string dest = path + '/' + string((pNameList->namelist[i])->d_name); + if ((pNameList->namelist[i])->d_type == DT_LNK) { + continue; + } if ((pNameList->namelist[i])->d_type == DT_DIR) { return size += GetDirSize(infos, dest); } @@ -261,6 +252,9 @@ int Copy::RecurCopyDir(const string &srcPath, const string &destPath, std::share for (int i = 0; i < num; i++) { string src = srcPath + '/' + string((pNameList->namelist[i])->d_name); string dest = destPath + '/' + string((pNameList->namelist[i])->d_name); + if ((pNameList->namelist[i])->d_type == DT_LNK) { + continue; + } if ((pNameList->namelist[i])->d_type == DT_DIR) { return CopySubDir(src, dest, infos); } @@ -285,6 +279,18 @@ int Copy::CopyDirFunc(const string &src, const string &dest, std::shared_ptr infos) +{ + CheckOrCreateDestPath(infos->destPath); + auto ret = SubscribeLocalListener(env, infos); + if (ret != ERRNO_NOERR) { + HILOGE("Failed to subscribe local listener, errno = %{public}d", ret); + return ret; + } + StartNotify(infos); + return ExecCopy(infos); +} + int Copy::SubscribeLocalListener(napi_env env, std::shared_ptr infos) { if (!infos->listener.TypeIs(napi_function)) { @@ -295,42 +301,45 @@ int Copy::SubscribeLocalListener(napi_env env, std::shared_ptr infos) HILOGE("Failed to init inotify, errno:%{public}d", errno); return errno; } - uint32_t watchEvents = IN_MODIFY; - int newWd = inotify_add_watch(infos->notifyFd, infos->destPath.c_str(), watchEvents); + auto errCode = ERRNO_NOERR; + int newWd = inotify_add_watch(infos->notifyFd, infos->destPath.c_str(), IN_MODIFY); if (newWd < 0) { - auto errNo = errno; - HILOGE("Failed to add inotify watch, errno:%{public}d, notifyFd = %{public}d, destPath = %{public}s", errNo, - infos->notifyFd, infos->destPath.c_str()); + errCode = errno; + HILOGE("Failed to add watch, notifyFd: %{public}d, destPath: %{public}s", infos->notifyFd, + infos->destPath.c_str()); close(infos->notifyFd); - return errNo; + return errCode; } auto receiveInfo = CreateSharedPtr(); - if (receiveInfo == nullptr) { - HILOGE("Failed to request heap memory."); - return ENOMEM; - } - receiveInfo->path = infos->destPath; auto callback = CreateSharedPtr(env, infos->listener); - if (callback == nullptr) { - HILOGE("Failed to request heap memory."); - return ENOMEM; - } - callback->wds.push_back({ newWd, receiveInfo }); - if (IsFile(infos->srcPath)) { - auto [err, fileSize] = GetFileSize(infos->srcPath); - if (err != ERRNO_NOERR) { - return err; + do { + if (receiveInfo == nullptr || callback == nullptr) { + HILOGE("Failed to request heap memory."); + errCode = ENOMEM; + break; } - callback->totalSize = fileSize; - } else { - callback->totalSize = GetDirSize(infos, infos->srcPath); + receiveInfo->path = infos->destPath; + callback->wds.push_back({ newWd, receiveInfo }); + if (IsFile(infos->srcPath)) { + auto [err, fileSize] = GetFileSize(infos->srcPath); + if (err == ERRNO_NOERR) { + callback->totalSize = fileSize; + } + errCode = err; + } else { + callback->totalSize = GetDirSize(infos, infos->srcPath); + } + } while (0); + if (errCode != ERRNO_NOERR) { + inotify_rm_watch(infos->notifyFd, newWd); + close(infos->notifyFd); + return errCode; } if (RegisterListener(infos, callback)) { return ERRNO_NOERR; } inotify_rm_watch(infos->notifyFd, newWd); close(infos->notifyFd); - HILOGE("Can not copy repeat."); return EINVAL; } @@ -391,7 +400,7 @@ void Copy::ReceiveComplete(uv_work_t *work, int stat) return; } NVal obj = NVal::CreateObject(env); - if (processedSize <= numeric_limits::max() && entry->totalSize <= numeric_limits::max()) { + if (processedSize <= numeric_limits::max() && entry->totalSize <= numeric_limits::max()) { obj.AddProp("processedSize", NVal::CreateInt64(env, processedSize).val_); obj.AddProp("totalSize", NVal::CreateInt64(env, entry->totalSize).val_); } @@ -452,6 +461,7 @@ uv_work_t *Copy::GetUVwork(std::shared_ptr infos) uv_work_t *work = new (std::nothrow) uv_work_t; if (work == nullptr) { HILOGE("Failed to create uv_work_t pointer"); + delete entry; return nullptr; } work->data = entry; @@ -580,18 +590,14 @@ void Copy::RemoveWatch(int notifyFd, std::shared_ptr callback) void Copy::GetNotifyEvent(std::shared_ptr infos) { - if (infos->run || infos->notifyFd < 0) { - HILOGE("Already run or notifyFd is invalid, notifyFd: %{public}d.", infos->notifyFd); - infos->exceptionCode = EINVAL; - return; - } + prctl(PR_SET_NAME, "NotifyThread"); infos->run = true; char buf[BUF_SIZE] = { 0 }; struct inotify_event *event = nullptr; fd_set fds = InitFds(infos->notifyFd); while (infos->run && infos->exceptionCode == ERRNO_NOERR) { auto ret = select(infos->notifyFd + 1, &fds, nullptr, nullptr, nullptr); - if (ret <= 0) { + if (ret < 0) { HILOGD("Failed to select, ret = %{public}d.", ret); infos->exceptionCode = errno; break; @@ -683,13 +689,9 @@ int Copy::ParseJsParam(napi_env env, NFuncArg &funcArg, std::shared_ptrsrcUri)) { - auto ret = SubscribeLocalListener(env, infos); - if (ret != ERRNO_NOERR) { - NError(ret).ThrowErr(env); - return nullptr; - } - } - std::shared_ptr tempInfos = infos; - auto cbExec = [env, tempInfos]() -> NError { - if (IsRemoteUri(tempInfos->srcUri)) { + auto cbExec = [env, infos]() -> NError { + if (IsRemoteUri(infos->srcUri)) { // copyRemoteUri - return SubscribeRemoteListener(env, tempInfos); + return SubscribeRemoteListener(env, infos); } - StartNotify(tempInfos); - auto result = ExecCopy(tempInfos); - if (result != ERRNO_NOERR) { - tempInfos->exceptionCode = result; + auto ret = ExecLocal(env, infos); + if (ret != ERRNO_NOERR) { + infos->exceptionCode = ret; } - return NError(tempInfos->exceptionCode); + return NError(infos->exceptionCode); }; auto cbCompl = [](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.h b/interfaces/kits/js/src/mod_fs/properties/copy.h index 4cd9662ac..e1f308c27 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.h +++ b/interfaces/kits/js/src/mod_fs/properties/copy.h @@ -93,10 +93,11 @@ private: // operator of napi static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg); static tuple GetListenerFromOptionArg(napi_env env, const NFuncArg &funcArg); - static bool CheckValidParam(const std::string &srcUri, const std::string &destUri); + static void CheckOrCreateDestPath(const std::string &destPath); static int ParseJsParam(napi_env env, NFuncArg &funcArg, std::shared_ptr &fileInfos); // operator of local listener + static int ExecLocal(napi_env env, std::shared_ptr infos); static fd_set InitFds(int notifyFd); static int SubscribeLocalListener(napi_env env, std::shared_ptr infos); static bool RegisterListener(std::shared_ptr fileInfos, std::shared_ptr callback); -- Gitee