From 95936549e2be8e8f8c1aa343bf4b794d9c14204a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 7 Feb 2020 11:30:37 +0100 Subject: [PATCH] [libcalamares] Add a createTargetBasedirs() - Used to ensure that the directories leading up to a given path exist. Implementation is incomplete and broken for now. - While here, avoid removing an empty pathname in removeTargetFile() (the empty pathname indicates a broken configuration). --- .../utils/CalamaresUtilsSystem.cpp | 30 ++++++++++++++++++- src/libcalamares/utils/CalamaresUtilsSystem.h | 11 +++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index ede96305a..3b36a7ccf 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -338,7 +338,35 @@ System::removeTargetFile( const QString& path ) const cWarning() << "Will not remove non-absolute path" << path; return; } - QFile::remove( targetPath( path ) ); + QString target = targetPath( path ); + if ( !target.isEmpty() ) + { + QFile::remove( target ); + } + // If it was empty, a warning was already printed +} + +int +System::createTargetBasedirs(const QString& path) const +{ + if ( !isAbsolutePath( path ) ) + { + cWarning() << "Will not create basedirs for non-absolute path" << path; + return -1; + } + + QString target = targetPath( path ); + if ( target.isEmpty() ) + { + // If it was empty, a warning was already printed + return -1; + } + + QString base( "/" ); + QStringList parts = target.split( '/', QString::SplitBehavior::SkipEmptyParts ); + + cDebug() << parts; + return -1; } diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index 5c5e04eb2..09a606e2c 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -255,6 +255,17 @@ public: */ DLLEXPORT void removeTargetFile( const QString& path ) const; + /** @brief Ensure that the directories above @p path exist + * + * @param path a full pathname to a desired file. + * + * All the directory components before the last path component are + * created, as needed, with 0755 permissions. Returns the number + * of components that needed to be created; 0 if they all already + * existed, and < 0 on failure. + */ + DLLEXPORT int createTargetBasedirs( const QString& path ) const; + /** * @brief getTotalMemoryB returns the total main memory, in bytes. *