refactor: use std::filesystem for path deletion

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-07-30 20:40:25 -03:00
parent 5932f36285
commit be3fae6511
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469

View File

@ -179,49 +179,15 @@ bool copy::operator()(const QString& offset)
bool deletePath(QString path)
{
bool OK = true;
QFileInfo finfo(path);
if (finfo.isFile()) {
return QFile::remove(path);
std::error_code err;
std::filesystem::remove_all(path.toStdString(), err);
if (err) {
qWarning() << "Failed to remove files:" << QString::fromStdString(err.message());
}
QDir dir(path);
if (!dir.exists()) {
return OK;
}
auto allEntries = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
for (auto& info : allEntries) {
#if defined Q_OS_WIN32
QString nativePath = QDir::toNativeSeparators(info.absoluteFilePath());
auto wString = nativePath.toStdWString();
DWORD dwAttrs = GetFileAttributesW(wString.c_str());
// Windows: check for junctions, reparse points and other nasty things of that sort
if (dwAttrs & FILE_ATTRIBUTE_REPARSE_POINT) {
if (info.isFile()) {
OK &= QFile::remove(info.absoluteFilePath());
} else if (info.isDir()) {
OK &= dir.rmdir(info.absoluteFilePath());
}
}
#else
// We do not trust Qt with reparse points, but do trust it with unix symlinks.
if (info.isSymLink()) {
OK &= QFile::remove(info.absoluteFilePath());
}
#endif
else if (info.isDir()) {
OK &= deletePath(info.absoluteFilePath());
} else if (info.isFile()) {
OK &= QFile::remove(info.absoluteFilePath());
} else {
OK = false;
qCritical() << "Delete ERROR: Unknown filesystem object:" << info.absoluteFilePath();
}
}
OK &= dir.rmdir(dir.absolutePath());
return OK;
return err.value() == 0;
}
bool trash(QString path, QString *pathInTrash = nullptr)