• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23774132158

30 Mar 2026 09:35PM UTC coverage: 72.398% (+0.2%) from 72.208%
23774132158

push

github

daandemeyer
Only enable `NoAuto=true` for supported partitions

When `Format=empty` is set we need to check for `NoAuto` support for
the partition type, else we print a warning later in the build.

Followup for 381304a

1 of 1 new or added line in 1 file covered. (100.0%)

7091 existing lines in 92 files now uncovered.

318474 of 439892 relevant lines covered (72.4%)

1151474.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.69
/src/sysupdate/sysupdate-transfer.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4
#include <sys/stat.h>
5
#include <unistd.h>
6

7
#include "sd-id128.h"
8

9
#include "alloc-util.h"
10
#include "build-path.h"
11
#include "chase.h"
12
#include "conf-parser.h"
13
#include "dirent-util.h"
14
#include "errno-util.h"
15
#include "event-util.h"
16
#include "extract-word.h"
17
#include "fd-util.h"
18
#include "fs-util.h"
19
#include "glyph-util.h"
20
#include "gpt.h"
21
#include "hashmap.h"
22
#include "hexdecoct.h"
23
#include "install-file.h"
24
#include "mkdir.h"
25
#include "notify-recv.h"
26
#include "parse-helpers.h"
27
#include "parse-util.h"
28
#include "percent-util.h"
29
#include "pidref.h"
30
#include "process-util.h"
31
#include "rm-rf.h"
32
#include "signal-util.h"
33
#include "specifier.h"
34
#include "stdio-util.h"
35
#include "strv.h"
36
#include "sync-util.h"
37
#include "sysupdate.h"
38
#include "sysupdate-feature.h"
39
#include "sysupdate-instance.h"
40
#include "sysupdate-pattern.h"
41
#include "sysupdate-resource.h"
42
#include "sysupdate-transfer.h"
43
#include "time-util.h"
44
#include "web-util.h"
45

46
/* Default value for InstancesMax= for fs object targets */
47
#define DEFAULT_FILE_INSTANCES_MAX 3
48

49
Transfer* transfer_free(Transfer *t) {
2,888✔
50
        if (!t)
2,888✔
51
                return NULL;
52

53
        free(t->temporary_partial_path);
2,888✔
54
        free(t->temporary_pending_path);
2,888✔
55

56
        free(t->id);
2,888✔
57

58
        free(t->min_version);
2,888✔
59
        strv_free(t->protected_versions);
2,888✔
60
        free(t->current_symlink);
2,888✔
61
        free(t->final_path);
2,888✔
62

63
        strv_free(t->features);
2,888✔
64
        strv_free(t->requisite_features);
2,888✔
65

66
        strv_free(t->changelog);
2,888✔
67
        strv_free(t->appstream);
2,888✔
68

69
        partition_info_destroy(&t->partition_info);
2,888✔
70
        free(t->final_partition_label);
2,888✔
71

72
        resource_destroy(&t->source);
2,888✔
73
        resource_destroy(&t->target);
2,888✔
74

75
        return mfree(t);
2,888✔
76
}
77

78
Transfer* transfer_new(Context *ctx) {
2,888✔
79
        Transfer *t;
2,888✔
80

81
        t = new(Transfer, 1);
2,888✔
82
        if (!t)
2,888✔
83
                return NULL;
84

85
        *t = (Transfer) {
2,888✔
86
                .source.type = _RESOURCE_TYPE_INVALID,
87
                .target.type = _RESOURCE_TYPE_INVALID,
88
                .remove_temporary = true,
89
                .mode = MODE_INVALID,
90
                .tries_left = UINT64_MAX,
91
                .tries_done = UINT64_MAX,
92
                .verify = true,
93

94
                /* the three flags, as configured by the user */
95
                .no_auto = -1,
96
                .read_only = -1,
97
                .growfs = -1,
98

99
                /* the read only flag, as ultimately determined */
100
                .install_read_only = -1,
101

102
                .partition_info = PARTITION_INFO_NULL,
103

104
                .context = ctx,
105
        };
106

107
        return t;
2,888✔
108
}
109

110
static int config_parse_protect_version(
×
111
                const char *unit,
112
                const char *filename,
113
                unsigned line,
114
                const char *section,
115
                unsigned section_line,
116
                const char *lvalue,
117
                int ltype,
118
                const char *rvalue,
119
                void *data,
120
                void *userdata) {
121

122
        _cleanup_free_ char *resolved = NULL;
×
123
        char ***protected_versions = ASSERT_PTR(data);
×
124
        int r;
×
125

126
        assert(rvalue);
×
127

128
        r = specifier_printf(rvalue, NAME_MAX, specifier_table, arg_root, NULL, &resolved);
×
129
        if (r < 0) {
×
130
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
131
                           "Failed to expand specifiers in ProtectVersion=, ignoring: %s", rvalue);
132
                return 0;
×
133
        }
134

135
        if (!version_is_valid(resolved))  {
×
136
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
137
                           "ProtectVersion= string is not valid, ignoring: %s", resolved);
138
                return 0;
×
139
        }
140

141
        r = strv_extend(protected_versions, resolved);
×
142
        if (r < 0)
×
143
                return log_oom();
×
144

145
        return 0;
146
}
147

148
static int config_parse_min_version(
×
149
                const char *unit,
150
                const char *filename,
151
                unsigned line,
152
                const char *section,
153
                unsigned section_line,
154
                const char *lvalue,
155
                int ltype,
156
                const char *rvalue,
157
                void *data,
158
                void *userdata) {
159

160
        _cleanup_free_ char *resolved = NULL;
×
161
        char **version = ASSERT_PTR(data);
×
162
        int r;
×
163

164
        assert(rvalue);
×
165

166
        r = specifier_printf(rvalue, NAME_MAX, specifier_table, arg_root, NULL, &resolved);
×
167
        if (r < 0) {
×
168
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
169
                           "Failed to expand specifiers in MinVersion=, ignoring: %s", rvalue);
170
                return 0;
×
171
        }
172

173
        if (!version_is_valid(rvalue)) {
×
174
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
175
                           "MinVersion= string is not valid, ignoring: %s", resolved);
176
                return 0;
×
177
        }
178

179
        return free_and_replace(*version, resolved);
×
180
}
181

182
static int config_parse_url_specifiers(
×
183
                const char *unit,
184
                const char *filename,
185
                unsigned line,
186
                const char *section,
187
                unsigned section_line,
188
                const char *lvalue,
189
                int ltype,
190
                const char *rvalue,
191
                void *data,
192
                void *userdata) {
193
        char ***s = ASSERT_PTR(data);
×
194
        _cleanup_free_ char *resolved = NULL;
×
195
        int r;
×
196

197
        assert(rvalue);
×
198

199
        if (isempty(rvalue)) {
×
200
                *s = strv_free(*s);
×
201
                return 0;
×
202
        }
203

204
        r = specifier_printf(rvalue, NAME_MAX, specifier_table, arg_root, NULL, &resolved);
×
205
        if (r < 0) {
×
206
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
207
                           "Failed to expand specifiers in %s=, ignoring: %s", lvalue, rvalue);
208
                return 0;
×
209
        }
210

211
        if (!http_url_is_valid(resolved)) {
×
212
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
213
                           "%s= URL is not valid, ignoring: %s", lvalue, rvalue);
214
                return 0;
×
215
        }
216

217
        r = strv_push(s, TAKE_PTR(resolved));
×
218
        if (r < 0)
×
219
                return log_oom();
×
220

221
        return 0;
222
}
223

224
static int config_parse_current_symlink(
480✔
225
                const char *unit,
226
                const char *filename,
227
                unsigned line,
228
                const char *section,
229
                unsigned section_line,
230
                const char *lvalue,
231
                int ltype,
232
                const char *rvalue,
233
                void *data,
234
                void *userdata) {
235

236
        _cleanup_free_ char *resolved = NULL;
480✔
237
        char **current_symlink = ASSERT_PTR(data);
480✔
238
        int r;
480✔
239

240
        assert(rvalue);
480✔
241

242
        r = specifier_printf(rvalue, NAME_MAX, specifier_table, arg_root, NULL, &resolved);
480✔
243
        if (r < 0) {
480✔
244
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
245
                           "Failed to expand specifiers in CurrentSymlink=, ignoring: %s", rvalue);
246
                return 0;
×
247
        }
248

249
        r = path_simplify_and_warn(resolved, 0, unit, filename, line, lvalue);
480✔
250
        if (r < 0)
480✔
251
                return 0;
252

253
        return free_and_replace(*current_symlink, resolved);
480✔
254
}
255

256
static int config_parse_instances_max(
1,920✔
257
                const char *unit,
258
                const char *filename,
259
                unsigned line,
260
                const char *section,
261
                unsigned section_line,
262
                const char *lvalue,
263
                int ltype,
264
                const char *rvalue,
265
                void *data,
266
                void *userdata) {
267

268
        uint64_t *instances_max = data, i;
1,920✔
269
        int r;
1,920✔
270

271
        assert(rvalue);
1,920✔
272
        assert(data);
1,920✔
273

274
        if (isempty(rvalue)) {
1,920✔
275
                *instances_max = 0; /* Revert to default logic, see transfer_read_definition() */
×
276
                return 0;
×
277
        }
278

279
        r = safe_atou64(rvalue, &i);
1,920✔
280
        if (r < 0) {
1,920✔
281
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
282
                           "Failed to parse InstancesMax= value, ignoring: %s", rvalue);
283
                return 0;
×
284
        }
285

