From b7890d865f33f59e630c2e715509d2bd9482e481 Mon Sep 17 00:00:00 2001
From: Adriaan de Groot <groot@kde.org>
Date: Wed, 23 May 2018 09:09:03 -0400
Subject: [PATCH] [preservefiles] Save GS, munge destination

 - using `from: config` now writes a JSON file
 - using @@ROOT@@ and @@USER@@ in dest does a sensible
   substitution.
---
 src/modules/preservefiles/PreserveFiles.cpp  | 30 ++++++++++++++++----
 src/modules/preservefiles/preservefiles.conf |  7 ++++-
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/src/modules/preservefiles/PreserveFiles.cpp b/src/modules/preservefiles/PreserveFiles.cpp
index 9ef473f27..6490f8303 100644
--- a/src/modules/preservefiles/PreserveFiles.cpp
+++ b/src/modules/preservefiles/PreserveFiles.cpp
@@ -54,6 +54,20 @@ QString targetPrefix()
     return QLatin1Literal( "/" );
 }
 
+QString atReplacements( QString s )
+{
+    Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
+    QString root( "/" );
+    QString user;
+
+    if ( gs && gs->contains( "rootMountPoint" ) )
+        root = gs->value( "rootMountPoint" ).toString();
+    if ( gs && gs->contains( "username" ) )
+        user = gs->value( "username" ).toString();
+
+    return s.replace( "@@ROOT@@", root ).replace( "@@USER@@", user );
+}
+
 PreserveFiles::PreserveFiles( QObject* parent )
     : Calamares::CppJob( parent )
 {
@@ -82,13 +96,19 @@ Calamares::JobResult PreserveFiles::exec()
     for ( const auto it : m_items )
     {
         QString source = it.source;
+        QString dest = prefix + atReplacements( it.dest );
+
         if ( it.type == ItemType::Log )
             source = Logger::logFile();
         if ( it.type == ItemType::Config )
-            cDebug() << "Config-preserving is not implemented yet.";
-
-        if ( source.isEmpty() )
-            cWarning() << "Skipping unnamed source file for" << it.dest;
+        {
+            if ( Calamares::JobQueue::instance()->globalStorage()->save( dest ) )
+                cWarning() << "Could not write config for" << dest;
+            else
+                ++count;
+        }
+        else if ( source.isEmpty() )
+            cWarning() << "Skipping unnamed source file for" << dest;
         else
         {
             QFile sourcef( source );
@@ -98,7 +118,7 @@ Calamares::JobResult PreserveFiles::exec()
                 continue;
             }
 
-            QFile destf( prefix + it.dest );
+            QFile destf( dest );
             if ( !destf.open( QFile::WriteOnly ) )
             {
                 sourcef.close();
diff --git a/src/modules/preservefiles/preservefiles.conf b/src/modules/preservefiles/preservefiles.conf
index 4c33d93d9..ab9114d20 100644
--- a/src/modules/preservefiles/preservefiles.conf
+++ b/src/modules/preservefiles/preservefiles.conf
@@ -15,7 +15,12 @@
 #   - *src*, to refer to a path interpreted in the host system. Relative
 #     paths are not recommended, and are interpreted relative to where
 #     Calamares is being run.
-#   Only one of the two other keys may be set.
+#   Only one of the two other keys (either *from* or *src*) may be set.
+#
+# The target filename is modified as follows:
+#   - `@@ROOT@@` is replaced by the path to the target root (may be /)
+#   - `@@USER@@` is replaced by the username entered by on the user
+#       page (may be empty, for instance if no user page is enabled)
 #
 # Special values for the key *from* are:
 # - *log*, for the complete log file (up to the moment the preservefiles