1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#include "util.h"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QProcessEnvironment>
#include <QString>
#ifdef Q_OS_WIN
#include <windows.h>
#else
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <cassert>
QProcessEnvironment Util::_env;
bool Util::_envInitialised;
/**
* @brief Util::initialiseEnvironment set the correct PATH for use with gpg, git
* etc.
*/
void Util::initialiseEnvironment() {
if (!_envInitialised) {
_env = QProcessEnvironment::systemEnvironment();
#ifdef __APPLE__
// TODO(annejan) checks here
QString path = _env.value("PATH");
if (!path.contains("/usr/local/MacGPG2/bin") &&
QFile("/usr/local/MacGPG2/bin").exists())
path += ":/usr/local/MacGPG2/bin";
if (!path.contains("/usr/local/bin"))
path += ":/usr/local/bin";
_env.insert("PATH", path);
#endif
_envInitialised = true;
}
}
/**
* @brief Util::findPasswordStore look for common .password-store folder
* location.
* @return
*/
QString Util::findPasswordStore() {
QString path;
initialiseEnvironment();
if (_env.contains("PASSWORD_STORE_DIR")) {
path = _env.value("PASSWORD_STORE_DIR");
} else {
#ifdef Q_OS_WIN
path = QDir::homePath() + QDir::separator() + "password-store" +
QDir::separator();
#else
path = QDir::homePath() + QDir::separator() + ".password-store" +
QDir::separator();
#endif
}
return Util::normalizeFolderPath(path);
}
/**
* @brief Util::normalizeFolderPath let's always end folders with a
* QDir::separator()
* @param path
* @return
*/
QString Util::normalizeFolderPath(QString path) {
if (!path.endsWith("/") && !path.endsWith(QDir::separator()))
path += QDir::separator();
return path;
}
/**
* @brief Util::findBinaryInPath search for executables.
* @param binary
* @return
*/
QString Util::findBinaryInPath(QString binary) {
initialiseEnvironment();
QString ret = "";
binary.prepend(QDir::separator());
if (_env.contains("PATH")) {
QString path = _env.value("PATH");
QStringList entries;
#ifndef Q_OS_WIN
entries = path.split(':');
if (entries.length() < 2) {
#endif
entries = path.split(';');
#ifndef Q_OS_WIN
}
#endif
foreach (QString entry, entries) {
QScopedPointer<QFileInfo> qfi(new QFileInfo(entry.append(binary)));
#ifdef Q_OS_WIN
if (!qfi->exists())
qfi.reset(new QFileInfo(entry.append(".exe")));
#endif
qDebug() << entry;
if (!qfi->isExecutable())
continue;
ret = qfi->absoluteFilePath();
break;
}
}
return ret;
}
/**
* @brief Util::checkConfig do we have prequisite settings?
* @param passStore
* @param passExecutable
* @param gpgExecutable
* @return
*/
bool Util::checkConfig(QString passStore, QString passExecutable,
QString gpgExecutable) {
return !QFile(passStore + ".gpg-id").exists() ||
(!QFile(passExecutable).exists() && !QFile(gpgExecutable).exists());
}
/**
* @brief Util::qSleep because . . windows sleep.
* @param ms
*/
void Util::qSleep(int ms) {
#ifdef Q_OS_WIN
Sleep(uint(ms));
#else
struct timespec ts = {ms / 1000, (ms % 1000) * 1000 * 1000};
nanosleep(&ts, NULL);
#endif
}
quint32 Util::boundedRandom(quint32 bound) {
static int fd = -1;
if (bound < 2)
return 0;
if (fd == -1)
assert((fd = open("/dev/urandom", O_RDONLY)) >= 0);
quint32 randval;
const quint32 max_mod_bound = (1 + ~bound) % bound;
do
assert(read(fd, &randval, sizeof(randval)) == sizeof(randval));
while (randval < max_mod_bound);
return randval % bound;
}
|