286
        if (i < 2) {
1,920✔
287
                log_syntax(unit, LOG_WARNING, filename, line, 0,
×
288
                           "InstancesMax= value must be at least 2, bumping: %s", rvalue);
289
                *instances_max = 2;
×
290
        } else
291
                *instances_max = i;
1,920✔
292

293
        return 0;
294
}
295

296
static int config_parse_resource_pattern(
5,776✔
297
                const char *unit,
298
                const char *filename,
299
                unsigned line,
300
                const char *section,
301
                unsigned section_line,
302
                const char *lvalue,
303
                int ltype,
304
                const char *rvalue,
305
                void *data,
306
                void *userdata) {
307

308
        char ***patterns = ASSERT_PTR(data);
5,776✔
309
        int r;
5,776✔
310

311
        assert(rvalue);
5,776✔
312

313
        if (isempty(rvalue)) {
5,776✔
314
                *patterns = strv_free(*patterns);
×
315
                return 0;
×
316
        }
317

318
        for (;;) {
19,248✔
319
                _cleanup_free_ char *word = NULL, *resolved = NULL;
6,736✔
320

321
                r = extract_first_word(&rvalue, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX);
12,512✔
322
                if (r < 0) {
12,512✔
323
                        log_syntax(unit, LOG_WARNING, filename, line, r,
×
324
                                   "Failed to extract first pattern from MatchPattern=, ignoring: %s", rvalue);
325
                        return 0;
×
326
                }
327
                if (r == 0)
12,512✔
328
                        break;
329

330
                r = specifier_printf(word, NAME_MAX, specifier_table, arg_root, NULL, &resolved);
6,736✔
331
                if (r < 0) {
6,736✔
332
                        log_syntax(unit, LOG_WARNING, filename, line, r,
×
333
                                   "Failed to expand specifiers in MatchPattern=, ignoring: %s", rvalue);
334
                        return 0;
×
335
                }
336

337
                if (!pattern_valid(resolved))
6,736✔
338
                        return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
×
339
                                          "MatchPattern= string is not valid, refusing: %s", resolved);
340

341
                r = strv_consume(patterns, TAKE_PTR(resolved));
6,736✔
342
                if (r < 0)
6,736✔
343
                        return log_oom();
×
344
        }
345

346
        strv_uniq(*patterns);
5,776✔
347
        return 0;
5,776✔
348
}
349

350
static int config_parse_resource_path(
5,776✔
351
                const char *unit,
352
                const char *filename,
353
                unsigned line,
354
                const char *section,
355
                unsigned section_line,
356
                const char *lvalue,
357
                int ltype,
358
                const char *rvalue,
359
                void *data,
360
                void *userdata) {
361
        _cleanup_free_ char *resolved = NULL;
5,776✔
362
        Resource *rr = ASSERT_PTR(data);
5,776✔
363
        int r;
5,776✔
364

365
        assert(rvalue);
5,776✔
366

367
        if (streq(rvalue, "auto")) {
5,776✔
368
                rr->path_auto = true;
×
369
                rr->path = mfree(rr->path);
×
370
                return 0;
×
371
        }
372

373
        r = specifier_printf(rvalue, PATH_MAX-1, specifier_table, arg_root, NULL, &resolved);
5,776✔
374
        if (r < 0) {
5,776✔
375
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
376
                           "Failed to expand specifiers in Path=, ignoring: %s", rvalue);
377
                return 0;
×
378
        }
379

380
        /* Note that we don't validate the path as being absolute or normalized. We'll do that in
381
         * transfer_read_definition() as we might not know yet whether Path refers to a URL or a file system
382
         * path. */
383

384
        rr->path_auto = false;
5,776✔
385
        return free_and_replace(rr->path, resolved);
5,776✔
386
}
387

388
static DEFINE_CONFIG_PARSE_ENUM(config_parse_resource_type, resource_type, ResourceType);
5,776✔
389

390
static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_resource_path_relto, path_relative_to, PathRelativeTo,
1,440✔
391
                                             PATH_RELATIVE_TO_ROOT);
392

393
static int config_parse_resource_ptype(
968✔
394
                const char *unit,
395
                const char *filename,
396
                unsigned line,
397
                const char *section,
398
                unsigned section_line,
399
                const char *lvalue,
400
                int ltype,
401
                const char *rvalue,
402
                void *data,
403
                void *userdata) {
404

405
        Resource *rr = ASSERT_PTR(data);
968✔
406
        int r;
968✔
407

408
        assert(rvalue);
968✔
409

410
        r = gpt_partition_type_from_string(rvalue, &rr->partition_type);
968✔
411
        if (r < 0) {
968✔
412
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
413
                           "Failed to parse partition type, ignoring: %s", rvalue);
414
                return 0;
×
415
        }
416

417
        rr->partition_type_set = true;
968✔
418
        return 0;
968✔
419
}
420

421
static int config_parse_partition_uuid(
×
422
                const char *unit,
423
                const char *filename,
424
                unsigned line,
425
                const char *section,
426
                unsigned section_line,
427
                const char *lvalue,
428
                int ltype,
429
                const char *rvalue,
430
                void *data,
431
                void *userdata) {
432

433
        Transfer *t = ASSERT_PTR(data);
×
434
        int r;
×
435

436
        assert(rvalue);
×
437

438
        r = sd_id128_from_string(rvalue, &t->partition_uuid);
×
439
        if (r < 0) {
×
440
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
441
                           "Failed to parse partition UUID, ignoring: %s", rvalue);
442
                return 0;
×
443
        }
444

445
        t->partition_uuid_set = true;
×
446
        return 0;
×
447
}
448

449
static int config_parse_partition_flags(
×
450
                const char *unit,
451
                const char *filename,
452
                unsigned line,
453
                const char *section,
454
                unsigned section_line,
455
                const char *lvalue,
456
                int ltype,
457
                const char *rvalue,
458
                void *data,
459
                void *userdata) {
460

461
        Transfer *t = ASSERT_PTR(data);
×
462
        int r;
×
463

464
        assert(rvalue);
×
465

466
        r = safe_atou64(rvalue, &t->partition_flags);
×
467
        if (r < 0) {
×
468
                log_syntax(unit, LOG_WARNING, filename, line, r,
×
469
                           "Failed to parse partition flags, ignoring: %s", rvalue);
470
                return 0;
×
471
        }
472

473
        t->partition_flags_set = true;
×
474
        return 0;
×
475
}
476

477
static bool transfer_decide_if_enabled(Transfer *t, Hashmap *known_features) {
2,888✔
478
        assert(t);
2,888✔
479

480
        /* Requisite feature disabled -> transfer disabled */
481
        STRV_FOREACH(id, t->requisite_features) {
2,888✔
482
                Feature *f = hashmap_get(known_features, *id);
×
483
                if (!f || !f->enabled) /* missing features are implicitly disabled */
×
484
                        return false;
485
        }
486

487
        /* No features defined -> transfer implicitly enabled */
488
        if (strv_isempty(t->features))
2,888✔
489
                return true;
490

491
        /* At least one feature enabled -> transfer enabled */
492
        STRV_FOREACH(id, t->features) {
912✔
493
                Feature *f = hashmap_get(known_features, *id);
480✔
494
                if (f && f->enabled)
480✔
495
                        return true;
496
        }
497

498
        /* All listed features disabled -> transfer disabled */
499
        return false;
500
}
501

502
int transfer_read_definition(Transfer *t, const char *path, const char **dirs, Hashmap *known_features) {
2,888✔
503
        assert(t);
2,888✔
504

505
        ConfigTableItem table[] = {
2,888✔
506
                { "Transfer",    "MinVersion",              config_parse_min_version,          0, &t->min_version             },
2,888✔
507
                { "Transfer",    "ProtectVersion",          config_parse_protect_version,      0, &t->protected_versions      },
2,888✔
508
                { "Transfer",    "Verify",                  config_parse_bool,                 0, &t->verify                  },
2,888✔
509
                { "Transfer",    "ChangeLog",               config_parse_url_specifiers,       0, &t->changelog               },
2,888✔
510
                { "Transfer",    "AppStream",               config_parse_url_specifiers,       0, &t->appstream               },
2,888✔
511
                { "Transfer",    "Features",                config_parse_strv,                 0, &t->features                },
2,888✔
512
                { "Transfer",    "RequisiteFeatures",       config_parse_strv,                 0, &t->requisite_features      },
2,888✔
513
                { "Source",      "Type",                    config_parse_resource_type,        0, &t->source.type             },
2,888✔
514
                { "Source",      "Path",                    config_parse_resource_path,        0, &t->source                  },
2,888✔
515
                { "Source",      "PathRelativeTo",          config_parse_resource_path_relto,  0, &t->source.path_relative_to },
2,888✔
516
                { "Source",      "MatchPattern",            config_parse_resource_pattern,     0, &t->source.patterns         },
2,888✔
517
                { "Target",      "Type",                    config_parse_resource_type,        0, &t->target.type             },
2,888✔
518
                { "Target",      "Path",                    config_parse_resource_path,        0, &t->target                  },
2,888✔
519
                { "Target",      "PathRelativeTo",          config_parse_resource_path_relto,  0, &t->target.path_relative_to },
2,888✔
520
                { "Target",      "MatchPattern",            config_parse_resource_pattern,     0, &t->target.patterns         },
2,888✔
521
                { "Target",      "MatchPartitionType",      config_parse_resource_ptype,       0, &t->target                  },
522
                { "Target",      "PartitionUUID",           config_parse_partition_uuid,       0, t                           },
523
                { "Target",      "PartitionFlags",          config_parse_partition_flags,      0, t                           },
524
                { "Target",      "PartitionNoAuto",         config_parse_tristate,             0, &t->no_auto                 },
2,888✔
525
                { "Target",      "PartitionGrowFileSystem", config_parse_tristate,             0, &t->growfs                  },
2,888✔
526
                { "Target",      "ReadOnly",                config_parse_tristate,             0, &t->read_only               },
2,888✔
527
                { "Target",      "Mode",                    config_parse_mode,                 0, &t->mode                    },
2,888✔
528
                { "Target",      "TriesLeft",               config_parse_uint64,               0, &t->tries_left              },
2,888✔
529
                { "Target",      "TriesDone",               config_parse_uint64,               0, &t->tries_done              },
2,888✔
530
                { "Target",      "InstancesMax",            config_parse_instances_max,        0, &t->instances_max           },
2,888✔
531
                { "Target",      "RemoveTemporary",         config_parse_bool,                 0, &t->remove_temporary        },
2,888✔
532
                { "Target",      "CurrentSymlink",          config_parse_current_symlink,      0, &t->current_symlink         },
2,888✔
533
                {}
534
        };
535

536
        _cleanup_free_ char *filename = NULL;
2,888✔
537
        char *e;
2,888✔
538
        int r;
2,888✔
539

540
        assert(path);
2,888✔
541
        assert(dirs);
2,888✔
542

543
        r = path_extract_filename(path, &filename);
2,888✔
544
        if (r < 0)
2,888✔
545
                return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);
