From 1a8543537265cd63bcb38d736c5cb0e15adb3fc0 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 5 Jul 2019 13:05:49 +0200 Subject: [PATCH] [libcalamares] Get target path relative to host / --- .../utils/CalamaresUtilsSystem.cpp | 14 +++++++++- src/libcalamares/utils/CalamaresUtilsSystem.h | 26 ++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.cpp b/src/libcalamares/utils/CalamaresUtilsSystem.cpp index 698a96f30..10c76e482 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.cpp +++ b/src/libcalamares/utils/CalamaresUtilsSystem.cpp @@ -244,7 +244,7 @@ System::runCommand( } QString -System::createTargetFile( const QString& path, const QByteArray& contents ) +System::targetPath( const QString& path ) const { QString completePath; @@ -265,6 +265,18 @@ System::createTargetFile( const QString& path, const QByteArray& contents ) completePath = QStringLiteral( "/" ) + path; } + return completePath; +} + +QString +System::createTargetFile( const QString& path, const QByteArray& contents ) const +{ + QString completePath = targetPath( path ); + if ( completePath.isEmpty() ) + { + return QString(); + } + QFile f( completePath ); if ( f.exists() ) { diff --git a/src/libcalamares/utils/CalamaresUtilsSystem.h b/src/libcalamares/utils/CalamaresUtilsSystem.h index 8c8e61a5e..f778b67c9 100644 --- a/src/libcalamares/utils/CalamaresUtilsSystem.h +++ b/src/libcalamares/utils/CalamaresUtilsSystem.h @@ -205,9 +205,27 @@ public: return targetEnvOutput( QStringList{ command }, output, workingPath, stdInput, timeoutSec ); } + + /** @brief Gets a path to a file in the target system, from the host. + * + * @param path Path to the file; this is interpreted + * from the root of the target system (whatever that may be, + * but / in the chroot, or / in OEM modes). + * + * @return The complete path to the target file, from + * the root of the host system, or empty on failure. + * + * For instance, during installation where the target root is + * mounted on /tmp/calamares-something, asking for targetPath("/etc/passwd") + * will give you /tmp/calamares-something/etc/passwd. + * + * No attempt is made to canonicalize anything, since paths might not exist. + */ + DLLEXPORT QString targetPath( const QString& path ) const; + /** @brief Create a (small-ish) file in the target system. * - * @param path Path to the file; this is **always** interpreted + * @param path Path to the file; this is interpreted * from the root of the target system (whatever that may be, * but / in the chroot, or / in OEM modes). * @param contents Actual content of the file. @@ -215,9 +233,11 @@ public: * Will not overwrite files. Returns an empty string if the * target file already exists. * - * @return The complete canonical path to the target file, or empty on failure. + * @return The complete canonical path to the target file from the + * root of the host system, or empty on failure. (Here, it is + * possible to be canonical because the file exists). */ - QString createTargetFile( const QString& path, const QByteArray& contents ); + DLLEXPORT QString createTargetFile( const QString& path, const QByteArray& contents ) const; /** * @brief getTotalMemoryB returns the total main memory, in bytes.