qt: Support writing signed/encrypted archives directly to a file

* lang/qt/src/encryptarchivejob.cpp, lang/qt/src/encryptarchivejob.h
(EncryptArchiveJob): Add member functions setRecipients, recipients,
setInputPaths, inputPaths, setOutputFile, outputFile,
setEncryptionFlags, encryptionFlags.
* lang/qt/src/encryptarchivejob_p.h (EncryptArchiveJobPrivate): Add
members m_recipients, m_inputPaths, m_outputFilePath, m_encryptionFlags.
* lang/qt/src/qgpgmeencryptarchivejob.cpp (encrypt): Move creation of
outdata to encrypt_to_io_device.
(encrypt_to_io_device, encrypt_to_filename): New.
(QGpgMEEncryptArchiveJob::start): Use encrypt_to_io_device instead of
encrypt.
(QGpgMEEncryptArchiveJobPrivate::startIt): Start the job with the
values from member variables.
* lang/qt/src/qgpgmesignarchivejob.cpp (sign): Move creation of
outdata to sign_to_io_device.
(sign_to_io_device, sign_to_filename): New.
(QGpgMESignArchiveJob::start): Use sign_to_io_device instead of sign.
(QGpgMESignArchiveJobPrivate::startIt): Start the job with the
values from member variables.
* lang/qt/src/qgpgmesignencryptarchivejob.cpp (sign_encrypt): Move
creation of outdata to sign_encrypt_to_io_device.
(sign_encrypt_to_io_device, sign_encrypt_to_filename): New.
(QGpgMESignEncryptArchiveJob::start): Use sign_encrypt_to_io_device
instead of sign_encrypt.
(QGpgMESignEncryptArchiveJobPrivate::startIt): Start the job with the
values from member variables.
* lang/qt/src/signarchivejob.cpp, lang/qt/src/signarchivejob.h
(SignArchiveJob): Add member functions setSigner, signers,
setInputPaths, inputPaths, setOutputFile, outputFile.
* lang/qt/src/signarchivejob_p.h (SignArchiveJobPrivate): Add
members m_signers, m_inputPaths, m_outputFilePath.
* lang/qt/src/signencryptarchivejob.cpp,
lang/qt/src/signencryptarchivejob.h (SignEncryptArchiveJob): Add
member functions setSigner, signers, setRecipients, recipients,
setInputPaths, inputPaths, setOutputFile, outputFile,
setEncryptionFlags, encryptionFlags.
* lang/qt/src/signencryptarchivejob_p.h (SignEncryptArchiveJobPrivate):
Add members m_signers, m_recipients, m_inputPaths, m_outputFilePath,
m_encryptionFlags.

* lang/qt/tests/run-encryptarchivejob.cpp (createOutput): Remove.
(checkOutputFilePath): New.
(main): Create file output writing to stdout if no archive name (or "-")
is given. Exit if file with given archive name already exists. Make
the jobs write the created archive directly to the given archive name.
* lang/qt/tests/run-signarchivejob.cpp (createOutput): Remove.
(checkOutputFilePath): New.
(main): Create file output writing to stdout if no archive name (or "-")
is given. Exit if file with given archive name already exists. Make
the jobs write the created archive directly to the given archive name.
--

This makes it possible to tell gpgtar to write the created archive
directly to a specified file bypassing GpgME's Data IO.

GnuPG-bug-id: 6530
This commit is contained in:
Ingo Klöcker 2023-06-19 17:52:30 +02:00
parent c38b620039
commit e608315392
No known key found for this signature in database
GPG Key ID: F5A5D1692277A1E9
15 changed files with 535 additions and 94 deletions

43
NEWS
View File

