Skip to content

Commit 748509c

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 7ab2d65 commit 748509c

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

3rdparty/interface/archiveinterface/cliinterface.cpp

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

1101+
void CliInterface::removeExtractedFilesOnFailure(const QString &strTargetPath, const QList<FileEntry> &entries)
1102+
{
1103+
QList<FileEntry> listToRemove = entries;
1104+
if (listToRemove.isEmpty()) {
1105+
listToRemove = DataManager::get_instance().archiveData().mapFileEntry.values();
1106+
}
1107+
if (listToRemove.isEmpty()) {
1108+
return;
1109+
}
1110+
1111+
QDir targetDir(strTargetPath);
1112+
if (!targetDir.exists()) {
1113+
return;
1114+
}
1115+
1116+
QList<QPair<QString, bool> > paths; // path, isDirectory
1117+
for (const FileEntry &entry : listToRemove) {
1118+
QString relPath = entry.strFullPath;
1119+
if (relPath.endsWith(QLatin1Char('/'))) {
1120+
relPath.chop(1);
1121+
}
1122+
if (relPath.isEmpty()) {
1123+
continue;
1124+
}
1125+
paths.append(qMakePair(targetDir.absoluteFilePath(relPath), entry.isDirectory));
1126+
}
1127+
1128+
for (const auto &p : paths) {
1129+
const QString &path = p.first;
1130+
if (!p.second) { // 文件
1131+
QFileInfo fi(path);
1132+
if (fi.exists() && fi.isFile() && fi.size() == 0) {
1133+
QFile::remove(path);
1134+
}
1135+
}
1136+
}
1137+
// 空目录可能有多层,循环直到本轮没有可删的空目录
1138+
bool removed;
1139+
do {
1140+
removed = false;
1141+
for (const auto &p : paths) {
1142+
if (!p.second) {
1143+
continue;
1144+
}
1145+
QDir d(p.first);
1146+
if (d.exists() && d.isEmpty()) {
1147+
d.removeRecursively();
1148+
removed = true;
1149+
}
1150+
}
1151+
} while (removed);
1152+
}
1153+
11011154
bool CliInterface::handleLongNameExtract(const QList<FileEntry> &files)
11021155
{
11031156
ExtractionOptions &options = m_extractOptions;
@@ -1272,6 +1325,7 @@ void CliInterface::readStdout(bool handleAll)
12721325
// 第二个判断条件是处理rar的list,当rar文件含有comment信息的时候需要根据空行解析
12731326
if (!line.isEmpty() || (m_listEmptyLines && m_workStatus == WT_List)) {
12741327
if (!handleLine(QString::fromLocal8Bit(line), m_workStatus)) {
1328+
emit signalprogress(100);
12751329
killProcess();
12761330
return;
12771331
}
@@ -1316,6 +1370,11 @@ void CliInterface::extractProcessFinished(int exitCode, QProcess::ExitStatus exi
13161370
m_indexOfListRootEntry = 0;
13171371
m_isEmptyArchive = false;
13181372

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

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)