×
546

547
        r = config_parse_many_full(
8,664✔
548
                        STRV_MAKE_CONST(path),
2,888✔
549
                        dirs,
550
                        strjoina(filename, ".d"),
14,440✔
551
                        arg_root,
552
                        /* root_fd= */ -EBADF,
553
                        "Transfer\0"
554
                        "Source\0"
555
                        "Target\0",
556
                        config_item_table_lookup, table,
557
                        CONFIG_PARSE_WARN,
558
                        /* userdata= */ NULL,
559
                        /* stats_by_path= */ NULL,
560
                        /* drop_in_files= */ NULL);
561
        if (r < 0)
2,888✔
562
                return r;
563

564
        e = ASSERT_PTR(endswith(filename, ".transfer") ?: endswith(filename, ".conf"));
2,888✔
565
        *e = 0; /* Remove the file extension */
2,888✔
566
        t->id = TAKE_PTR(filename);
2,888✔
567

568
        t->enabled = transfer_decide_if_enabled(t, known_features);
2,888✔
569

570
        if (!RESOURCE_IS_SOURCE(t->source.type))
2,888✔
571
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
572
                                  "Source Type= must be one of url-file, url-tar, tar, regular-file, directory, subvolume.");
573

574
        if (t->target.type < 0) {
2,888✔
575
                switch (t->source.type) {
×
576

577
                case RESOURCE_URL_FILE:
×
578
                case RESOURCE_REGULAR_FILE:
579
                        t->target.type =
×
580
                                t->target.path && path_startswith(t->target.path, "/dev/") ?
×
581
                                RESOURCE_PARTITION : RESOURCE_REGULAR_FILE;
×
582
                        break;
×
583

584
                case RESOURCE_URL_TAR:
×
585
                case RESOURCE_TAR:
586
                case RESOURCE_DIRECTORY:
587
                        t->target.type = RESOURCE_DIRECTORY;
×
588
                        break;
×
589

590
                case RESOURCE_SUBVOLUME:
×
591
                        t->target.type = RESOURCE_SUBVOLUME;
×
592
                        break;
×
593

594
                default:
×
595
                        assert_not_reached();
×
596
                }
597
        }
598

599
        if (!RESOURCE_IS_TARGET(t->target.type))
2,888✔
600
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
601
                                  "Target Type= must be one of partition, regular-file, directory, subvolume.");
602

603
        if ((IN_SET(t->source.type, RESOURCE_URL_FILE, RESOURCE_PARTITION, RESOURCE_REGULAR_FILE) &&
2,888✔
604
             !IN_SET(t->target.type, RESOURCE_PARTITION, RESOURCE_REGULAR_FILE)) ||
2,408✔
605
            (IN_SET(t->source.type, RESOURCE_URL_TAR, RESOURCE_TAR, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME) &&
2,888✔
606
             !IN_SET(t->target.type, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME)))
480✔
607
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
608
                                  "Target type '%s' is incompatible with source type '%s', refusing.",
609
                                  resource_type_to_string(t->target.type), resource_type_to_string(t->source.type));
610

611
        if (!t->source.path && !t->source.path_auto)
2,888✔
612
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
613
                                  "Source specification lacks Path=.");
614

615
        if (t->source.path_relative_to == PATH_RELATIVE_TO_EXPLICIT && !arg_transfer_source)
2,888✔
616
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
617
                                  "PathRelativeTo=explicit requires --transfer-source= to be specified.");
618

619
        if (t->target.path_relative_to == PATH_RELATIVE_TO_EXPLICIT)
2,888✔
620
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
621
                                  "PathRelativeTo=explicit can only be used in source specifications.");
622

623
        if (t->source.path) {
2,888✔
624
                if (RESOURCE_IS_FILESYSTEM(t->source.type) || t->source.type == RESOURCE_PARTITION)
2,888✔
625
                        if (!path_is_absolute(t->source.path) || !path_is_normalized(t->source.path))
2,696✔
626
                                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
627
                                                  "Source path is not a normalized, absolute path: %s", t->source.path);
628

629
                /* We unofficially support file:// in addition to http:// and https:// for url
630
                 * sources. That's mostly for testing, since it relieves us from having to set up a HTTP
631
                 * server, and CURL abstracts this away from us thankfully. */
632
                if (RESOURCE_IS_URL(t->source.type))
2,888✔
633
                        if (!http_url_is_valid(t->source.path) && !file_url_is_valid(t->source.path))
192✔
634
                                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
635
                                                  "Source path is not a valid HTTP or HTTPS URL: %s", t->source.path);
636
        }
637

638
        if (strv_isempty(t->source.patterns))
2,888✔
639
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
640
                                  "Source specification lacks MatchPattern=.");
641

642
        if (!t->target.path && !t->target.path_auto)
2,888✔
643
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
644
                                  "Target specification lacks Path= field.");
645

646
        if (t->target.path &&
2,888✔
647
            (!path_is_absolute(t->target.path) || !path_is_normalized(t->target.path)))
2,888✔
648
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
649
                                  "Target path is not a normalized, absolute path: %s", t->target.path);
650

651
        if (strv_isempty(t->target.patterns)) {
2,888✔
652
                log_syntax(NULL, LOG_INFO, path, 1, 0, "Target specification lacks MatchPattern= expression. Assuming same value as in source specification.");
×
653
                strv_free(t->target.patterns);
×
654
                t->target.patterns = strv_copy(t->source.patterns);
×
655
                if (!t->target.patterns)
×
656
                        return log_oom();
×
657
        }
658

659
        if (t->current_symlink && !RESOURCE_IS_FILESYSTEM(t->target.type) && !path_is_absolute(t->current_symlink))
2,888✔
660
                return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
×
661
                                  "Current symlink must be absolute path if target is partition: %s", t->current_symlink);
662

663
        /* When no instance limit is set, use all available partition slots in case of partitions, or 3 in case of fs objects */
664
        if (t->instances_max == 0)
2,888✔
665
                t->instances_max = t->target.type == RESOURCE_PARTITION ? UINT64_MAX : DEFAULT_FILE_INSTANCES_MAX;
968✔
666

667
        return 0;
668
}
669

670
int transfer_resolve_paths(
2,888✔
671
                Transfer *t,
672
                const char *root,
673
                const char *node) {
674

675
        int r;
2,888✔
676

677
        /* If Path=auto is used in [Source] or [Target] sections, let's automatically detect the path of the
678
         * block device to use. Moreover, if this path points to a directory but we need a block device,
679
         * automatically determine the backing block device, so that users can reference block devices by
680
         * mount point. */
681

682
        assert(t);
2,888✔
683

684
        r = resource_resolve_path(&t->source, root, arg_transfer_source, node);
2,888✔
685
        if (r < 0)
2,888✔
686
                return r;
687

688
        r = resource_resolve_path(&t->target, root, /* relative_to_directory= */ NULL, node);
2,888✔
689
        if (r < 0)
2,888✔
690
                return r;
×
691

692
        return 0;
693
}
694

695
static void transfer_remove_temporary(Transfer *t) {
408✔
696
        _cleanup_closedir_ DIR *d = NULL;
408✔
697
        int r;
408✔
698

699
        assert(t);
408✔
700

701
        if (!t->remove_temporary)
408✔
702
                return;
703

704
        if (!IN_SET(t->target.type, RESOURCE_REGULAR_FILE, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME))
408✔
705
                return;
706

707
        /* Removes all temporary files/dirs from previous runs in the target directory, i.e. all those starting with '.#' */
708

709
        d = opendir(t->target.path);
280✔
710
        if (!d) {
280✔
711
                if (errno == ENOENT)
×
712
                        return;
713

714
                log_debug_errno(errno, "Failed to open target directory '%s', ignoring: %m", t->target.path);
×
715
                return;
×
716
        }
717

718
        for (;;) {
1,064✔
719
                struct dirent *de;
1,064✔
720

721
                errno = 0;
1,064✔
722
                de = readdir_no_dot(d);
1,064✔
723
                if (!de) {
1,064✔
724
                        if (errno != 0)
280✔
725
                                log_debug_errno(errno, "Failed to read target directory '%s', ignoring: %m", t->target.path);
×
726
                        break;
280✔
727
                }
728

729
                if (!startswith(de->d_name, ".#"))
784✔
730
                        continue;
784✔
731

732
                r = rm_rf_child(dirfd(d), de->d_name, REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_CHMOD);
×
733
                if (r == -ENOENT)
×
734
                        continue;
×
735
                if (r < 0) {
×
736
                        log_warning_errno(r, "Failed to remove temporary resource instance '%s/%s', ignoring: %m", t->target.path, de->d_name);
×
737
                        continue;
×
738
                }
739

740
                log_debug("Removed temporary resource instance '%s/%s'.", t->target.path, de->d_name);
×
741
        }
742
}
743