@ -1,18 +1,49 @@
Noteworthy changes in version 1.21.0 (unreleased) Noteworthy changes in version 1.21.0 (unreleased)
------------------------------------------------- -------------------------------------------------
* Qt Jobs working with QIODeviceDataProvider now properly * Extended gpgme_op_encrypt*, gpgme_op_encrypt_sign*, and
handle input-size hints and progress for files larger. gpgme_op_sign* to allow writing the output directly to a
2^32 bytes in 32 bit builds. [T6534] file. [T6530]
* Error::isCanceled now also returns true for error code * qt: Allow writing the created archives directly to a
file. [T6530]
* qt: Qt Jobs working with QIODeviceDataProvider now properly
handle input-size hints and progress for files larger.
2^32 bytes in 32 bit builds. [T6534]
* cpp: Error::isCanceled now also returns true for error code
GPG_ERR_FULLY_CANCELED. [T6510] GPG_ERR_FULLY_CANCELED. [T6510]
* Interface changes relative to the 1.20.0 release: * Interface changes relative to the 1.20.0 release:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cpp: Data::setFlag NEW. cpp: Data::setFlag NEW.
cpp: Data::setSizeHint NEW. cpp: Data::setSizeHint NEW.
qt: Job::startIt NEW. qt: Job::startIt NEW.
qt: EncryptArchiveJob::setRecipients NEW.
qt: EncryptArchiveJob::recipients NEW.
qt: EncryptArchiveJob::setInputPaths NEW.
qt: EncryptArchiveJob::inputPaths NEW.
qt: EncryptArchiveJob::setOutputFile NEW.
qt: EncryptArchiveJob::outputFile NEW.
qt: EncryptArchiveJob::setEncryptionFlags NEW.
qt: EncryptArchiveJob::encryptionFlags NEW.
qt: SignArchiveJob::setSigners NEW.
qt: SignArchiveJob::signers NEW.
qt: SignArchiveJob::setInputPaths NEW.
qt: SignArchiveJob::inputPaths NEW.
qt: SignArchiveJob::setOutputFile NEW.
qt: SignArchiveJob::outputFile NEW.
qt: SignEncryptArchiveJob::setSigners NEW.
qt: SignEncryptArchiveJob::signers NEW.
qt: SignEncryptArchiveJob::setRecipients NEW.
qt: SignEncryptArchiveJob::recipients NEW.
qt: SignEncryptArchiveJob::setInputPaths NEW.
qt: SignEncryptArchiveJob::inputPaths NEW.
qt: SignEncryptArchiveJob::setOutputFile NEW.
qt: SignEncryptArchiveJob::outputFile NEW.
qt: SignEncryptArchiveJob::setEncryptionFlags NEW.
qt: SignEncryptArchiveJob::encryptionFlags NEW.
Noteworthy changes in version 1.20.0 (2023-04-20) Noteworthy changes in version 1.20.0 (2023-04-20)
------------------------------------------------- -------------------------------------------------

View File

@ -56,6 +56,54 @@ bool EncryptArchiveJob::isSupported()
return (gpgVersion >= "2.4.1") || (gpgVersion >= "2.2.42" && gpgVersion < "2.3.0"); return (gpgVersion >= "2.4.1") || (gpgVersion >= "2.2.42" && gpgVersion < "2.3.0");
} }
void EncryptArchiveJob::setRecipients(const std::vector<GpgME::Key> &recipients)
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
d->m_recipients = recipients;
}
std::vector<GpgME::Key> EncryptArchiveJob::recipients() const
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
return d->m_recipients;
}
void EncryptArchiveJob::setInputPaths(const std::vector<QString> &paths)
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
d->m_inputPaths = paths;
}
std::vector<QString> EncryptArchiveJob::inputPaths() const
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
return d->m_inputPaths;
}
void EncryptArchiveJob::setOutputFile(const QString &path)
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
d->m_outputFilePath = path;
}
QString EncryptArchiveJob::outputFile() const
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
return d->m_outputFilePath;
}
void EncryptArchiveJob::setEncryptionFlags(GpgME::Context::EncryptionFlags flags)
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
d->m_encryptionFlags = static_cast<GpgME::Context::EncryptionFlags>(flags | GpgME::Context::EncryptArchive);
}
GpgME::Context::EncryptionFlags EncryptArchiveJob::encryptionFlags() const
{
auto d = jobPrivate<EncryptArchiveJobPrivate>(this);
return d->m_encryptionFlags;
}
void EncryptArchiveJob::setBaseDirectory(const QString &baseDirectory) void EncryptArchiveJob::setBaseDirectory(const QString &baseDirectory)
{ {
auto d = jobPrivate<EncryptArchiveJobPrivate>(this); auto d = jobPrivate<EncryptArchiveJobPrivate>(this);

View File

@ -63,6 +63,53 @@ public:
static bool isSupported(); static bool isSupported();
/**
* Sets the keys to use for encrypting the archive.
*
* Used if the job is started with startIt().
*/
void setRecipients(const std::vector<GpgME::Key> &recipients);
std::vector<GpgME::Key> recipients() const;
/**
* Sets the paths of the files and folders to put into the archive.
*
* If base directory is set, then the paths must be relative to the
* base directory.
*
* Used if the job is started with startIt().
*/
void setInputPaths(const std::vector<QString> &paths);
std::vector<QString> inputPaths() const;
/**
* Sets the path of the file to write the created archive to.
*
* If \a path is a relative path and base directory is set, then the
* path is interpreted relative to the base directory.
*
* Used if the job is started with startIt().
*
* \note If a file with this path exists, then the job will fail, i.e. you
* need to delete an existing file that shall be overwritten before you
* start the job.
*/
void setOutputFile(const QString &path);
QString outputFile() const;
/**
* Sets the flags to use for encryption. Defaults to \c EncryptArchive.
* The \c EncryptArchive flag is always assumed set for this job.
*
* Used if the job is started with startIt().
*/
void setEncryptionFlags(GpgME::Context::EncryptionFlags flags);
GpgME::Context::EncryptionFlags encryptionFlags() const;
/**
* Sets the base directory for the relative paths of the input files and
* the output file.
*/
void setBaseDirectory(const QString &baseDirectory); void setBaseDirectory(const QString &baseDirectory);
QString baseDirectory() const; QString baseDirectory() const;

View File

@ -41,7 +41,11 @@ namespace QGpgME
struct EncryptArchiveJobPrivate : public JobPrivate struct EncryptArchiveJobPrivate : public JobPrivate
{ {
std::vector<GpgME::Key> m_recipients;
std::vector<QString> m_inputPaths;
QString m_outputFilePath;
QString m_baseDirectory; QString m_baseDirectory;
GpgME::Context::EncryptionFlags m_encryptionFlags = GpgME::Context::EncryptArchive;
}; };
} }

