From c503edfe49b06ad7f16537586df67d833b0d9620 Mon Sep 17 00:00:00 2001 From: Andrey Astafyev Date: Fri, 15 May 2020 13:17:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D1=8B=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=BC=D1=91=D0=BD?= =?UTF-8?q?=20=D0=BA=D0=B0=D1=82=D0=B0=D0=BB=D0=BE=D0=B3=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/myx/filesystem/paths.cpp | 203 ++++++++++++++++------------------- src/myx/filesystem/paths.hpp | 4 + 2 files changed, 94 insertions(+), 113 deletions(-) diff --git a/src/myx/filesystem/paths.cpp b/src/myx/filesystem/paths.cpp index 302a072..bfa6832 100644 --- a/src/myx/filesystem/paths.cpp +++ b/src/myx/filesystem/paths.cpp @@ -21,105 +21,109 @@ namespace filesystem { MYXLIB_INLINE Paths::Paths() = default; -MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() // -V2008 + +MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultEtcDirectory, + const QString& defaultConstDataDirectory, + const QString& defaultVarDataDirectory, + const QString& defaultLogDirectory ) +{ + QFileInfo etcDirInfo { defaultEtcDirectory }; + if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) + { + m_systemConfigDirectory = defaultEtcDirectory; + } + else + { + m_systemConfigDirectory = m_userConfigDirectory; + } + + QFileInfo constDataDirInfo { defaultConstDataDirectory }; + if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) + { + m_systemConstDataDirectory = defaultConstDataDirectory; + } + else + { + m_systemConstDataDirectory = m_userConstDataDirectory; + } + + QFileInfo varDataDirInfo { defaultVarDataDirectory }; + if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) + { + m_systemVarDataDirectory = defaultVarDataDirectory; + } + else + { + m_systemVarDataDirectory = m_userVarDataDirectory; + } + + QFileInfo logDirInfo { defaultLogDirectory }; + if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) + { + m_systemLogDirectory = defaultLogDirectory; + } + else + { + m_systemLogDirectory = m_userLogDirectory; + } +} // Paths::setupSystemDirectories + + +Paths::HierarchyType Paths::getHierarchyType() { QRegExp binUnityRegexp( "/s*bin/unity$" ); - QRegExp binRegexp( "/s*bin$" ); auto binaryDir = m_executableDirectory; if ( binUnityRegexp.indexIn( binaryDir ) >= 0 ) { binaryDir.remove( binUnityRegexp ); - - QFileInfo etcDirInfo { binaryDir + "/etc" }; - if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo constDataDirInfo { binaryDir + "/files/data" }; - if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo varDataDirInfo { binaryDir + "/files/lib" }; - if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - QFileInfo logDirInfo { binaryDir + "/files/log" }; - if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - m_systemConfigDirectory = etcDirInfo.canonicalFilePath(); - m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath(); - m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath(); - m_systemLogDirectory = logDirInfo.canonicalFilePath(); + setupSystemDirectories( binaryDir + "/etc", + binaryDir + "/files/data", + binaryDir + "/files/lib", + binaryDir + "/files/log" ); return ( HierarchyType::kDevelopment ); } + QRegExp binRegexp( "/s*bin$" ); if ( binRegexp.indexIn( binaryDir ) == -1 ) { + m_systemConstDataDirectory = m_executableDirectory; + m_systemVarDataDirectory = m_executableDirectory; + m_systemConfigDirectory = m_executableDirectory; + m_systemLogDirectory = m_executableDirectory; + return ( HierarchyType::kFlat ); } QRegExp optRegexp( "^/opt(/|/.+/)" + m_projectName + "/" ); - if ( optRegexp.indexIn( binaryDir ) >= 0 ) { binaryDir.remove( binRegexp ); - QFileInfo etcDirInfo { binaryDir + "/etc" }; - if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo constDataDirInfo { binaryDir + "/files/data" }; - if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo varDataDirInfo { binaryDir + "/files/lib" }; - if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - QFileInfo logDirInfo { binaryDir + "/files/log" }; - if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - m_systemConfigDirectory = etcDirInfo.canonicalFilePath(); - m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath(); - m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath(); - m_systemLogDirectory = logDirInfo.canonicalFilePath(); + setupSystemDirectories( binaryDir + "/etc", + binaryDir + "/files/data", + binaryDir + "/files/lib", + binaryDir + "/files/log" ); return ( HierarchyType::kOpt ); } if ( binaryDir.startsWith( QStringLiteral( "/usr/local" ) ) ) { - QFileInfo etcDirInfo { "/usr/local/etc/" + m_projectName }; - if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo constDataDirInfo { "/usr/local/share/" + m_projectName }; - if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo varDataDirInfo { "/var/lib/" + m_projectName }; - if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - QFileInfo logDirInfo { "/var/log/" + m_projectName }; - if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - m_systemConfigDirectory = etcDirInfo.canonicalFilePath(); - m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath(); - m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath(); - m_systemLogDirectory = logDirInfo.canonicalFilePath(); + setupSystemDirectories( "/usr/local/etc/" + m_projectName, + "/usr/local/share/" + m_projectName, + "/var/lib/" + m_projectName, + "/var/log/" + m_projectName ); return ( HierarchyType::kUsr ); } + if ( binaryDir.startsWith( QStringLiteral( "/usr" ) ) ) { - QFileInfo etcDirInfo { "/etc/" + m_projectName }; - if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo constDataDirInfo { "/usr/share/" + m_projectName }; - if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo varDataDirInfo { "/var/lib/" + m_projectName }; - if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - QFileInfo logDirInfo { "/var/log/" + m_projectName }; - if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - m_systemConfigDirectory = etcDirInfo.canonicalFilePath(); - m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath(); - m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath(); - m_systemLogDirectory = logDirInfo.canonicalFilePath(); + setupSystemDirectories( "/etc/" + m_projectName, + "/usr/share/" + m_projectName, + "/var/lib/" + m_projectName, + "/var/log/" + m_projectName ); return ( HierarchyType::kUsr ); } @@ -127,44 +131,19 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() // -V2008 if ( binaryDir.startsWith( m_homeDirectory + "/.local/bin" ) || binaryDir.startsWith( m_homeDirectory + "/bin" ) ) { - QFileInfo etcDirInfo { m_userConfigDirectory }; - if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo constDataDirInfo { m_userConstDataDirectory }; - if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo varDataDirInfo { m_userVarDataDirectory }; - if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - QFileInfo logDirInfo { m_userLogDirectory }; - if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - m_systemConfigDirectory = etcDirInfo.canonicalFilePath(); - m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath(); - m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath(); - m_systemLogDirectory = logDirInfo.canonicalFilePath(); + m_systemConfigDirectory = m_userConfigDirectory; + m_systemConstDataDirectory = m_userConstDataDirectory; + m_systemVarDataDirectory = m_userVarDataDirectory; + m_systemLogDirectory = m_userLogDirectory; return( HierarchyType::kHome ); } binaryDir.remove( binRegexp ); - - QFileInfo etcDirInfo { binaryDir + "/etc" }; - if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo constDataDirInfo { binaryDir + "/files/data" }; - if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); } - - QFileInfo varDataDirInfo { binaryDir + "/files/lib" }; - if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - QFileInfo logDirInfo { binaryDir + "/files/log" }; - if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); } - - m_systemConfigDirectory = etcDirInfo.canonicalFilePath(); - m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath(); - m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath(); - m_systemLogDirectory = logDirInfo.canonicalFilePath(); + setupSystemDirectories( binaryDir + "/etc", + binaryDir + "/files/data", + binaryDir + "/files/lib", + binaryDir + "/files/log" ); return ( HierarchyType::kDevelopment ); } // Paths::getHierarchyType @@ -205,14 +184,6 @@ MYXLIB_INLINE bool Paths::init( const QString& projectDir, const QString& config m_hierarchyType = getHierarchyType(); - if ( m_hierarchyType == HierarchyType::kFlat ) - { - m_systemConstDataDirectory = m_executableDirectory; - m_systemVarDataDirectory = m_executableDirectory; - m_systemConfigDirectory = m_executableDirectory; - m_systemLogDirectory = m_executableDirectory; - } - m_configFilePath = m_systemConfigDirectory + "/" + m_configFileName; return( true ); @@ -222,11 +193,17 @@ MYXLIB_INLINE bool Paths::init( const QString& projectDir, const QString& config MYXLIB_INLINE bool Paths::makeDefaultDirectories() { bool status = true; + QDir dir; + + if (dir.mkpath( m_userConfigDirectory )) { status = false; }; + if (dir.mkpath( m_userVarDataDirectory )) { status = false; }; + if (dir.mkpath( m_userConstDataDirectory )) { status = false; }; + if (dir.mkpath( m_userLogDirectory )) { status = false; }; + if (dir.mkpath( m_systemConfigDirectory )) { status = false; }; + if (dir.mkpath( m_systemVarDataDirectory )) { status = false; }; + if (dir.mkpath( m_systemConstDataDirectory )) { status = false; }; + if (dir.mkpath( m_systemLogDirectory )) { status = false; }; -// FIXME if ( !m_userConfigDirectory.mkpath( m_userConfigDirectory ) ) { status = false; } -// if ( !m_userVarDataDirectory.mkpath( m_userVarDataDirectory ) ) { status = false; } -// if ( !m_userConstDataDirectory.mkpath( m_userConstDataDirectory ) ) { status = false; } -// if ( !m_userLogDirectory.mkpath( m_userLogDirectory ) ) { status = false; } return( status ); } diff --git a/src/myx/filesystem/paths.hpp b/src/myx/filesystem/paths.hpp index 04ffce5..b3767aa 100644 --- a/src/myx/filesystem/paths.hpp +++ b/src/myx/filesystem/paths.hpp @@ -85,6 +85,10 @@ class Paths /// @brief Расширение для файла настройки QString m_configFileExtension; + void setupSystemDirectories( const QString& defaultEtcDirectory, + const QString& defaultConstDataDirectory, + const QString& defaultVarDataDirectory, + const QString& defaultLogDirectory ); HierarchyType getHierarchyType(); protected: