Skip to content

Commit

Permalink
fix: 手机MTP内部,拖拽文件到压缩文件图标上,压缩完成后,压缩文件变为tmp后缀文件
Browse files Browse the repository at this point in the history
mtp内部重命名失败,建立临时文件copy过去

Log: mtp内部重命名失败,建立临时文件copy过去
Bug: https://pms.uniontech.com/bug-view-200113.html
  • Loading branch information
qingfuliu authored and deepin-bot[bot] committed Jan 5, 2024
1 parent 7a9d090 commit 213c948
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
39 changes: 37 additions & 2 deletions 3rdparty/interface/archiveinterface/cliinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include <QDir>
#include <QRegularExpression>
#include <QUrl>
#include <QScopedPointer>
#include <QTemporaryDir>

#include "common.h"
#include <linux/limits.h>

Expand Down Expand Up @@ -383,8 +386,30 @@ PluginFinishType CliInterface::addFiles(const QList<FileEntry> &files, const Com
}
}
}
// 压缩命令的参数
QStringList arguments = m_cliProps->addArgs(m_strArchiveName,

//判断是否是在以mtp方式挂载的目录中进行压缩.建立临时文件
QString temp_archiveName("");
QScopedPointer<QTemporaryDir> temp_dir;
if (IsMtpFileOrDirectory(m_strArchiveName)) {
temp_dir.reset(new QTemporaryDir);
temp_dir->setAutoRemove(true);
qInfo()<< "mtp 挂载压缩,建立临时文件夹:" << temp_dir->path();
temp_archiveName = temp_dir->path() + QDir::separator() + QFileInfo(m_strArchiveName).fileName();
//如果文件已经存在了,则为追加操作,move过去
if(QFileInfo(m_strArchiveName).exists()){
QStringList args_list;
args_list << m_strArchiveName << temp_archiveName;
QProcess mover;
ret = 0 == mover.execute("mv",args_list);
ret = mover.exitCode() == QProcess::NormalExit;
if (!ret) {
qInfo() << "建立临时文件失败!";
return PFT_Error;
}
}
}
// 压缩命令的参数,在mtp中进行压缩的时候,先放在临时文件 temp_archiveName 中,最后再mv过去
QStringList arguments = m_cliProps->addArgs(temp_archiveName.isEmpty() ? m_strArchiveName:temp_archiveName,
fileList,
password,
options.bHeaderEncryption,
Expand All @@ -404,6 +429,16 @@ PluginFinishType CliInterface::addFiles(const QList<FileEntry> &files, const Com
} else {
ret = runProcess(m_cliProps->property("addProgram").toString(), arguments);
}

if (ret && !temp_archiveName.isEmpty()) {
qInfo()<< "mtp 压缩完成,现在开始移动";
QStringList args_list;
args_list<< temp_archiveName << m_strArchiveName;
QProcess mover;
ret = 0 == mover.execute("mv",args_list);
ret = mover.exitCode() == QProcess::NormalExit;
qInfo()<< "mtp 移动成功? " << ret;
}
//删除临时目录
for(QTemporaryDir *tmdDir: lstTmpDir) {
delete tmdDir;
Expand Down
3 changes: 3 additions & 0 deletions 3rdparty/interface/archiveinterface/cliinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class CliInterface : public ReadWriteArchiveInterface

// ReadWriteArchiveInterface interface
public:
/**
* @brief note:在mtp中压缩的时候,需要先建立一个临时目录,在该临时目录中压缩,压缩完毕后move到正确的位置
*/
PluginFinishType addFiles(const QList<FileEntry> &files, const CompressOptions &options) override;
PluginFinishType moveFiles(const QList<FileEntry> &files, const CompressOptions &options) override;
PluginFinishType copyFiles(const QList<FileEntry> &files, const CompressOptions &options) override;
Expand Down
6 changes: 6 additions & 0 deletions 3rdparty/interface/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,9 @@ bool Common::findDlnfsPath(const QString &target, Compare func)
if (iter) mnt_free_iter(iter);
return false;
}


bool IsMtpFileOrDirectory(QString path) noexcept {
const static QRegExp regexp("((/run/user/[0-9]+/gvfs/mtp:)|(/root/.gvfs/mtp:)).+");
return regexp.exactMatch(path);
}
7 changes: 7 additions & 0 deletions 3rdparty/interface/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ class Common: public QObject
bool findDlnfsPath(const QString &target, Compare func);
};

/**
* 判断文件夹或文件夹是否是由mtp挂载的:
* /run/user/$UID/gvfs/mtp:xxxxxxxxx/xxx
* /root/.gvfs/mtp:xxxxxxxxx/xxx
*/
bool IsMtpFileOrDirectory(QString path) noexcept;

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,10 @@
#include <QProcess>

#include <archive_entry.h>

#include "common.h"
// 300M
#define MB300 314572800 /*(300*1024*1024)*/

/**
* 判断文件夹或文件夹是否是由mtp挂载的:
* /run/user/$UID/gvfs/mtp:xxxxxxxxx/xxx
* /root/.gvfs/mtp:xxxxxxxxx/xxx
*/
static bool IsMtpFileOrDirectory(QString path) noexcept {
const static QRegExp regexp("((/run/user/[0-9]+/gvfs/mtp:)|(/root/.gvfs/mtp:)).+");
return regexp.exactMatch(path);
}

ReadWriteLibarchivePluginFactory::ReadWriteLibarchivePluginFactory()
{
registerPlugin<ReadWriteLibarchivePlugin>();
Expand Down

0 comments on commit 213c948

Please sign in to comment.