744
static int transfer_instance_vacuum(
200✔
745
                Transfer *t,
746
                Instance *instance) {
747
        int r;
200✔
748

749
        assert(t);
200✔
750
        assert(instance);
200✔
751

752
        switch (t->target.type) {
200✔
753

754
        case RESOURCE_REGULAR_FILE:
120✔
755
        case RESOURCE_DIRECTORY:
756
        case RESOURCE_SUBVOLUME:
757
                r = rm_rf(instance->path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_MISSING_OK|REMOVE_CHMOD);
120✔
758
                if (r < 0 && r != -ENOENT)
120✔
759
                        return log_error_errno(r, "Failed to make room, deleting '%s' failed: %m", instance->path);
×
760

761
                (void) rmdir_parents(instance->path, t->target.path);
120✔
762

763
                break;
120✔
764

765
        case RESOURCE_PARTITION: {
80✔
766
                PartitionInfo pinfo = instance->partition_info;
80✔
767
                PartitionChange change = PARTITION_LABEL;
80✔
768

769
                /* label "_empty" means "no contents" for our purposes */
770
                pinfo.label = (char*) "_empty";
80✔
771

772
                /* If the partition had a derived partial/pending type UUID, restore the original
773
                 * partition type so that the slot is properly recognized as empty in subsequent
774
                 * scans. */
775
                if ((instance->is_partial || instance->is_pending) && t->target.partition_type_set) {
80✔
776
                        pinfo.type = t->target.partition_type.uuid;
×
777
                        change |= PARTITION_TYPE;
×
778
                }
779

780
                log_debug("Resetting partition '%s' to empty.", pinfo.device);
80✔
781
                r = patch_partition(t->target.path, &pinfo, change);
80✔
782
                if (r < 0)
80✔
783
                        return r;
×
784

785
                t->target.n_empty++;
80✔
786
                break;
80✔
787
        }
788

789
        default:
×
790
                assert_not_reached();
×
791
        }
792

793
        return 0;
794
}
795

796
int transfer_vacuum(
408✔
797
                Transfer *t,
798
                uint64_t space,
799
                const char *extra_protected_version) {
800

801
        uint64_t instances_max, limit;
408✔
802
        int r, count = 0;
408✔
803

804
        assert(t);
408✔
805

806
        transfer_remove_temporary(t);
408✔
807

808
        /* First, remove any partial or pending instances (unless protected) */
809
        for (size_t i = 0; i < t->target.n_instances;) {
984✔
810
                Instance *instance = t->target.instances[i];
576✔
811

812
                assert(instance);
576✔
813

814
                if (!instance->is_pending && !instance->is_partial) {
576✔
815
                        i++;
576✔
816
                        continue;
576✔
817
                }
818

819
                /* If this is listed among the protected versions, then let's not remove it */
820
                if (strv_contains(t->protected_versions, instance->metadata.version) ||
×
821
                    (extra_protected_version && streq(extra_protected_version, instance->metadata.version))) {
×
822
                        log_debug("Version '%s' is pending/partial but protected, not removing.", instance->metadata.version);
×
823
                        i++;
×
824
                        continue;
×
825
                }
826

827
                assert(instance->resource);
×
828

829
                log_info("%s Removing old %s '%s' (%s).",
×
830
                         glyph(GLYPH_RECYCLING),
831
                         instance->is_partial ? "partial" : "pending",
832
                         instance->path,
833
                         resource_type_to_string(instance->resource->type));
834

835
                r = transfer_instance_vacuum(t, instance);
×
836
                if (r < 0)
×
837
                        return 0;
838

839
                instance_free(instance);
×
840
                memmove(t->target.instances + i, t->target.instances + i + 1, (t->target.n_instances - i - 1) * sizeof(Instance*));
×
841
                t->target.n_instances--;
×
842

843
                count++;
×
844
        }
845

846
        /* Second, calculate how many instances to keep, based on the instance limit — but keep at least one */
847

848
        instances_max = arg_instances_max != UINT64_MAX ? arg_instances_max : t->instances_max;
408✔
849
        assert(instances_max >= 1);
408✔
850
        if (instances_max == UINT64_MAX) /* Keep infinite instances? */
408✔
851
                limit = UINT64_MAX;
852
        else if (space == UINT64_MAX) /* forcibly delete all instances? */
280✔
853
                limit = 0;
854
        else if (space > instances_max)
208✔
855
                return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
×
856
                                       "Asked to delete more instances than total maximum allowed number of instances, refusing.");
857
        else if (space == instances_max)
208✔
858
                return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
×
859
                                       "Asked to delete all possible instances, can't allow that. One instance must always remain.");
860
        else
861
                limit = instances_max - space;
208✔
862

863
        if (t->target.type == RESOURCE_PARTITION && space != UINT64_MAX) {
408✔
864
                _cleanup_free_ char *patterns = NULL;
128✔
865
                uint64_t rm, remain;
128✔
866

867
                patterns = strv_join(t->target.patterns, "|");
128✔
868
                if (!patterns)
128✔
869
                        (void) log_oom_debug();
×
870

871
                /* If we are looking at a partition table, we also have to take into account how many
872
                 * partition slots of the right type are available */
873

874
                if (t->target.n_empty + t->target.n_instances < 2)
128✔
875
                        return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
×
876
                                               "Partition table has less than two partition slots of the right type " SD_ID128_UUID_FORMAT_STR " (%s)%s%s%s, refusing.",
877
                                               SD_ID128_FORMAT_VAL(t->target.partition_type.uuid),
878
                                               gpt_partition_type_uuid_to_string(t->target.partition_type.uuid),
879
                                               !isempty(patterns) ? " and matching the expected pattern '" : "",
880
                                               strempty(patterns),
881
                                               !isempty(patterns) ? "'" : "");
882
                if (space > t->target.n_empty + t->target.n_instances)
128✔
883
                        return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
×
884
                                               "Partition table does not have enough partition slots of right type " SD_ID128_UUID_FORMAT_STR " (%s)%s%s%s for operation.",
885
                                               SD_ID128_FORMAT_VAL(t->target.partition_type.uuid),
886
                                               gpt_partition_type_uuid_to_string(t->target.partition_type.uuid),
887
                                               !isempty(patterns) ? " and matching the expected pattern '" : "",
888
                                               strempty(patterns),
889
                                               !isempty(patterns) ? "'" : "");
890
                if (space == t->target.n_empty + t->target.n_instances)
128✔
891
                        return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
×
892
                                               "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.",
893
                                               SD_ID128_FORMAT_VAL(t->target.partition_type.uuid),
894
                                               gpt_partition_type_uuid_to_string(t->target.partition_type.uuid));
895

896
                rm = LESS_BY(space, t->target.n_empty);
128✔
897
                remain = LESS_BY(t->target.n_instances, rm);
128✔
898
                limit = MIN(limit, remain);
128✔
899
        }
900

901
        while (t->target.n_instances > limit) {
608✔
902
                Instance *oldest;
200✔
903
                size_t p = t->target.n_instances - 1;
200✔
904

905
                for (;;) {
200✔
906
                        oldest = t->target.instances[p];
200✔
907
                        assert(oldest);
200✔
908

909
                        /* If this is listed among the protected versions, then let's not remove it */
910
                        if (!strv_contains(t->protected_versions, oldest->metadata.version) &&
200✔
911
                            (!extra_protected_version || !streq(extra_protected_version, oldest->metadata.version)))
192✔
912
                                break;
913

914
                        log_debug("Version '%s' is protected, not removing.", oldest->metadata.version);
×
915
                        if (p == 0) {
×
916
                                oldest = NULL;
917
                                break;
918
                        }
919

920
                        p--;
×
921
                }
922

923
                if (!oldest) /* Nothing more to remove */
200✔
924
                        break;
925

926
                assert(oldest->resource);
200✔
927

928
                log_info("%s Removing %s '%s' (%s).",
400✔
929
                         glyph(GLYPH_RECYCLING),
930
                         space == UINT64_MAX ? "disabled" : "old",
931
                         oldest->path,
932
                         resource_type_to_string(oldest->resource->type));
933

934
                r = transfer_instance_vacuum(t, oldest);
200✔
935
                if (r < 0)
200✔
936
                        return 0;
937

938
                instance_free(oldest);
200✔
939
                memmove(t->target.instances + p, t->target.instances + p + 1, (t->target.n_instances - p - 1) * sizeof(Instance*));
200✔
940
                t->target.n_instances--;
200✔
941

942
                count++;
200✔
943
        }
944

945
        return count;
946
}
947