View File

@ -44,6 +44,8 @@
#include "encryptarchivejob_p.h" #include "encryptarchivejob_p.h"
#include "filelistdataprovider.h" #include "filelistdataprovider.h"
#include <QFile>
#include <data.h> #include <data.h>
using namespace QGpgME; using namespace QGpgME;
@ -65,11 +67,7 @@ public:
~QGpgMEEncryptArchiveJobPrivate() override = default; ~QGpgMEEncryptArchiveJobPrivate() override = default;
private: private:
GpgME::Error startIt() override GpgME::Error startIt() override;
{
Q_ASSERT(!"Not supported by this Job class.");
return Error::fromCode(GPG_ERR_NOT_SUPPORTED);
}
void startNow() override void startNow() override
{ {
@ -90,25 +88,18 @@ QGpgMEEncryptArchiveJob::QGpgMEEncryptArchiveJob(Context *context)
} }
static QGpgMEEncryptArchiveJob::result_type encrypt(Context *ctx, static QGpgMEEncryptArchiveJob::result_type encrypt(Context *ctx,
QThread *thread,
const std::vector<Key> &recipients, const std::vector<Key> &recipients,
const std::vector<QString> &paths, const std::vector<QString> &paths,
const std::weak_ptr<QIODevice> &cipherText_, GpgME::Data &outdata,
Context::EncryptionFlags flags, Context::EncryptionFlags flags,
const QString &baseDirectory) const QString &baseDirectory)
{ {
const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
const _detail::ToThreadMover ctMover(cipherText, thread);
QGpgME::FileListDataProvider in{paths}; QGpgME::FileListDataProvider in{paths};
Data indata(&in); Data indata(&in);
if (!baseDirectory.isEmpty()) { if (!baseDirectory.isEmpty()) {
indata.setFileName(baseDirectory.toStdString()); indata.setFileName(baseDirectory.toStdString());
} }
QGpgME::QIODeviceDataProvider out{cipherText};
Data outdata(&out);
flags = static_cast<Context::EncryptionFlags>(flags | Context::EncryptArchive); flags = static_cast<Context::EncryptionFlags>(flags | Context::EncryptArchive);
const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, flags); const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, flags);
Error ae; Error ae;
@ -116,6 +107,35 @@ static QGpgMEEncryptArchiveJob::result_type encrypt(Context *ctx,
return std::make_tuple(res, log, ae); return std::make_tuple(res, log, ae);
} }
static QGpgMEEncryptArchiveJob::result_type encrypt_to_io_device(Context *ctx,
QThread *thread,
const std::vector<Key> &recipients,
const std::vector<QString> &paths,
const std::weak_ptr<QIODevice> &cipherText_,
Context::EncryptionFlags flags,
const QString &baseDirectory)
{
const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
const _detail::ToThreadMover ctMover(cipherText, thread);
QGpgME::QIODeviceDataProvider out{cipherText};
Data outdata(&out);
return encrypt(ctx, recipients, paths, outdata, flags, baseDirectory);
}
static QGpgMEEncryptArchiveJob::result_type encrypt_to_filename(Context *ctx,
const std::vector<Key> &recipients,
const std::vector<QString> &paths,
const QString &outputFile,
Context::EncryptionFlags flags,
const QString &baseDirectory)
{
Data outdata;
outdata.setFileName(QFile::encodeName(outputFile).constData());
return encrypt(ctx, recipients, paths, outdata, flags, baseDirectory);
}
GpgME::Error QGpgMEEncryptArchiveJob::start(const std::vector<GpgME::Key> &recipients, GpgME::Error QGpgMEEncryptArchiveJob::start(const std::vector<GpgME::Key> &recipients,
const std::vector<QString> &paths, const std::vector<QString> &paths,
const std::shared_ptr<QIODevice> &cipherText, const std::shared_ptr<QIODevice> &cipherText,
@ -125,7 +145,7 @@ GpgME::Error QGpgMEEncryptArchiveJob::start(const std::vector<GpgME::Key> &recip
return Error::fromCode(GPG_ERR_INV_VALUE); return Error::fromCode(GPG_ERR_INV_VALUE);
} }
run(std::bind(&encrypt, run(std::bind(&encrypt_to_io_device,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
recipients, recipients,
@ -137,4 +157,17 @@ GpgME::Error QGpgMEEncryptArchiveJob::start(const std::vector<GpgME::Key> &recip
return {}; return {};
} }
GpgME::Error QGpgMEEncryptArchiveJobPrivate::startIt()
{
if (m_outputFilePath.isEmpty()) {
return Error::fromCode(GPG_ERR_INV_VALUE);
}
q->run([=](Context *ctx) {
return encrypt_to_filename(ctx, m_recipients, m_inputPaths, m_outputFilePath, m_encryptionFlags, m_baseDirectory);
});
return {};
}
#include "qgpgmeencryptarchivejob.moc" #include "qgpgmeencryptarchivejob.moc"

View File

@ -44,6 +44,8 @@
#include "signarchivejob_p.h" #include "signarchivejob_p.h"
#include "filelistdataprovider.h" #include "filelistdataprovider.h"
#include <QFile>
#include <data.h> #include <data.h>
using namespace QGpgME; using namespace QGpgME;
@ -65,11 +67,7 @@ public:
~QGpgMESignArchiveJobPrivate() override = default; ~QGpgMESignArchiveJobPrivate() override = default;
private: private:
GpgME::Error startIt() override GpgME::Error startIt() override;
{
Q_ASSERT(!"Not supported by this Job class.");
return Error::fromCode(GPG_ERR_NOT_SUPPORTED);
}
void startNow() override void startNow() override
{ {
@ -90,24 +88,17 @@ QGpgMESignArchiveJob::QGpgMESignArchiveJob(Context *context)
} }
static QGpgMESignArchiveJob::result_type sign(Context *ctx, static QGpgMESignArchiveJob::result_type sign(Context *ctx,
QThread *thread,
const std::vector<Key> &signers, const std::vector<Key> &signers,
const std::vector<QString> &paths, const std::vector<QString> &paths,
const std::weak_ptr<QIODevice> &output_, GpgME::Data &outdata,
const QString &baseDirectory) const QString &baseDirectory)
{ {
const std::shared_ptr<QIODevice> output = output_.lock();
const _detail::ToThreadMover ctMover(output, thread);
QGpgME::FileListDataProvider in{paths}; QGpgME::FileListDataProvider in{paths};
Data indata(&in); Data indata(&in);
if (!baseDirectory.isEmpty()) { if (!baseDirectory.isEmpty()) {
indata.setFileName(baseDirectory.toStdString()); indata.setFileName(baseDirectory.toStdString());
} }
QGpgME::QIODeviceDataProvider out{output};
Data outdata(&out);
ctx->clearSigningKeys(); ctx->clearSigningKeys();
for (const Key &signer : signers) { for (const Key &signer : signers) {
if (!signer.isNull()) { if (!signer.isNull()) {
@ -123,6 +114,33 @@ static QGpgMESignArchiveJob::result_type sign(Context *ctx,
return std::make_tuple(res, log, ae); return std::make_tuple(res, log, ae);
} }
static QGpgMESignArchiveJob::result_type sign_to_io_device(Context *ctx,
QThread *thread,
const std::vector<Key> &signers,
const std::vector<QString> &paths,
const std::weak_ptr<QIODevice> &output_,
const QString &baseDirectory)
{
const std::shared_ptr<QIODevice> output = output_.lock();
const _detail::ToThreadMover ctMover(output, thread);
QGpgME::QIODeviceDataProvider out{output};
Data outdata(&out);
return sign(ctx, signers, paths, outdata, baseDirectory);
}
static QGpgMESignArchiveJob::result_type sign_to_filename(Context *ctx,
const std::vector<Key> &signers,
const std::vector<QString> &paths,
const QString &outputFile,
const QString &baseDirectory)
{
Data outdata;
outdata.setFileName(QFile::encodeName(outputFile).constData());
return sign(ctx, signers, paths, outdata, baseDirectory);
}
GpgME::Error QGpgMESignArchiveJob::start(const std::vector<GpgME::Key> &signers, GpgME::Error QGpgMESignArchiveJob::start(const std::vector<GpgME::Key> &signers,
const std::vector<QString> &paths, const std::vector<QString> &paths,
const std::shared_ptr<QIODevice> &output) const std::shared_ptr<QIODevice> &output)
@ -131,7 +149,7 @@ GpgME::Error QGpgMESignArchiveJob::start(const std::vector<GpgME::Key> &signers,
return Error::fromCode(GPG_ERR_INV_VALUE); return Error::fromCode(GPG_ERR_INV_VALUE);
} }
run(std::bind(&sign, run(std::bind(&sign_to_io_device,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
signers, signers,
@ -142,4 +160,18 @@ GpgME::Error QGpgMESignArchiveJob::start(const std::vector<GpgME::Key> &signers,
return {}; return {};
} }
GpgME::Error QGpgMESignArchiveJobPrivate::startIt()
{
if (m_outputFilePath.isEmpty()) {
return Error::fromCode(GPG_ERR_INV_VALUE);
}
q->run([=](Context *ctx) {
return sign_to_filename(ctx, m_signers, m_inputPaths, m_outputFilePath, m_baseDirectory);
});
return {};
}
#include "qgpgmesignarchivejob.moc" #include "qgpgmesignarchivejob.moc"

View File

@ -44,14 +44,9 @@
#include "signencryptarchivejob_p.h" #include "signencryptarchivejob_p.h"
#include "filelistdataprovider.h" #include "filelistdataprovider.h"
// #include <context.h> #include <QFile>
#include <data.h> #include <data.h>
// #include <encryptionresult.h>
//
// #include <QBuffer>
// #include <QFileInfo>
//
// #include <cassert>
using namespace QGpgME; using namespace QGpgME;
using namespace GpgME; using namespace GpgME;
@ -72,11 +67,7 @@ public:
~QGpgMESignEncryptArchiveJobPrivate() override = default; ~QGpgMESignEncryptArchiveJobPrivate() override = default;
private: private:
GpgME::Error startIt() override GpgME::Error startIt() override;
{
Q_ASSERT(!"Not supported by this Job class.");
return Error::fromCode(GPG_ERR_NOT_SUPPORTED);
}
void startNow() override void startNow() override
{ {
@ -97,26 +88,19 @@ QGpgMESignEncryptArchiveJob::QGpgMESignEncryptArchiveJob(Context *context)
} }
static QGpgMESignEncryptArchiveJob::result_type sign_encrypt(Context *ctx, static QGpgMESignEncryptArchiveJob::result_type sign_encrypt(Context *ctx,
QThread *thread,
const std::vector<GpgME::Key> &signers, const std::vector<GpgME::Key> &signers,
const std::vector<Key> &recipients, const std::vector<Key> &recipients,
const std::vector<QString> &paths, const std::vector<QString> &paths,
const std::weak_ptr<QIODevice> &cipherText_, GpgME::Data &outdata,
Context::EncryptionFlags encryptionFlags, Context::EncryptionFlags encryptionFlags,
const QString &baseDirectory) const QString &baseDirectory)
{ {
const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
const _detail::ToThreadMover ctMover(cipherText, thread);
QGpgME::FileListDataProvider in{paths}; QGpgME::FileListDataProvider in{paths};
Data indata(&in); Data indata(&in);
if (!baseDirectory.isEmpty()) { if (!baseDirectory.isEmpty()) {
indata.setFileName(baseDirectory.toStdString()); indata.setFileName(baseDirectory.toStdString());
} }
QGpgME::QIODeviceDataProvider out{cipherText};
Data outdata(&out);
ctx->clearSigningKeys(); ctx->clearSigningKeys();
for (const Key &signer : signers) { for (const Key &signer : signers) {
if (!signer.isNull()) { if (!signer.isNull()) {
@ -133,6 +117,37 @@ static QGpgMESignEncryptArchiveJob::result_type sign_encrypt(Context *ctx,
return std::make_tuple(res.first, res.second, log, ae); return std::make_tuple(res.first, res.second, log, ae);
} }
static QGpgMESignEncryptArchiveJob::result_type sign_encrypt_to_io_device(Context *ctx,
QThread *thread,
const std::vector<GpgME::Key> &signers,
const std::vector<Key> &recipients,
const std::vector<QString> &paths,
const std::weak_ptr<QIODevice> &cipherText_,
Context::EncryptionFlags encryptionFlags,
const QString &baseDirectory)
{
const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
const _detail::ToThreadMover ctMover(cipherText, thread);
QGpgME::QIODeviceDataProvider out{cipherText};
Data outdata(&out);
return sign_encrypt(ctx, signers, recipients, paths, outdata, encryptionFlags, baseDirectory);
}
static QGpgMESignEncryptArchiveJob::result_type sign_encrypt_to_filename(Context *ctx,
const std::vector<GpgME::Key> &signers,
const std::vector<Key> &recipients,
const std::vector<QString> &paths,
const QString &outputFile,
Context::EncryptionFlags encryptionFlags,
const QString &baseDirectory)
{
Data outdata;
outdata.setFileName(QFile::encodeName(outputFile).constData());
return sign_encrypt(ctx, signers, recipients, paths, outdata, encryptionFlags, baseDirectory);
}
GpgME::Error QGpgMESignEncryptArchiveJob::start(const std::vector<GpgME::Key> &signers, GpgME::Error QGpgMESignEncryptArchiveJob::start(const std::vector<GpgME::Key> &signers,
const std::vector<GpgME::Key> &recipients, const std::vector<GpgME::Key> &recipients,
const std::vector<QString> &paths, const std::vector<QString> &paths,
@ -143,7 +158,7 @@ GpgME::Error QGpgMESignEncryptArchiveJob::start(const std::vector<GpgME::Key> &s
return Error::fromCode(GPG_ERR_INV_VALUE); return Error::fromCode(GPG_ERR_INV_VALUE);
} }
run(std::bind(&sign_encrypt, run(std::bind(&sign_encrypt_to_io_device,
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
signers, signers,
@ -156,4 +171,17 @@ GpgME::Error QGpgMESignEncryptArchiveJob::start(const std::vector<GpgME::Key> &s
return {}; return {};
} }
GpgME::Error QGpgMESignEncryptArchiveJobPrivate::startIt()
{
if (m_outputFilePath.isEmpty()) {
return Error::fromCode(GPG_ERR_INV_VALUE);
}
q->run([=](Context *ctx) {
return sign_encrypt_to_filename(ctx, m_signers, m_recipients, m_inputPaths, m_outputFilePath, m_encryptionFlags, m_baseDirectory);
});
return {};
}
#include "qgpgmesignencryptarchivejob.moc" #include "qgpgmesignencryptarchivejob.moc"

View File

@ -56,6 +56,42 @@ bool SignArchiveJob::isSupported()
return (gpgVersion >= "2.4.1") || (gpgVersion >= "2.2.42" && gpgVersion < "2.3.0"); return (gpgVersion >= "2.4.1") || (gpgVersion >= "2.2.42" && gpgVersion < "2.3.0");
} }
void SignArchiveJob::setSigners(const std::vector<GpgME::Key> &signers)
{
auto d = jobPrivate<SignArchiveJobPrivate>(this);
d->m_signers = signers;
}
std::vector<GpgME::Key> SignArchiveJob::signers() const
{
auto d = jobPrivate<SignArchiveJobPrivate>(this);
return d->m_signers;
}
void SignArchiveJob::setInputPaths(const std::vector<QString> &paths)
{
auto d = jobPrivate<SignArchiveJobPrivate>(this);
d->m_inputPaths = paths;
}
std::vector<QString> SignArchiveJob::inputPaths() const
{
auto d = jobPrivate<SignArchiveJobPrivate>(this);
return d->m_inputPaths;
}
void SignArchiveJob::setOutputFile(const QString &path)
{
auto d = jobPrivate<SignArchiveJobPrivate>(this);
d->m_outputFilePath = path;
}
QString SignArchiveJob::outputFile() const
{
auto d = jobPrivate<SignArchiveJobPrivate>(this);
return d->m_outputFilePath;
}
void SignArchiveJob::setBaseDirectory(const QString &baseDirectory) void SignArchiveJob::setBaseDirectory(const QString &baseDirectory)
{ {
auto d = jobPrivate<SignArchiveJobPrivate>(this); auto d = jobPrivate<SignArchiveJobPrivate>(this);

View File

@ -63,6 +63,44 @@ public:
static bool isSupported(); static bool isSupported();
/**
* Sets the keys to use for signing the archive.
*
* Used if the job is started with startIt().
*/
void setSigners(const std::vector<GpgME::Key> &signers);
std::vector<GpgME::Key> signers() const;
/**
* Sets the paths of the files and folders to put into the archive.
*
* If base directory is set, then the paths must be relative to the
* base directory.
*
* Used if the job is started with startIt().
*/
void setInputPaths(const std::vector<QString> &paths);
std::vector<QString> inputPaths() const;
/**
* Sets the path of the file to write the created archive to.
*
* If \a path is a relative path and base directory is set, then the
* path is interpreted relative to the base directory.
*
* Used if the job is started with startIt().
*
* \note If a file with this path exists, then the job will fail, i.e. you
* need to delete an existing file that shall be overwritten before you
* start the job.
*/
void setOutputFile(const QString &path);
QString outputFile() const;
/**
* Sets the base directory for the relative paths of the input files and
* the output file.
*/
void setBaseDirectory(const QString &baseDirectory); void setBaseDirectory(const QString &baseDirectory);
QString baseDirectory() const; QString baseDirectory() const;

View File

@ -41,6 +41,9 @@ namespace QGpgME
struct SignArchiveJobPrivate : public JobPrivate struct SignArchiveJobPrivate : public JobPrivate
{ {
std::vector<GpgME::Key> m_signers;
std::vector<QString> m_inputPaths;
QString m_outputFilePath;
QString m_baseDirectory; QString m_baseDirectory;
}; };

View File

@ -56,6 +56,66 @@ bool SignEncryptArchiveJob::isSupported()
return (gpgVersion >= "2.4.1") || (gpgVersion >= "2.2.42" && gpgVersion < "2.3.0"); return (gpgVersion >= "2.4.1") || (gpgVersion >= "2.2.42" && gpgVersion < "2.3.0");
} }
void SignEncryptArchiveJob::setSigners(const std::vector<GpgME::Key> &signers)
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
d->m_signers = signers;
}
std::vector<GpgME::Key> SignEncryptArchiveJob::signers() const
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
return d->m_signers;
}
void SignEncryptArchiveJob::setRecipients(const std::vector<GpgME::Key> &recipients)
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
d->m_recipients = recipients;
}
std::vector<GpgME::Key> SignEncryptArchiveJob::recipients() const
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
return d->m_recipients;
}
void SignEncryptArchiveJob::setInputPaths(const std::vector<QString> &paths)
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
d->m_inputPaths = paths;
}
std::vector<QString> SignEncryptArchiveJob::inputPaths() const
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
return d->m_inputPaths;
}
void SignEncryptArchiveJob::setOutputFile(const QString &path)
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
d->m_outputFilePath = path;
}
QString SignEncryptArchiveJob::outputFile() const
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
return d->m_outputFilePath;
}
void SignEncryptArchiveJob::setEncryptionFlags(GpgME::Context::EncryptionFlags flags)
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
d->m_encryptionFlags = static_cast<GpgME::Context::EncryptionFlags>(flags | GpgME::Context::EncryptArchive);
}
GpgME::Context::EncryptionFlags SignEncryptArchiveJob::encryptionFlags() const
{
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);
return d->m_encryptionFlags;
}
void SignEncryptArchiveJob::setBaseDirectory(const QString &baseDirectory) void SignEncryptArchiveJob::setBaseDirectory(const QString &baseDirectory)
{ {
auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this); auto d = jobPrivate<SignEncryptArchiveJobPrivate>(this);

View File

@ -63,6 +63,61 @@ public:
static bool isSupported(); static bool isSupported();
/**
* Sets the keys to use for signing the archive.
*
* Used if the job is started with startIt().
*/
void setSigners(const std::vector<GpgME::Key> &signers);
std::vector<GpgME::Key> signers() const;
/**
* Sets the keys to use for encrypting the archive.
*
* Used if the job is started with startIt().
*/
void setRecipients(const std::vector<GpgME::Key> &recipients);
std::vector<GpgME::Key> recipients() const;
/**
* Sets the paths of the files and folders to put into the archive.
*
* If base directory is set, then the paths must be relative to the
* base directory.
*
* Used if the job is started with startIt().
*/
void setInputPaths(const std::vector<QString> &paths);
std::vector<QString> inputPaths() const;
/**
* Sets the path of the file to write the created archive to.
*
* If \a path is a relative path and base directory is set, then the
* path is interpreted relative to the base directory.
*
* Used if the job is started with startIt().
*
* \note If a file with this path exists, then the job will fail, i.e. you
* need to delete an existing file that shall be overwritten before you
* start the job.
*/
void setOutputFile(const QString &path);
QString outputFile() const;
/**
* Sets the flags to use for encryption. Defaults to \c EncryptArchive.
* The \c EncryptArchive flag is always assumed set for this job.
*
* Used if the job is started with startIt().
*/
void setEncryptionFlags(GpgME::Context::EncryptionFlags flags);
GpgME::Context::EncryptionFlags encryptionFlags() const;
/**
* Sets the base directory for the relative paths of the input files and
* the output file.
*/
void setBaseDirectory(const QString &baseDirectory); void setBaseDirectory(const QString &baseDirectory);
QString baseDirectory() const; QString baseDirectory() const;

View File

@ -41,7 +41,12 @@ namespace QGpgME
struct SignEncryptArchiveJobPrivate : public JobPrivate struct SignEncryptArchiveJobPrivate : public JobPrivate
{ {
std::vector<GpgME::Key> m_signers;
std::vector<GpgME::Key> m_recipients;
std::vector<QString> m_inputPaths;
QString m_outputFilePath;
QString m_baseDirectory; QString m_baseDirectory;
GpgME::Context::EncryptionFlags m_encryptionFlags = GpgME::Context::EncryptArchive;
}; };
} }

View File

@ -41,7 +41,9 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo>
#include <context.h> #include <context.h>
#include <encryptionresult.h> #include <encryptionresult.h>
@ -95,23 +97,14 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
return options; return options;
} }
std::shared_ptr<QIODevice> createOutput(const QString &fileName) QString checkOutputFilePath(const QString &fileName, const QString &baseDirectory)
{ {
std::shared_ptr<QFile> output; const QFileInfo fi{QDir{baseDirectory}, fileName};
if (fi.exists()) {
if (fileName.isEmpty()) { qCritical() << "File" << fi.filePath() << "exists. Bailing out.";
output.reset(new QFile); return {};
output->open(stdout, QIODevice::WriteOnly);
} else {
if (QFile::exists(fileName)) {
qCritical() << "File" << fileName << "exists. Bailing out.";
} else {
output.reset(new QFile{fileName});
output->open(QIODevice::WriteOnly);
}
} }
return fileName;
return output;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -129,9 +122,16 @@ int main(int argc, char **argv)
return 1; return 1;
} }
auto output = createOutput(options.archiveName); std::shared_ptr<QFile> output;
if (!output) { QString outputFilePath;
return 1; if (options.archiveName.isEmpty() || options.archiveName == QLatin1String{"-"}) {
output.reset(new QFile);
output->open(stdout, QIODevice::WriteOnly);
} else {
outputFilePath = checkOutputFilePath(options.archiveName, options.baseDirectory);
if (outputFilePath.isEmpty()) {
return 1;
}
} }
if (options.sign) { if (options.sign) {
@ -148,7 +148,14 @@ int main(int argc, char **argv)
qApp->quit(); qApp->quit();
}); });
const auto err = job->start({}, {}, options.filesAndDirectories, output, GpgME::Context::None); GpgME::Error err;
if (output) {
err = job->start({}, {}, options.filesAndDirectories, output, GpgME::Context::None);
} else {
job->setInputPaths(options.filesAndDirectories);
job->setOutputFile(outputFilePath);
err = job->startIt();
}
if (err) { if (err) {
std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl; std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl;
return 1; return 1;
@ -166,7 +173,14 @@ int main(int argc, char **argv)
qApp->quit(); qApp->quit();
}); });
const auto err = job->start({}, options.filesAndDirectories, output, GpgME::Context::None); GpgME::Error err;
if (output) {
err = job->start({}, options.filesAndDirectories, output, GpgME::Context::None);
} else {
job->setInputPaths(options.filesAndDirectories);
job->setOutputFile(outputFilePath);
err = job->startIt();
}
if (err) { if (err) {
std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl; std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl;
return 1; return 1;

View File

@ -40,7 +40,9 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo>
#include <context.h> #include <context.h>
#include <signingresult.h> #include <signingresult.h>
@ -90,23 +92,14 @@ CommandLineOptions parseCommandLine(const QStringList &arguments)
return options; return options;
} }
std::shared_ptr<QIODevice> createOutput(const QString &fileName) QString checkOutputFilePath(const QString &fileName, const QString &baseDirectory)
{ {
std::shared_ptr<QFile> output; const QFileInfo fi{QDir{baseDirectory}, fileName};
if (fi.exists()) {
if (fileName.isEmpty()) { qCritical() << "File" << fi.filePath() << "exists. Bailing out.";
output.reset(new QFile); return {};
output->open(stdout, QIODevice::WriteOnly);
} else {
if (QFile::exists(fileName)) {
qCritical() << "File" << fileName << "exists. Bailing out.";
} else {
output.reset(new QFile{fileName});
output->open(QIODevice::WriteOnly);
}
} }
return fileName;
return output;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -123,9 +116,16 @@ int main(int argc, char **argv)
return 1; return 1;
} }
auto output = createOutput(options.archiveName); std::shared_ptr<QFile> output;
if (!output) { QString outputFilePath;
return 1; if (options.archiveName.isEmpty() || options.archiveName == QLatin1String{"-"}) {
output.reset(new QFile);
output->open(stdout, QIODevice::WriteOnly);
} else {
outputFilePath = checkOutputFilePath(options.archiveName, options.baseDirectory);
if (outputFilePath.isEmpty()) {
return 1;
}
} }
auto job = QGpgME::openpgp()->signArchiveJob(options.armor); auto job = QGpgME::openpgp()->signArchiveJob(options.armor);
@ -140,7 +140,14 @@ int main(int argc, char **argv)
qApp->quit(); qApp->quit();
}); });
const auto err = job->start({}, options.filesAndDirectories, output); GpgME::Error err;
if (output) {
err = job->start({}, options.filesAndDirectories, output);
} else {
job->setInputPaths(options.filesAndDirectories);
job->setOutputFile(outputFilePath);
err = job->startIt();
}
if (err) { if (err) {
std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl; std::cerr << "Error: Starting the job failed: " << err.asString() << std::endl;
return 1; return 1;