Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Sandbox: add deactivate(), complete API doc
  • Loading branch information
martyone committed Apr 8, 2013
1 parent 5a9a189 commit 97455f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
44 changes: 36 additions & 8 deletions libssu/sandbox.cpp
Expand Up @@ -17,7 +17,6 @@
#include <QtCore/QSet>

#include "libssu/ssucoreconfig.h"
// TODO: rename to ssuconstants.h?
#include "constants.h"

class Sandbox::FileEngineHandler : public QAbstractFileEngineHandler {
Expand All @@ -37,20 +36,35 @@ class Sandbox::FileEngineHandler : public QAbstractFileEngineHandler {

/**
* @class Sandbox
* @brief Simple sandboxing with Qt file system abstraction.
*
* Redirects all file operations on system configuration files to files under
* sandbox directory. When constructed without arguments, the directory is get
* from @c SSU_TESTS_SANDBOX environment variable.
* Redirects all file operations on selected files to files under sandbox
* directory. The term <em>world files</em> is used to reffer files outside
* sandbox.
*
* Its effect is controlled by activate() and deactivate() calls. Only one
* Sandbox instance can be active at any time. Active sandbox is automatically
* deactivated upon destruction.
*
* When constructed without arguments, path to sandbox directory is get from
* @c SSU_TESTS_SANDBOX environment variable.
*
* @attention When constructed without arguments, it is activated automatically
* and failure to do so is reported with @c qFatal(), i.e., application will be
* abort()ed.
*
* When constructed with @a usage UseAsSkeleton, it will first make temporary
* copy of @a sandboxPath to work on and files in the original directory will
* stay untouched.
* stay untouched. Also see addWorldFiles().
*
* The argument @scopes allows to control if the sandbox will be used by this
* process, its children processes (@c SSU_TESTS_SANDBOX environment variable
* will be exported), or both.
*
* Internally it is based on QAbstractFileEngineHandler.
*
* @attention QDir lists entries presented in the world directory. The behavior
* changed with Qt 4.8.0 (Qt commit b9b55234a777c3b206332bafbe227e1355ca9186)
*/

Sandbox *Sandbox::s_activeInstance = 0;
Expand All @@ -71,15 +85,15 @@ Sandbox::Sandbox(const QString &sandboxPath, Usage usage, Scopes scopes)
}

Sandbox::~Sandbox(){
delete m_handler;
if (isActive()){
deactivate();
}

if (!m_tempDir.isEmpty() && QFileInfo(m_tempDir).exists()){
if (QProcess::execute("rm", QStringList() << "-rf" << m_tempDir) != 0){
qWarning("%s: Failed to remove temporary directory", Q_FUNC_INFO);
}
}

s_activeInstance = 0;
}

bool Sandbox::isActive() const{
Expand All @@ -105,6 +119,20 @@ bool Sandbox::activate(){
return true;
}

void Sandbox::deactivate(){
Q_ASSERT(isActive());

if (m_scopes & ThisProcess){
delete m_handler;
}

if (m_scopes & ChildProcesses){
unsetenv("SSU_TESTS_SANDBOX");
}

s_activeInstance = 0;
}

/**
* Copies selected files into sandbox. Existing files in sandbox are not overwriten.
*
Expand Down
1 change: 1 addition & 0 deletions libssu/sandbox_p.h
Expand Up @@ -33,6 +33,7 @@ class Sandbox {
~Sandbox();

bool activate();
void deactivate();
bool isActive() const;

bool addWorldFiles(const QString &directory, QDir::Filters filters = QDir::NoFilter,
Expand Down

0 comments on commit 97455f5

Please sign in to comment.