Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement in-process tmp dir creation
  • Loading branch information
martyone authored and Bernd Wachter committed May 27, 2013
1 parent 652f343 commit 1cfaa30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
29 changes: 20 additions & 9 deletions libssu/sandbox.cpp
Expand Up @@ -238,19 +238,11 @@ bool Sandbox::prepare(){
}

if (m_usage == UseAsSkeleton){
QProcess mktemp;
mktemp.start("mktemp", QStringList() << "-t" << "-d" << "ssu-sandbox.XXX");
if (!mktemp.waitForFinished() || mktemp.exitCode() != 0){
if (m_tempDir = createTmpDir("ssu-sandbox.%1"), m_tempDir.isEmpty()){
qWarning("%s: Failed to create sandbox directory", Q_FUNC_INFO);
return false;
}

m_tempDir = mktemp.readAllStandardOutput().trimmed();
if (!QFileInfo(m_tempDir).isDir()){
qWarning("%s: Temporary directory disappeared: '%s'", Q_FUNC_INFO, qPrintable(m_tempDir));
return false;
}

const QString sandboxCopyPath = QDir(m_tempDir).filePath("configroot");

if (!copyDir(m_sandboxPath, sandboxCopyPath)){
Expand All @@ -267,6 +259,25 @@ bool Sandbox::prepare(){
return true;
}

QString Sandbox::createTmpDir(const QString &nameTemplate){
static const int REASONABLE_REPEAT_COUNT = 10;

for (int i = 0; i < REASONABLE_REPEAT_COUNT; ++i){
QString path;
int suffix = 0;
do{
path = QDir::temp().filePath(nameTemplate.arg(++suffix));
}while(QFileInfo(path).exists());

if (QDir().mkpath(path)){
return path;
}
}

qWarning("%s: Failed to create temporary directory", Q_FUNC_INFO);
return QString();
}

bool Sandbox::copyDir(const QString &directory, const QString &newName){
const QDir sourceRoot(directory);
const QDir destinationRoot(newName);
Expand Down
1 change: 1 addition & 0 deletions libssu/sandbox_p.h
Expand Up @@ -43,6 +43,7 @@ class Sandbox {

private:
bool prepare();
static QString createTmpDir(const QString &nameTemplate);
static bool copyDir(const QString &directory, const QString &newName);

private:
Expand Down

0 comments on commit 1cfaa30

Please sign in to comment.