From ab0b641e1792df726f7cff04ee9a4023159d879e Mon Sep 17 00:00:00 2001 From: liyigang Date: Mon, 14 Oct 2024 17:18:25 +0800 Subject: [PATCH] fix: [filecopy]When copying large files, the swap partition is heavily used and the UI interface lags severely Modify the copying method to remove mmap and use copy_file_range Log: When copying large files, the swap partition is heavily used and the UI interface lags severely Bug: https://pms.uniontech.com/bug-view-273191.html --- .../fileoperationutils/docopyfileworker.cpp | 204 ++++++++++++++---- .../fileoperationutils/docopyfileworker.h | 8 +- .../fileoperatebaseworker.cpp | 165 +------------- .../fileoperatebaseworker.h | 11 +- .../ut_fileoperatebaseworker.cpp | 58 +---- 5 files changed, 172 insertions(+), 274 deletions(-) diff --git a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.cpp b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.cpp index 043610c46f..ea8479ad09 100644 --- a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.cpp +++ b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.cpp @@ -78,53 +78,6 @@ void DoCopyFileWorker::doFileCopy(const DFileInfoPointer fromInfo, const DFileIn workData->completeFileCount++; } -void DoCopyFileWorker::doMemcpyLocalBigFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, char *dest, char *source, size_t size) -{ - size_t copySize = size; - char *destStart = dest; - char *sourceStart = source; - size_t everyCopySize = kMaxBufferLength; - while (copySize > 0 && !isStopped()) { - if (Q_UNLIKELY(!stateCheck())) { - break; - } - everyCopySize = copySize >= everyCopySize ? everyCopySize : copySize; - - AbstractJobHandler::SupportAction action { AbstractJobHandler::SupportAction::kNoAction }; - - do { - action = AbstractJobHandler::SupportAction::kNoAction; - if (!memcpy(destStart, sourceStart, everyCopySize)) { - auto lastError = strerror(errno); - fmWarning() << "file memcpy error, url from: " << fromInfo->uri() - << " url to: " << toInfo->uri() - << " error code: " << errno << " error msg: " << lastError; - - action = doHandleErrorAndWait(fromInfo->uri(), toInfo->uri(), - AbstractJobHandler::JobErrorType::kWriteError, - true, lastError); - } - } while (action == AbstractJobHandler::SupportAction::kRetryAction && !isStopped()); - - checkRetry(); - - if (!actionOperating(action, static_cast(copySize), nullptr)) { - if (action == AbstractJobHandler::SupportAction::kSkipAction) - emit skipCopyLocalBigFile(fromInfo->uri()); - return; - } - - copySize -= everyCopySize; - destStart += everyCopySize; - sourceStart += everyCopySize; - - if (memcpySkipUrl.isValid() && memcpySkipUrl == fromInfo->uri()) - return; - - workData->currentWriteSize += static_cast(everyCopySize); - } -} - bool DoCopyFileWorker::doDfmioFileCopy(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip) { @@ -206,6 +159,54 @@ void DoCopyFileWorker::syncBlockFile(const DFileInfoPointer toInfo) close(tofd); } } +/*! + * \brief DoCopyFileWorker::openFile + * \param fromInfo + * \param toInfo + * \param flags + * \param skip + * \param isSource + * \return + */ +int DoCopyFileWorker::openFileBySys(const DFileInfoPointer &fromInfo, const DFileInfoPointer &toInfo, + const int flags, bool *skip, const bool isSource) +{ + int fd = -1; + AbstractJobHandler::SupportAction action = AbstractJobHandler::SupportAction::kNoAction; + auto openUrl = isSource ? fromInfo->uri() : toInfo->uri(); + do { + action = AbstractJobHandler::SupportAction::kNoAction; + if (flags & O_CREAT) { + fd = open(openUrl.path().toStdString().c_str(), flags, 0666); + } else { + fd = open(openUrl.path().toStdString().c_str(), flags); + } + + if (fd < 0) { + auto lastError = strerror(errno); + fmWarning() << "file open error, url from: " << fromInfo->uri() + << " url to: " << toInfo->uri() << " open flag: " << flags + << " open url : " << openUrl << " error msg: " << lastError; + + action = doHandleErrorAndWait(fromInfo->uri(), toInfo->uri(), + AbstractJobHandler::JobErrorType::kOpenError, + !isSource, lastError); + } + } while (action == AbstractJobHandler::SupportAction::kRetryAction && !isStopped()); + + checkRetry(); + + auto fileSize = fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong(); + if (!actionOperating(action, fileSize <= 0 ? FileUtils::getMemoryPageSize() : fileSize, skip)) { + close(fd); + return -1; + } + + if (isSource && fileSize > 100 * 1024 * 1024) + readahead(fd, 0, static_cast(fileSize)); + + return fd; +} // copy thread using DoCopyFileWorker::NextDo DoCopyFileWorker::doCopyFilePractically(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip) @@ -300,6 +301,115 @@ DoCopyFileWorker::NextDo DoCopyFileWorker::doCopyFilePractically(const DFileInfo return NextDo::kDoCopyNext; } +/*! + * \brief DoCopyFileWorker::doCopyFileByRange + * \param fromInfo + * \param toInfo + * \param skip + * \return + */ +DoCopyFileWorker::NextDo DoCopyFileWorker::doCopyFileByRange(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip) +{ + if (isStopped()) + return NextDo::kDoCopyErrorAddCancel; + + // emit current task url + emit currentTask(fromInfo->uri(), toInfo->uri()); + + // open source file + int sourcFd = openFileBySys(fromInfo, toInfo, O_RDONLY, skip); + if (sourcFd < 0) + return NextDo::kDoCopyErrorAddCancel; + + int targetFd = openFileBySys(fromInfo, toInfo, O_CREAT | O_WRONLY | O_TRUNC, skip, false); + if (targetFd < 0) { + close(sourcFd); + return NextDo::kDoCopyErrorAddCancel; + } + + // 源文件大小如果为0 + auto fromSize = fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong(); + if (fromSize <= 0) { + // 对文件加权 + setTargetPermissions(fromInfo->uri(), toInfo->uri()); + workData->zeroOrlinkOrDirWriteSize += FileUtils::getMemoryPageSize(); + FileUtils::notifyFileChangeManual(DFMBASE_NAMESPACE::Global::FileNotifyType::kFileAdded, toInfo->uri()); + if (workData->exBlockSyncEveryWrite || DeviceUtils::isSamba(toInfo->uri())) + syncfs(targetFd); + close(sourcFd); + close(targetFd); + return NextDo::kDoCopyNext; + } + + // 循环读取和写入文件,拷贝 + auto toIsSmb = DeviceUtils::isSamba(toInfo->uri()); + size_t blockSize = static_cast(fromSize > kMaxBufferLength ? kMaxBufferLength : fromSize); + + off_t offset_in = 0; + off_t offset_out = 0; + ssize_t result = -1; + AbstractJobHandler::SupportAction action { AbstractJobHandler::SupportAction::kNoAction }; + do { + if (Q_UNLIKELY(!stateCheck())) { + close(sourcFd); + close(targetFd); + return NextDo::kDoCopyErrorAddCancel; + } + + do { + if (Q_UNLIKELY(!stateCheck())) { + close(sourcFd); + close(targetFd); + return NextDo::kDoCopyErrorAddCancel; + } + result = copy_file_range(sourcFd, &offset_in, targetFd, &offset_out, blockSize, 0); + + if (result < 0) { + auto lastError = strerror(errno); + fmWarning() << "copy file range error, url from: " << fromInfo->uri() + << " url to: " << toInfo->uri() << " error msg: " << lastError; + + action = doHandleErrorAndWait(fromInfo->uri(), toInfo->uri(), + AbstractJobHandler::JobErrorType::kOpenError, + false, lastError); + offset_in = qMin(offset_in, offset_out); + offset_out = offset_in; + } else { + workData->currentWriteSize += result; + } + } while (action == AbstractJobHandler::SupportAction::kRetryAction && !isStopped()); + + checkRetry(); + + if (!actionOperating(action, fromSize - offset_out, skip)) { + close(sourcFd); + close(targetFd); + return NextDo::kDoCopyErrorAddCancel; + } + + // 执行同步策略 + if (workData->exBlockSyncEveryWrite || toIsSmb) + syncfs(targetFd); + + } while (offset_out != fromSize); + + // 执行同步策略 + if (workData->exBlockSyncEveryWrite || toIsSmb) + syncfs(targetFd); + + close(sourcFd); + close(targetFd); + + // 对文件加权 + setTargetPermissions(fromInfo->uri(), toInfo->uri()); + if (!stateCheck()) + return NextDo::kDoCopyErrorAddCancel; + + if (skip && *skip) + FileUtils::notifyFileChangeManual(DFMBASE_NAMESPACE::Global::FileNotifyType::kFileAdded, toInfo->uri()); + + return NextDo::kDoCopyNext; +} bool DoCopyFileWorker::stateCheck() { diff --git a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.h b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.h index 33096769d5..8b3fdc7277 100644 --- a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.h +++ b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/docopyfileworker.h @@ -59,10 +59,12 @@ class DoCopyFileWorker : public QObject // normal copy NextDo doCopyFilePractically(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip); + // normal copy + NextDo doCopyFileByRange(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, + bool *skip); // small file copy void doFileCopy(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo); - // big file copy in system device - void doMemcpyLocalBigFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, char *dest, char *source, size_t size); + // copy file by dfmio bool doDfmioFileCopy(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip); signals: @@ -117,6 +119,8 @@ class DoCopyFileWorker : public QObject void checkRetry(); bool isStopped(); void syncBlockFile(const DFileInfoPointer toInfo); + int openFileBySys(const DFileInfoPointer &fromInfo, const DFileInfoPointer &toInfo, + const int flags, bool *skip, const bool isSource = true); public: static void progressCallback(int64_t current, int64_t total, void *progressData); diff --git a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.cpp b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.cpp index 7aadb568ae..30f9a199d7 100644 --- a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.cpp +++ b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.cpp @@ -642,7 +642,7 @@ bool FileOperateBaseWorker::checkAndCopyFile(const DFileInfoPointer fromInfo, co } if (fromSize > bigFileSize) { bigFileCopy = true; - auto result = doCopyLocalBigFile(fromInfo, toInfo, skip); + auto result = doCopyLocalByRange(fromInfo, toInfo, skip); bigFileCopy = false; return result; } @@ -885,166 +885,15 @@ bool FileOperateBaseWorker::doCopyLocalFile(const DFileInfoPointer fromInfo, con return true; } -bool FileOperateBaseWorker::doCopyLocalBigFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip) +bool FileOperateBaseWorker::doCopyLocalByRange(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip) { waitThreadPoolOver(); - // open file - auto fromFd = doOpenFile(fromInfo, toInfo, false, O_RDONLY, skip); - if (fromFd < 0) - return false; - auto toFd = doOpenFile(fromInfo, toInfo, true, O_CREAT | O_RDWR, skip); - if (toFd < 0) { - close(fromFd); - return false; - } - // resize target file - if (!doCopyLocalBigFileResize(fromInfo, toInfo, toFd, skip)) { - close(fromFd); - close(toFd); - return false; - } - // mmap file - auto fromPoint = doCopyLocalBigFileMap(fromInfo, toInfo, fromFd, PROT_READ, skip); - if (!fromPoint) { - close(fromFd); - close(toFd); - return false; - } - auto toPoint = doCopyLocalBigFileMap(fromInfo, toInfo, toFd, PROT_WRITE, skip); - if (!toPoint) { - munmap(fromPoint, static_cast(fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong())); - close(fromFd); - close(toFd); - return false; - } - // memcpy file in other thread - memcpyLocalBigFile(fromInfo, toInfo, fromPoint, toPoint); - // wait copy - waitThreadPoolOver(); - // clear - doCopyLocalBigFileClear(static_cast(fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong()), fromFd, toFd, fromPoint, toPoint); - // set permissions - setTargetPermissions(fromInfo->uri(), toInfo->uri()); - return true; -} - -bool FileOperateBaseWorker::doCopyLocalBigFileResize(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, int toFd, bool *skip) -{ - AbstractJobHandler::SupportAction action { AbstractJobHandler::SupportAction::kNoAction }; - __off_t length = fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong(); - do { - action = AbstractJobHandler::SupportAction::kNoAction; - if (-1 == ftruncate(toFd, length)) { - auto lastError = strerror(errno); - fmWarning() << "file resize error, url from: " << fromInfo->uri() - << " url to: " << toInfo->uri() << " open flag: " << O_RDONLY - << " error code: " << errno << " error msg: " << lastError; - - action = doHandleErrorAndWait(fromInfo->uri(), toInfo->uri(), - AbstractJobHandler::JobErrorType::kResizeError, true, lastError); - } - } while (action == AbstractJobHandler::SupportAction::kRetryAction && !isStopped()); - - checkRetry(); - - if (!actionOperating(action, length <= 0 ? FileUtils::getMemoryPageSize() : length, skip)) - return false; - - return true; -} - -char *FileOperateBaseWorker::doCopyLocalBigFileMap(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, int fd, const int per, bool *skip) -{ - AbstractJobHandler::SupportAction action { AbstractJobHandler::SupportAction::kNoAction }; - void *point = nullptr; - auto fromSize = fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong(); - do { - action = AbstractJobHandler::SupportAction::kNoAction; - point = mmap(nullptr, static_cast(fromSize), - per, MAP_SHARED, fd, 0); - if (!point || point == MAP_FAILED) { - auto lastError = strerror(errno); - fmWarning() << "file mmap error, url from: " << fromInfo->uri() - << " url to: " << fromInfo->uri() - << " error code: " << errno << " error msg: " << lastError; - - action = doHandleErrorAndWait(fromInfo->uri(), toInfo->uri(), - AbstractJobHandler::JobErrorType::kOpenError, fd == PROT_WRITE, lastError); - } - } while (action == AbstractJobHandler::SupportAction::kRetryAction && !isStopped()); - - checkRetry(); - - if (!actionOperating(action, fromSize <= 0 ? FileUtils::getMemoryPageSize() : fromSize, skip)) - return nullptr; - - return static_cast(point); -} - -void FileOperateBaseWorker::memcpyLocalBigFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, char *fromPoint, char *toPoint) -{ - auto fromSize = fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong(); - auto offset = fromSize / threadCount; - char *fromPointStart = fromPoint; - char *toPointStart = toPoint; - for (int i = 0; i < threadCount; i++) { - offset = (i == (threadCount - 1) ? fromSize - (threadCount - 1) * offset : offset); - - char *tempfFromPointStart = fromPointStart; - char *tempfToPointStart = toPointStart; - size_t tempOffet = static_cast(offset); - QtConcurrent::run(threadPool.data(), threadCopyWorker[i].data(), - static_cast(&DoCopyFileWorker::doMemcpyLocalBigFile), - fromInfo, toInfo, tempfToPointStart, tempfFromPointStart, tempOffet); - - fromPointStart += offset; - toPointStart += offset; - } -} - -void FileOperateBaseWorker::doCopyLocalBigFileClear(const size_t size, - const int fromFd, const int toFd, char *fromPoint, char *toPoint) -{ - munmap(fromPoint, size); - munmap(toPoint, size); - close(fromFd); - close(toFd); -} - -int FileOperateBaseWorker::doOpenFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, - const bool isTo, const int openFlag, bool *skip) -{ - AbstractJobHandler::SupportAction action { AbstractJobHandler::SupportAction::kNoAction }; - emitCurrentTaskNotify(fromInfo->uri(), toInfo->uri()); - int fd = -1; - do { - QUrl url = isTo ? toInfo->uri() : fromInfo->uri(); - std::string path = url.path().toStdString(); - fd = open(path.c_str(), openFlag, 0666); - action = AbstractJobHandler::SupportAction::kNoAction; - if (fd < 0) { - auto lastError = strerror(errno); - fmWarning() << "file open error, url from: " << fromInfo->uri() - << " url to: " << fromInfo->uri() << " open flag: " << openFlag - << " error code: " << errno << " error msg: " << lastError; - - action = doHandleErrorAndWait(fromInfo->uri(), toInfo->uri(), - AbstractJobHandler::JobErrorType::kOpenError, isTo, lastError); - } - } while (action == AbstractJobHandler::SupportAction::kRetryAction && !isStopped()); - - checkRetry(); - - auto fromSize = fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong(); - if (!actionOperating(action, fromSize <= 0 ? FileUtils::getMemoryPageSize() : fromSize, skip)) { - if (fd >= 0) - close(fd); - return -1; - } + const QString &targetUrl = toInfo->uri().toString(); - return fd; + FileUtils::cacheCopyingFileUrl(targetUrl); + DoCopyFileWorker::NextDo nextDo = threadCopyWorker[0]->doCopyFileByRange(fromInfo, toInfo, skip); + FileUtils::removeCopyingFileUrl(targetUrl); + return nextDo != DoCopyFileWorker::NextDo::kDoCopyNext; } bool FileOperateBaseWorker::doCopyOtherFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip) diff --git a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.h b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.h index 8c6fe2a15c..774aa4c007 100644 --- a/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.h +++ b/src/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperationutils/fileoperatebaseworker.h @@ -97,16 +97,7 @@ class FileOperateBaseWorker : public AbstractWorker, public QEnableSharedFromThi QUrl createNewTargetUrl(const DFileInfoPointer &toInfo, const QString &fileName); bool doCopyLocalFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo); bool doCopyOtherFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip); - bool doCopyLocalBigFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip); - -private: // do copy local big file - bool doCopyLocalBigFileResize(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, int toFd, bool *skip); - char *doCopyLocalBigFileMap(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, int fd, const int per, bool *skip); - void memcpyLocalBigFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, char *fromPoint, char *toPoint); - void doCopyLocalBigFileClear(const size_t size, const int fromFd, - const int toFd, char *fromPoint, char *toPoint); - int doOpenFile(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, const bool isTo, - const int openFlag, bool *skip); + bool doCopyLocalByRange(const DFileInfoPointer fromInfo, const DFileInfoPointer toInfo, bool *skip); protected Q_SLOTS: void emitErrorNotify(const QUrl &from, const QUrl &to, const AbstractJobHandler::JobErrorType &error, diff --git a/tests/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperations/fileoperationutils/ut_fileoperatebaseworker.cpp b/tests/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperations/fileoperationutils/ut_fileoperatebaseworker.cpp index 82dc6b8ba8..8754888f76 100644 --- a/tests/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperations/fileoperationutils/ut_fileoperatebaseworker.cpp +++ b/tests/plugins/common/core/dfmplugin-fileoperations/fileoperations/fileoperations/fileoperationutils/ut_fileoperatebaseworker.cpp @@ -450,7 +450,7 @@ TEST_F(UT_FileOperateBaseWorker, testCheckAndCopyFile) worker.isTargetFileLocal = true; worker.workData->signalThread = false; stub.set_lamda(VADDR(SyncFileInfo, size), []{ __DBG_STUB_INVOKE__ return 100 * 1024 *1024;}); - stub.set_lamda(&FileOperateBaseWorker::doCopyLocalBigFile, []{ __DBG_STUB_INVOKE__ return false;}); + stub.set_lamda(&FileOperateBaseWorker::doCopyLocalByRange, []{ __DBG_STUB_INVOKE__ return false;}); EXPECT_FALSE(worker.checkAndCopyFile(fileInfo, fileInfo, &skip)); stub.set_lamda(&FileOperateBaseWorker::doCopyLocalFile, []{ __DBG_STUB_INVOKE__ return false;}); @@ -596,43 +596,6 @@ TEST_F(UT_FileOperateBaseWorker, testInitThreadCopy) worker.stopAllThread(); } -TEST_F(UT_FileOperateBaseWorker, testDoCopyLocalBigFile) -{ - QProcess::execute("rm sourceUrl.txt targetUrl.txt"); - FileOperateBaseWorker worker; - QUrl url = QUrl::fromLocalFile(QDir::currentPath()); - auto sorceUrl = QUrl::fromLocalFile(QDir::currentPath() + "/sourceUrl.txt"); - auto targetUrl = QUrl::fromLocalFile(QDir::currentPath() + "/targetUrl.txt"); - DFileInfoPointer targetInfo(new DFileInfo(targetUrl)); - DFileInfoPointer sorceInfo(new DFileInfo(sorceUrl)); - bool skip{false}; - stub_ext::StubExt stub; - worker.workData.reset(new WorkerData); - stub.set_lamda(&FileOperateBaseWorker::doHandleErrorAndWait, []{ __DBG_STUB_INVOKE__ return AbstractJobHandler::SupportAction::kSkipAction; }); - EXPECT_FALSE(worker.doCopyLocalBigFile(sorceInfo, targetInfo, &skip)); - - QProcess::execute("touch sourceUrl.txt"); - sorceUrl.setPath(url.path() + QDir::separator() + "sourceUrl.txt"); - EXPECT_FALSE(worker.doCopyLocalBigFile(sorceInfo, targetInfo, &skip)); - - QFile sorcefile(sorceUrl.path()); - if (sorcefile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - QByteArray data("tttttttt"); - for (int i = 0; i < 10000; ++i) { - sorcefile.write(data); - } - sorcefile.close(); - worker.threadPool.reset(new QThreadPool); - worker.initThreadCopy(); - EXPECT_FALSE(worker.doCopyLocalBigFile(sorceInfo, targetInfo, &skip)); - sorceInfo->refresh(); - EXPECT_TRUE(worker.doCopyLocalBigFile(sorceInfo, targetInfo, &skip)); - } - - QProcess::execute("rm sourceUrl.txt targetUrl.txt"); - -} - int openTest(int fd, __off_t offset) { Q_UNUSED(fd); Q_UNUSED(offset); @@ -640,25 +603,6 @@ int openTest(int fd, __off_t offset) { return -1; }; -TEST_F(UT_FileOperateBaseWorker, testDoCopyLocalBigFileCallFunc) -{ - FileOperateBaseWorker worker; - auto sorceUrl = QUrl::fromLocalFile(QDir::currentPath() + "/sourceUrl.txt"); - auto targetUrl = QUrl::fromLocalFile(QDir::currentPath() + "/targetUrl.txt"); - DFileInfoPointer targetInfo(new DFileInfo(targetUrl)); - DFileInfoPointer sorceInfo(new DFileInfo(sorceUrl)); - stub_ext::StubExt stub; - worker.workData.reset(new WorkerData); - - stub.set(&::ftruncate, openTest); - bool skip{false}; - stub.set_lamda(&FileOperateBaseWorker::doHandleErrorAndWait, []{ __DBG_STUB_INVOKE__ return AbstractJobHandler::SupportAction::kSkipAction; }); - EXPECT_FALSE(worker.doCopyLocalBigFileResize(sorceInfo, targetInfo, -1, &skip)); - - stub.set_lamda(&DoCopyFileWorker::doHandleErrorAndWait, []{ __DBG_STUB_INVOKE__ return AbstractJobHandler::SupportAction::kSkipAction; }); - EXPECT_FALSE(worker.doCopyOtherFile(sorceInfo, targetInfo, &skip)); -} - TEST_F(UT_FileOperateBaseWorker, testCreateNewTargetInfo) { FileOperateBaseWorker worker;