PDA

View Full Version : QFile::remove() returns true although it failed



Binary91
8th July 2015, 14:56
Hi,

I'm subclassing QDir for a "removeAll" method to fully remove a directory with all its sub-directories.

My problem is the error-handling. To avoid infinity loops, I break the iteration if QFile::remove() returns false.

When all files are closed, in some cases even if they are open, the removeAll method does great job.

BUT:
Let's take an example situation:
I have a bitmap test.bmp in a sub-directory.
Now there are 3 cases:
1. test.bmp is closed --> everything works fine and test.bmp is deleted
2. test.bmp is opend for viewing (doubleclick on file) --> everything works fine and test.bmp is deleted (although it is still opened on my screen, but when I close the window, it is deleted)
3. test.bmp is opend for editing (paint.exe) --> infinity loop, because it is not deleted and program always tries and fails to delete the parent directory!!

That is the code:

myQDir::myQDir(const QString &stringPath) : QDir(stringPath)
{
this->diriteratorAllEntries = new QDirIterator(stringPath, QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
this->diriteratorAllSubDirs = new QDirIterator(stringPath, QDir::AllDirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
this->stringlistAllSubDirs = new QStringList;
this->stringlistErrors = new QStringList;
this->stringlistErrors->clear();
}

bool myQDir::removeAll(bool boolIncludeParent)
{
this->stringlistErrors->clear();
QString stringEntry;
while(this->diriteratorAllEntries->hasNext())
{
stringEntry = this->diriteratorAllEntries->next();
if(QDir(stringEntry).exists())
{
this->stringlistAllSubDirs->append(stringEntry);
continue;
}
if(!QFile::remove(stringEntry)) // there is the problem --> QFile::remove() always returns true, even when the bitmap is not deleted!!
this->stringlistErrors->append(stringEntry);
}
if(this->stringlistErrors->count() > 0) // this should break when any files are not deleted and prevent from going into the next loop where the directories are deleted --> also fails!!
return false;

while(this->stringlistAllSubDirs->count() > 0)
{
for(int i = 0; i < this->stringlistAllSubDirs->count(); i++)
{
if(QDir(this->stringlistAllSubDirs->at(i)).rmdir(this->stringlistAllSubDirs->at(i)))
this->stringlistAllSubDirs->removeAt(i);
}
}
if(boolIncludeParent)
{
if(!this->rmdir(this->absolutePath()))
return false;
}
return true;
}

Does anybody know why QFile::remove fails when the bitmap is opend for editing? Maybe it doesn't realize that a second program (e.g. paint.exe) has access to it... Don't know.

Thank you in anticipation!
Binary