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

This commit is contained in:
LoRd_MuldeR 2018-04-09 22:18:39 +02:00
parent 17dbb10810
commit d0a8249508

View File

@ -398,58 +398,39 @@ static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile
bool MUtils::remove_file(const QString &fileName) bool MUtils::remove_file(const QString &fileName)
{ {
QFileInfo fileInfo(fileName); QFileInfo fileInfo(fileName);
if(!(fileInfo.exists() && fileInfo.isFile()))
{
return true;
}
for(int i = 0; i < 32; i++) for(size_t round = 0; round < 13; ++round)
{ {
QFile file(fileName); if (round > 0)
file.setPermissions(FILE_PERMISSIONS_NONE);
if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
{ {
return true; MUtils::OS::sleep_ms(round);
fileInfo.refresh();
}
if (fileInfo.exists())
{
QFile file(fileName);
if (round > 0)
{
file.setPermissions(FILE_PERMISSIONS_NONE);
}
file.remove();
fileInfo.refresh();
}
if (!fileInfo.exists())
{
return true; /*success*/
} }
MUtils::OS::sleep_ms(1);
fileInfo.refresh();
} }
qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName)); qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
return false; return false;
} }
static bool remove_directory_helper(const QDir &folder)
{
if(!folder.exists())
{
return true;
}
const QString dirName = folder.dirName();
if(!dirName.isEmpty())
{
QDir parent(folder);
if(parent.cdUp())
{
QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
if(parent.rmdir(dirName))
{
return true;
}
}
}
return false;
}
bool MUtils::remove_directory(const QString &folderPath, const bool &recursive) bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
{ {
QDir folder(folderPath); const QDir folder(folderPath);
if(!folder.exists())
{
return true;
}
if(recursive) if(recursive && folder.exists())
{ {
const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden); const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++) for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
@ -458,21 +439,37 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
{ {
remove_directory(iter->canonicalFilePath(), true); remove_directory(iter->canonicalFilePath(), true);
} }
else if(iter->isFile()) else
{ {
remove_file(iter->canonicalFilePath()); remove_file(iter->canonicalFilePath());
} }
} }
} }
for(int i = 0; i < 32; i++) for(size_t round = 0; round < 13; ++round)
{ {
if(remove_directory_helper(folder)) if(round > 0)
{ {
return true; MUtils::OS::sleep_ms(round);
folder.refresh();
}
if (folder.exists())
{
QDir parent = folder;
if (parent.cdUp())
{
if (round > 0)
{
QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
}
parent.rmdir(folder.dirName());
folder.refresh();
}
}
if (!folder.exists())
{
return true; /*success*/
} }
MUtils::OS::sleep_ms(1);
folder.refresh();
} }
qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath)); qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));