948
static void compile_pattern_fields(
594✔
949
                const Transfer *t,
950
                const Instance *i,
951
                InstanceMetadata *ret) {
952

953
        assert(t);
594✔
954
        assert(i);
594✔
955
        assert(ret);
594✔
956

957
        *ret = (InstanceMetadata) {
1,188✔
958
                .version = i->metadata.version,
594✔
959

960
                /* We generally prefer explicitly configured values for the transfer over those automatically
961
                 * derived from the source instance. Also, if the source is a tar archive, then let's not
962
                 * patch mtime/mode and use the one embedded in the tar file */
963
                .partition_uuid = t->partition_uuid_set ? t->partition_uuid : i->metadata.partition_uuid,
594✔
964
                .partition_uuid_set = t->partition_uuid_set || i->metadata.partition_uuid_set,
594✔
965
                .partition_flags = t->partition_flags_set ? t->partition_flags : i->metadata.partition_flags,
594✔
966
                .partition_flags_set = t->partition_flags_set || i->metadata.partition_flags_set,
594✔
967
                .mtime = RESOURCE_IS_TAR(i->resource->type) ? USEC_INFINITY : i->metadata.mtime,
594✔
968
                .mode = t->mode != MODE_INVALID ? t->mode : (RESOURCE_IS_TAR(i->resource->type) ? MODE_INVALID : i->metadata.mode),
594✔
969
                .size = i->metadata.size,
594✔
970
                .tries_done = t->tries_done != UINT64_MAX ? t->tries_done :
594✔
971
                              i->metadata.tries_done != UINT64_MAX ? i->metadata.tries_done : 0,
478✔
972
                .tries_left = t->tries_left != UINT64_MAX ? t->tries_left :
594✔
973
                              i->metadata.tries_left != UINT64_MAX ? i->metadata.tries_left : 3,
478✔
974
                .no_auto = t->no_auto >= 0 ? t->no_auto : i->metadata.no_auto,
594✔
975
                .read_only = t->read_only >= 0 ? t->read_only : i->metadata.read_only,
594✔
976
                .growfs = t->growfs >= 0 ? t->growfs : i->metadata.growfs,
594✔
977
                .sha256sum_set = i->metadata.sha256sum_set,
594✔
978
        };
979

980
        memcpy(ret->sha256sum, i->metadata.sha256sum, sizeof(ret->sha256sum));
594✔
981
}
594✔
982

983
typedef struct CalloutContext {
984
        const Transfer *transfer;
985
        const Instance *instance;
986
        TransferProgress callback;
987
        PidRef pid;
988
        const char *name;
989
        int helper_errno;
990
        void* userdata;
991
} CalloutContext;
992

993
static CalloutContext *callout_context_free(CalloutContext *ctx) {
296✔
994
        if (!ctx)
296✔
995
                return NULL;
996

997
        /* We don't own any data but need to clean up the job pid */
998
        pidref_done(&ctx->pid);
296✔
999

1000
        return mfree(ctx);
296✔
1001
}
1002

1003
DEFINE_TRIVIAL_CLEANUP_FUNC(CalloutContext*, callout_context_free);
592✔
1004

1005
static int callout_context_new(const Transfer *t, const Instance *i, TransferProgress cb,
296✔
1006
                               const char *name, void* userdata, CalloutContext **ret) {
1007
        _cleanup_(callout_context_freep) CalloutContext *ctx = NULL;
296✔
1008

1009
        assert(t);
296✔
1010
        assert(i);
296✔
1011
        assert(cb);
296✔
1012

1013
        ctx = new(CalloutContext, 1);
296✔
1014
        if (!ctx)
296✔
1015
                return -ENOMEM;
1016

1017
        *ctx = (CalloutContext) {
296✔
1018
                .transfer = t,
1019
                .instance = i,
1020
                .callback = cb,
1021
                .pid = PIDREF_NULL,
1022
                .name = name,
1023
                .userdata = userdata,
1024
        };
1025

1026
        *ret = TAKE_PTR(ctx);
296✔
1027
        return 0;
296✔
1028
}
1029

1030
static int helper_on_exit(sd_event_source *s, const siginfo_t *si, void *userdata) {
296✔
1031
        CalloutContext *ctx = ASSERT_PTR(userdata);
296✔
1032
        int r;
296✔
1033

1034
        assert(s);
296✔
1035
        assert(si);
296✔
1036
        assert(ctx);
296✔
1037

1038
        if (si->si_code == CLD_EXITED) {
296✔
1039
                if (si->si_status == EXIT_SUCCESS) {
296✔
1040
                        r = 0;
296✔
1041
                        log_debug("%s succeeded.", ctx->name);
296✔
1042
                } else if (ctx->helper_errno != 0) {
×
1043
                        r = -ctx->helper_errno;
×
1044
                        log_error_errno(r, "%s failed with exit status %i: %m", ctx->name, si->si_status);
×
1045
                } else {
1046
                        r = -EPROTO;
×
1047
                        log_error("%s failed with exit status %i.", ctx->name, si->si_status);
×
1048
                }
1049
        } else {
1050
                r = -EPROTO;
×
1051
                if (IN_SET(si->si_code, CLD_KILLED, CLD_DUMPED))
×
1052
                        log_error("%s terminated by signal %s.", ctx->name, signal_to_string(si->si_status));
×
1053
                else
1054
                        log_error("%s failed due to unknown reason.", ctx->name);
×
1055
        }
1056

1057
        return sd_event_exit(sd_event_source_get_event(s), r);
296✔
1058
}
1059

1060
static int helper_on_notify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
546✔
1061
        CalloutContext *ctx = ASSERT_PTR(userdata);
546✔
1062
        int r;
546✔
1063

1064
        assert(fd >= 0);
546✔
1065

1066
        _cleanup_free_ char *buf = NULL;
546✔
1067
        _cleanup_(pidref_done) PidRef sender_pid = PIDREF_NULL;
546✔
1068
        r = notify_recv(fd, &buf, /* ret_ucred= */ NULL, &sender_pid);
546✔
1069
        if (r == -EAGAIN)
546✔
1070
                return 0;
1071
        if (r < 0)
546✔
1072
                return r;
1073

1074
        if (!pidref_equal(&ctx->pid, &sender_pid)) {
546✔
1075
                log_warning("Got notification datagram from unexpected peer, ignoring.");
×
1076
                return 0;
×
1077
        }
1078

1079
        char *errno_str = find_line_startswith(buf, "ERRNO=");
546✔
1080
        if (errno_str) {
546✔
1081
                truncate_nl(errno_str);
×
1082
                r = parse_errno(errno_str);
×
1083
                if (r < 0)
×
1084
                        log_warning_errno(r, "Got invalid errno value '%s', ignoring: %m", errno_str);
×
1085
                else {
1086
                        ctx->helper_errno = r;
×
1087
                        log_debug_errno(r, "Got errno from callout: %i (%m)", r);
×
1088
                }
1089
        }
1090

1091
        char *progress_str = find_line_startswith(buf, "X_IMPORT_PROGRESS=");
546✔
1092
        if (progress_str) {
546✔
1093
                truncate_nl(progress_str);
250✔
1094

1095
                int progress = parse_percent(progress_str);
250✔
1096
                if (progress < 0)
250✔
1097
                        log_warning("Got invalid percent value '%s', ignoring.", progress_str);
×
1098
                else {
1099
                        r = ctx->callback(ctx->transfer, ctx->instance, progress);
250✔
1100
                        if (r < 0)
250✔
1101
                                return r;
×
1102
                }
1103
        }
1104

1105
        return 0;
1106
}
1107

1108
static int run_callout(
296✔
1109
                const char *name,
1110
                char *cmdline[],
1111
                const Transfer *transfer,
1112
                const Instance *instance,
1113
                TransferProgress callback,
1114
                void *userdata) {
1115

1116
        int r;
296✔
1117

1118
        assert(name);
296✔
1119
        assert(cmdline);
296✔
1120
        assert(cmdline[0]);
296✔
1121

1122
        _cleanup_(callout_context_freep) CalloutContext *ctx = NULL;
×
1123
        r = callout_context_new(transfer, instance, callback, name, userdata, &ctx);
296✔
1124
        if (r < 0)
296✔
1125
                return log_oom();
×
1126

1127
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
296✔
1128
        r = sd_event_new(&event);
296✔
1129
        if (r < 0)
296✔
1130
                return log_error_errno(r, "Failed to create event: %m");
×
1131

1132
        /* Kill the helper & return an error if we get interrupted by a signal */
1133
        r = sd_event_add_signal(event, NULL, SIGINT | SD_EVENT_SIGNAL_PROCMASK, NULL, INT_TO_PTR(-ECANCELED));
296✔
1134
        if (r < 0)
296✔
1135
                return log_error_errno(r, "Failed to register signal to event: %m");
×
1136
        r = sd_event_add_signal(event, NULL, SIGTERM | SD_EVENT_SIGNAL_PROCMASK, NULL, INT_TO_PTR(-ECANCELED));
296✔
1137
        if (r < 0)
296✔
1138
                return log_error_errno(r, "Failed to register signal to event: %m");
×
1139

1140
        _cleanup_free_ char *bind_name = NULL;
296✔
1141
        r = notify_socket_prepare(
296✔
1142
                        event,
1143
                        SD_EVENT_PRIORITY_NORMAL - 5,
1144
                        helper_on_notify,
1145
                        ctx,
1146
                        &bind_name);
1147
        if (r < 0)
296✔
1148
                return log_error_errno(r, "Failed to prepare notify socket: %m");
×
1149

1150
        r = pidref_safe_fork(ctx->name, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &ctx->pid);
296✔
1151
        if (r < 0)
592✔
1152
                return log_error_errno(r, "Failed to fork process %s: %m", ctx->name);
×
1153
        if (r == 0) {
592✔
1154
                /* Child */
1155
                if (setenv("NOTIFY_SOCKET", bind_name, 1) < 0) {
296✔
1156
                        log_error_errno(errno, "setenv() failed: %m");
×
1157
                        _exit(EXIT_FAILURE);
×
1158
                }
1159
                r = invoke_callout_binary(cmdline[0], (char *const*) cmdline);
296✔
1160
                log_error_errno(r, "Failed to execute %s tool: %m", cmdline[0]);
×
1161
                _exit(EXIT_FAILURE);
×
1162
        }
1163

1164
        /* Quit the loop w/ when child process exits */
1165
        _cleanup_(sd_event_source_unrefp) sd_event_source *exit_source = NULL;
296✔
1166
        r = event_add_child_pidref(event, &exit_source, &ctx->pid, WEXITED, helper_on_exit, ctx);
296✔
1167
        if (r < 0)
296✔
1168
                return log_error_errno(r, "Failed to add child process to event loop: %m");
×
1169

1170
        r = sd_event_source_set_child_process_own(exit_source, true);
296✔
1171
        if (r < 0)
296✔
1172
                return log_error_errno(r, "Failed to take ownership of child process: %m");
×
1173

1174
        /* Process events until the helper quits */
1175
        return sd_event_loop(event);
296✔
1176
}
1177

