Fix: keep renamed file content during compression#327
Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/snipefrom Nov 21, 2025
Merged
Conversation
- copy aliased entries into a temp directory before invoking createArchive - update FileEntry paths to point at the real copies and clear aliases - cleanup the temp copies on success, failure, cancel, and reset - ensures libzip sees actual files instead of symlink paths Log: fix bug Bug: https://pms.uniontech.com/bug-view-340081.html
Reviewer's GuideIntroduces a temporary copy mechanism for renamed files during compression by recursively copying aliased entries into a unique temp directory, updating FileEntry paths, clearing aliases, and ensuring cleanup on all completion paths. Sequence diagram for compression with temporary alias file handlingsequenceDiagram
participant MainWindow
participant CompressPage
participant FileSystem
participant LibZip
MainWindow->>CompressPage: getEntrys()
CompressPage-->>MainWindow: listEntry
MainWindow->>MainWindow: prepareCompressAliasEntries(listEntry)
alt Aliased entries exist
MainWindow->>FileSystem: Create temp directory
MainWindow->>FileSystem: Copy aliased files/directories
MainWindow->>MainWindow: Update FileEntry paths, clear aliases
end
MainWindow->>LibZip: createArchive(listEntry)
alt On success/failure/cancel/reset
MainWindow->>MainWindow: cleanupCompressAliasEntries()
MainWindow->>FileSystem: Remove temp directory
end
Class diagram for MainWindow and FileEntry changesclassDiagram
class MainWindow {
+QString m_strCompressAliasRoot
+bool m_needCleanupCompressAlias
+bool prepareCompressAliasEntries(QList<FileEntry>&)
+void cleanupCompressAliasEntries()
}
class FileEntry {
+QString strFullPath
+QString strAlias
+bool isDirectory
}
MainWindow "1" -- "*" FileEntry : manages
MainWindow : prepareCompressAliasEntries(QList<FileEntry>&)
MainWindow : cleanupCompressAliasEntries()
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review我来对这个diff进行详细的代码审查:
// 改进copyDirectoryRecursively函数
bool copyDirectoryRecursively(const QString &sourcePath, const QString &targetPath) {
QDir sourceDir(sourcePath);
if (!sourceDir.exists()) {
qWarning() << "Source directory does not exist:" << sourcePath;
return false;
}
if (!QDir().mkpath(targetPath)) {
qWarning() << "Failed to create target directory:" << targetPath;
return false;
}
QDirIterator it(sourcePath, QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files | QDir::Hidden | QDir::System);
while (it.hasNext()) {
it.next();
const QFileInfo &info = it.fileInfo();
const QString srcFilePath = info.absoluteFilePath();
const QString dstFilePath = targetPath + QDir::separator() + info.fileName();
if (info.isDir()) {
if (!copyDirectoryRecursively(srcFilePath, dstFilePath)) {
return false;
}
} else {
QFile::remove(dstFilePath);
if (!QFile::copy(srcFilePath, dstFilePath)) {
qWarning() << "Failed to copy file:" << srcFilePath << "to" << dstFilePath;
return false;
}
}
}
return true;
}
// 在prepareCompressAliasEntries中添加源文件存在性检查
bool MainWindow::prepareCompressAliasEntries(QList<FileEntry> &listEntry) {
m_needCleanupCompressAlias = false;
m_strCompressAliasRoot.clear();
bool hasAlias = false;
QString aliasRoot;
for (FileEntry &entry : listEntry) {
if (entry.strAlias.isEmpty() || entry.strFullPath.isEmpty()) {
continue;
}
// 检查源文件是否存在
if (!QFileInfo::exists(entry.strFullPath)) {
showWarningDialog(tr("Source file \"%1\" does not exist").arg(entry.strFullPath));
return false;
}
if (!hasAlias) {
// 使用QTemporaryDir管理临时目录
m_tempDir.reset(new QTemporaryDir(TEMPPATH + QDir::separator() + m_strProcessID));
if (!m_tempDir->isValid()) {
showWarningDialog(tr("Failed to create temporary directory, please check and try again."));
return false;
}
aliasRoot = m_tempDir->path() + QDir::separator() + QUuid::createUuid().toString(QUuid::WithoutBraces);
if (!QDir().mkpath(aliasRoot)) {
showWarningDialog(tr("Failed to create temporary directory, please check and try again."));
return false;
}
}
// 其余代码保持不变...
}
return true;
}
#include <QScopedPointer>
#include <QTemporaryDir>在private成员中添加: QScopedPointer<QTemporaryDir> m_tempDir;这些改进将使代码更健壮、更安全,同时提供更好的错误处理和资源管理。 |
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- Consider using QTemporaryDir for your alias root instead of manually generating and cleaning up the temp directory, which will simplify lifecycle management and avoid orphaned dirs.
- The multiple scattered calls to cleanupCompressAliasEntries could be replaced with an RAII guard or a single ‘finally’‐style handler to ensure cleanup always happens without duplicating calls.
- Since copyDirectoryRecursively can take time on large file trees, you might want to move it off the UI thread or add progress feedback to prevent UI freezing during the copy step.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using QTemporaryDir for your alias root instead of manually generating and cleaning up the temp directory, which will simplify lifecycle management and avoid orphaned dirs.
- The multiple scattered calls to cleanupCompressAliasEntries could be replaced with an RAII guard or a single ‘finally’‐style handler to ensure cleanup always happens without duplicating calls.
- Since copyDirectoryRecursively can take time on large file trees, you might want to move it off the UI thread or add progress feedback to prevent UI freezing during the copy step.
## Individual Comments
### Comment 1
<location> `src/source/mainwindow.cpp:2424-2427` </location>
<code_context>
+ }
+
+ QDir aliasRoot(m_strCompressAliasRoot);
+ if (aliasRoot.exists()) {
+ aliasRoot.removeRecursively();
+ }
+ m_strCompressAliasRoot.clear();
</code_context>
<issue_to_address>
**suggestion (bug_risk):** removeRecursively may fail silently if files are locked or permissions are insufficient.
Check the result of removeRecursively and handle failures, such as logging or retrying, to prevent leftover files.
```suggestion
if (aliasRoot.exists()) {
bool removed = aliasRoot.removeRecursively();
if (!removed) {
qWarning() << "Failed to remove alias root directory recursively:" << m_strCompressAliasRoot;
// Optionally, you could retry or set a flag here if needed
}
}
m_strCompressAliasRoot.clear();
```
</issue_to_address>
### Comment 2
<location> `src/source/mainwindow.cpp:2345` </location>
<code_context>
}
}
+bool MainWindow::prepareCompressAliasEntries(QList<FileEntry> &listEntry)
+{
+ m_needCleanupCompressAlias = false;
</code_context>
<issue_to_address>
**issue (complexity):** Consider refactoring manual temp directory and cleanup logic into a single RAII helper class to centralize resource management and reduce code duplication.
```markdown
You can collapse all of that manual‐temp‐dir and cleanup logic into one RAII helper, and move `copyDirectoryRecursively` into a small “file-utils” unit. Here’s one way:
1. Create a helper class in its own files (e.g. CompressAliasHelper.h/.cpp):
```cpp
// CompressAliasHelper.h
#pragma once
#include <QTemporaryDir>
#include <QList>
#include "FileEntry.h" // your struct
class CompressAliasHelper {
public:
explicit CompressAliasHelper(const QString &processId);
~CompressAliasHelper(); // auto-cleans on destruction
bool prepare(QList<FileEntry> &entries);
private:
QTemporaryDir m_tempRoot;
QString m_uuidRoot; // e.g. /tmp/.../compress_alias/UUID
bool makeTarget(const QString &sub, QString &outPath);
};
```
```cpp
// CompressAliasHelper.cpp
#include "CompressAliasHelper.h"
#include <QDirIterator>
#include <QUuid>
#include <QFile>
#include <QDir>
#include <QDebug>
CompressAliasHelper::CompressAliasHelper(const QString &pid)
: m_tempRoot(QDir::tempPath() + QDir::separator() + pid + QDir::separator() + "compress_alias")
{
if (m_tempRoot.isValid()) {
m_uuidRoot = m_tempRoot.path() + QDir::separator()
+ QUuid::createUuid().toString(QUuid::WithoutBraces);
QDir().mkpath(m_uuidRoot);
}
}
CompressAliasHelper::~CompressAliasHelper() = default;
static bool copyDirectoryRecursively(const QString &src, const QString &dst) {
QDir source(src);
if (!source.exists()) return false;
QDir target;
if (!target.mkpath(dst)) return false;
for (auto &info : source.entryInfoList(
QDir::NoDotAndDotDot|QDir::AllDirs|QDir::Files|QDir::Hidden|QDir::System)) {
QString to = dst + QDir::separator() + info.fileName();
if (info.isDir()) {
if (!copyDirectoryRecursively(info.absoluteFilePath(), to))
return false;
} else {
QFile::remove(to);
if (!QFile::copy(info.absoluteFilePath(), to))
return false;
}
}
return true;
}
bool CompressAliasHelper::makeTarget(const QString &sub, QString &outPath) {
outPath = m_uuidRoot + QDir::separator() + sub;
return QDir().mkpath(QFileInfo(outPath).path());
}
bool CompressAliasHelper::prepare(QList<FileEntry> &entries) {
if (!m_tempRoot.isValid()) return false;
bool any = false;
for (auto &e : entries) {
if (e.strAlias.isEmpty()) continue;
QString tgt;
if (!makeTarget(e.strAlias, tgt)) return false;
bool ok = e.isDirectory
? copyDirectoryRecursively(e.strFullPath, tgt)
: QFile::copy(e.strFullPath, tgt);
if (!ok) return false;
e.strFullPath = tgt;
e.strAlias.clear();
any = true;
}
return true;
}
```
2. In `MainWindow`, simply:
```cpp
void MainWindow::onStartCompress() {
QList<FileEntry> files = m_pCompressPage->getEntrys();
CompressAliasHelper alias(m_strProcessID);
if (!alias.prepare(files)) {
showWarningDialog(tr("Failed to prepare renamed items"));
return;
}
// … now use `files` and let alias’s destructor auto‐clean…
}
```
3. Remove all the scattered `cleanupCompressAliasEntries()` calls.
4. Move the free `copyDirectoryRecursively` into your new `CompressAliasHelper.cpp` (or a shared `FileUtils.cpp`).
This preserves behavior but removes:
- manual `m_needCleanupCompressAlias` flags
- repeated `cleanupCompressAliasEntries()` calls
- deep nesting and duplication in `MainWindow`
- keeps temp dirs auto‐removed via `QTemporaryDir`’s RAII destructor.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
lzwind
approved these changes
Nov 21, 2025
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: GongHeng2017, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Contributor
Author
|
/merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Log: fix bug
Bug: https://pms.uniontech.com/bug-view-340081.html
Summary by Sourcery
Handle renamed files during compression by copying aliased entries into real temporary files before archiving and cleaning them up afterwards
Bug Fixes:
Enhancements: