Skip to content

Commit

Permalink
fix: During the cutting process, merging directories became a copy an…
Browse files Browse the repository at this point in the history
…d delete operation

Modify the logic to iterate the directory and rename all files in the source directory when cutting the merged directory

Log: During the cutting process, merging directories became a copy and delete operation
Bug: https://pms.uniontech.com/bug-view-276025.html
  • Loading branch information
liyigang1 authored and deepin-bot[bot] committed Nov 13, 2024
1 parent 02a9ad6 commit 89849af
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bool DoCutFilesWorker::cutFiles()
}

DFileInfoPointer fileInfo(new DFileInfo(url));

fileInfo->initQuerier();
// check self
if (checkSelf(fileInfo))
continue;
Expand All @@ -127,15 +127,15 @@ bool DoCutFilesWorker::cutFiles()
else
return false;
}

if (!doCutFile(fileInfo, targetInfo)) {
bool skip = false;
if (!doCutFile(fileInfo, targetInfo, &skip) && !skip) {
return false;
}
}
return true;
}

bool DoCutFilesWorker::doCutFile(const DFileInfoPointer &fromInfo, const DFileInfoPointer &targetPathInfo)
bool DoCutFilesWorker::doCutFile(const DFileInfoPointer &fromInfo, const DFileInfoPointer &targetPathInfo, bool *skip)
{
// try rename
bool ok = false;
Expand All @@ -147,7 +147,8 @@ bool DoCutFilesWorker::doCutFile(const DFileInfoPointer &fromInfo, const DFileIn
trashInfoUrl= trashInfo(fromInfo);
fileName = fileOriginName(trashInfoUrl);
}
DFileInfoPointer toInfo = doRenameFile(fromInfo, targetPathInfo, fileName, &ok);
DFileInfoPointer toInfo = doRenameFile(fromInfo, targetPathInfo, fileName, &ok, skip);

auto fromSize = fromInfo->attribute(DFileInfo::AttributeID::kStandardSize).toLongLong();
if (ok) {
workData->currentWriteSize += fromSize;
Expand Down Expand Up @@ -185,16 +186,18 @@ bool DoCutFilesWorker::doCutFile(const DFileInfoPointer &fromInfo, const DFileIn
return false;
}

if (skip && *skip)
return false;

if (toInfo.isNull()) {
fmWarning() << " do rename failed ! create null target Info";
return false;
}

fmDebug() << "do rename failed, use copy and delete way, from url: " << fromInfo->uri() << " to url: "
<< targetPathInfo->uri();
bool result = false;
if (!copyAndDeleteFile(fromInfo, targetPathInfo, toInfo, &result))
return result;
if (!copyAndDeleteFile(fromInfo, targetPathInfo, toInfo, skip))
return false;

workData->currentWriteSize += fromSize;
QUrl orignalUrl = fromInfo->uri();
Expand Down Expand Up @@ -239,6 +242,38 @@ void DoCutFilesWorker::emitCompleteFilesUpdatedNotify(const qint64 &writCount)
emit stateChangedNotify(info);
}

bool DoCutFilesWorker::doMergDir(const DFileInfoPointer &fromInfo, const DFileInfoPointer &toInfo, bool *skip)
{
// 遍历源文件,执行一个一个的拷贝
QString error;
const AbstractDirIteratorPointer &iterator = DirIteratorFactory::create<AbstractDirIterator>(fromInfo->uri(), &error);
if (!iterator) {
fmCritical() << "create dir's iterator failed, case : " << error;
doHandleErrorAndWait(fromInfo->uri(), toInfo->uri(), AbstractJobHandler::JobErrorType::kProrogramError);
return false;
}

iterator->setProperty("QueryAttributes", "standard::name");
while (iterator->hasNext()) {
if (!stateCheck()) {
return false;
}

const QUrl &url = iterator->next();
DFileInfoPointer info(new DFileInfo(url));
info->initQuerier();
bool ok = doCutFile(info, toInfo, skip);
if (!ok && (!skip || !*skip)) {
return false;
}

if (!ok)
continue;
}

return true;
}

bool DoCutFilesWorker::checkSymLink(const DFileInfoPointer &fileInfo)
{
const QUrl &sourceUrl = fileInfo->uri();
Expand Down Expand Up @@ -290,16 +325,24 @@ bool DoCutFilesWorker::renameFileByHandler(const DFileInfoPointer &sourceInfo, c

DFileInfoPointer DoCutFilesWorker::doRenameFile(const DFileInfoPointer &sourceInfo,
const DFileInfoPointer &targetPathInfo,
const QString fileName, bool *ok)
const QString fileName, bool *ok, bool *skip)
{
const QUrl &sourceUrl = sourceInfo->uri();
if (DFMIO::DFMUtils::deviceNameFromUrl(sourceUrl) == DFMIO::DFMUtils::deviceNameFromUrl(targetOrgUrl)) {
auto newTargetInfo = doCheckFile(sourceInfo, targetPathInfo, fileName, ok);
auto newTargetInfo = doCheckFile(sourceInfo, targetPathInfo, fileName, skip);
if (newTargetInfo.isNull())
return nullptr;

emitCurrentTaskNotify(sourceUrl, newTargetInfo->uri());
bool result = renameFileByHandler(sourceInfo, newTargetInfo);
bool result = false;
if (isCutMerge) {
newTargetInfo->initQuerier();
isCutMerge = false;
result = doMergDir( sourceInfo, newTargetInfo, skip);
} else {
result = renameFileByHandler(sourceInfo, newTargetInfo);
}

if (result) {
if (targetPathInfo == this->targetInfo) {
completeSourceFiles.append(sourceUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ class DoCutFilesWorker : public FileOperateBaseWorker
void endWork() override;

bool cutFiles();
bool doCutFile(const DFileInfoPointer &fromInfo, const DFileInfoPointer &targetPathInfo);
bool doCutFile(const DFileInfoPointer &fromInfo, const DFileInfoPointer &targetPathInfo, bool *skip);
DFileInfoPointer doRenameFile(const DFileInfoPointer &sourceInfo, const DFileInfoPointer &targetPathInfo,
const QString fileName, bool *ok);
const QString fileName, bool *ok, bool *skip);
bool renameFileByHandler(const DFileInfoPointer &sourceInfo, const DFileInfoPointer &targetInfo);

void emitCompleteFilesUpdatedNotify(const qint64 &writCount);
bool doMergDir(const DFileInfoPointer &fromInfo, const DFileInfoPointer &toInfo, bool *skip);

private:
bool checkSymLink(const DFileInfoPointer &fromInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ protected slots:
QAtomicInteger<qint64> bigFileSize { 0 }; // bigger than this is big file
QElapsedTimer *speedtimer { nullptr }; // time eslape
std::atomic_int64_t elapsed { 0 };
std::atomic_int64_t deleteFirstFileSize { false };
std::atomic_int64_t deleteFirstFileSize{ false };
bool isCutMerge{false};
};
DPFILEOPERATIONS_END_NAMESPACE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ DFileInfoPointer FileOperateBaseWorker::doCheckNewFile(const DFileInfoPointer &f
const QVariant &var = doActionMerge(fromInfo, newTargetInfo, isCountSize);
if (var.isValid())
return var.toBool() ? newTargetInfo : nullptr;
newTargetInfo->initQuerier();
if (jobType == AbstractJobHandler::JobType::kCutType && newTargetInfo)
isCutMerge = true;
break;
}
case AbstractJobHandler::SupportAction::kSkipAction: {
Expand Down Expand Up @@ -722,6 +725,7 @@ bool FileOperateBaseWorker::checkAndCopyDir(const DFileInfoPointer &fromInfo, co

const QUrl &url = iterator->next();
DFileInfoPointer info(new DFileInfo(url));
info->initQuerier();
bool ok = doCopyFile(info, toInfo, skip);
if (!ok && (!skip || !*skip)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,30 +158,31 @@ TEST_F(UT_DoCutFilesWorker, testDoCutFile)
stub.set_lamda(&FileUtils::isTrashFile, []{ __DBG_STUB_INVOKE__ return true;});
stub.set_lamda(&DoCutFilesWorker::doRenameFile, []{ __DBG_STUB_INVOKE__ return nullptr;});
worker.stopWork = true;
EXPECT_FALSE(worker.doCutFile(sorceInfo, targetInfo));
bool skip = false;
EXPECT_FALSE(worker.doCutFile(sorceInfo, targetInfo, &skip));

stub.set_lamda(VADDR(SyncFileInfo, size), []{ __DBG_STUB_INVOKE__ return 0;});
stub.set_lamda(&DoCutFilesWorker::doRenameFile, []{ __DBG_STUB_INVOKE__ return nullptr;});
stub.set_lamda(&DoCutFilesWorker::removeTrashInfo,[]{ __DBG_STUB_INVOKE__ });
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo));
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo, &skip));

stub.reset(&FileUtils::isTrashFile);
stub.set_lamda(&DoCutFilesWorker::doRenameFile, []{ __DBG_STUB_INVOKE__ return nullptr;});
stub.set_lamda(VADDR(SyncFileInfo, isAttributes), []{ __DBG_STUB_INVOKE__ return true;});
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo));
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo, &skip));

stub.set_lamda(&DoCutFilesWorker::doRenameFile, []{ __DBG_STUB_INVOKE__ return nullptr;});
stub.set(ADDR(DoCutFilesWorker, checkDiskSpaceAvailable), checkDiskSpaceAvailableFunc);
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo));
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo, &skip));

