Added the copy_file() function + some improvements to directory clean-up code.
This commit is contained in:
parent
b58347f7eb
commit
4572f69d33
@ -86,7 +86,7 @@ namespace MUtils
|
||||
|
||||
//Remove File/Dir
|
||||
MUTILS_API bool remove_file(const QString &fileName);
|
||||
MUTILS_API bool remove_directory(const QString &folderPath);
|
||||
MUTILS_API bool remove_directory(const QString &folderPath, const bool &recursive);
|
||||
|
||||
//String sorting
|
||||
MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase);
|
||||
|
@ -102,6 +102,9 @@ namespace MUtils
|
||||
//CLI Arguments
|
||||
MUTILS_API const QStringList &arguments(void);
|
||||
|
||||
//Copy file
|
||||
MUTILS_API bool copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite = true);
|
||||
|
||||
//Get the OS version
|
||||
MUTILS_API const Version::os_version_t &os_version(void);
|
||||
MUTILS_API const char *os_friendly_name(const MUtils::OS::Version::os_version_t &os_version);
|
||||
|
@ -86,23 +86,24 @@ namespace MUtils
|
||||
bool okay = false;
|
||||
if(!m_lockFile.isNull())
|
||||
{
|
||||
for(int i = 0; i < 8; i++)
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
if(m_lockFile->remove())
|
||||
{
|
||||
break;
|
||||
}
|
||||
OS::sleep_ms(1);
|
||||
OS::sleep_ms(125);
|
||||
}
|
||||
m_lockFile.reset(NULL);
|
||||
}
|
||||
for(int i = 0; i < 8; i++)
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
if(MUtils::remove_directory(m_dirPath))
|
||||
if(MUtils::remove_directory(m_dirPath, true))
|
||||
{
|
||||
okay = true;
|
||||
break;
|
||||
}
|
||||
OS::sleep_ms(1);
|
||||
OS::sleep_ms(125);
|
||||
}
|
||||
if(!okay)
|
||||
{
|
||||
|
@ -231,17 +231,34 @@ bool MUtils::remove_file(const QString &fileName)
|
||||
{
|
||||
QFile file(fileName);
|
||||
file.setPermissions(QFile::ReadOther | QFile::WriteOther);
|
||||
if(file.remove())
|
||||
if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
fileInfo.refresh();
|
||||
}
|
||||
|
||||
qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MUtils::remove_directory(const QString &folderPath)
|
||||
static bool remove_directory_helper(QDir folder)
|
||||
{
|
||||
if(!folder.exists())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
const QString dirName = folder.dirName();
|
||||
if(dirName.isEmpty() || (!folder.cdUp()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return folder.rmdir(dirName);
|
||||
}
|
||||
|
||||
bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
|
||||
{
|
||||
QDir folder(folderPath);
|
||||
if(!folder.exists())
|
||||
@ -249,25 +266,41 @@ bool MUtils::remove_directory(const QString &folderPath)
|
||||
return true;
|
||||
}
|
||||
|
||||
const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
|
||||
for(int i = 0; i < entryList.count(); i++)
|
||||
if(recursive)
|
||||
{
|
||||
if(entryList.at(i).isDir())
|
||||
const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
|
||||
for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
|
||||
{
|
||||
remove_directory(entryList.at(i).canonicalFilePath());
|
||||
}
|
||||
else
|
||||
{
|
||||
remove_file(entryList.at(i).canonicalFilePath());
|
||||
if(iter->isDir())
|
||||
{
|
||||
remove_directory(iter->canonicalFilePath(), true);
|
||||
}
|
||||
else if(iter->isFile())
|
||||
{
|
||||
remove_file(iter->canonicalFilePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 32; i++)
|
||||
{
|
||||
if(folder.rmdir("."))
|
||||
if(!folder.exists())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
const QString dirName = folder.dirName();
|
||||
if(!dirName.isEmpty())
|
||||
{
|
||||
QDir parent(folder);
|
||||
if(parent.cdUp())
|
||||
{
|
||||
if(parent.rmdir(dirName))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
folder.refresh();
|
||||
}
|
||||
|
||||
qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
|
||||
|
@ -113,6 +113,23 @@ const QStringList &MUtils::OS::arguments(void)
|
||||
return (*(g_arguments_list.data()));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// COPY FILE
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
MUTILS_API bool MUtils::OS::copy_file(const QString &sourcePath, const QString &outputPath, const bool &overwrite)
|
||||
{
|
||||
const BOOL result = CopyFileW(MUTILS_WCHR(QDir::toNativeSeparators(sourcePath)), MUTILS_WCHR(QDir::toNativeSeparators(outputPath)), overwrite ? FALSE : TRUE);
|
||||
|
||||
if(result == FALSE)
|
||||
{
|
||||
const DWORD errorCode = GetLastError();
|
||||
qWarning("CopyFile() failed with error code 0x%08X!", errorCode);
|
||||
}
|
||||
|
||||
return (result != FALSE);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// OS VERSION DETECTION
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
Loading…
Reference in New Issue
Block a user