Skip to content

Commit 9e2c988

Browse files
LiHua000deepin-bot[bot]
authored andcommitted
fix: remove empty files left when split-volume encrypted extraction fails (e.g. wrong password)
Log: as title Bug: https://pms.uniontech.com/bug-view-343249.html
1 parent 4facf75 commit 9e2c988

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

3rdparty/interface/archiveinterface/cliinterface.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,59 @@ bool CliInterface::moveExtractTempFilesToDest(const QList<FileEntry> &files, con
11101110
return moveSuccess;
11111111
}
11121112

1113+
void CliInterface::removeExtractedFilesOnFailure(const QString &strTargetPath, const QList<FileEntry> &entries)
1114+
{
1115+
QList<FileEntry> listToRemove = entries;
1116+
if (listToRemove.isEmpty()) {
1117+
listToRemove = DataManager::get_instance().archiveData().mapFileEntry.values();
1118+
}
1119+
if (listToRemove.isEmpty()) {
1120+
return;
1121+
}
1122+
1123+
QDir targetDir(strTargetPath);
1124+
if (!targetDir.exists()) {
1125+
return;
1126+
}
1127+
1128+
QList<QPair<QString, bool> > paths; // path, isDirectory
1129+
for (const FileEntry &entry : listToRemove) {
1130+
QString relPath = entry.strFullPath;
1131+
if (relPath.endsWith(QLatin1Char('/'))) {
1132+
relPath.chop(1);
1133+
}
1134+
if (relPath.isEmpty()) {
1135+
continue;
1136+
}
1137+
paths.append(qMakePair(targetDir.absoluteFilePath(relPath), entry.isDirectory));
1138+
}
1139+
1140+
for (const auto &p : paths) {
1141+
const QString &path = p.first;
1142+
if (!p.second) { // 文件
1143+
QFileInfo fi(path);
1144+
if (fi.exists() && fi.isFile() && fi.size() == 0) {
1145+
QFile::remove(path);
1146+
}
1147+
}
1148+
}
1149+
// 空目录可能有多层,循环直到本轮没有可删的空目录
1150+
bool removed;
1151+
do {
1152+
removed = false;
1153+
for (const auto &p : paths) {
1154+
if (!p.second) {
1155+
continue;
1156+
}
1157+
QDir d(p.first);
1158+
if (d.exists() && d.isEmpty()) {
1159+
d.removeRecursively();
1160+
removed = true;
1161+
}
1162+
}
1163+
} while (removed);
1164+
}
1165+
11131166
bool CliInterface::handleLongNameExtract(const QList<FileEntry> &files)
11141167
{
11151168
ExtractionOptions &options = m_extractOptions;
@@ -1284,6 +1337,7 @@ void CliInterface::readStdout(bool handleAll)
12841337
// 第二个判断条件是处理rar的list,当rar文件含有comment信息的时候需要根据空行解析
12851338
if (!line.isEmpty() || (m_listEmptyLines && m_workStatus == WT_List)) {
12861339
if (!handleLine(QString::fromLocal8Bit(line), m_workStatus)) {
1340+
emit signalprogress(100);
12871341
killProcess();
12881342
return;
12891343
}
@@ -1328,6 +1382,11 @@ void CliInterface::extractProcessFinished(int exitCode, QProcess::ExitStatus exi
13281382
m_indexOfListRootEntry = 0;
13291383
m_isEmptyArchive = false;
13301384

1385+
// 解压失败(如分卷加密包输错密码)且为全部解压到目标路径时,清理已生成的 size 为 0 等残留文件
1386+
if (0 != exitCode && m_extractOptions.bAllExtract && !m_extractOptions.strTargetPath.isEmpty()) {
1387+
removeExtractedFilesOnFailure(m_extractOptions.strTargetPath, m_files);
1388+
}
1389+
13311390
if (!m_extractOptions.bAllExtract && (!(m_extractOptions.strTargetPath.startsWith("/tmp") && m_extractOptions.strTargetPath.contains("/deepin-compressor-") && m_extractOptions.strDestination.isEmpty()))) {
13321391
if (0 == exitCode) { // job正常结束
13331392
// 提取操作和打开解压列表文件非第一层的文件

3rdparty/interface/archiveinterface/cliinterface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ class CliInterface : public ReadWriteArchiveInterface
193193
*/
194194
bool moveExtractTempFilesToDest(const QList<FileEntry> &files, const ExtractionOptions &options);
195195

196+
/**
197+
* @brief removeExtractedFilesOnFailure 解压失败时清理已生成的文件(如分卷加密包输错密码时产生的 size 为 0 的文件)
198+
* @param strTargetPath 解压目标路径
199+
* @param entries 本次解压涉及的条目列表(可为空,为空时从 ArchiveData 获取全部)
200+
*/
201+
void removeExtractedFilesOnFailure(const QString &strTargetPath, const QList<FileEntry> &entries);
202+
196203
bool handleLongNameExtract(const QList<FileEntry> &files);
197204

198205
private slots:

0 commit comments

Comments
 (0)