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
|
//Remove File/Dir
|
||||||
MUTILS_API bool remove_file(const QString &fileName);
|
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
|
//String sorting
|
||||||
MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase);
|
MUTILS_API void natural_string_sort(QStringList &list, const bool bIgnoreCase);
|
||||||
|
@ -102,6 +102,9 @@ namespace MUtils
|
|||||||
//CLI Arguments
|
//CLI Arguments
|
||||||
MUTILS_API const QStringList &arguments(void);
|
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
|
//Get the OS version
|
||||||
MUTILS_API const Version::os_version_t &os_version(void);
|
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);
|
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;
|
bool okay = false;
|
||||||
if(!m_lockFile.isNull())
|
if(!m_lockFile.isNull())
|
||||||
{
|
{
|
||||||
for(int i = 0; i < 8; i++)
|
for(int i = 0; i < 16; i++)
|
||||||
{
|
{
|
||||||
if(m_lockFile->remove())
|
if(m_lockFile->remove())
|
||||||
{
|
{
|
||||||
break;
|
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;
|
okay = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
OS::sleep_ms(1);
|
OS::sleep_ms(125);
|
||||||
}
|
}
|
||||||
if(!okay)
|
if(!okay)
|
||||||
{
|
{
|
||||||
|
@ -231,17 +231,34 @@ bool MUtils::remove_file(const QString &fileName)
|
|||||||
{
|
{
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
file.setPermissions(QFile::ReadOther | QFile::WriteOther);
|
file.setPermissions(QFile::ReadOther | QFile::WriteOther);
|
||||||
if(file.remove())
|
if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
fileInfo.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
|
qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
|
||||||
return false;
|
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);
|
QDir folder(folderPath);
|
||||||
if(!folder.exists())
|
if(!folder.exists())
|
||||||
@ -249,25 +266,41 @@ bool MUtils::remove_directory(const QString &folderPath)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(recursive)
|
||||||
|
{
|
||||||
const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
|
const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
|
||||||
for(int i = 0; i < entryList.count(); i++)
|
for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
|
||||||
{
|
{
|
||||||
if(entryList.at(i).isDir())
|
if(iter->isDir())
|
||||||
{
|
{
|
||||||
remove_directory(entryList.at(i).canonicalFilePath());
|
remove_directory(iter->canonicalFilePath(), true);
|
||||||
}
|
}
|
||||||
else
|
else if(iter->isFile())
|
||||||
{
|
{
|
||||||
remove_file(entryList.at(i).canonicalFilePath());
|
remove_file(iter->canonicalFilePath());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < 32; i++)
|
for(int i = 0; i < 32; i++)
|
||||||
{
|
{
|
||||||
if(folder.rmdir("."))
|
if(!folder.exists())
|
||||||
{
|
{
|
||||||
return true;
|
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));
|
qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
|
||||||
|
@ -113,6 +113,23 @@ const QStringList &MUtils::OS::arguments(void)
|
|||||||
return (*(g_arguments_list.data()));
|
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
|
// OS VERSION DETECTION
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user