[preservefiles] Introduce the notion of optionally-preserved files

This commit is contained in:
Adriaan de Groot 2021-12-13 16:34:38 +01:00
parent 445ed870cc
commit 778c2855f4
4 changed files with 37 additions and 19 deletions

View File

@ -12,6 +12,7 @@
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include "utils/Units.h"
#include "utils/Variant.h"
#include <QFile>
@ -56,7 +57,7 @@ Item::fromVariant( const QVariant& v, const CalamaresUtils::Permissions& default
QString filename = v.toString();
if ( !filename.isEmpty() )
{
return { filename, filename, defaultPermissions, ItemType::Path };
return { filename, filename, defaultPermissions, ItemType::Path, false };
}
else
{
@ -70,6 +71,7 @@ Item::fromVariant( const QVariant& v, const CalamaresUtils::Permissions& default
CalamaresUtils::Permissions perm( defaultPermissions );
ItemType t = ItemType::None;
bool optional = CalamaresUtils::getBool( map, "optional", false );
{
QString perm_string = map[ "perm" ].toString();
@ -99,11 +101,11 @@ Item::fromVariant( const QVariant& v, const CalamaresUtils::Permissions& default
switch ( t )
{
case ItemType::Config:
return { QString(), dest, perm, t };
return { QString(), dest, perm, t, optional };
case ItemType::Log:
return { QString(), dest, perm, t };
return { QString(), dest, perm, t, optional };
case ItemType::Path:
return { map[ "src" ].toString(), dest, perm, t };
return { map[ "src" ].toString(), dest, perm, t, optional };
case ItemType::None:
cWarning() << "Invalid type for preservefiles, item" << v;
return {};

View File

@ -35,14 +35,16 @@ class Item
QString source;
QString dest;
CalamaresUtils::Permissions perm;
ItemType m_type;
ItemType m_type = ItemType::None;
bool m_optional = false;
public:
Item( const QString& src, const QString& d, CalamaresUtils::Permissions p, ItemType t )
Item( const QString& src, const QString& d, CalamaresUtils::Permissions p, ItemType t, bool optional )
: source( src )
, dest( d )
, perm( std::move( p ) )
, m_type( t )
, m_optional( optional )
{
}
@ -53,6 +55,7 @@ public:
operator bool() const { return m_type != ItemType::None; }
ItemType type() const { return m_type; }
bool isOptional() const { return m_optional; }
bool exec( const std::function< QString( QString ) >& replacements ) const;

View File

@ -72,7 +72,9 @@ PreserveFiles::exec()
++count;
continue;
}
if ( it.exec( atReplacements ) )
// Try to preserve the file. If it's marked as optional, count it
// as a success regardless.
if ( it.exec( atReplacements ) || it.isOptional() )
{
++count;
}

View File

@ -7,24 +7,38 @@
# the list should have one of these forms:
#
# - an absolute path (probably within the host system). This will be preserved
# as the same path within the target system (chroot). If, globally, dontChroot
# is true, then these items are ignored (since the destination is the same
# as the source).
# as the same path within the target system (chroot). If, globally,
# *dontChroot* is true, then these items will be ignored (since the
# destination is the same as the source).
# - a map with a *dest* key. The *dest* value is a path interpreted in the
# target system (if the global *dontChroot* is true, then the host is the
# target as well). Relative paths are not recommended. There are three
# possible other keys in the map:
# target as well). Relative paths are not recommended. There are two
# ways to select the source data for the file:
# - *from*, which must have one of the values, below; it is used to
# preserve files whose pathname is known to Calamares internally.
# - *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.
# Exactly one of the two source keys (either *from* or *src*) must be set.
#
# Special values for the key *from* are:
# - *log*, for the complete log file (up to the moment the preservefiles
# module is run),
# - *config*, for a JSON dump of the contents of global storage.
# Note that this may contain sensitive information, and should be
# given restrictive permissions.
#
# A map with a *dest* key can have these additional fields:
# - *perm*, is a colon-separated tuple of <user>:<group>:<mode>
# where <mode> is in octal (e.g. 4777 for wide-open, 0400 for read-only
# by owner). If set, the file's ownership and permissions are set to
# those values within the target system; if not set, no permissions
# are changed.
# Exactly one of the two source keys (either *from* or *src*) must be set.
# - *optional*, is a boolean; if this is set to `true` then failure to
# preserve the file will **not** be counted as a failure of the
# module, and installation will proceed. Set this for files that might
# not exist in the host system (e.g. nvidia configuration files that
# are created in some boot scenarios and not in others).
#
# The target path (*dest*) is modified as follows:
# - `@@ROOT@@` is replaced by the path to the target root (may be /).
@ -33,12 +47,6 @@
# - `@@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
# module is run),
# - *config*, for a JSON dump of the contents of global storage.
# Note that this may contain sensitive information, and should be
# given restrictive permissions.
#
#
files:
@ -48,6 +56,9 @@ files:
- from: config
dest: /var/log/Calamares-install.json
perm: root:wheel:600
# - src: /var/log/nvidia.conf
# dest: /var/log/Calamares-nvidia.conf
# optional: true
# The *perm* key contains a default value to apply to all files listed
# above that do not have a *perm* key of their own. If not set,