Skip to content

Commit

Permalink
move files
Browse files Browse the repository at this point in the history
wrapper for gpg and git
  • Loading branch information
Janosch Knack committed Dec 1, 2016
1 parent 8b01b81 commit 7a90685
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 130 deletions.
91 changes: 49 additions & 42 deletions imitatepass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ ImitatePass::ImitatePass() {}
* @brief ImitatePass::GitInit git init wrapper
*/
void ImitatePass::GitInit() {
executeWrapper(QtPassSettings::getGitExecutable(),
"init \"" + QtPassSettings::getPassStore() + '"');
executeGit("init \"" + QtPassSettings::getPassStore() + '"');
}

/**
* @brief ImitatePass::GitPull git init wrapper
*/
void ImitatePass::GitPull() {
executeWrapper(QtPassSettings::getGitExecutable(), "pull");
executeGit("pull");
}

/**
* @brief ImitatePass::GitPush git init wrapper
*/
void ImitatePass::GitPush() {
executeWrapper(QtPassSettings::getGitExecutable(), "push");
if(QtPassSettings::isUseGit()){
executeGit("push");
}
}

/**
Expand All @@ -32,8 +33,7 @@ void ImitatePass::GitPush() {
QProcess::ExitStatus ImitatePass::Show(QString file, bool block) {
// TODO(bezet): apparently not yet needed
// file += ".gpg";
executeWrapper(QtPassSettings::getGpgExecutable(),
"-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
executeGpg("-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
file + '"');
if (block)
return waitForProcess();
Expand All @@ -59,17 +59,15 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
return;
}
QString force(overwrite ? " --yes " : " ");
executeWrapper(QtPassSettings::getGpgExecutable(),
force + "--batch -eq --output \"" + file + "\" " + recipients +
executeGpg(force + "--batch -eq --output \"" + file + "\" " + recipients +
" -",
newValue);
if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
if (!overwrite)
executeWrapper(QtPassSettings::getGitExecutable(), "add \"" + file + '"');
executeGit("add \"" + file + '"');
QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file);
path.replace(QRegExp("\\.gpg$"), "");
executeWrapper(QtPassSettings::getGitExecutable(),
"commit \"" + file + "\" -m \"" +
executeGit("commit \"" + file + "\" -m \"" +
(overwrite ? "Edit" : "Add") + " for " + path +
" using QtPass.\"");
}
Expand All @@ -80,13 +78,11 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
*/
void ImitatePass::Remove(QString file, bool isDir) {
if (QtPassSettings::isUseGit()) {
executeWrapper(QtPassSettings::getGitExecutable(),
QString("rm ") + (isDir ? "-rf " : "-f ") + '"' + file +
executeGit(QString("rm ") + (isDir ? "-rf " : "-f ") + '"' + file +
'"');
// TODO(bezet): commit message used to have pass-like file name inside(ie.
// getFile(file, true)
executeWrapper(QtPassSettings::getGitExecutable(),
"commit \"" + file + "\" -m \"Remove for " + file +
executeGit("commit \"" + file + "\" -m \"Remove for " + file +
" using QtPass.\"");
} else {
if (isDir) {
Expand Down Expand Up @@ -140,12 +136,10 @@ void ImitatePass::Init(QString path, const QList<UserInfo> &users) {
if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit() &&
!QtPassSettings::getGitExecutable().isEmpty()) {
if (addFile)
executeWrapper(QtPassSettings::getGitExecutable(),
"add \"" + gpgIdFile + '"');
executeGit("add \"" + gpgIdFile + '"');
QString path = gpgIdFile;
path.replace(QRegExp("\\.gpg$"), "");
executeWrapper(QtPassSettings::getGitExecutable(),
"commit \"" + gpgIdFile + "\" -m \"Added " + path +
executeGit("commit \"" + gpgIdFile + "\" -m \"Added " + path +
" using QtPass.\"");
}
reencryptPath(path);
Expand Down Expand Up @@ -206,8 +200,7 @@ void ImitatePass::reencryptPath(QString dir) {
gpgId.sort();
}
process.waitForFinished();
executeWrapper(QtPassSettings::getGpgExecutable(),
"-v --no-secmem-warning "
executeGpg( "-v --no-secmem-warning "
"--no-permission-warning --list-only "
"--keyid-format long " +
fileName);
Expand All @@ -233,8 +226,7 @@ void ImitatePass::reencryptPath(QString dir) {
qDebug() << "reencrypt " << fileName << " for " << gpgId;
QString local_lastDecrypt = "Could not decrypt";
emit lastDecrypt(local_lastDecrypt);
executeWrapper(QtPassSettings::getGpgExecutable(),
"-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
executeGpg("-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
fileName + '"');
process.waitForFinished(30000); // long wait (passphrase stuff)
local_lastDecrypt = process.readAllStandardOutput();
Expand All @@ -253,20 +245,18 @@ void ImitatePass::reencryptPath(QString dir) {
"file missing or invalid."));
return;
}
executeWrapper(QtPassSettings::getGpgExecutable(),
executeGpg(
"--yes --batch -eq --output \"" + fileName + "\" " +
recipients + " -",
local_lastDecrypt);
process.waitForFinished(3000);

if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
executeWrapper(QtPassSettings::getGitExecutable(),
"add \"" + fileName + '"');
executeGit("add \"" + fileName + '"');
QString path =
QDir(QtPassSettings::getPassStore()).relativeFilePath(fileName);
path.replace(QRegExp("\\.gpg$"), "");
executeWrapper(QtPassSettings::getGitExecutable(),
"commit \"" + fileName + "\" -m \"" + "Edit for " +
executeGit("commit \"" + fileName + "\" -m \"" + "Edit for " +
path + " using QtPass.\"");
process.waitForFinished(3000);
}
Expand All @@ -283,24 +273,22 @@ void ImitatePass::reencryptPath(QString dir) {
emit endReencryptPath();
}

void ImitatePass::Move(QDir srcDir, QDir destDir, bool force)
void ImitatePass::Move(const QString src, const QString dest, const bool force)
{
QString src = srcDir.absolutePath();
QString dest= destDir.absolutePath();
if (QtPassSettings::isUseGit()) {
QString args = QString("mv %1 %2 %3");
// do we need force mode?
// force mode?
if(force){
args = args.arg("-f");
}else{
args = args.arg("");
}
args = args.arg(src).arg(dest);
executeWrapper(QtPassSettings::getGitExecutable(),args);
executeGit(args);

QString args1=QString("commit -m \"moved dir from %1 to %2 using QTPass.\"");
QString args1=QString("commit -m \"moved from %1 to %2 using QTPass.\"");
args1= args1.arg(src).arg(dest);
executeWrapper(QtPassSettings::getGitExecutable(), args1);
executeGit(args1);

if(QtPassSettings::isAutoPush()){
GitPush();
Expand All @@ -315,14 +303,33 @@ void ImitatePass::Move(QDir srcDir, QDir destDir, bool force)
}
}

void ImitatePass::Move(QFile srcFile, QFile destFile, bool force)
{
}

void ImitatePass::Copy(QDir srcDir, QDir destDir, bool force)
void ImitatePass::Copy(const QString src, const QString dest, const bool force)
{
}
if (QtPassSettings::isUseGit()) {
QString args = QString("cp %1 %2 %3");
// do we need force mode?
if(force){
args = args.arg("-f");
}else{
args = args.arg("");
}
args = args.arg(src).arg(dest);
executeGit(args);

void ImitatePass::Copy(QFile srcFile, QFile destFile, bool force)
{
QString args1=QString("commit -m \"copied from %1 to %2 using QTPass.\"");
args1= args1.arg(src).arg(dest);
executeGit(args1);

if(QtPassSettings::isAutoPush()){
GitPush();
}

} else {
QDir qDir;
if(force){
qDir.remove(dest);
}
qDir.rename(src, dest);
}
}
6 changes: 2 additions & 4 deletions imitatepass.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ class ImitatePass : public Pass {

// Pass interface
public:
void Move(QDir srcDir, QDir destDir, bool force = false);
void Move(QFile srcFile, QFile destFile, bool force = false);
void Copy(QDir srcDir, QDir destDir, bool force = false);
void Copy(QFile srcFile, QFile destFile, bool force = false);
void Move(const QString src, const QString dest, const bool force = false);
void Copy(const QString src, const QString dest, const bool force = false);
};

#endif // IMITATEPASS_H
26 changes: 26 additions & 0 deletions pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,29 @@ QString Pass::getRecipientString(QString for_file, QString separator,
recipients_str += separator + '"' + recipient + '"';
return recipients_str;
}

/**
* @brief Pass::executePass easy wrapper for running pass
* @param args
*/
void Pass::executePass(QString args, QString input) {

This comment has been minimized.

Copy link
@tezeb

tezeb Jan 3, 2017

Contributor

I don't think that's the correct place for this method(same for next two). It breaks interface, as they might be called from inside subclasses(ie. we can call it from ImitatePass, so what kind of imitation that would be?).

This comment has been minimized.

Copy link
@YoshiMan

YoshiMan Jan 12, 2017

yeah. You are right, will be fixed with #253.
I like your code review. good job

executeWrapper(QtPassSettings::getPassExecutable(), args, input);
}

/**
* @brief Pass::executeGpg easy wrapper for running gpg commands
* @param args
*/
void Pass::executeGpg(QString args, QString input)
{
executeWrapper(QtPassSettings::getGpgExecutable(), args, input);
}
/**
* @brief Pass::executeGit easy wrapper for running git commands
* @param args
*/
void Pass::executeGit(QString args, QString input) {
executeWrapper(QtPassSettings::getGitExecutable(), args, input);
}


12 changes: 7 additions & 5 deletions pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class Pass : public QObject {
bool wrapperRunning;
QStringList env;

protected:
private :
void executeWrapper(QString, QString, QString = QString());
protected:
QProcess process;

public:
Expand All @@ -55,10 +56,8 @@ class Pass : public QObject {
virtual QProcess::ExitStatus Show(QString file, bool block = false) = 0;
virtual void Insert(QString file, QString value, bool force) = 0;
virtual void Remove(QString file, bool isDir) = 0;
virtual void Move(QDir srcDir, QDir destDir, bool force = false) = 0;
virtual void Move(QFile srcFile, QFile destFile, bool force = false) = 0;
virtual void Copy(QDir srcDir, QDir destDir, bool force = false) = 0;
virtual void Copy(QFile srcFile, QFile destFile, bool force = false) = 0;
virtual void Move(const QString srcDir, const QString dest, const bool force = false) = 0;
virtual void Copy(const QString srcDir, const QString dest, const bool force = false) = 0;
virtual void Init(QString path, const QList<UserInfo> &users) = 0;
virtual QString Generate(int length, const QString &charset);

Expand All @@ -77,6 +76,9 @@ class Pass : public QObject {
static QString getRecipientString(QString for_file, QString separator = " ",
int *count = NULL);

void executeGit(QString args, QString input = QString());
void executePass(QString args, QString input = QString());
void executeGpg(QString args, QString input = QString());
private slots:
void processFinished(int, QProcess::ExitStatus);

Expand Down
48 changes: 29 additions & 19 deletions realpass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

RealPass::RealPass() {}

/**
* @brief RealPass::executePass easy wrapper for running pass
* @param args
*/
void RealPass::executePass(QString args, QString input) {
executeWrapper(QtPassSettings::getPassExecutable(), args, input);
}


/**
* @brief RealPass::GitInit git init wrapper
Expand Down Expand Up @@ -78,29 +72,45 @@ void RealPass::Init(QString path, const QList<UserInfo> &users) {
path.remove(0, QtPassSettings::getPassStore().size());
executePass("init --path=" + dirWithoutPassdir + " " + gpgIds);
}
void RealPass::Move(QDir srcDir, QDir destDir, bool force)
void RealPass::Move(const QString src, const QString dest, const bool force)
{
QString args = QString("mv %1 %2 %3");
// do we need force mode?
// force mode?
if(force){
args = args.arg("-f");
}else{
args = args.arg("");
}
QString passSrcDir = QDir(QtPassSettings::getPassStore()).relativeFilePath(srcDir.absolutePath());
QString passDestDir = QDir(QtPassSettings::getPassStore()).relativeFilePath(destDir.absolutePath());
args = args.arg(passSrcDir).arg(passDestDir);
QString passSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(src).absolutePath());
QString passDest= QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(dest).absolutePath());
QFileInfo srcFileInfo= QFileInfo(src);
QFileInfo destFileInfo= QFileInfo(dest);

// remove the .gpg because pass will not work
if(srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg"){
passSrc.replace(QRegExp("\\.gpg$"), "");
}
if(destFileInfo.isFile() && destFileInfo.suffix() == "gpg"){
passDest.replace(QRegExp("\\.gpg$"), "");
}

args = args.arg(passSrc).arg(passDest);
executePass(args);
}

void RealPass::Move(QFile srcFile, QFile destFile, bool force)
{
}

void RealPass::Copy(QDir srcDir, QDir destDir, bool force)
void RealPass::Copy(const QString src, const QString dest, const bool force)
{
QString args = QString("cp %1 %2 %3");
// do we need force mode?
if(force){
args = args.arg("-f");
}else{
args = args.arg("");
}
QString passSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(src).absolutePath());
QString passDest= QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(dest).absolutePath());
args = args.arg(passSrc).arg(passDest);
executePass(args);
}

void RealPass::Copy(QFile srcFile, QFile destFile, bool force)
{
}
9 changes: 2 additions & 7 deletions realpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#include "pass.h"

class RealPass : public Pass {
private:
void executePass(QString args, QString input = QString());

public:
RealPass();
virtual ~RealPass() {}
Expand All @@ -21,10 +18,8 @@ class RealPass : public Pass {

// Pass interface
public:
void Move(QDir srcDir, QDir destDir, bool force = false);
void Move(QFile srcFile, QFile destFile, bool force = false);
void Copy(QDir srcDir, QDir destDir, bool force = false);
void Copy(QFile srcFile, QFile destFile, bool force = false);
void Move(const QString src, const QString dest, const bool force = false);
void Copy(const QString src, const QString dest, const bool force = false);
};


Expand Down
Loading

0 comments on commit 7a90685

Please sign in to comment.