[libcalamares] Re-do createTargetDirs()

- Drop the basedirs idea, replace return with just bool
 - Use QDir::mkpath, with some extra validation
 - Test it a bit
main
Adriaan de Groot 5 years ago
parent 8b8ecf7b7b
commit e65969d587

@ -347,27 +347,35 @@ System::removeTargetFile( const QString& path ) const
// If it was empty, a warning was already printed // If it was empty, a warning was already printed
} }
int bool
System::createTargetBasedirs( const QString& path ) const System::createTargetDirs( const QString& path ) const
{ {
if ( !isAbsolutePath( path ) ) if ( !isAbsolutePath( path ) )
{ {
cWarning() << "Will not create basedirs for non-absolute path" << path; cWarning() << "Will not create basedirs for non-absolute path" << path;
return -1; return false;
} }
QString target = targetPath( path ); QString target = targetPath( path );
if ( target.isEmpty() ) if ( target.isEmpty() )
{ {
// If it was empty, a warning was already printed // If it was empty, a warning was already printed
return -1; return false;
} }
QString base( "/" ); QString root = Calamares::JobQueue::instance()->globalStorage()->value( "rootMountPoint" ).toString();
QStringList parts = target.split( '/', QString::SplitBehavior::SkipEmptyParts ); if ( root.isEmpty() )
{
return false;
}
cDebug() << parts; QDir d( root );
return -1; if ( !d.exists() )
{
cWarning() << "Root mountpoint" << root << "does not exist.";
return false;
}
return d.mkpath( target ); // This re-does everything starting from the **host** /
} }

@ -257,14 +257,14 @@ public:
/** @brief Ensure that the directories above @p path exist /** @brief Ensure that the directories above @p path exist
* *
* @param path a full pathname to a desired file. * @param path a full pathname to a desired directory.
* *
* All the directory components before the last path component are * All the directory components including the last path component are
* created, as needed, with 0755 permissions. Returns the number * created, as needed, with 0755 permissions. Returns true on success.
* of components that needed to be created; 0 if they all already *
* existed, and < 0 on failure. * @see QDir::mkpath
*/ */
DLLEXPORT int createTargetBasedirs( const QString& path ) const; DLLEXPORT bool createTargetDirs( const QString& path ) const;
/** /**
* @brief getTotalMemoryB returns the total main memory, in bytes. * @brief getTotalMemoryB returns the total main memory, in bytes.

@ -25,13 +25,14 @@
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "JobQueue.h" #include "JobQueue.h"
#include <QTemporaryFile> #include <QDir>
// #include <QTemporaryFile>
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <fcntl.h> // #include <fcntl.h>
#include <sys/stat.h> // #include <sys/stat.h>
#include <unistd.h> // #include <unistd.h>
class TestPaths : public QObject class TestPaths : public QObject
{ {
@ -47,6 +48,7 @@ private Q_SLOTS:
void testTargetPath(); void testTargetPath();
void testCreateTarget(); void testCreateTarget();
void testCreateTargetBasedirs();
private: private:
CalamaresUtils::System* m_system = nullptr; // Points to singleton instance, not owned CalamaresUtils::System* m_system = nullptr; // Points to singleton instance, not owned
@ -126,6 +128,33 @@ TestPaths::testCreateTarget()
QVERIFY( !fi2.exists() ); QVERIFY( !fi2.exists() );
} }
struct DirRemover
{
DirRemover( const QString& base, const QString& dir )
: m_base( base )
, m_dir( dir )
{
}
~DirRemover() { QDir( m_base ).rmpath( m_dir ); }
bool exists() const { return QDir( m_base ).exists( m_dir ); }
QString m_base, m_dir;
};
void
TestPaths::testCreateTargetBasedirs()
{
{
DirRemover dirrm( "/tmp", "var/lib/dbus" );
QVERIFY( m_system->createTargetDirs( "/" ) );
QVERIFY( m_system->createTargetDirs( "/var/lib/dbus" ) );
QVERIFY( QFile( "/tmp/var/lib/dbus" ).exists() );
QVERIFY( dirrm.exists() );
}
QVERIFY( !QFile( "/tmp/var/lib/dbus" ).exists() );
}
QTEST_GUILESS_MAIN( TestPaths ) QTEST_GUILESS_MAIN( TestPaths )
#include "utils/moc-warnings.h" #include "utils/moc-warnings.h"

Loading…
Cancel
Save