From 5f89a2166efe4940a0c8a55cc7e37fe4904296ab Mon Sep 17 00:00:00 2001 From: Martin Kampas Date: Thu, 4 Apr 2013 12:41:51 +0200 Subject: [PATCH] Sandbox: accept sandboxPath as constructor arg --- libssu/sandboxfileenginehandler.cpp | 85 +++++++++++++++++------------ libssu/sandboxfileenginehandler_p.h | 8 +++ 2 files changed, 59 insertions(+), 34 deletions(-) diff --git a/libssu/sandboxfileenginehandler.cpp b/libssu/sandboxfileenginehandler.cpp index 3c26f80..ab506c3 100644 --- a/libssu/sandboxfileenginehandler.cpp +++ b/libssu/sandboxfileenginehandler.cpp @@ -24,55 +24,72 @@ * directory specified by SSU_TESTS_SANDBOX environment variable. */ -SandboxFileEngineHandler::SandboxFileEngineHandler() {} +QSet SandboxFileEngineHandler::s_ssuConfigFiles = QSet() + << SSU_CONFIGURATION + << SSU_REPO_CONFIGURATION + << SSU_DEFAULT_CONFIGURATION + << SSU_BOARD_MAPPING_CONFIGURATION; -QAbstractFileEngine *SandboxFileEngineHandler::create(const QString &fileName) const{ - static bool enabled = false; - static bool firstCall = true; +QSet SandboxFileEngineHandler::s_ssuConfigDirectories = QSet() + << SSU_BOARD_MAPPING_CONFIGURATION_DIR; - if (!enabled && !firstCall){ - return 0; +SandboxFileEngineHandler::SandboxFileEngineHandler(){ + m_enabled = false; + m_sandboxPath = QProcessEnvironment::systemEnvironment().value("SSU_TESTS_SANDBOX"); + + if (m_sandboxPath.isEmpty()){ + return; } - static QString sandboxPath = - QProcessEnvironment::systemEnvironment().value("SSU_TESTS_SANDBOX"); + if (!QFileInfo(m_sandboxPath).exists()){ + qFatal("%s: Invalid SSU_TESTS_SANDBOX value: No such file or directory", + qPrintable(m_sandboxPath)); + } - if (firstCall){ - firstCall = false; + if (!QFileInfo(m_sandboxPath).isDir()){ + qFatal("%s: Invalid SSU_TESTS_SANDBOX value: Not a directory", + qPrintable(m_sandboxPath)); + } - if (sandboxPath.isEmpty()){ - return 0; - } + m_enabled = true; +} - if (!QFileInfo(sandboxPath).exists()){ - qFatal("%s: Invalid SSU_TESTS_SANDBOX value: No such file or directory", - qPrintable(sandboxPath)); - } +SandboxFileEngineHandler::SandboxFileEngineHandler(const QString &sandboxPath){ + m_enabled = false; + m_sandboxPath = sandboxPath; - if (!QFileInfo(sandboxPath).isDir()){ - qFatal("%s: Invalid SSU_TESTS_SANDBOX value: Not a directory", - qPrintable(sandboxPath)); - } + if (m_sandboxPath.isEmpty()){ + qWarning("%s: Empty sandboxPath", Q_FUNC_INFO); + return; + } - enabled = true; + if (!QFileInfo(m_sandboxPath).exists()){ + qFatal("%s: Invalid sandboxPath: No such file or directory", + qPrintable(m_sandboxPath)); } - if (!fileName.startsWith('/')){ - return 0; + if (!QFileInfo(m_sandboxPath).isDir()){ + qFatal("%s: Invalid sandboxPath: Not a directory", + qPrintable(m_sandboxPath)); } - static QSet ssuConfigFiles = QSet() - << SSU_CONFIGURATION - << SSU_REPO_CONFIGURATION - << SSU_DEFAULT_CONFIGURATION - << SSU_BOARD_MAPPING_CONFIGURATION; + m_enabled = true; +} + +QAbstractFileEngine *SandboxFileEngineHandler::create(const QString &fileName) const{ + Q_ASSERT(!m_enabled || !m_sandboxPath.isEmpty()); - static QSet ssuConfigDirectories = QSet() - << SSU_BOARD_MAPPING_CONFIGURATION_DIR; + if (!m_enabled){ + return 0; + } + + if (!fileName.startsWith('/')){ + return 0; + } - if (!ssuConfigFiles.contains(fileName)){ + if (!s_ssuConfigFiles.contains(fileName)){ bool match = false; - foreach (const QString &ssuConfigDirectory, ssuConfigDirectories){ + foreach (const QString &ssuConfigDirectory, s_ssuConfigDirectories){ if (fileName.startsWith(ssuConfigDirectory + '/')){ match = true; break; @@ -83,7 +100,7 @@ QAbstractFileEngine *SandboxFileEngineHandler::create(const QString &fileName) c } } - const QString fileName_ = QDir(sandboxPath).absoluteFilePath(QString(fileName).remove(0, 1)); + const QString fileName_ = QDir(m_sandboxPath).absoluteFilePath(QString(fileName).remove(0, 1)); return new QFSFileEngine(fileName_); } diff --git a/libssu/sandboxfileenginehandler_p.h b/libssu/sandboxfileenginehandler_p.h index 1dbabe6..5c5b3f5 100644 --- a/libssu/sandboxfileenginehandler_p.h +++ b/libssu/sandboxfileenginehandler_p.h @@ -13,7 +13,15 @@ class SandboxFileEngineHandler : public QAbstractFileEngineHandler { public: SandboxFileEngineHandler(); + SandboxFileEngineHandler(const QString &sandboxPath); + QAbstractFileEngine *create(const QString &fileName) const; + + private: + static QSet s_ssuConfigFiles; + static QSet s_ssuConfigDirectories; + bool m_enabled; + QString m_sandboxPath; }; #endif