• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版d0a824950818b6d86abc9c4fef72f9c866cebaf9 (tree)
时间2018-04-10 05:18:39
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Some improvements to remove_file() and remove_directory() functions.

更改概述

差异

--- a/src/Global.cpp
+++ b/src/Global.cpp
@@ -398,58 +398,39 @@ static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile
398398 bool MUtils::remove_file(const QString &fileName)
399399 {
400400 QFileInfo fileInfo(fileName);
401- if(!(fileInfo.exists() && fileInfo.isFile()))
402- {
403- return true;
404- }
405401
406- for(int i = 0; i < 32; i++)
402+ for(size_t round = 0; round < 13; ++round)
407403 {
408- QFile file(fileName);
409- file.setPermissions(FILE_PERMISSIONS_NONE);
410- if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
404+ if (round > 0)
411405 {
412- return true;
406+ MUtils::OS::sleep_ms(round);
407+ fileInfo.refresh();
413408 }
414- MUtils::OS::sleep_ms(1);
415- fileInfo.refresh();
416- }
417-
418- qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
419- return false;
420-}
421-
422-static bool remove_directory_helper(const QDir &folder)
423-{
424- if(!folder.exists())
425- {
426- return true;
427- }
428- const QString dirName = folder.dirName();
429- if(!dirName.isEmpty())
430- {
431- QDir parent(folder);
432- if(parent.cdUp())
409+ if (fileInfo.exists())
433410 {
434- QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
435- if(parent.rmdir(dirName))
411+ QFile file(fileName);
412+ if (round > 0)
436413 {
437- return true;
414+ file.setPermissions(FILE_PERMISSIONS_NONE);
438415 }
416+ file.remove();
417+ fileInfo.refresh();
418+ }
419+ if (!fileInfo.exists())
420+ {
421+ return true; /*success*/
439422 }
440423 }
424+
425+ qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
441426 return false;
442427 }
443428
444429 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
445430 {
446- QDir folder(folderPath);
447- if(!folder.exists())
448- {
449- return true;
450- }
431+ const QDir folder(folderPath);
451432
452- if(recursive)
433+ if(recursive && folder.exists())
453434 {
454435 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
455436 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
@@ -458,21 +439,37 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
458439 {
459440 remove_directory(iter->canonicalFilePath(), true);
460441 }
461- else if(iter->isFile())
442+ else
462443 {
463444 remove_file(iter->canonicalFilePath());
464445 }
465446 }
466447 }
467448
468- for(int i = 0; i < 32; i++)
449+ for(size_t round = 0; round < 13; ++round)
469450 {
470- if(remove_directory_helper(folder))
451+ if(round > 0)
471452 {
472- return true;
453+ MUtils::OS::sleep_ms(round);
454+ folder.refresh();
455+ }
456+ if (folder.exists())
457+ {
458+ QDir parent = folder;
459+ if (parent.cdUp())
460+ {
461+ if (round > 0)
462+ {
463+ QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
464+ }
465+ parent.rmdir(folder.dirName());
466+ folder.refresh();
467+ }
468+ }
469+ if (!folder.exists())
470+ {
471+ return true; /*success*/
473472 }
474- MUtils::OS::sleep_ms(1);
475- folder.refresh();
476473 }
477474
478475 qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
Show on old repository browser