From 50694ef610fca5abee59a399997234476304e939 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 29 Apr 2019 06:22:24 -0400 Subject: [PATCH] [libcalamares] Split dirs-functionality into separate file --- src/libcalamares/CMakeLists.txt | 1 + src/libcalamares/utils/CalamaresUtils.cpp | 152 ----------------- src/libcalamares/utils/CalamaresUtils.h | 38 ----- src/libcalamares/utils/Dirs.cpp | 194 ++++++++++++++++++++++ src/libcalamares/utils/Dirs.h | 73 ++++++++ 5 files changed, 268 insertions(+), 190 deletions(-) create mode 100644 src/libcalamares/utils/Dirs.cpp create mode 100644 src/libcalamares/utils/Dirs.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index bd42ba5c5..5efd850a9 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -23,6 +23,7 @@ set( utilsSources utils/CalamaresUtils.cpp utils/CalamaresUtilsSystem.cpp utils/CommandList.cpp + utils/Dirs.cpp utils/LocaleLabel.cpp utils/Logger.cpp utils/PluginFactory.cpp diff --git a/src/libcalamares/utils/CalamaresUtils.cpp b/src/libcalamares/utils/CalamaresUtils.cpp index 6d8862269..25eceb159 100644 --- a/src/libcalamares/utils/CalamaresUtils.cpp +++ b/src/libcalamares/utils/CalamaresUtils.cpp @@ -41,154 +41,10 @@ using std::cerr; namespace CalamaresUtils { -static QDir s_appDataDir( CMAKE_INSTALL_FULL_DATADIR ); -static QDir s_qmlModulesDir( QString( CMAKE_INSTALL_FULL_DATADIR ) + "/qml" ); -static bool s_isAppDataDirOverridden = false; - static QTranslator* s_brandingTranslator = nullptr; static QTranslator* s_translator = nullptr; static QString s_translatorLocaleName; -static bool s_haveExtraDirs = false; -static QStringList s_extraConfigDirs; -static QStringList s_extraDataDirs; - -static bool -isWritableDir( const QDir& dir ) -{ - // We log with cerr here because we might be looking for the log dir - QString path = dir.absolutePath(); - if ( !dir.exists() ) - { - if ( !dir.mkpath( "." ) ) - { - cerr << "warning: failed to create " << qPrintable( path ) << '\n'; - return false; - } - return true; - } - - QFileInfo info( path ); - if ( !info.isDir() ) - { - cerr << "warning: " << qPrintable( path ) << " is not a dir\n"; - return false; - } - if ( !info.isWritable() ) - { - cerr << "warning: " << qPrintable( path ) << " is not writable\n"; - return false; - } - return true; -} - - -QDir -qmlModulesDir() -{ - return s_qmlModulesDir; -} - - -void -setAppDataDir( const QDir& dir ) -{ - s_appDataDir = dir; - s_isAppDataDirOverridden = true; -} - -/* Split $ENV{@p name} on :, append to @p l, making sure each ends in / */ -static void -mungeEnvironment( QStringList& l, const char* name, const char* defaultDirs ) -{ - static const QString calamaresSubdir = QStringLiteral( "calamares/" ); - - QStringList dirs = QString( qgetenv( name ) ).split( ':' ); - if ( dirs.isEmpty() ) - dirs = QString( defaultDirs ).split( ':' ); - - for ( auto s : dirs ) - { - if ( s.isEmpty() ) - continue; - if ( s.endsWith( '/' ) ) - l << ( s + calamaresSubdir ) << s; - else - l << ( s + '/' + calamaresSubdir ) << ( s + '/' ); - } -} - -void -setXdgDirs() -{ - mungeEnvironment( s_extraConfigDirs, "XDG_CONFIG_DIRS", "/etc/xdg" ); - mungeEnvironment( s_extraDataDirs, "XDG_DATA_DIRS", "/usr/local/share/:/usr/share/" ); - - s_haveExtraDirs = !( s_extraConfigDirs.isEmpty() && s_extraDataDirs.isEmpty() ); -} - -QStringList -extraConfigDirs() -{ - if ( s_haveExtraDirs ) - return s_extraConfigDirs; - return QStringList(); -} - -QStringList -extraDataDirs() -{ - if ( s_haveExtraDirs ) - return s_extraDataDirs; - return QStringList(); -} - -bool -haveExtraDirs() -{ - return s_haveExtraDirs && ( !s_extraConfigDirs.isEmpty() || !s_extraDataDirs.isEmpty() ); -} - -bool -isAppDataDirOverridden() -{ - return s_isAppDataDirOverridden; -} - - -QDir -appDataDir() -{ - return s_appDataDir; -} - - -QDir -systemLibDir() -{ - QDir path( CMAKE_INSTALL_FULL_LIBDIR ); - return path; -} - - -QDir -appLogDir() -{ - QString path = QStandardPaths::writableLocation( QStandardPaths::CacheLocation ); - QDir dir( path ); - if ( isWritableDir( dir ) ) - return dir; - - cerr << "warning: Could not find a standard writable location for log dir, falling back to $HOME\n"; - dir = QDir::home(); - if ( isWritableDir( dir ) ) - return dir; - - cerr << "warning: Found no writable location for log dir, falling back to the temp dir\n"; - return QDir::temp(); -} - - void installTranslator( const QLocale& locale, const QString& brandingTranslationsPrefix, @@ -278,14 +134,6 @@ translatorLocaleName() return s_translatorLocaleName; } - -void -setQmlModulesDir( const QDir& dir ) -{ - s_qmlModulesDir = dir; -} - - QString removeDiacritics( const QString& string ) { diff --git a/src/libcalamares/utils/CalamaresUtils.h b/src/libcalamares/utils/CalamaresUtils.h index d4f48ec2a..49abf0cd6 100644 --- a/src/libcalamares/utils/CalamaresUtils.h +++ b/src/libcalamares/utils/CalamaresUtils.h @@ -37,26 +37,6 @@ class QObject; */ namespace CalamaresUtils { - DLLEXPORT QDir qmlModulesDir(); - - /** - * @brief appDataDir returns the directory with common application data. - * Defaults to CMAKE_INSTALL_FULL_DATADIR (usually /usr/share/calamares). - */ - DLLEXPORT QDir appDataDir(); - - /** - * @brief appLogDir returns the directory for Calamares logs. - * Defaults to QStandardPaths::CacheLocation (usually ~/.cache/Calamares). - */ - DLLEXPORT QDir appLogDir(); - - /** - * @brief systemLibDir returns the system's lib directory. - * Defaults to CMAKE_INSTALL_FULL_LIBDIR (usually /usr/lib64 or /usr/lib). - */ - DLLEXPORT QDir systemLibDir(); - /** * @brief installTranslator changes the application language. * @param locale the new locale. @@ -69,24 +49,6 @@ namespace CalamaresUtils DLLEXPORT QString translatorLocaleName(); - /** - * Override app data dir. Only for testing purposes. - */ - DLLEXPORT void setAppDataDir( const QDir& dir ); - DLLEXPORT bool isAppDataDirOverridden(); - - DLLEXPORT void setQmlModulesDir( const QDir& dir ); - - /** @brief Setup extra config and data dirs from the XDG variables. - */ - DLLEXPORT void setXdgDirs(); - /** @brief Are any extra directories configured? */ - DLLEXPORT bool haveExtraDirs(); - /** @brief XDG_CONFIG_DIRS, each guaranteed to end with / */ - DLLEXPORT QStringList extraConfigDirs(); - /** @brief XDG_DATA_DIRS, each guaranteed to end with / */ - DLLEXPORT QStringList extraDataDirs(); - /** * @brief removeDiacritics replaces letters with diacritics and ligatures with * alternative forms and digraphs. diff --git a/src/libcalamares/utils/Dirs.cpp b/src/libcalamares/utils/Dirs.cpp new file mode 100644 index 000000000..1fd309584 --- /dev/null +++ b/src/libcalamares/utils/Dirs.cpp @@ -0,0 +1,194 @@ +/* === This file is part of Calamares - === + * + * Copyright 2013-2016, Teo Mrnjavac + * Copyright 2018, Adriaan de Groot + * + * Originally from Tomahawk, portions: + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2010-2012, Jeff Mitchell + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "Dirs.h" + +#include "CalamaresConfig.h" +#include "Logger.h" + +#include +#include +#include +#include +#include +#include + +#include + +using std::cerr; + +namespace CalamaresUtils +{ + +static QDir s_appDataDir( CMAKE_INSTALL_FULL_DATADIR ); +static QDir s_qmlModulesDir( QString( CMAKE_INSTALL_FULL_DATADIR ) + "/qml" ); +static bool s_isAppDataDirOverridden = false; + +static bool s_haveExtraDirs = false; +static QStringList s_extraConfigDirs; +static QStringList s_extraDataDirs; + +static bool +isWritableDir( const QDir& dir ) +{ + // We log with cerr here because we might be looking for the log dir + QString path = dir.absolutePath(); + if ( !dir.exists() ) + { + if ( !dir.mkpath( "." ) ) + { + cerr << "warning: failed to create " << qPrintable( path ) << '\n'; + return false; + } + return true; + } + + QFileInfo info( path ); + if ( !info.isDir() ) + { + cerr << "warning: " << qPrintable( path ) << " is not a dir\n"; + return false; + } + if ( !info.isWritable() ) + { + cerr << "warning: " << qPrintable( path ) << " is not writable\n"; + return false; + } + return true; +} + + +QDir +qmlModulesDir() +{ + return s_qmlModulesDir; +} + + +void +setAppDataDir( const QDir& dir ) +{ + s_appDataDir = dir; + s_isAppDataDirOverridden = true; +} + +/* Split $ENV{@p name} on :, append to @p l, making sure each ends in / */ +static void +mungeEnvironment( QStringList& l, const char* name, const char* defaultDirs ) +{ + static const QString calamaresSubdir = QStringLiteral( "calamares/" ); + + QStringList dirs = QString( qgetenv( name ) ).split( ':' ); + if ( dirs.isEmpty() ) + dirs = QString( defaultDirs ).split( ':' ); + + for ( auto s : dirs ) + { + if ( s.isEmpty() ) + continue; + if ( s.endsWith( '/' ) ) + l << ( s + calamaresSubdir ) << s; + else + l << ( s + '/' + calamaresSubdir ) << ( s + '/' ); + } +} + +void +setXdgDirs() +{ + mungeEnvironment( s_extraConfigDirs, "XDG_CONFIG_DIRS", "/etc/xdg" ); + mungeEnvironment( s_extraDataDirs, "XDG_DATA_DIRS", "/usr/local/share/:/usr/share/" ); + + s_haveExtraDirs = !( s_extraConfigDirs.isEmpty() && s_extraDataDirs.isEmpty() ); +} + +QStringList +extraConfigDirs() +{ + if ( s_haveExtraDirs ) + return s_extraConfigDirs; + return QStringList(); +} + +QStringList +extraDataDirs() +{ + if ( s_haveExtraDirs ) + return s_extraDataDirs; + return QStringList(); +} + +bool +haveExtraDirs() +{ + return s_haveExtraDirs && ( !s_extraConfigDirs.isEmpty() || !s_extraDataDirs.isEmpty() ); +} + +bool +isAppDataDirOverridden() +{ + return s_isAppDataDirOverridden; +} + + +QDir +appDataDir() +{ + return s_appDataDir; +} + + +QDir +systemLibDir() +{ + QDir path( CMAKE_INSTALL_FULL_LIBDIR ); + return path; +} + + +QDir +appLogDir() +{ + QString path = QStandardPaths::writableLocation( QStandardPaths::CacheLocation ); + QDir dir( path ); + if ( isWritableDir( dir ) ) + return dir; + + cerr << "warning: Could not find a standard writable location for log dir, falling back to $HOME\n"; + dir = QDir::home(); + if ( isWritableDir( dir ) ) + return dir; + + cerr << "warning: Found no writable location for log dir, falling back to the temp dir\n"; + return QDir::temp(); +} + + +void +setQmlModulesDir( const QDir& dir ) +{ + s_qmlModulesDir = dir; +} + +} // namespace diff --git a/src/libcalamares/utils/Dirs.h b/src/libcalamares/utils/Dirs.h new file mode 100644 index 000000000..5532cb79b --- /dev/null +++ b/src/libcalamares/utils/Dirs.h @@ -0,0 +1,73 @@ +/* === This file is part of Calamares - === + * + * Copyright 2013-2016, Teo Mrnjavac + * Copyright 2018, Adriaan de Groot + * + * Originally from Tomahawk, portions: + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2010-2012, Jeff Mitchell + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef UTILS_DIRS_H +#define UTILS_DIRS_H + +#include "DllMacro.h" + +#include + +namespace CalamaresUtils +{ + DLLEXPORT QDir qmlModulesDir(); + + /** + * @brief appDataDir returns the directory with common application data. + * Defaults to CMAKE_INSTALL_FULL_DATADIR (usually /usr/share/calamares). + */ + DLLEXPORT QDir appDataDir(); + + /** + * @brief appLogDir returns the directory for Calamares logs. + * Defaults to QStandardPaths::CacheLocation (usually ~/.cache/Calamares). + */ + DLLEXPORT QDir appLogDir(); + + /** + * @brief systemLibDir returns the system's lib directory. + * Defaults to CMAKE_INSTALL_FULL_LIBDIR (usually /usr/lib64 or /usr/lib). + */ + DLLEXPORT QDir systemLibDir(); + + /** + * Override app data dir. Only for testing purposes. + */ + DLLEXPORT void setAppDataDir( const QDir& dir ); + DLLEXPORT bool isAppDataDirOverridden(); + + DLLEXPORT void setQmlModulesDir( const QDir& dir ); + + /** @brief Setup extra config and data dirs from the XDG variables. + */ + DLLEXPORT void setXdgDirs(); + /** @brief Are any extra directories configured? */ + DLLEXPORT bool haveExtraDirs(); + /** @brief XDG_CONFIG_DIRS, each guaranteed to end with / */ + DLLEXPORT QStringList extraConfigDirs(); + /** @brief XDG_DATA_DIRS, each guaranteed to end with / */ + DLLEXPORT QStringList extraDataDirs(); +} // namespace + +#endif