1178
/* Build the filenames and paths which is normally done by transfer_acquire_instance(), but for partial
1179
 * and pending instances which are about to be installed (in which case, transfer_acquire_instance() is
1180
 * skipped). */
1181
int transfer_compute_temporary_paths(Transfer *t, Instance *i, InstanceMetadata *f) {
594✔
1182
        _cleanup_free_ char *formatted_pattern = NULL;
594✔
1183
        int r;
594✔
1184

1185
        assert(t);
594✔
1186
        assert(i);
594✔
1187

1188
        assert(!t->final_path);
594✔
1189
        assert(!t->temporary_partial_path);
594✔
1190
        assert(!t->temporary_pending_path);
594✔
1191
        assert(!t->final_partition_label);
594✔
1192
        assert(!strv_isempty(t->target.patterns));
594✔
1193

1194
        /* Format the target name using the first pattern specified */
1195
        compile_pattern_fields(t, i, f);
594✔
1196
        r = pattern_format(t->target.patterns[0], f, &formatted_pattern);
594✔
1197
        if (r < 0)
594✔
1198
                return log_error_errno(r, "Failed to format target pattern: %m");
×
1199

1200
        if (RESOURCE_IS_FILESYSTEM(t->target.type)) {
594✔
1201
                _cleanup_free_ char *final_dir = NULL, *final_filename = NULL, *partial_filename = NULL, *pending_filename = NULL;
362✔
1202

1203
                if (!path_is_safe(formatted_pattern))
362✔
1204
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Formatted pattern is not suitable as file name, refusing: %s", formatted_pattern);
×
1205

1206
                t->final_path = path_join(t->target.path, formatted_pattern);
362✔
1207
                if (!t->final_path)
362✔
1208
                        return log_oom();
×
1209

1210
                /* Build the paths for the partial and pending files, which hold the resource while it’s
1211
                 * being acquired and after it’s been acquired (but before it’s moved to the final_path
1212
                 * when it’s installed).
1213
                 *
1214
                 * Split the filename off the `final_path`, then add a prefix to it for each of partial and
1215
                 * pending, then join them back on to the same directory. */
1216
                r = path_split_prefix_filename(t->final_path, &final_dir, &final_filename);
362✔
1217
                if (r < 0)
362✔
1218
                        return log_error_errno(r, "Failed to parse path: %m");
×
1219

1220
                if (!strprepend(&partial_filename, ".sysupdate.partial.", final_filename))
362✔
1221
                        return log_oom();
×
1222

1223
                if (!strprepend(&pending_filename, ".sysupdate.pending.", final_filename))
362✔
1224
                        return log_oom();
×
1225

1226
                t->temporary_partial_path = path_join(final_dir, partial_filename);
362✔
1227
                if (!t->temporary_partial_path)
362✔
1228
                        return log_oom();
×
1229

1230
                t->temporary_pending_path = path_join(final_dir, pending_filename);
362✔
1231
                if (!t->temporary_pending_path)
362✔
1232
                        return log_oom();
×
1233
        }
1234

1235
        if (t->target.type == RESOURCE_PARTITION) {
594✔
1236
                r = gpt_partition_label_valid(formatted_pattern);
232✔
1237
                if (r < 0)
232✔
1238
                        return log_error_errno(r, "Failed to determine if formatted pattern is suitable as GPT partition label: %s", formatted_pattern);
×
1239
                if (!r)
232✔
1240
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Formatted pattern is not suitable as GPT partition label, refusing: %s", formatted_pattern);
×
1241

1242
                if (!t->target.partition_type_set)
232✔
1243
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Partition type must be set for partition targets.");
×
1244

1245
                /* Derive temporary partition type UUIDs for partial/pending states from the configured
1246
                 * partition type. This avoids the need for label prefixes. */
1247
                r = gpt_partition_type_uuid_for_sysupdate_partial(t->target.partition_type.uuid, &t->partition_type_partial);
232✔
1248
                if (r < 0)
232✔
1249
                        return log_error_errno(r, "Failed to derive partial partition type UUID: %m");
×
1250

1251
                r = gpt_partition_type_uuid_for_sysupdate_pending(t->target.partition_type.uuid, &t->partition_type_pending);
232✔
1252
                if (r < 0)
232✔
1253
                        return log_error_errno(r, "Failed to derive pending partition type UUID: %m");
×
1254

1255
                t->final_partition_label = TAKE_PTR(formatted_pattern);
232✔
1256
        }
1257

1258
        return 0;
1259
}
1260

