HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/sysupdate/sysupdate-transfer.c
Git blob: 930e453737dc04cdb2dcf4749faaa2d96460f7af
Lines 1–1000 of 1,862
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "sd-id128.h"
#include "alloc-util.h"
#include "build-path.h"
#include "chase.h"
#include "conf-parser.h"
#include "dirent-util.h"
#include "errno-util.h"
#include "event-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "gpt.h"
#include "hashmap.h"
#include "hexdecoct.h"
#include "install-file.h"
#include "mkdir.h"
#include "notify-recv.h"
#include "parse-helpers.h"
#include "parse-util.h"
#include "path-util.h"
#include "percent-util.h"
#include "pidref.h"
#include "process-util.h"
#include "rm-rf.h"
#include "signal-util.h"
#include "specifier.h"
#include "stdio-util.h"
#include "strv.h"
#include "sync-util.h"
#include "sysupdate.h"
#include "sysupdate-cleanup.h"
#include "sysupdate-config.h"
#include "sysupdate-feature.h"
#include "sysupdate-instance.h"
#include "sysupdate-pattern.h"
#include "sysupdate-resource.h"
#include "sysupdate-transfer.h"
#include "time-util.h"
#include "web-util.h"
/* Default value for InstancesMax= for fs object targets */
#define DEFAULT_FILE_INSTANCES_MAX 3
enum {
MATCH_PATTERN_SOURCE,
MATCH_PATTERN_TARGET,
};
Transfer* transfer_free(Transfer *t) {
if (!t)
return NULL;
free(t->temporary_partial_path);
free(t->temporary_pending_path);
free(t->id);
free(t->min_version);
strv_free(t->protected_versions);
free(t->current_symlink);
free(t->final_path);
strv_free(t->features);
strv_free(t->requisite_features);
strv_free(t->changelog);
strv_free(t->appstream);
partition_info_destroy(&t->partition_info);
free(t->final_partition_label);
resource_destroy(&t->source);
resource_destroy(&t->target);
return mfree(t);
}
Transfer* transfer_new(Context *ctx) {
Transfer *t;
t = new(Transfer, 1);
if (!t)
return NULL;
*t = (Transfer) {
.source.type = _RESOURCE_TYPE_INVALID,
.target.type = _RESOURCE_TYPE_INVALID,
.remove_temporary = true,
.mode = MODE_INVALID,
.tries_left = UINT64_MAX,
.tries_done = UINT64_MAX,
.verify = true,
/* the three flags, as configured by the user */
.no_auto = -1,
.read_only = -1,
.growfs = -1,
/* whether to fill up the rest of the partition with zeroes, as configured by the user */
.zero_fill = -1,
/* the read only flag, as ultimately determined */
.install_read_only = -1,
.partition_info = PARTITION_INFO_NULL,
.context = ctx,
};
return t;
}
static int config_parse_protect_version(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *resolved = NULL;
char ***protected_versions = ASSERT_PTR(data);
Transfer *t = ASSERT_PTR(userdata);
int r;
assert(rvalue);
if (isempty(rvalue)) {
*protected_versions = strv_free(*protected_versions);
return 0;
}
r = specifier_printf(rvalue, NAME_MAX, system_and_tmp_specifier_table, t->context->root, NULL, &resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in ProtectVersion=, ignoring: %s", rvalue);
return 0;
}
if (!version_is_valid(resolved, VERSION_ALLOW_UNDERSCORE|VERSION_ALLOW_PLUS)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"ProtectVersion= string is not valid, ignoring: %s", resolved);
return 0;
}
r = strv_extend(protected_versions, resolved);
if (r < 0)
return log_oom();
return 0;
}
static int config_parse_min_version(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *resolved = NULL;
char **version = ASSERT_PTR(data);
Transfer *t = ASSERT_PTR(userdata);
int r;
assert(rvalue);
if (isempty(rvalue)) {
*version = mfree(*version);
return 0;
}
r = specifier_printf(rvalue, NAME_MAX, system_and_tmp_specifier_table, t->context->root, NULL, &resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in MinVersion=, ignoring: %s", rvalue);
return 0;
}
if (!version_is_valid(resolved, VERSION_ALLOW_UNDERSCORE|VERSION_ALLOW_PLUS)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"MinVersion= string is not valid, ignoring: %s", resolved);
return 0;
}
return free_and_replace(*version, resolved);
}
static int config_parse_transfer_url_specifiers_many(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Transfer *t = ASSERT_PTR(userdata);
/* Here we expect userdata to point to our Transfer object, but config_parse_url_specifiers() wants
* the root dir there, hence fix this up */
return config_parse_url_specifiers_many(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, t->context->root);
}
static int config_parse_current_symlink(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *resolved = NULL;
char **current_symlink = ASSERT_PTR(data);
Transfer *t = ASSERT_PTR(userdata);
int r;
assert(rvalue);
if (isempty(rvalue)) {
*current_symlink = mfree(*current_symlink);
return 0;
}
r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, t->context->root, NULL, &resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in CurrentSymlink=, ignoring: %s", rvalue);
return 0;
}
r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
if (r < 0)
return 0;
return free_and_replace(*current_symlink, resolved);
}
static int config_parse_instances_max(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
uint64_t *instances_max = data, i;
int r;
assert(rvalue);
assert(data);
if (isempty(rvalue)) {
*instances_max = 0; /* Revert to default logic, see transfer_read_definition() */
return 0;
}
r = safe_atou64(rvalue, &i);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse InstancesMax= value, ignoring: %s", rvalue);
return 0;
}
if (i < 2) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"InstancesMax= value must be at least 2, bumping: %s", rvalue);
*instances_max = 2;
} else
*instances_max = i;
return 0;
}
static int config_parse_resource_pattern(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char ***patterns = ASSERT_PTR(data);
Transfer *t = ASSERT_PTR(userdata);
bool is_source = ltype == MATCH_PATTERN_SOURCE;
int r;
assert(rvalue);
if (isempty(rvalue)) {
*patterns = strv_free(*patterns);
return 0;
}
for (;;) {
_cleanup_free_ char *word = NULL, *resolved = NULL;
const char *body;
r = extract_first_word(&rvalue, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to extract first pattern from MatchPattern=, ignoring: %s", rvalue);
return 0;
}
if (r == 0)
break;
r = specifier_printf(word, NAME_MAX, system_and_tmp_specifier_table, t->context->root, NULL, &resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in MatchPattern=, ignoring: %s", rvalue);
return 0;
}
/* The glob directory prefix on a source MatchPattern= means "match the rest against the
* basename" thus the remainder must be a valid filename (no slashes).
* Target patterns can not use it. */
body = resolved;
if (pattern_skip_glob_directory_prefix(&body)) {
if (!is_source)
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
"'**/' prefix is only supported in a source MatchPattern=, refusing: %s", resolved);
if (!filename_is_valid(body))
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
"The pattern after a '**/' prefix must be a valid filename, refusing: %s", resolved);
}
/* The glob directory prefix is not allowed in the remainder and pattern_valid will catch this. */
r = pattern_valid(body);
if (r <= 0)
return log_syntax(unit, LOG_ERR, filename, line, r < 0 ? r : SYNTHETIC_ERRNO(EINVAL),
"MatchPattern= string is not valid, refusing: %s", resolved);
r = strv_consume(patterns, TAKE_PTR(resolved));
if (r < 0)
return log_oom();
}
strv_uniq(*patterns);
return 0;
}
static int config_parse_resource_path(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *resolved = NULL;
Resource *rr = ASSERT_PTR(data);
Transfer *t = ASSERT_PTR(userdata);
int r;
assert(rvalue);
if (streq(rvalue, "auto")) {
rr->path_auto = true;
rr->path = mfree(rr->path);
return 0;
}
r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, t->context->root, NULL, &resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in Path=, ignoring: %s", rvalue);
return 0;
}
/* Note that we don't validate the path as being absolute or normalized. We'll do that in
* transfer_read_definition() as we might not know yet whether Path refers to a URL or a file system
* path. */
rr->path_auto = false;
return free_and_replace(rr->path, resolved);
}
static DEFINE_CONFIG_PARSE_ENUM(config_parse_resource_type, resource_type, ResourceType);
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_resource_path_relto, path_relative_to, PathRelativeTo,
PATH_RELATIVE_TO_ROOT);
static int config_parse_resource_ptype(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Resource *rr = ASSERT_PTR(data);
int r;
assert(rvalue);
if (isempty(rvalue)) {
rr->partition_type = (GptPartitionType) {};
rr->partition_type_set = false;
return 0;
}
r = gpt_partition_type_from_string(rvalue, &rr->partition_type);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse partition type, ignoring: %s", rvalue);
return 0;
}
rr->partition_type_set = true;
return 0;
}
static int config_parse_partition_uuid(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Transfer *t = ASSERT_PTR(data);
int r;
assert(rvalue);
if (isempty(rvalue)) {
t->partition_uuid = SD_ID128_NULL;
t->partition_uuid_set = false;
return 0;
}
r = sd_id128_from_string(rvalue, &t->partition_uuid);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse partition UUID, ignoring: %s", rvalue);
return 0;
}
t->partition_uuid_set = true;
return 0;
}
static int config_parse_partition_flags(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Transfer *t = ASSERT_PTR(data);
int r;
assert(rvalue);
if (isempty(rvalue)) {
t->partition_flags = 0;
t->partition_flags_set = false;
return 0;
}
r = safe_atoux64(rvalue, &t->partition_flags);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse partition flags, ignoring: %s", rvalue);
return 0;
}
t->partition_flags_set = true;
return 0;
}
static bool transfer_decide_if_enabled(Transfer *t, Hashmap *known_features) {
assert(t);
/* Requisite feature disabled -> transfer disabled */
STRV_FOREACH(id, t->requisite_features) {
Feature *f = hashmap_get(known_features, *id);
if (!f || !f->enabled) /* missing features are implicitly disabled */
return false;
}
/* No features defined -> transfer implicitly enabled */
if (strv_isempty(t->features))
return true;
/* At least one feature enabled -> transfer enabled */
STRV_FOREACH(id, t->features) {
Feature *f = hashmap_get(known_features, *id);
if (f && f->enabled)
return true;
}
/* All listed features disabled -> transfer disabled */
return false;
}
int transfer_read_definition(Transfer *t, const char *path, const char **dirs, Hashmap *known_features) {
assert(t);
ConfigTableItem table[] = {
{ "Transfer", "MinVersion", config_parse_min_version, 0, &t->min_version },
{ "Transfer", "ProtectVersion", config_parse_protect_version, 0, &t->protected_versions },
{ "Transfer", "Verify", config_parse_bool, 0, &t->verify },
{ "Transfer", "ChangeLog", config_parse_transfer_url_specifiers_many, 0, &t->changelog },
{ "Transfer", "AppStream", config_parse_transfer_url_specifiers_many, 0, &t->appstream },
{ "Transfer", "Features", config_parse_strv, 0, &t->features },
{ "Transfer", "RequisiteFeatures", config_parse_strv, 0, &t->requisite_features },
{ "Source", "Type", config_parse_resource_type, 0, &t->source.type },
{ "Source", "Path", config_parse_resource_path, 0, &t->source },
{ "Source", "PathRelativeTo", config_parse_resource_path_relto, 0, &t->source.path_relative_to },
{ "Source", "MatchPattern", config_parse_resource_pattern, MATCH_PATTERN_SOURCE, &t->source.patterns },
{ "Target", "Type", config_parse_resource_type, 0, &t->target.type },
{ "Target", "Path", config_parse_resource_path, 0, &t->target },
{ "Target", "PathRelativeTo", config_parse_resource_path_relto, 0, &t->target.path_relative_to },
{ "Target", "MatchPattern", config_parse_resource_pattern, MATCH_PATTERN_TARGET, &t->target.patterns },
{ "Target", "MatchPartitionType", config_parse_resource_ptype, 0, &t->target },
{ "Target", "PartitionUUID", config_parse_partition_uuid, 0, t },
{ "Target", "PartitionFlags", config_parse_partition_flags, 0, t },
{ "Target", "PartitionNoAuto", config_parse_tristate, 0, &t->no_auto },
{ "Target", "PartitionGrowFileSystem", config_parse_tristate, 0, &t->growfs },
{ "Target", "PartitionZeroFill", config_parse_tristate, 0, &t->zero_fill },
{ "Target", "ReadOnly", config_parse_tristate, 0, &t->read_only },
{ "Target", "Mode", config_parse_mode, 0, &t->mode },
{ "Target", "TriesLeft", config_parse_uint64, 0, &t->tries_left },
{ "Target", "TriesDone", config_parse_uint64, 0, &t->tries_done },
{ "Target", "InstancesMax", config_parse_instances_max, 0, &t->instances_max },
{ "Target", "RemoveTemporary", config_parse_bool, 0, &t->remove_temporary },
{ "Target", "CurrentSymlink", config_parse_current_symlink, 0, &t->current_symlink },
{}
};
_cleanup_free_ char *filename = NULL;
char *e;
int r;
assert(path);
assert(dirs);
r = path_extract_filename(path, &filename);
if (r < 0)
return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);
r = config_parse_many_full(
STRV_MAKE_CONST(path),
dirs,
strjoina(filename, ".d"),
t->context->root,
/* root_fd= */ -EBADF,
"Transfer\0"
"Source\0"
"Target\0",
config_item_table_lookup, table,
CONFIG_PARSE_WARN,
t,
/* ret_stats_by_path= */ NULL,
/* ret_drop_in_files= */ NULL);
if (r < 0)
return r;
e = ASSERT_PTR(endswith(filename, ".transfer") ?: endswith(filename, ".conf"));
*e = 0; /* Remove the file extension */
t->id = TAKE_PTR(filename);
t->enabled = transfer_decide_if_enabled(t, known_features);
if (!RESOURCE_IS_SOURCE(t->source.type))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Source Type= must be one of url-file, url-tar, tar, regular-file, directory, subvolume.");
if (t->target.type < 0) {
switch (t->source.type) {
case RESOURCE_URL_FILE:
case RESOURCE_REGULAR_FILE:
t->target.type =
t->target.path && path_startswith(t->target.path, "/dev/") ?
RESOURCE_PARTITION : RESOURCE_REGULAR_FILE;
break;
case RESOURCE_URL_TAR:
case RESOURCE_TAR:
case RESOURCE_DIRECTORY:
t->target.type = RESOURCE_DIRECTORY;
break;
case RESOURCE_SUBVOLUME:
t->target.type = RESOURCE_SUBVOLUME;
break;
default:
assert_not_reached();
}
}
if (!RESOURCE_IS_TARGET(t->target.type))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Target Type= must be one of partition, regular-file, directory, subvolume.");
if (t->target.type == RESOURCE_PARTITION && !t->target.partition_type_set) {
t->target.partition_type = gpt_partition_type_from_uuid(SD_GPT_LINUX_GENERIC);
t->target.partition_type_set = true;
}
if ((IN_SET(t->source.type, RESOURCE_URL_FILE, RESOURCE_PARTITION, RESOURCE_REGULAR_FILE) &&
!IN_SET(t->target.type, RESOURCE_PARTITION, RESOURCE_REGULAR_FILE)) ||
(IN_SET(t->source.type, RESOURCE_URL_TAR, RESOURCE_TAR, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME) &&
!IN_SET(t->target.type, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME)))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Target type '%s' is incompatible with source type '%s', refusing.",
resource_type_to_string(t->target.type), resource_type_to_string(t->source.type));
if (!t->source.path && !t->source.path_auto)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Source specification lacks Path=.");
if (t->source.path_relative_to == PATH_RELATIVE_TO_EXPLICIT && !t->context->transfer_source)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"PathRelativeTo=explicit requires --transfer-source= to be specified.");
if (t->target.path_relative_to == PATH_RELATIVE_TO_EXPLICIT)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"PathRelativeTo=explicit can only be used in source specifications.");
if (t->source.path) {
if (RESOURCE_IS_FILESYSTEM(t->source.type) || t->source.type == RESOURCE_PARTITION)
if (!path_is_absolute(t->source.path) || !path_is_normalized(t->source.path))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Source path is not a normalized, absolute path: %s", t->source.path);
/* We unofficially support file:// in addition to http:// and https:// for url
* sources. That's mostly for testing, since it relieves us from having to set up a HTTP
* server, and CURL abstracts this away from us thankfully. */
if (RESOURCE_IS_URL(t->source.type))
if (!http_url_is_valid(t->source.path) && !file_url_is_valid(t->source.path))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Source path is not a valid HTTP or HTTPS URL: %s", t->source.path);
}
if (strv_isempty(t->source.patterns))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Source specification lacks MatchPattern=.");
if (IN_SET(t->source.type, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME) &&
resource_has_glob_directory_pattern(&t->source))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"MatchPattern= with '**/' prefix is not supported for source Type=directory and Type=subvolume, refusing.");
if (!t->target.path && !t->target.path_auto)
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Target specification lacks Path= field.");
if (t->target.path &&
(!path_is_absolute(t->target.path) || !path_is_normalized(t->target.path)))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Target path is not a normalized, absolute path: %s", t->target.path);
if (strv_isempty(t->target.patterns)) {
log_syntax(NULL, LOG_INFO, path, 1, 0, "Target specification lacks MatchPattern= expression. Assuming same value as in source specification.");
strv_free(t->target.patterns);
t->target.patterns = strv_copy(t->source.patterns);
if (!t->target.patterns)
return log_oom();
/* Strip any glob directory prefix when inheriting from source because it's only used for finding and
* not to replicate the same directory layout at the target, so we don't support it there. */
STRV_FOREACH(p, t->target.patterns) {
const char *body = *p;
if (pattern_skip_glob_directory_prefix(&body))
memmove(*p, body, strlen(body) + 1);
}
/* Stripping the glob directory prefix can turn distinct source patterns into duplicates,
* so re-uniq for parity with the source side. */
strv_uniq(t->target.patterns);
}
if (t->current_symlink && !RESOURCE_IS_FILESYSTEM(t->target.type) && !path_is_absolute(t->current_symlink))
return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
"Current symlink must be absolute path if target is partition: %s", t->current_symlink);
/* When no instance limit is set, use all available partition slots in case of partitions, or 3 in case of fs objects */
if (t->instances_max == 0)
t->instances_max = t->target.type == RESOURCE_PARTITION ? UINT64_MAX : DEFAULT_FILE_INSTANCES_MAX;
return 0;
}
int transfer_resolve_paths(
Transfer *t,
const char *root,
const char *node) {
int r;
/* If Path=auto is used in [Source] or [Target] sections, let's automatically detect the path of the
* block device to use. Moreover, if this path points to a directory but we need a block device,
* automatically determine the backing block device, so that users can reference block devices by
* mount point. */
assert(t);
r = resource_resolve_path(&t->source, root, t->context->transfer_source, node);
if (r < 0)
return r;
r = resource_resolve_path(&t->target, root, /* relative_to_directory= */ NULL, node);
if (r < 0)
return r;
return 0;
}
static void transfer_remove_temporary(Transfer *t) {
_cleanup_closedir_ DIR *d = NULL;
int r;
assert(t);
if (!t->remove_temporary)
return;
if (!IN_SET(t->target.type, RESOURCE_REGULAR_FILE, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME))
return;
/* Removes all incomplete files/dirs from previous runs in the target directory. Clean both the
* current partial sysupdate prefix and the legacy '.#' prefix. */
d = opendir(t->target.path);
if (!d) {
if (errno == ENOENT)
return;
log_debug_errno(errno, "Failed to open target directory '%s', ignoring: %m", t->target.path);
return;
}
for (;;) {
struct dirent *de;
errno = 0;
de = readdir_no_dot(d);
if (!de) {
if (errno != 0)
log_debug_errno(errno, "Failed to read target directory '%s', ignoring: %m", t->target.path);
break;
}
if (!STARTSWITH_SET(de->d_name, ".#", ".sysupdate.partial."))
continue;
r = rm_rf_child(dirfd(d), de->d_name, REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_CHMOD);
if (r == -ENOENT)
continue;
if (r < 0) {
log_warning_errno(r, "Failed to remove temporary resource instance '%s/%s', ignoring: %m", t->target.path, de->d_name);
continue;
}
log_debug("Removed temporary resource instance '%s/%s'.", t->target.path, de->d_name);
}
}
static int transfer_instance_vacuum(
Transfer *t,
Instance *instance) {
int r;
assert(t);
assert(instance);
switch (t->target.type) {
case RESOURCE_REGULAR_FILE:
case RESOURCE_DIRECTORY:
case RESOURCE_SUBVOLUME:
r = rm_rf(instance->path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_MISSING_OK|REMOVE_CHMOD);
if (r < 0 && r != -ENOENT)
return log_error_errno(r, "Failed to make room, deleting '%s' failed: %m", instance->path);
(void) rmdir_parents(instance->path, t->target.path);
break;
case RESOURCE_PARTITION: {
PartitionInfo pinfo = instance->partition_info;
PartitionChange change = PARTITION_LABEL;
/* label "_empty" means "no contents" for our purposes */
pinfo.label = (char*) "_empty";
/* If the partition had a derived partial/pending type UUID, restore the original
* partition type so that the slot is properly recognized as empty in subsequent
* scans. */
if ((instance->is_partial || instance->is_pending) && t->target.partition_type_set) {
pinfo.type = t->target.partition_type.uuid;
change |= PARTITION_TYPE;
}
log_debug("Resetting partition '%s' to empty.", pinfo.device);
r = patch_partition(t->target.path, &pinfo, change);
if (r < 0)
return r;
t->target.n_empty++;
break;
}
default:
assert_not_reached();
}
return 0;
}
int transfer_vacuum(
Transfer *t,
uint64_t space,
const char *extra_protected_version) {
uint64_t instances_max, limit;
int r, count = 0;
assert(t);
transfer_remove_temporary(t);
/* First, remove any partial or pending instances (unless protected) */
for (size_t i = 0; i < t->target.n_instances;) {
Instance *instance = t->target.instances[i];
assert(instance);
if (!instance->is_pending && !instance->is_partial) {
i++;
continue;
}
/* If this is pending and listed among the protected versions, then let's not remove it.
* In future, we will also want to keep partial protected versions, but that’s only useful
* once we support resuming downloads. */
if (instance->is_pending &&
(strv_contains(t->protected_versions, instance->metadata.version) ||
(extra_protected_version && streq(extra_protected_version, instance->metadata.version)))) {
log_debug("Version '%s' is pending but protected, not removing.", instance->metadata.version);
i++;
continue;
}
assert(instance->resource);
log_info("%s Removing old %s '%s' (%s).",
glyph(GLYPH_RECYCLING),
instance->is_partial ? "partial" : "pending",
instance->path,
resource_type_to_string(instance->resource->type));
r = transfer_instance_vacuum(t, instance);
if (r < 0)
return r;
instance_free(instance);
memmove(t->target.instances + i, t->target.instances + i + 1, (t->target.n_instances - i - 1) * sizeof(Instance*));
t->target.n_instances--;
count++;
}
/* Second, calculate how many instances to keep, based on the instance limit — but keep at least one */
instances_max = t->context->instances_max != UINT64_MAX ? t->context->instances_max : t->instances_max;
assert(instances_max >= 1);
if (instances_max == UINT64_MAX) /* Keep infinite instances? */
limit = UINT64_MAX;
else if (space == UINT64_MAX) /* forcibly delete all instances? */
limit = 0;
else if (space > instances_max)
return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
"Asked to delete more instances than total maximum allowed number of instances, refusing.");
else if (space == instances_max)
return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
"Asked to delete all possible instances, can't allow that. One instance must always remain.");
else
limit = instances_max - space;
if (t->target.type == RESOURCE_PARTITION && space != UINT64_MAX) {
_cleanup_free_ char *patterns = NULL;
uint64_t rm, remain;
patterns = strv_join(t->target.patterns, "|");
if (!patterns)
(void) log_oom_debug();
/* If we are looking at a partition table, we also have to take into account how many
* partition slots of the right type are available */
if (t->target.n_empty + t->target.n_instances < 2)
return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
"Partition table has less than two partition slots of the right type " SD_ID128_UUID_FORMAT_STR " (%s)%s%s%s, refusing.",
SD_ID128_FORMAT_VAL(t->target.partition_type.uuid),
gpt_partition_type_uuid_to_string(t->target.partition_type.uuid),
!isempty(patterns) ? " and matching the expected pattern '" : "",
strempty(patterns),
!isempty(patterns) ? "'" : "");
if (space > t->target.n_empty + t->target.n_instances)
return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
"Partition table does not have enough partition slots of right type " SD_ID128_UUID_FORMAT_STR " (%s)%s%s%s for operation.",
SD_ID128_FORMAT_VAL(t->target.partition_type.uuid),
gpt_partition_type_uuid_to_string(t->target.partition_type.uuid),
!isempty(patterns) ? " and matching the expected pattern '" : "",
strempty(patterns),
!isempty(patterns) ? "'" : "");
if (space == t->target.n_empty + t->target.n_instances)
return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
"Asked to empty all partition table slots of the right type " SD_ID128_UUID_FORMAT_STR " (%s), can't allow that. One instance must always remain.",
SD_ID128_FORMAT_VAL(t->target.partition_type.uuid),
gpt_partition_type_uuid_to_string(t->target.partition_type.uuid));
rm = LESS_BY(space, t->target.n_empty);
remain = LESS_BY(t->target.n_instances, rm);
limit = MIN(limit, remain);
}
while (t->target.n_instances > limit) {
Instance *oldest;
size_t p = t->target.n_instances - 1;
for (;;) {
oldest = t->target.instances[p];
assert(oldest);
/* If this is listed among the protected versions, then let's not remove it */
if (!strv_contains(t->protected_versions, oldest->metadata.version) &&
(!extra_protected_version || !streq(extra_protected_version, oldest->metadata.version)))
break;
log_debug("Version '%s' is protected, not removing.", oldest->metadata.version);
if (p == 0) {
oldest = NULL;
break;
}
p--;
}
if (!oldest) /* Nothing more to remove */
break;
assert(oldest->resource);
log_info("%s Removing %s '%s' (%s).",