hkothari
21st August 2009, 13:40
So I'm writing a function to copy a file or directory recursively, if it's just a file It just uses QFile Copy, and it copies the file and it works, but when it's a directory, I get the directory Entry List and loop through it calling the copy method for each file, for some reason I keep getting put into some endless loop. Can anyone help me out with this? My code is below.
bool copy(QString sourceFilePath, QString destinationFilePath)
{
QFile sourceFile(sourceFilePath);
QFile destinationFile(destinationFilePath);
QFileInfo sourceFileInfo(sourceFile);
QFileInfo destinationFileInfo(destinationFile);
if(sourceFileInfo.fileName() == "." || sourceFileInfo.fileName() == "..")
return true;
//If we're just copying a file and it's already readable.
if(sourceFileInfo.isFile() && sourceFileInfo.isReadable())
{
if(destinationFileInfo.isFile() && destinationFile.isWritable())
{
//Warning, this deletes the file and replaces it.
QString newFileName = destinationFile.fileName();
destinationFile.remove();
return sourceFile.copy(newFileName);
}
else if(destinationFileInfo.isDir() && destinationFileInfo.isWritable())
{
//We can use fileName in this case because we know that the FULL PATH is set above.
QString newFileName = destinationFile.fileName();
newFileName.append("/");
//We have to use this to get JUST THE FILE NAME of the original file.
newFileName.append(QFileInfo(sourceFile).fileName( ));
return sourceFile.copy(newFileName);
}
}
//If we're copying a directory, it'll have to be done recursively.
else if(sourceFileInfo.isDir() && sourceFileInfo.isReadable())
{
qDebug() << "SFI Abs Path:" << sourceFileInfo.absolutePath();
QDir sourceFileDir(sourceFileInfo.absolutePath());
sourceFileDir.setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
QDir destinationFileDir(destinationFileInfo.absolutePat h());
qDebug() << "Mkdir output" << destinationFileDir.mkdir(sourceFileInfo.baseName() );
destinationFileDir.cd(sourceFileInfo.baseName());
const QFileInfoList sourceFileDirInfoList = sourceFileDir.entryInfoList();
for(int i = 0; i < sourceFileDirInfoList.size(); i++)
{
qDebug() << "Copy params: " << sourceFileDirInfoList[i].absoluteFilePath() << destinationFileDir.absolutePath();
copy(sourceFileDirInfoList[i].absoluteFilePath(), destinationFileDir.absolutePath());
}
return true;
}
//If we end up here, something went wrong.
return false;
}
bool copy(QString sourceFilePath, QString destinationFilePath)
{
QFile sourceFile(sourceFilePath);
QFile destinationFile(destinationFilePath);
QFileInfo sourceFileInfo(sourceFile);
QFileInfo destinationFileInfo(destinationFile);
if(sourceFileInfo.fileName() == "." || sourceFileInfo.fileName() == "..")
return true;
//If we're just copying a file and it's already readable.
if(sourceFileInfo.isFile() && sourceFileInfo.isReadable())
{
if(destinationFileInfo.isFile() && destinationFile.isWritable())
{
//Warning, this deletes the file and replaces it.
QString newFileName = destinationFile.fileName();
destinationFile.remove();
return sourceFile.copy(newFileName);
}
else if(destinationFileInfo.isDir() && destinationFileInfo.isWritable())
{
//We can use fileName in this case because we know that the FULL PATH is set above.
QString newFileName = destinationFile.fileName();
newFileName.append("/");
//We have to use this to get JUST THE FILE NAME of the original file.
newFileName.append(QFileInfo(sourceFile).fileName( ));
return sourceFile.copy(newFileName);
}
}
//If we're copying a directory, it'll have to be done recursively.
else if(sourceFileInfo.isDir() && sourceFileInfo.isReadable())
{
qDebug() << "SFI Abs Path:" << sourceFileInfo.absolutePath();
QDir sourceFileDir(sourceFileInfo.absolutePath());
sourceFileDir.setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
QDir destinationFileDir(destinationFileInfo.absolutePat h());
qDebug() << "Mkdir output" << destinationFileDir.mkdir(sourceFileInfo.baseName() );
destinationFileDir.cd(sourceFileInfo.baseName());
const QFileInfoList sourceFileDirInfoList = sourceFileDir.entryInfoList();
for(int i = 0; i < sourceFileDirInfoList.size(); i++)
{
qDebug() << "Copy params: " << sourceFileDirInfoList[i].absoluteFilePath() << destinationFileDir.absolutePath();
copy(sourceFileDirInfoList[i].absoluteFilePath(), destinationFileDir.absolutePath());
}
return true;
}
//If we end up here, something went wrong.
return false;
}