From b86cf01973ffc8c764dc422b278956539bb581d4 Mon Sep 17 00:00:00 2001 From: Andrey Astafyev Date: Wed, 3 Jun 2020 12:20:59 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D1=91?= =?UTF-8?q?=D0=BD=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=BA=D0=B0?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=D0=BE=D0=B3=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 198 ++++++++++++++++++++- examples/filesystem/01_minimal/minimal.cpp | 15 +- src/myx/filesystem/CMakeLists.txt | 10 +- src/myx/filesystem/current_executable.cpp | 41 ----- src/myx/filesystem/current_executable.hpp | 51 ------ src/myx/filesystem/paths.cpp | 46 +++-- src/myx/filesystem/paths.hpp | 7 +- src/myx/filesystem/paths_mt.cpp | 44 ----- src/myx/filesystem/paths_mt.hpp | 59 ------ 9 files changed, 240 insertions(+), 231 deletions(-) delete mode 100644 src/myx/filesystem/current_executable.cpp delete mode 100644 src/myx/filesystem/current_executable.hpp delete mode 100644 src/myx/filesystem/paths_mt.cpp delete mode 100644 src/myx/filesystem/paths_mt.hpp diff --git a/.gitignore b/.gitignore index d1e6e2a..817c82c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,200 @@ -CMakeLists.txt.user* +# Каталог для результатов сборки проекта _build + +# Каталог для тестирования установки _output -.cmake/ -*.autosave + +# Файлы настроек, редактируемые во время отладки, +# за исключением шаблонных файлов +files/etc/*.conf +!files/etc/*.example.conf + +# Каталоги, в которые разрешена запись данных во время работы программы files/lib/* files/log/* + +### +### Общие настройки для C, C++, Fortran, Qt, CMake, Ninja, LaTeX и редакторов +### + +### +### Временные файлы текстовых редакторов +### +*.bak +*.gho +*.tmp +*.dotdropbak + +### +### LyX +### +*.lyx~ +*.lyx# + +### +### Vim +### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + + +### +### C++ +### + +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.so.* +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + + +### +### CMake +### +CMakeLists.txt.user* +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + + +### +### Ninja +### +.ninja_deps +.ninja_log + + +### +### GCC coverage testing tool files +### +*.gcno +*.gcda +*.gcov + + +### +### Qt +### +object_script.*.Release +object_script.*.Debug +*_plugin_import.cpp +/.qmake.cache +/.qmake.stash +*.pro.user +*.pro.user.* +*.qbs.user +*.qbs.user.* +*.moc +moc_*.cpp +moc_*.h +qrc_*.cpp +ui_*.h +*.qmlc +*.jsc +Makefile* +*build-* +*.qm +*.prl + +# Qt unit tests +target_wrapper.* + +# QtCreator +*.autosave + +# QtCreator Qml +*.qmlproject.user +*.qmlproject.user.* + +# QtCreator local machine specific files for imported projects +*creator.user* + +*_qmlcache.qrc + + +### +### Latex +### +*.acn +*.acr +*.alg +*.aux +*.bbl +*.blg +*.dvi +*.fdb_latexmk +*.glg +*.glo +*.gls +*.idx +*.ilg +*.ind +*.ist +*.lof +*.log +*.lot +*.maf +*.mtc +*.mtc0 +*.nav +*.nlo +*.out +*.pdfsync +*.ps +*.snm +*.synctex.gz +*.toc +*.vrb +*.xdy +*.tdo + diff --git a/examples/filesystem/01_minimal/minimal.cpp b/examples/filesystem/01_minimal/minimal.cpp index a1e16f8..739a6db 100644 --- a/examples/filesystem/01_minimal/minimal.cpp +++ b/examples/filesystem/01_minimal/minimal.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include @@ -10,26 +9,18 @@ namespace MF = myx::filesystem; -// Переменные для защиты экземпляра класса MF::PathsMT -std::atomic< MF::PathsMT* > MF::PathsMT::sInstance; -std::mutex MF::PathsMT::sMutex; - int main( int argc, char** argv ) { (void)argc; (void)argv; QCoreApplication::setApplicationName( QStringLiteral( CMLIB_PROJECT_NAME ) ); - MF::PathsMT& pathsMT = MF::PathsMT::instance(); - MF::Paths& paths = MF::Paths::instance(); - - pathsMT.init( QStringLiteral( CMLIB_PROJECT_NAME ), QStringLiteral( "conf" ) ); - pathsMT.findConfigFile( QStringLiteral( "test" ) ); - qDebug() << pathsMT.systemLogDirectory(); - qDebug() << pathsMT.systemConfigDirectory(); + MF::Paths& paths = MF::Paths::instance(); paths.init( QStringLiteral( CMLIB_PROJECT_NAME ), QStringLiteral( "conf" ) ); + qDebug() << "prefixDirectory : " << paths.prefixDirectory(); + qDebug() << "executableName : " << paths.executableName(); qDebug() << "executableFilePath : " << paths.executableFilePath(); qDebug() << "executableDirectory : " << paths.executableDirectory(); diff --git a/src/myx/filesystem/CMakeLists.txt b/src/myx/filesystem/CMakeLists.txt index 283eb5e..be1f89a 100644 --- a/src/myx/filesystem/CMakeLists.txt +++ b/src/myx/filesystem/CMakeLists.txt @@ -3,16 +3,10 @@ set(TRGT filesystem) # cmake-format: off # Список файлов исходных текстов -set(TRGT_cpp - ${CMAKE_CURRENT_SOURCE_DIR}/current_executable.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/paths_mt.cpp) +set(TRGT_cpp ${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp) # Список заголовочных файлов -set(TRGT_hpp - ${CMAKE_CURRENT_SOURCE_DIR}/current_executable.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/paths.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/paths_mt.hpp) +set(TRGT_hpp ${CMAKE_CURRENT_SOURCE_DIR}/paths.hpp) set(TRGT_headers ${TRGT_hpp}) # cmake-format: on diff --git a/src/myx/filesystem/current_executable.cpp b/src/myx/filesystem/current_executable.cpp deleted file mode 100644 index 95c8ae8..0000000 --- a/src/myx/filesystem/current_executable.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef MYX_BASE_CURRENT_EXECUTABLE_CPP_ -#define MYX_BASE_CURRENT_EXECUTABLE_CPP_ - -#ifndef MYXLIB_HEADER_ONLY -#include -#else -#pragma once -#endif - -#include - -#include - -#include -#include - -namespace myx { - -namespace filesystem { - -#if !defined ( __linux__ ) -error "Class CurrentExecutable is supported only in Linux" -#endif - -MYXLIB_INLINE CurrentExecutable::CurrentExecutable() : - m_procFilePath( QStringLiteral( "/proc/self/exe" ) ) -{ - m_canonicalFilePath = m_procFilePath.canonicalFilePath(); -} - - -MYXLIB_INLINE const QFileInfo& CurrentExecutable::canonicalFilePath() const -{ - return( m_canonicalFilePath ); -} - -} // namespace filesystem - -} // namespace myx - -#endif // MYX_BASE_CURRENT_EXECUTABLE_CPP_ diff --git a/src/myx/filesystem/current_executable.hpp b/src/myx/filesystem/current_executable.hpp deleted file mode 100644 index f0c027c..0000000 --- a/src/myx/filesystem/current_executable.hpp +++ /dev/null @@ -1,51 +0,0 @@ -/** - * @file current_executable.hpp - * @brief Параметры исполняемого файла - */ - -#ifndef MYX_FILESYSTEM_CURRENT_EXECUTABLE_HPP_ -#define MYX_FILESYSTEM_CURRENT_EXECUTABLE_HPP_ - -#pragma once - -#include - -#include -#include -#include - - -namespace myx { - -namespace filesystem { - -class CurrentExecutable -{ - /// @brief Путь к символической ссылке, указывающей на текущий исполняемый файл - QFileInfo m_procFilePath; - - /// @brief Канонический путь к текущему исполняемому файлу - QFileInfo m_canonicalFilePath; - - friend class Paths; -public: - /** - * @brief Конструктор, собирающий информацию о текущем исполняемом файле. - */ - CurrentExecutable(); - - /** - * @brief Канонический путь к текущему исполняемому файлу - */ - const QFileInfo& canonicalFilePath() const; -}; // class CurrentExecutable - -} // namespace filesystem - -} // namespace myx - -#ifdef MYXLIB_HEADER_ONLY -#include "current_executable.cpp" -#endif - -#endif // MYX_FILESYSTEM_CURRENT_EXECUTABLE_HPP_ diff --git a/src/myx/filesystem/paths.cpp b/src/myx/filesystem/paths.cpp index 812cef7..deec05c 100644 --- a/src/myx/filesystem/paths.cpp +++ b/src/myx/filesystem/paths.cpp @@ -8,7 +8,6 @@ #endif #include -#include #include @@ -22,11 +21,22 @@ namespace filesystem { MYXLIB_INLINE Paths::Paths() = default; -MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultEtcDirectory, +MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultPrefixDirectory, + const QString& defaultEtcDirectory, const QString& defaultConstDataDirectory, const QString& defaultVarDataDirectory, const QString& defaultLogDirectory ) { + QFileInfo prefixDirInfo { defaultPrefixDirectory }; + if ( prefixDirInfo.isDir() && prefixDirInfo.isReadable() ) + { + m_prefixDirectory = defaultPrefixDirectory; + } + else + { + m_prefixDirectory = "."; + } + QFileInfo etcDirInfo { defaultEtcDirectory }; if ( etcDirInfo.isDir() && etcDirInfo.isReadable() ) { @@ -77,7 +87,8 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() if ( binUnityRegexp.indexIn( binaryDir ) >= 0 ) { binaryDir.remove( binUnityRegexp ); - setupSystemDirectories( binaryDir + "/etc", + setupSystemDirectories( binaryDir, + binaryDir + "/etc", binaryDir + "/files/data", binaryDir + "/files/lib", binaryDir + "/files/log" ); @@ -88,6 +99,7 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() QRegExp binRegexp( "/s*bin$" ); if ( binRegexp.indexIn( binaryDir ) == -1 ) { + m_prefixDirectory = m_executableDirectory; m_systemConstDataDirectory = m_executableDirectory; m_systemVarDataDirectory = m_executableDirectory; m_systemConfigDirectory = m_executableDirectory; @@ -100,7 +112,8 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() if ( optRegexp.indexIn( binaryDir ) >= 0 ) { binaryDir.remove( binRegexp ); - setupSystemDirectories( binaryDir + "/etc", + setupSystemDirectories( binaryDir, + binaryDir + "/etc", binaryDir + "/files/data", binaryDir + "/files/lib", binaryDir + "/files/log" ); @@ -110,7 +123,8 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() if ( binaryDir.startsWith( QStringLiteral( "/usr/local" ) ) ) { - setupSystemDirectories( "/usr/local/etc/" + m_projectName, + setupSystemDirectories( "/usr/local", + "/usr/local/etc/" + m_projectName, "/usr/local/share/" + m_projectName, "/var/lib/" + m_projectName, "/var/log/" + m_projectName ); @@ -120,7 +134,8 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() if ( binaryDir.startsWith( QStringLiteral( "/usr" ) ) ) { - setupSystemDirectories( "/etc/" + m_projectName, + setupSystemDirectories( "/usr", + "/etc/" + m_projectName, "/usr/share/" + m_projectName, "/var/lib/" + m_projectName, "/var/log/" + m_projectName ); @@ -131,6 +146,7 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() if ( binaryDir.startsWith( m_homeDirectory + "/.local/bin" ) || binaryDir.startsWith( m_homeDirectory + "/bin" ) ) { + m_prefixDirectory = m_homeDirectory; m_systemConfigDirectory = m_userConfigDirectory; m_systemConstDataDirectory = m_userConstDataDirectory; m_systemVarDataDirectory = m_userVarDataDirectory; @@ -140,7 +156,8 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() } binaryDir.remove( binRegexp ); - setupSystemDirectories( binaryDir + "/etc", + setupSystemDirectories( binaryDir, + binaryDir + "/etc", binaryDir + "/files/data", binaryDir + "/files/lib", binaryDir + "/files/log" ); @@ -151,10 +168,11 @@ MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() MYXLIB_INLINE bool Paths::init( const QString& projectDir, const QString& configFileExtension ) { - CurrentExecutable exe; - m_executableName = exe.m_canonicalFilePath.fileName(); - m_executableFilePath = exe.m_canonicalFilePath.absoluteFilePath(); - m_executableDirectory = exe.m_canonicalFilePath.absolutePath(); + QFileInfo procSelfExe( QStringLiteral( "/proc/self/exe" ) ); + QFileInfo currentExecutable = procSelfExe.canonicalFilePath(); + m_executableName = currentExecutable.fileName(); + m_executableFilePath = currentExecutable.absoluteFilePath(); + m_executableDirectory = currentExecutable.absolutePath(); m_projectName = projectDir.isEmpty() ? m_executableName : projectDir; @@ -334,6 +352,12 @@ MYXLIB_INLINE const QString& Paths::executableDirectory() const return( m_executableDirectory ); } + +MYXLIB_INLINE const QString& Paths::prefixDirectory() const +{ + return( m_prefixDirectory ); +} + } // namespace filesystem } // namespace myx diff --git a/src/myx/filesystem/paths.hpp b/src/myx/filesystem/paths.hpp index b3767aa..802d020 100644 --- a/src/myx/filesystem/paths.hpp +++ b/src/myx/filesystem/paths.hpp @@ -9,7 +9,6 @@ #pragma once #include -#include #include #include @@ -53,6 +52,9 @@ class Paths QString m_executableFilePath; QString m_executableDirectory; + /// @brief Общий префикс для файлов проекта + QString m_prefixDirectory; + /// @brief Путь к каталогу с временными файлами QString m_tempDirectory; /// @brief Путь к домашнему каталогу текущего пользователя @@ -85,7 +87,8 @@ class Paths /// @brief Расширение для файла настройки QString m_configFileExtension; - void setupSystemDirectories( const QString& defaultEtcDirectory, + void setupSystemDirectories( const QString& defaultPrefixDirectory, + const QString& defaultEtcDirectory, const QString& defaultConstDataDirectory, const QString& defaultVarDataDirectory, const QString& defaultLogDirectory ); diff --git a/src/myx/filesystem/paths_mt.cpp b/src/myx/filesystem/paths_mt.cpp deleted file mode 100644 index b1adf3c..0000000 --- a/src/myx/filesystem/paths_mt.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef MYX_BASE_PATHS_MT_CPP_ -#define MYX_BASE_PATHS_MT_CPP_ - -#ifndef MYXLIB_HEADER_ONLY -#include -#else -#pragma once -#endif - -#include -#include - -#include - -#include -#include - -namespace myx { - -namespace filesystem { - -MYXLIB_INLINE PathsMT::PathsMT() = default; - -MYXLIB_INLINE PathsMT& PathsMT::instance() -{ - volatile PathsMT* localInstance = sInstance.load( std::memory_order_acquire ); - if ( localInstance == nullptr ) - { - std::lock_guard< std::mutex > myLock( sMutex ); - localInstance = sInstance.load( std::memory_order_relaxed ); - if ( localInstance == nullptr ) // -V1036 - { - localInstance = new PathsMT(); - sInstance.store( const_cast< PathsMT* >( localInstance ), std::memory_order_release ); // NOLINT - } - } - return( const_cast< PathsMT& >( *localInstance ) ); // NOLINT -} - -} // namespace filesystem - -} // namespace myx - -#endif // MYX_BASE_PATHS_MT_CPP_ diff --git a/src/myx/filesystem/paths_mt.hpp b/src/myx/filesystem/paths_mt.hpp deleted file mode 100644 index 3247c75..0000000 --- a/src/myx/filesystem/paths_mt.hpp +++ /dev/null @@ -1,59 +0,0 @@ -/** - * @file paths.hpp - * @brief Стандартные пути к каталогам и файлам - */ - -#ifndef MYX_FILESYSTEM_PATHS_MT_HPP_ -#define MYX_FILESYSTEM_PATHS_MT_HPP_ - -#pragma once - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -namespace myx { - -namespace filesystem { - -/// @brief Потокобезопасная версия класса myx::filesystem::Paths -class PathsMT : public Paths -{ - PathsMT(); - ~PathsMT() = default; - - static std::atomic< PathsMT* > sInstance; - static std::mutex sMutex; - - -public: - PathsMT( const PathsMT& ) = delete; - PathsMT& operator=( const PathsMT& ) = delete; - PathsMT( PathsMT&& ) = delete; - PathsMT& operator=( PathsMT&& ) = delete; - - /** - * @brief getInstance - * @return Уникальный экземпляр класса PathsMT - */ - static PathsMT& instance(); -}; // class PathsMT - -} // namespace filesystem - -} // namespace myx - -#ifdef MYXLIB_HEADER_ONLY -#include "paths_mt.cpp" -#endif - -#endif // MYX_FILESYSTEM_PATHS_MT_HPP_