Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libcommhistory] Fix race when sending a temporary file with MMS. JB#56314 #3

Merged
merged 1 commit into from Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion declarative/src/mmshelper.cpp
Expand Up @@ -38,6 +38,8 @@
#include <QMimeDatabase>
#include <QDebug>

#include <unistd.h>

struct MmsPart
{
QString fileName;
Expand Down Expand Up @@ -198,6 +200,24 @@ static QString createTemporaryTextFile(const QString &dir, const QMimeDatabase &
return file.fileName();
}

static QString createTemporaryFile(const QString &dir, const QString &source)
{
QFileInfo fileInfo(source);
QString targetFile(dir + '/' + fileInfo.fileName());
// try to create a hard link and if that fails fall back to file copy
int result = link(source.toUtf8().constData(), targetFile.toUtf8().constData());

if (result < 0) {
QFile file(source);
if (!file.copy(targetFile)) {
qWarning() << "Failed to copy file for MMS" << source;
return QString();
}
}

return targetFile;
}

bool MmsHelper::sendMessage(const QStringList &to, const QStringList &cc, const QStringList &bcc, const QString &subject, const QVariantList &parts)
{
return sendMessage(QString(), to, cc, bcc, subject, parts);
Expand Down Expand Up @@ -262,7 +282,7 @@ QDBusPendingCallWatcher *MmsHelper::sendMessage(const TempDir &tempDir,
return NULL;
}
} else if (part.fileName.startsWith("file://")) {
part.fileName = QUrl(part.fileName).toLocalFile();
part.fileName = createTemporaryFile(tempDir.path(), QUrl(part.fileName).toLocalFile());
}

if (part.contentType.isEmpty()) {
Expand All @@ -283,6 +303,8 @@ QDBusPendingCallWatcher *MmsHelper::sendMessage(const TempDir &tempDir,

params << to << cc << bcc << subject << QVariant::fromValue(outParts);

// TODO: ideally there would be sendMessageFd in the handler interface, as there is in mms-engine,
// to avoid some temporary files
QDBusMessage call(QDBusMessage::createMethodCall(MMS_HANDLER_SERVICE,
MMS_HANDLER_PATH, MMS_HANDLER_INTERFACE, "sendMessage"));
call.setArguments(params);
Expand Down