1261
int transfer_acquire_instance(Transfer *t, Instance *i, InstanceMetadata *f, TransferProgress cb, void *userdata) {
296✔
1262
        _cleanup_free_ char *digest = NULL;
296✔
1263
        char offset[DECIMAL_STR_MAX(uint64_t)+1], max_size[DECIMAL_STR_MAX(uint64_t)+1];
296✔
1264
        const char *where = NULL;
296✔
1265
        Instance *existing;
296✔
1266
        int r;
296✔
1267

1268
        assert(t);
296✔
1269
        assert(i);
296✔
1270
        assert(f);
296✔
1271
        assert(i->resource == &t->source);
296✔
1272
        assert(cb);
296✔
1273

1274
        /* Does this instance already exist in the target? Then we don't need to acquire anything */
1275
        existing = resource_find_instance(&t->target, i->metadata.version);
296✔
1276
        if (existing && (existing->is_partial || existing->is_pending))
296✔
1277
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to acquire '%s', instance is already partial or pending in the target.", i->path);
×
1278
        if (existing) {
296✔
1279
                log_info("No need to acquire '%s', already installed.", i->path);
×
1280
                return 0;
×
1281
        }
1282

1283
        if (RESOURCE_IS_FILESYSTEM(t->target.type)) {
296✔
1284
                r = mkdir_parents(t->temporary_partial_path, 0755);
184✔
1285
                if (r < 0)
184✔
1286
                        return log_error_errno(r, "Cannot create target directory: %m");
×
1287

1288
                r = mkdir_parents(t->temporary_pending_path, 0755);
184✔
1289
                if (r < 0)
184✔
1290
                        return log_error_errno(r, "Cannot create target directory: %m");
×
1291

1292
                r = mkdir_parents(t->final_path, 0755);
184✔
1293
                if (r < 0)
184✔
1294
                        return log_error_errno(r, "Cannot create target directory: %m");
×
1295

1296
                where = t->final_path;
184✔
1297
        }
1298

1299
        if (t->target.type == RESOURCE_PARTITION) {
296✔
1300
                r = find_suitable_partition(
112✔
1301
                                t->target.path,
112✔
1302
                                i->metadata.size,
1303
                                t->target.partition_type_set ? &t->target.partition_type.uuid : NULL,
112✔
1304
                                &t->partition_info);
1305
                if (r < 0)
112✔
1306
                        return r;
1307

1308
                xsprintf(offset, "%" PRIu64, t->partition_info.start);
112✔
1309
                xsprintf(max_size, "%" PRIu64, t->partition_info.size);
112✔
1310

1311
                where = t->partition_info.device;
112✔
1312

1313
                /* Set the partition label and change the partition type to the derived "partial" type UUID
1314
                 * to indicate that a transfer to it is in progress. */
1315
                r = free_and_strdup_warn(&t->partition_info.label, t->final_partition_label);
112✔
1316
                if (r < 0)
112✔
1317
                        return r;
1318
                t->partition_info.type = t->partition_type_partial;
112✔
1319
                t->partition_change = PARTITION_LABEL | PARTITION_TYPE;
112✔
1320

1321
                log_debug("Marking partition '%s' as partial (label='%s', type=%s).",
112✔
1322
                          t->partition_info.device,
1323
                          t->partition_info.label,
1324
                          SD_ID128_TO_UUID_STRING(t->partition_info.type));
1325
                r = patch_partition(
224✔
1326
                                t->target.path,
112✔
1327
                                &t->partition_info,
1328
                                t->partition_change);
1329
                if (r < 0)
112✔
1330
                        return r;
1331
        }
1332

1333
        assert(where);
296✔
1334

1335
        log_info("%s Acquiring %s %s %s...", glyph(GLYPH_DOWNLOAD), i->path, glyph(GLYPH_ARROW_RIGHT), where);
592✔
1336

1337
        if (RESOURCE_IS_URL(i->resource->type)) {
296✔
1338
                /* For URL sources we require the SHA256 sum to be known so that we can validate the
1339
                 * download. */
1340

1341
                if (!i->metadata.sha256sum_set)
32✔
1342
                        return log_error_errno(r, "SHA256 checksum not known for download '%s', refusing.", i->path);
×
1343

1344
                digest = hexmem(i->metadata.sha256sum, sizeof(i->metadata.sha256sum));
32✔
1345
                if (!digest)
32✔
1346
                        return log_oom();
×
1347
        }
1348

1349
        switch (i->resource->type) { /* Source */
296✔
1350

1351
        case RESOURCE_REGULAR_FILE:
224✔
1352

1353
                switch (t->target.type) { /* Target */
224✔
1354

1355
                case RESOURCE_REGULAR_FILE:
128✔
1356

1357
                        /* regular file → regular file (why fork off systemd-import for such a simple file
1358
                         * copy case? implicit decompression mostly, and thus also sandboxing. Also, the
1359
                         * importer has some tricks up its sleeve, such as sparse file generation, which we
1360
                         * want to take benefit of, too.) */
1361

1362
                        r = run_callout("(sd-import-raw)",
256✔
1363
                                        STRV_MAKE(
128✔
1364
                                               SYSTEMD_IMPORT_PATH,
1365
                                               "raw",
1366
                                               "--direct",          /* just copy/unpack the specified file, don't do anything else */
1367
                                               arg_sync ? "--sync=yes" : "--sync=no",
1368
                                               i->path,
1369
                                               t->temporary_partial_path),
1370
                                        t, i, cb, userdata);
1371
                        break;
224✔
1372

1373
                case RESOURCE_PARTITION:
96✔
1374

1375
                        /* regular file → partition */
1376

1377
                        r = run_callout("(sd-import-raw)",
192✔
1378
                                        STRV_MAKE(
96✔
1379
                                               SYSTEMD_IMPORT_PATH,
1380
                                               "raw",
1381
                                               "--direct",          /* just copy/unpack the specified file, don't do anything else */
1382
                                               "--offset", offset,
1383
                                               "--size-max", max_size,
1384
                                               arg_sync ? "--sync=yes" : "--sync=no",
1385
                                               i->path,
1386
                                               t->target.path),
1387
                                        t, i, cb, userdata);
1388
                        break;
1389

1390
                default:
×
1391
                        assert_not_reached();
×
1392
                }
1393

1394
                break;
224✔
1395

1396
        case RESOURCE_DIRECTORY:
40✔
1397
        case RESOURCE_SUBVOLUME:
1398
                assert(IN_SET(t->target.type, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME));
40✔
1399

1400
                /* directory/subvolume → directory/subvolume */
1401

1402
                r = run_callout("(sd-import-fs)",
80✔
1403
                                STRV_MAKE(
80✔
1404
                                       SYSTEMD_IMPORT_FS_PATH,
1405
                                       "run",
1406
                                       "--direct",          /* just untar the specified file, don't do anything else */
1407
                                       arg_sync ? "--sync=yes" : "--sync=no",
1408
                                       t->target.type == RESOURCE_SUBVOLUME ? "--btrfs-subvol=yes" : "--btrfs-subvol=no",
1409
                                       i->path,
1410
                                       t->temporary_partial_path),
1411
                                t, i, cb, userdata);
1412
                break;
1413

1414
        case RESOURCE_TAR:
×
1415
                assert(IN_SET(t->target.type, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME));
×
1416

1417
                /* tar → directory/subvolume */
1418

1419
                r = run_callout("(sd-import-tar)",
×
1420
                                STRV_MAKE(
×
1421
                                       SYSTEMD_IMPORT_PATH,
1422
                                       "tar",
1423
                                       "--direct",          /* just untar the specified file, don't do anything else */
1424
                                       arg_sync ? "--sync=yes" : "--sync=no",
1425
                                       t->target.type == RESOURCE_SUBVOLUME ? "--btrfs-subvol=yes" : "--btrfs-subvol=no",
1426
                                       i->path,
1427
                                       t->temporary_partial_path),
1428
                                t, i, cb, userdata);
1429
                break;
1430

1431
        case RESOURCE_URL_FILE:
16✔
1432

1433
                switch (t->target.type) {
16✔
1434

1435
                case RESOURCE_REGULAR_FILE:
×
1436

1437
                        /* url file → regular file */
1438

1439
                        r = run_callout("(sd-pull-raw)",
×
1440
                                       STRV_MAKE(
×
1441
                                               SYSTEMD_PULL_PATH,
1442
                                               "raw",
1443
                                               "--direct",          /* just download the specified URL, don't download anything else */
1444
                                               "--verify", digest,  /* validate by explicit SHA256 sum */
1445
                                               arg_sync ? "--sync=yes" : "--sync=no",
1446
                                               i->path,
1447
                                               t->temporary_partial_path),
1448
                                        t, i, cb, userdata);
1449
                        break;
16✔
1450

1451
                case RESOURCE_PARTITION:
16✔
1452

1453
                        /* url file → partition */
1454

1455
                        r = run_callout("(sd-pull-raw)",
32✔
1456
                                        STRV_MAKE(
16✔
1457
                                               SYSTEMD_PULL_PATH,
1458
                                               "raw",
1459
                                               "--direct",              /* just download the specified URL, don't download anything else */
1460
                                               "--verify", digest,      /* validate by explicit SHA256 sum */
1461
                                               "--offset", offset,
1462
                                               "--size-max", max_size,
1463
                                               arg_sync ? "--sync=yes" : "--sync=no",
1464
                                               i->path,
1465
                                               t->target.path),
1466
                                        t, i, cb, userdata);
1467
                        break;
1468

1469
                default:
×
1470
                        assert_not_reached();
×
1471
                }
1472

1473
                break;
16✔
1474

1475
        case RESOURCE_URL_TAR:
16✔
1476
                assert(IN_SET(t->target.type, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME));
16✔
1477

1478
                r = run_callout("(sd-pull-tar)",
32✔
1479
                                STRV_MAKE(
32✔
1480
                                       SYSTEMD_PULL_PATH,
1481
                                       "tar",
1482
                                       "--direct",          /* just download the specified URL, don't download anything else */
1483
                                       "--verify", digest,  /* validate by explicit SHA256 sum */
1484
                                       t->target.type == RESOURCE_SUBVOLUME ? "--btrfs-subvol=yes" : "--btrfs-subvol=no",
1485
                                       arg_sync ? "--sync=yes" : "--sync=no",
1486
                                       i->path,
1487
                                       t->temporary_partial_path),
1488
                                t, i, cb, userdata);
1489
                break;
1490

1491
        default:
×
1492
                assert_not_reached();
×
1493
        }
1494
        if (r < 0)
296✔
1495
                return r;
1496

1497
        if (RESOURCE_IS_FILESYSTEM(t->target.type)) {
296✔
1498
                bool need_sync = false;
184✔
1499
                assert(t->temporary_partial_path);
184✔
1500
                assert(t->temporary_pending_path);
184✔
1501

1502
                /* Apply file attributes if set */
1503
                if (f->mtime != USEC_INFINITY) {
184✔
1504
                        struct timespec ts;
168✔
1505

1506
                        timespec_store(&ts, f->mtime);
168✔
1507

1508
                        if (utimensat(AT_FDCWD, t->temporary_partial_path, (struct timespec[2]) { ts, ts }, AT_SYMLINK_NOFOLLOW) < 0)
168✔
1509
                                return log_error_errno(errno, "Failed to adjust mtime of '%s': %m", t->temporary_partial_path);
×
1510

1511
                        need_sync = true;
168✔
1512
                }
1513

1514
                if (f->mode != MODE_INVALID) {
184✔
1515
                        /* Try with AT_SYMLINK_NOFOLLOW first, because it's the safe thing to do. Older
1516
                         * kernels don't support that however, in that case we fall back to chmod(). Not as
1517
                         * safe, but shouldn't be a problem, given that we don't create symlinks here. */
1518
                        if (fchmodat(AT_FDCWD, t->temporary_partial_path, f->mode, AT_SYMLINK_NOFOLLOW) < 0 &&
168✔
1519
                            (!ERRNO_IS_NOT_SUPPORTED(errno) || chmod(t->temporary_partial_path, f->mode) < 0))
×
1520
                                return log_error_errno(errno, "Failed to adjust mode of '%s': %m", t->temporary_partial_path);
×
1521

1522
                        need_sync = true;
1523
                }
1524

1525
                /* Synchronize */
1526
                if (arg_sync && need_sync) {
184✔
1527
                        if (t->target.type == RESOURCE_REGULAR_FILE)
168✔
1528
                                r = fsync_path_and_parent_at(AT_FDCWD, t->temporary_partial_path);
128✔
1529
                        else {
1530
                                assert(IN_SET(t->target.type, RESOURCE_DIRECTORY, RESOURCE_SUBVOLUME));
40✔
1531
                                r = syncfs_path(AT_FDCWD, t->temporary_partial_path);
40✔
1532
                        }
1533
                        if (r < 0)
168✔
1534
                                return log_error_errno(r, "Failed to synchronize file system backing '%s': %m", t->temporary_partial_path);
×
1535
                }
1536

1537
                t->install_read_only = f->read_only;
184✔
1538

1539
                /* Rename the file from `.sysupdate.partial.<VERSION>` to `.sysupdate.pending.<VERSION>` to indicate it’s ready to install. */
1540
                log_debug("Renaming resource instance '%s' to '%s'.", t->temporary_partial_path, t->temporary_pending_path);
184✔
1541
                r = install_file(AT_FDCWD, t->temporary_partial_path,
552✔
1542
                                 AT_FDCWD, t->temporary_pending_path,
184✔
1543
                                 INSTALL_REPLACE|
184✔
1544
                                 (t->install_read_only > 0 ? INSTALL_READ_ONLY : 0)|
368✔
1545
                                 (t->target.type == RESOURCE_REGULAR_FILE ? INSTALL_FSYNC_FULL : INSTALL_SYNCFS));
184✔
1546
                if (r < 0)
184✔
1547
                        return log_error_errno(r, "Failed to move '%s' into pending place: %m", t->temporary_pending_path);
×
1548
        }
1549

1550
        if (t->target.type == RESOURCE_PARTITION) {
296✔
1551
                /* Now change the partition type to the derived "pending" type UUID to indicate that the
1552
                 * acquire is complete and the partition is ready for install. */
1553
                t->partition_info.type = t->partition_type_pending;
112✔
1554
                t->partition_change = PARTITION_TYPE;
112✔
1555

1556
                if (f->partition_uuid_set) {
112✔
1557
                        t->partition_info.uuid = f->partition_uuid;
×
1558
                        t->partition_change |= PARTITION_UUID;
×
1559
                }
1560

1561
                if (f->partition_flags_set) {
112✔
1562
                        t->partition_info.flags = f->partition_flags;
×
1563
                        t->partition_change |= PARTITION_FLAGS;
×
1564
                }
1565

1566
                if (f->no_auto >= 0) {
112✔
1567
                        t->partition_info.no_auto = f->no_auto;
×
1568
                        t->partition_change |= PARTITION_NO_AUTO;
×
1569
                }
1570

1571
                if (f->read_only >= 0) {
112✔
1572
                        t->partition_info.read_only = f->read_only;
×
1573
                        t->partition_change |= PARTITION_READ_ONLY;
×
1574
                }
1575

1576
                if (f->growfs >= 0) {
112✔
1577
                        t->partition_info.growfs = f->growfs;
×
1578
                        t->partition_change |= PARTITION_GROWFS;
×
1579
                }
1580

1581
                log_debug("Marking partition '%s' as pending (type=%s).",
112✔
1582
                          t->partition_info.device,
1583
                          SD_ID128_TO_UUID_STRING(t->partition_info.type));
1584
                r = patch_partition(
224✔
1585
                                t->target.path,
112✔
1586
                                &t->partition_info,
112✔
1587
                                t->partition_change);
1588
                if (r < 0)
112✔
1589
                        return r;
1590
        }
1591

1592
        /* For regular file cases the only step left is to install the file in place, which install_file()
1593
         * will do via rename(). For partition cases the only step left is to update the partition table,
1594
         * which is done at the same place. */
1595

1596
        log_info("Successfully acquired '%s'.", i->path);
296✔
1597
        return 0;
1598
}
1599