stub.set_lamda(&DoCutFilesWorker::checkDiskSpaceAvailable, []{ __DBG_STUB_INVOKE__ return true;});
stub.set_lamda(&DoCutFilesWorker::copyAndDeleteFile, []{ __DBG_STUB_INVOKE__ return false;});
EXPECT_FALSE(worker.doCutFile(sorceInfo, targetInfo));
EXPECT_FALSE(worker.doCutFile(sorceInfo, targetInfo, &skip));

stub.set_lamda(&DoCutFilesWorker::copyAndDeleteFile, []{ __DBG_STUB_INVOKE__ return true;});
stub.set_lamda(&FileUtils::isTrashFile, []{ __DBG_STUB_INVOKE__ return true;});
stub.set_lamda(&DoCutFilesWorker::removeTrashInfo, []{ __DBG_STUB_INVOKE__ });
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo));
EXPECT_TRUE(worker.doCutFile(sorceInfo, targetInfo, &skip));
worker.onUpdateProgress();
worker.emitCompleteFilesUpdatedNotify(199);
}
Expand Down Expand Up @@ -240,16 +241,16 @@ TEST_F(UT_DoCutFilesWorker, testRenameFileByHandler)

FileInfoPointer toInfo(nullptr);
stub.set_lamda(&DoCutFilesWorker::doCheckFile,[]{ __DBG_STUB_INVOKE__ return nullptr;});
bool skip{false};
EXPECT_FALSE(worker.doRenameFile(sorceInfo, targetInfo, "tests_iiii.txt", &skip));
bool ok{false},skip{false};
EXPECT_FALSE(worker.doRenameFile(sorceInfo, targetInfo, "tests_iiii.txt", &ok, &skip));

stub.set_lamda(&DFMUtils::deviceNameFromUrl, []{ __DBG_STUB_INVOKE__
return QByteArray("test-device");
});
EXPECT_FALSE(worker.doRenameFile(sorceInfo, targetInfo, "tests_iiii.txt", &skip));
EXPECT_FALSE(worker.doRenameFile(sorceInfo, targetInfo, "tests_iiii.txt", &ok, &skip));

stub.set_lamda(&DoCutFilesWorker::renameFileByHandler, []{ __DBG_STUB_INVOKE__ return true;});
stub.set(ADDR(DoCutFilesWorker, doCheckFile), doCheckFileFunc);
worker.targetInfo = targetInfo;
EXPECT_TRUE(worker.doRenameFile(sorceInfo, targetInfo, "tests_iiii.txt", &skip));
EXPECT_TRUE(worker.doRenameFile(sorceInfo, targetInfo, "tests_iiii.txt", &ok, &skip));
}

0 comments on commit 89849af

Please sign in to comment.