From fd3582c395ee81916cbeecb24640ba13f77b85cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reimar=20D=C3=B6ffinger?= Date: Thu, 30 Aug 2018 01:18:52 +0200 Subject: Add support for using Linux binaries on Windows. Uses WSL, selected by prefixing the binaries with "wsl ". --- src/configdialog.cpp | 4 ++-- src/executor.cpp | 22 ++++++++++++++++++---- src/imitatepass.cpp | 46 ++++++++++++++++++++++++++++++---------------- src/pass.cpp | 3 ++- src/util.cpp | 4 +++- 5 files changed, 55 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/configdialog.cpp b/src/configdialog.cpp index ae07ad2c..d2d88bb4 100644 --- a/src/configdialog.cpp +++ b/src/configdialog.cpp @@ -538,7 +538,7 @@ void ConfigDialog::wizard() { QString gpg = ui->gpgPath->text(); // QString gpg = mainWindow->getGpgExecutable(); - if (!QFile(gpg).exists()) { + if (!gpg.startsWith("wsl ") && !QFile(gpg).exists()) { criticalMessage( tr("GnuPG not found"), tr("Please install GnuPG on your system.
Install " @@ -555,7 +555,7 @@ void ConfigDialog::wizard() { dbg() << names; #endif - if (QFile(gpg).exists() && names.empty()) { + if ((gpg.startsWith("wsl ") || QFile(gpg).exists()) && names.empty()) { KeygenDialog d(this); if (!d.exec()) return; diff --git a/src/executor.cpp b/src/executor.cpp index cd20f11c..e84eec4d 100644 --- a/src/executor.cpp +++ b/src/executor.cpp @@ -31,7 +31,14 @@ void Executor::executeNext() { running = true; if (!i.workingDir.isEmpty()) m_process.setWorkingDirectory(i.workingDir); - m_process.start(i.app, i.args); + if (i.app.startsWith("wsl ")) + { + QStringList tmp = i.args; + QString app = i.app; + tmp.prepend(app.remove(0, 4)); + m_process.start("wsl", tmp); + } else + m_process.start(i.app, i.args); if (!i.input.isEmpty()) { m_process.waitForStarted(-1); QByteArray data = i.input.toUtf8(); @@ -111,8 +118,9 @@ void Executor::execute(int id, const QString &workDir, const QString &app, #endif return; } - QString appPath = - QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(app); + QString appPath = app; + if (!appPath.startsWith("wsl ")) + appPath = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(app); m_execQueue.push_back( {id, appPath, args, input, readStdout, readStderr, workDir}); executeNext(); @@ -134,7 +142,13 @@ int Executor::executeBlocking(QString app, const QStringList &args, QString input, QString *process_out, QString *process_err) { QProcess internal; - internal.start(app, args); + if (app.startsWith("wsl ")) + { + QStringList tmp = args; + tmp.prepend(app.remove(0, 4)); + internal.start("wsl", tmp); + } else + internal.start(app, args); if (!input.isEmpty()) { QByteArray data = input.toUtf8(); internal.waitForStarted(-1); diff --git a/src/imitatepass.cpp b/src/imitatepass.cpp index f1c84e40..862be677 100644 --- a/src/imitatepass.cpp +++ b/src/imitatepass.cpp @@ -14,11 +14,25 @@ using namespace Enums; */ ImitatePass::ImitatePass() {} +static QString pgit(const QString &path) +{ + if (!QtPassSettings::getGitExecutable().startsWith("wsl ")) return path; + QString res = "$(wslpath " + path + ")"; + return res.replace('\\', '/'); +} + +static QString pgpg(const QString &path) +{ + if (!QtPassSettings::getGpgExecutable().startsWith("wsl ")) return path; + QString res = "$(wslpath " + path + ")"; + return res.replace('\\', '/'); +} + /** * @brief ImitatePass::GitInit git init wrapper */ void ImitatePass::GitInit() { - executeGit(GIT_INIT, {"init", QtPassSettings::getPassStore()}); + executeGit(GIT_INIT, {"init", pgit(QtPassSettings::getPassStore())}); } /** @@ -48,7 +62,7 @@ void ImitatePass::GitPush() { void ImitatePass::Show(QString file) { file = QtPassSettings::getPassStore() + file + ".gpg"; QStringList args = {"-d", "--quiet", "--yes", "--no-encrypt-to", - "--batch", "--use-agent", file}; + "--batch", "--use-agent", pgpg(file)}; executeGpg(PASS_SHOW, args); } @@ -82,7 +96,7 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) { "file missing or invalid.")); return; } - QStringList args = {"--batch", "-eq", "--output", file}; + QStringList args = {"--batch", "-eq", "--output", pgpg(file)}; for (auto &r : recipients) { args.append("-r"); args.append(r); @@ -94,7 +108,7 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) { if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) { // TODO(bezet) why not? if (!overwrite) - executeGit(GIT_ADD, {"add", file}); + executeGit(GIT_ADD, {"add", pgit(file)}); QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file); path.replace(QRegExp("\\.gpg$"), ""); QString msg = @@ -110,7 +124,7 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) { * @param msg */ void ImitatePass::GitCommit(const QString &file, const QString &msg) { - executeGit(GIT_COMMIT, {"commit", "-m", msg, "--", file}); + executeGit(GIT_COMMIT, {"commit", "-m", msg, "--", pgit(file)}); } /** @@ -122,7 +136,7 @@ void ImitatePass::Remove(QString file, bool isDir) { if (!isDir) file += ".gpg"; if (QtPassSettings::isUseGit()) { - executeGit(GIT_RM, {"rm", (isDir ? "-rf" : "-f"), file}); + executeGit(GIT_RM, {"rm", (isDir ? "-rf" : "-f"), pgit(file)}); // TODO(bezet): commit message used to have pass-like file name inside(ie. // getFile(file, true) GitCommit(file, "Remove for " + file + " using QtPass."); @@ -180,7 +194,7 @@ void ImitatePass::Init(QString path, const QList &users) { if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit() && !QtPassSettings::getGitExecutable().isEmpty()) { if (addFile) - executeGit(GIT_ADD, {"add", gpgIdFile}); + executeGit(GIT_ADD, {"add", pgit(gpgIdFile)}); QString path = gpgIdFile; path.replace(QRegExp("\\.gpg$"), ""); GitCommit(gpgIdFile, "Added " + path + " using QtPass."); @@ -243,7 +257,7 @@ void ImitatePass::reencryptPath(QString dir) { // TODO(bezet): enable --with-colons for better future-proofness? QStringList args = { "-v", "--no-secmem-warning", "--no-permission-warning", - "--list-only", "--keyid-format=long", fileName}; + "--list-only", "--keyid-format=long", pgpg(fileName)}; QString keys, err; exec.executeBlocking(QtPassSettings::getGpgExecutable(), args, &keys, &err); QStringList actualKeys; @@ -268,7 +282,7 @@ void ImitatePass::reencryptPath(QString dir) { #endif QString local_lastDecrypt = "Could not decrypt"; args = QStringList{"-d", "--quiet", "--yes", "--no-encrypt-to", - "--batch", "--use-agent", fileName}; + "--batch", "--use-agent", pgpg(fileName)}; exec.executeBlocking(QtPassSettings::getGpgExecutable(), args, &local_lastDecrypt); @@ -284,7 +298,7 @@ void ImitatePass::reencryptPath(QString dir) { "file missing or invalid.")); return; } - args = QStringList{"--yes", "--batch", "-eq", "--output", fileName}; + args = QStringList{"--yes", "--batch", "-eq", "--output", pgpg(fileName)}; for (auto &i : recipients) { args.append("-r"); args.append(i); @@ -295,12 +309,12 @@ void ImitatePass::reencryptPath(QString dir) { if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) { exec.executeBlocking(QtPassSettings::getGitExecutable(), - {"add", fileName}); + {"add", pgit(fileName)}); QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(fileName); path.replace(QRegExp("\\.gpg$"), ""); exec.executeBlocking(QtPassSettings::getGitExecutable(), - {"commit", fileName, "-m", + {"commit", pgit(fileName), "-m", "Edit for " + path + " using QtPass."}); } @@ -329,8 +343,8 @@ void ImitatePass::Move(const QString src, const QString dest, if (force) { args << "-f"; } - args << src; - args << dest; + args << pgit(src); + args << pgit(dest); executeGit(GIT_MOVE, args); QString message = QString("moved from %1 to %2 using QTPass."); @@ -367,8 +381,8 @@ void ImitatePass::Copy(const QString src, const QString dest, if (force) { args << "-f"; } - args << src; - args << dest; + args << pgit(src); + args << pgit(dest); executeGit(GIT_COPY, args); QString message = QString("copied from %1 to %2 using QTPass."); diff --git a/src/pass.cpp b/src/pass.cpp index 788b868b..7eb8d7a4 100644 --- a/src/pass.cpp +++ b/src/pass.cpp @@ -23,6 +23,7 @@ Pass::Pass() : wrapperRunning(false), env(QProcess::systemEnvironment()) { // SIGNAL(error(QProcess::ProcessError))); connect(&exec, &Executor::starting, this, &Pass::startingExecuteWrapper); + env.append("WSLENV=PASSWORD_STORE_DIR/p"); } void Pass::executeWrapper(PROCESS id, const QString &app, @@ -233,7 +234,7 @@ void Pass::finished(int id, int exitCode, const QString &out, * switching profiles) */ void Pass::updateEnv() { - QStringList store = env.filter("PASSWORD_STORE_DIR"); + QStringList store = env.filter("PASSWORD_STORE_DIR="); // put PASSWORD_STORE_DIR in env if (store.isEmpty()) { // dbg()<< "Added diff --git a/src/util.cpp b/src/util.cpp index ac69bae7..c194c3fc 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -121,7 +121,9 @@ QString Util::findBinaryInPath(QString binary) { bool Util::checkConfig() { return !QFile(QDir(QtPassSettings::getPassStore()).filePath(".gpg-id")) .exists() || - (!QFile(QtPassSettings::getPassExecutable()).exists() && + (!QtPassSettings::getPassExecutable().startsWith("wsl ") && + !QFile(QtPassSettings::getPassExecutable()).exists() && + !QtPassSettings::getGpgExecutable().startsWith("wsl ") && !QFile(QtPassSettings::getGpgExecutable()).exists()); } -- cgit v1.2.3 From 3b10fd3a556f5b5fd3b4ec13951c006d6dd328c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reimar=20D=C3=B6ffinger?= Date: Thu, 30 Aug 2018 19:17:41 +0200 Subject: Automatically detect and use WSL binaries. Treat WSL as if it contained binaries that are in the PATH. --- src/executor.h | 12 ++++++------ src/util.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/executor.h b/src/executor.h index 2a8b67cd..2d96be22 100644 --- a/src/executor.h +++ b/src/executor.h @@ -73,13 +73,13 @@ public: const QStringList &args, QString input = QString(), bool readStdout = false, bool readStderr = true); - int executeBlocking(QString app, const QStringList &args, - QString input = QString(), - QString *process_out = Q_NULLPTR, - QString *process_err = Q_NULLPTR); + static int executeBlocking(QString app, const QStringList &args, + QString input = QString(), + QString *process_out = Q_NULLPTR, + QString *process_err = Q_NULLPTR); - int executeBlocking(QString app, const QStringList &args, - QString *process_out, QString *process_err = Q_NULLPTR); + static int executeBlocking(QString app, const QStringList &args, + QString *process_out, QString *process_err = Q_NULLPTR); void setEnvironment(const QStringList &env); diff --git a/src/util.cpp b/src/util.cpp index c194c3fc..147d609c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -110,6 +110,17 @@ QString Util::findBinaryInPath(QString binary) { break; } } +#ifdef Q_OS_WIN + if (ret.isEmpty()) + { + binary.remove(0, 1); + binary.prepend("wsl "); + QString out, err; + if (Executor::executeBlocking(binary, {"--version"}, &out, &err) == 0 && + !out.isEmpty() && err.isEmpty()) + ret = binary; + } +#endif return ret; } -- cgit v1.2.3