diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.cpp b/interfaces/kits/js/src/mod_fs/properties/copy.cpp index dedbcf8123307ae718d72ef2099668356fd859de..2b8ee39636be3271959fb8067ee9c1f106c09df9 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy.cpp @@ -50,6 +50,7 @@ const string PROCEDURE_COPY_NAME = "FileFSCopy"; constexpr int DISMATCH = 0; constexpr int MATCH = 1; constexpr int BUF_SIZE = 1024; +constexpr std::chrono::milliseconds NOTIFY_PROGRESS_DELAY(200); std::recursive_mutex Copy::mutex_; std::map> Copy::jsCbMap_; @@ -129,7 +130,7 @@ tuple Copy::GetFileSize(const std::string &path) void Copy::CheckOrCreatePath(const std::string &destPath) { if (!filesystem::exists(destPath)) { - HILOGE("destPath not exist, destPath = %{public}s", destPath.c_str()); + HILOGI("destPath not exist, destPath = %{public}s", destPath.c_str()); ofstream out; out.open(destPath.c_str()); out.close(); @@ -283,7 +284,13 @@ int Copy::CopyDirFunc(const string &src, const string &dest, std::shared_ptr infos, std::shared_ptr callback) { - CheckOrCreatePath(infos->destPath); + if (infos->srcPath == infos->destPath) { + HILOGE("The src and dest are the same."); + return EINVAL; + } + if (IsFile(infos->srcPath)) { + CheckOrCreatePath(infos->destPath); + } if (!infos->hasListener) { return ExecCopy(infos); } @@ -387,7 +394,7 @@ void Copy::ReceiveComplete(uv_work_t *work, int stat) return; } auto processedSize = entry->progressSize; - if (processedSize <= entry->callback->maxProgressSize) { + if (processedSize < entry->callback->maxProgressSize) { return; } entry->callback->maxProgressSize = processedSize; @@ -565,7 +572,11 @@ void Copy::ReadNotifyEvent(std::shared_ptr infos) infos->run = false; return; } - OnFileReceive(infos); + auto currentTime = std::chrono::steady_clock::now(); + if (currentTime >= infos->notifyTime) { + OnFileReceive(infos); + infos->notifyTime = currentTime + NOTIFY_PROGRESS_DELAY; + } index += sizeof(struct inotify_event) + event->len; } } @@ -622,7 +633,7 @@ tuple> Copy::CreateFileInfos( infos->listener = listener; infos->srcPath = ConvertUriToPath(infos->srcUri); infos->destPath = ConvertUriToPath(infos->destUri); - + infos->notifyTime = std::chrono::steady_clock::now() + NOTIFY_PROGRESS_DELAY; if (listener) { infos->hasListener = true; } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy.h b/interfaces/kits/js/src/mod_fs/properties/copy.h index ebb0403a4d7a60fd029f3ae0930e7dd62a69bafb..1694ddd7d5ab0512a01de2602a760c4dbe185171 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy.h +++ b/interfaces/kits/js/src/mod_fs/properties/copy.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "bundle_mgr_client_impl.h" #include "common_func.h" @@ -74,6 +75,7 @@ struct FileInfos { std::string destUri; std::string srcPath; std::string destPath; + std::chrono::steady_clock::time_point notifyTime; int32_t notifyFd = -1; int32_t eventFd = -1; bool run = true;