1600
int transfer_process_partial_and_pending_instance(Transfer *t, Instance *i) {
256✔
1601
        InstanceMetadata f;
256✔
1602
        Instance *existing;
256✔
1603
        int r;
256✔
1604

1605
        assert(t);
256✔
1606
        assert(i);
256✔
1607

1608
        log_debug("transfer_process_partial_and_pending_instance %s", i->path);
256✔
1609

1610
        /* Does this instance already exist in the target but isn’t pending? */
1611
        existing = resource_find_instance(&t->target, i->metadata.version);
256✔
1612
        if (existing && !existing->is_pending) {
256✔
1613
                log_info("Resource '%s' instance is already in the target but is not pending.", i->path);
30✔
1614
                return 0;
30✔
1615
        }
1616

1617
        /* All we need to do is compute the temporary paths. We don’t need to do any of the other work in
1618
         * transfer_acquire_instance(). */
1619
        r = transfer_compute_temporary_paths(t, i, &f);
226✔
1620
        if (r < 0)
226✔
1621
                return r;
1622

1623
        /* This is the analogue of find_suitable_partition(), but since finding the suitable partition has
1624
         * already happened in the acquire phase, the target should already have that information and it
1625
         * should already have been claimed with the pending partition type UUID. */
1626
        if (t->target.type == RESOURCE_PARTITION) {
226✔
1627
                assert(i->resource == &t->target);
88✔
1628
                assert(i->is_pending);
88✔
1629

1630
                r = partition_info_copy(&t->partition_info, &i->partition_info);
88✔
1631
                if (r < 0)
88✔
UNCOV
1632
                        return r;
×
1633
        }
1634

1635
        return 0;
1636
}
1637

1638
int transfer_install_instance(
296✔
1639
                Transfer *t,
1640
                Instance *i,
1641
                const char *root) {
1642

1643
        int r;
296✔
1644

1645
        assert(t);
296✔
1646
        assert(i);
296✔
1647
        assert(i->resource);
296✔
1648
        assert(i->is_pending || t == container_of(i->resource, Transfer, source));
296✔
1649

1650
        log_debug("transfer_install_instance %s %s %s %d", i->path, t->temporary_pending_path, t->final_partition_label, t->partition_change);
296✔
1651

1652
        if (t->temporary_pending_path) {
296✔
1653
                assert(RESOURCE_IS_FILESYSTEM(t->target.type));
184✔
1654
                assert(t->final_path);
184✔
1655

1656
                r = install_file(AT_FDCWD, t->temporary_pending_path,
552✔
1657
                                 AT_FDCWD, t->final_path,
1658
                                 INSTALL_REPLACE|
184✔
1659
                                 (t->install_read_only > 0 ? INSTALL_READ_ONLY : 0)|
368✔
1660
                                 (t->target.type == RESOURCE_REGULAR_FILE ? INSTALL_FSYNC_FULL : INSTALL_SYNCFS));
184✔
1661
                if (r < 0)
184✔
UNCOV
1662
                        return log_error_errno(r, "Failed to move '%s' into place: %m", t->final_path);
×
1663

1664
                log_info("Successfully installed '%s' (%s) as '%s' (%s).",
184✔
1665
                         i->path,
1666
                         resource_type_to_string(i->resource->type),
1667
                         t->final_path,
1668
                         resource_type_to_string(t->target.type));
1669

1670
                t->temporary_pending_path = mfree(t->temporary_pending_path);
184✔
1671
        }
1672

1673
        if (t->final_partition_label) {
296✔
1674
                assert(t->target.type == RESOURCE_PARTITION);
112✔
1675
                assert(t->target.partition_type_set);
112✔
1676

1677
                r = free_and_strdup_warn(&t->partition_info.label, t->final_partition_label);
112✔
1678
                if (r < 0)
112✔
1679
                        return r;
1680

1681
                /* Restore the original partition type UUID now that the partition is fully installed. */
1682
                t->partition_info.type = t->target.partition_type.uuid;
112✔
1683
                t->partition_change = PARTITION_LABEL | PARTITION_TYPE;
112✔
1684

1685
                r = patch_partition(
224✔
1686
                                t->target.path,
112✔
1687
                                &t->partition_info,
112✔
1688
                                t->partition_change);
1689
                if (r < 0)
112✔
1690
                        return r;
1691

1692
                log_info("Successfully installed '%s' (%s) as '%s' (%s).",
112✔
1693
                         i->path,
1694
                         resource_type_to_string(i->resource->type),
1695
                         t->partition_info.device,
1696
                         resource_type_to_string(t->target.type));
1697
        }
1698

1699
        if (t->current_symlink) {
296✔
1700
                _cleanup_free_ char *buf = NULL, *parent = NULL, *relative = NULL, *resolved = NULL;
56✔
1701
                const char *link_path, *link_target;
56✔
1702
                bool resolve_link_path = false;
56✔
1703

1704
                if (RESOURCE_IS_FILESYSTEM(t->target.type)) {
56✔
1705

1706
                        assert(t->target.path);
56✔
1707

1708
                        if (path_is_absolute(t->current_symlink)) {
56✔
1709
                                link_path = t->current_symlink;
1710
                                resolve_link_path = true;
1711
                        } else {
1712
                                buf = path_make_absolute(t->current_symlink, t->target.path);
×
UNCOV
1713
                                if (!buf)
×
UNCOV
1714
                                        return log_oom();
×
1715

1716
                                link_path = buf;
1717
                        }
1718

1719
                        link_target = t->final_path;
56✔
1720

1721
                } else if (t->target.type == RESOURCE_PARTITION) {
×
1722

1723
                        assert(path_is_absolute(t->current_symlink));
×
1724

UNCOV
1725
                        link_path = t->current_symlink;
×
1726
                        link_target = t->partition_info.device;
×
1727

1728
                        resolve_link_path = true;
×
1729
                } else
UNCOV
1730
                        assert_not_reached();
×
1731

1732
                if (resolve_link_path && root) {
56✔
1733
                        r = chase(link_path, root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT|CHASE_TRIGGER_AUTOFS, &resolved, NULL);
×
UNCOV
1734
                        if (r < 0)
×
1735
                                return log_error_errno(r, "Failed to resolve current symlink path '%s': %m", link_path);
×
1736

UNCOV
1737
                        link_path = resolved;
×
1738
                }
1739

1740
                if (link_target) {
56✔
1741
                        r = path_extract_directory(link_path, &parent);
56✔
1742
                        if (r < 0)
56✔
UNCOV
1743
                                return log_error_errno(r, "Failed to extract directory of target path '%s': %m", link_path);
×
1744

1745
                        r = path_make_relative(parent, link_target, &relative);
56✔
1746
                        if (r < 0)
56✔
UNCOV
1747
                                return log_error_errno(r, "Failed to make symlink path '%s' relative to '%s': %m", link_target, parent);
×
1748

1749
                        r = symlink_atomic(relative, link_path);
56✔
1750
                        if (r < 0)
56✔
UNCOV
1751
                                return log_error_errno(r, "Failed to update current symlink '%s' %s '%s': %m",
×
1752
                                                       link_path,
1753
                                                       glyph(GLYPH_ARROW_RIGHT),
1754
                                                       relative);
1755

1756
                        log_info("Updated symlink '%s' %s '%s'.",
56✔
1757
                                 link_path, glyph(GLYPH_ARROW_RIGHT), relative);
1758
                }
1759
        }
1760

1761
        return 0;
1762
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc