helixforge-engine 1.3.0

HelixForge's engine: plans, runners, history, health checks. Part of the HelixForge application, not a general-purpose library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Configuration backups in one place: the copies HelixForge's scripts leave
//! beside a file before changing it (`<file>.commander-backup-<time>`,
//! `<file>.helixforge-backup-<time>`), found in the user's configuration and
//! in the system places those scripts change, and restored in a way that can
//! itself be undone. Snapshots (Snapper, Timeshift) are taken before system
//! changes where the machine supports them; HelixForge does not roll
//! snapshots or packages back itself.

use crate::{Plan, Step, quote};
use helixforge_core::{Risk, printable};
use std::{
    fs,
    path::{Path, PathBuf},
};

/// One backup copy and the file it was taken from.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Backup {
    pub backup: PathBuf,
    pub original: PathBuf,
    /// When it was taken, `YYYY-MM-DD HH:MM:SS`, from its name.
    pub taken: Option<String>,
    /// Restoring it needs root.
    pub system: bool,
    pub directory: bool,
}

impl Backup {
    /// The time it was taken as one sortable number.
    fn stamp(&self) -> String {
        self.taken.clone().unwrap_or_default()
    }
}

/// The original's name and the time stamp, for a backup's file name.
pub fn parse_name(name: &str) -> Option<(&str, &str)> {
    for marker in [".commander-backup-", ".helixforge-backup-"] {
        if let Some((original, stamp)) = name.rsplit_once(marker)
            && !original.is_empty()
            && stamp.len() >= 14
            && stamp
                .chars()
                .all(|character| character.is_ascii_digit() || character == '-')
        {
            return Some((original, stamp));
        }
    }
    None
}

/// `20261015164340...` as `2026-10-15 16:43:40`.
fn readable(stamp: &str) -> Option<String> {
    let digits: String = stamp.chars().take_while(char::is_ascii_digit).collect();
    if digits.len() < 14 {
        return None;
    }
    Some(format!(
        "{}-{}-{} {}:{}:{}",
        &digits[0..4],
        &digits[4..6],
        &digits[6..8],
        &digits[8..10],
        &digits[10..12],
        &digits[12..14]
    ))
}

/// Where backups are looked for: the user's configuration (any depth, as
/// the dotfiles installers leave them) and the system places HelixForge's
/// scripts change (shallow).
pub struct Roots {
    /// Searched to any depth, skipping caches and repositories.
    pub user: Vec<PathBuf>,
    /// The home directory itself, one level.
    pub home: Option<PathBuf>,
    /// Searched a few levels deep; restoring these needs root.
    pub system: Vec<(PathBuf, usize)>,
}

impl Roots {
    /// This user's and this machine's places.
    pub fn here() -> Self {
        let home = std::env::var_os("HOME").map(PathBuf::from);
        let xdg = |name: &str, fallback: &str| {
            std::env::var_os(name)
                .map(PathBuf::from)
                .filter(|path| path.is_absolute())
                .or_else(|| home.as_ref().map(|home| home.join(fallback)))
        };
        Roots {
            user: [
                xdg("XDG_CONFIG_HOME", ".config"),
                xdg("XDG_DATA_HOME", ".local/share"),
                home.as_ref().map(|home| home.join(".local/bin")),
            ]
            .into_iter()
            .flatten()
            .collect(),
            home: home.clone(),
            system: vec![
                (PathBuf::from("/etc"), 3),
                (PathBuf::from("/boot"), 3),
                (PathBuf::from("/usr/share/sddm/themes"), 1),
                (PathBuf::from("/usr/share/fonts"), 2),
            ],
        }
    }
}

/// Directories not worth searching for configuration backups.
const SKIP: [&str; 8] = [
    ".git",
    "node_modules",
    "Trash",
    "containers",
    "Steam",
    "flatpak",
    "helixforge",
    "sources",
];

/// Every backup under the roots, newest first.
pub fn discover(roots: &Roots) -> Vec<Backup> {
    let mut found = Vec::new();
    if let Some(home) = &roots.home {
        walk(home, 1, false, &mut found);
    }
    for root in &roots.user {
        walk(root, usize::MAX, false, &mut found);
    }
    for (root, depth) in &roots.system {
        walk(root, *depth, true, &mut found);
    }
    found.sort_by_key(|backup| std::cmp::Reverse(backup.stamp()));
    found.dedup_by(|a, b| a.backup == b.backup);
    found
}

fn walk(directory: &Path, depth: usize, system: bool, into: &mut Vec<Backup>) {
    if depth == 0 {
        return;
    }
    let Ok(listing) = fs::read_dir(directory) else {
        return;
    };
    for item in listing.flatten() {
        let path = item.path();
        // Links are never followed, nor offered: they belong to another owner.
        let Ok(kind) = item.file_type() else { continue };
        if kind.is_symlink() {
            continue;
        }
        let name = item.file_name().to_string_lossy().into_owned();
        if let Some((original, stamp)) = parse_name(&name) {
            into.push(Backup {
                original: directory.join(original),
                taken: readable(stamp),
                system,
                directory: kind.is_dir(),
                backup: path,
            });
            continue;
        }
        if kind.is_dir() && !SKIP.contains(&name.as_str()) {
            walk(&path, depth - 1, system, into);
        }
    }
}

/// The step that puts a backup back: the current file, if any, is moved
/// aside as a backup of its own first, so the restore can be undone too.
pub fn restore_step(backup: &Backup) -> Step {
    let sudo = if backup.system { "sudo " } else { "" };
    let original = quote(&backup.original.to_string_lossy());
    let copy = quote(&backup.backup.to_string_lossy());
    let program = format!(
        "hf_stamp=$(date +%Y%m%d%H%M%S)\n\
         if [ -e {original} ] || [ -L {original} ]; then\n  \
         {sudo}mv -- {original} {original}.helixforge-backup-\"$hf_stamp\" || exit 1\n  \
         printf 'HelixForge: the current one is kept as %s\\n' {original}.helixforge-backup-\"$hf_stamp\"\n\
         fi\n\
         {sudo}cp -a -- {copy} {original} || exit 1\n\
         printf 'HelixForge: restored %s\\n' {original}"
    );
    Step::Shell {
        id: "restore".to_string(),
        name: printable(&format!("Restore {}", backup.original.display())),
        program,
        // Names on disk are shown made printable; the program quotes them as they are.
        display: printable(&format!(
            "{sudo}cp -a {} {}  (the current one is kept as a backup first)",
            backup.backup.display(),
            backup.original.display()
        )),
        requires: if backup.system {
            vec!["sudo".to_string()]
        } else {
            Vec::new()
        },
        changes: vec![crate::ShellChange {
            path: backup.original.clone(),
            from: (!backup.directory).then(|| backup.backup.clone()),
            text: "put back from the backup; the one there now is kept as a backup first"
                .to_string(),
        }],
    }
}

/// A plan that restores one backup.
pub fn plan_restore(backup: &Backup) -> Plan {
    let step = restore_step(backup);
    Plan {
        requirements: step_requirements(&step),
        privileged: backup.system,
        risk: if backup.system {
            Risk::Medium
        } else {
            Risk::Low
        },
        steps: vec![step],
        depends: vec![Vec::new()],
        ..Plan::default()
    }
}

fn step_requirements(step: &Step) -> Vec<String> {
    match step {
        Step::Shell { requires, .. } => requires.clone(),
        _ => Vec::new(),
    }
}

/// A snapshot tool the machine is set up for.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Snapshots {
    /// Snapper with a configuration for the root file system.
    Snapper,
    Timeshift,
}

impl Snapshots {
    /// The tool this machine has set up, if any.
    pub fn here() -> Option<Self> {
        Self::detect(
            &|path| Path::new(path).exists(),
            &helixforge_platform::binary_exists,
        )
    }

    /// `here` with the questions answered, for tests.
    pub fn detect(exists: &dyn Fn(&str) -> bool, on_path: &dyn Fn(&str) -> bool) -> Option<Self> {
        if on_path("snapper") && exists("/etc/snapper/configs/root") {
            Some(Snapshots::Snapper)
        } else if on_path("timeshift") && exists("/etc/timeshift/timeshift.json") {
            Some(Snapshots::Timeshift)
        } else {
            None
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Snapshots::Snapper => "Snapper",
            Snapshots::Timeshift => "Timeshift",
        }
    }

    /// Where its snapshots are browsed and rolled back.
    pub fn how_to_restore(self) -> &'static str {
        match self {
            Snapshots::Snapper => {
                "Btrfs Assistant, or snapper list and snapper rollback, from a terminal or the boot menu's snapshots"
            }
            Snapshots::Timeshift => "Timeshift, or timeshift --restore from a terminal",
        }
    }

    /// The step that takes a snapshot, described as `description`.
    pub fn step(self, description: &str) -> Step {
        let words: Vec<String> = match self {
            Snapshots::Snapper => vec![
                "sudo".into(),
                "snapper".into(),
                "-c".into(),
                "root".into(),
                "create".into(),
                "--cleanup-algorithm".into(),
                "number".into(),
                "--description".into(),
                description.into(),
            ],
            Snapshots::Timeshift => vec![
                "sudo".into(),
                "timeshift".into(),
                "--create".into(),
                "--comments".into(),
                description.into(),
            ],
        };
        let program = words
            .iter()
            .map(|word| quote(word))
            .collect::<Vec<_>>()
            .join(" ");
        Step::Shell {
            id: "snapshot".to_string(),
            name: format!("Take a {} snapshot", self.label()),
            program,
            display: words.join(" "),
            requires: vec![self.tool().to_string(), "sudo".to_string()],
            changes: Vec::new(),
        }
    }

    fn tool(self) -> &'static str {
        match self {
            Snapshots::Snapper => "snapper",
            Snapshots::Timeshift => "timeshift",
        }
    }
}

/// Whether a plan changes the system enough to snapshot first: risk high, or
/// a step whose entry changes privileged files, the kernel or disks.
pub fn worth_a_snapshot(plan: &Plan, flags: &dyn Fn(&str) -> Option<String>) -> bool {
    plan.risk == Risk::High
        || plan.steps.iter().any(|step| {
            flags(step.id()).is_some_and(|flags| {
                flags
                    .split_whitespace()
                    .any(|flag| matches!(flag, "PFM" | "K" | "D" | "SI"))
            })
        })
}

/// Put a snapshot step first, taken before the plan's other steps run.
pub fn with_snapshot(plan: &mut Plan, tool: Snapshots) {
    let names: Vec<&str> = plan.steps.iter().map(Step::name).collect();
    let description = format!("HelixForge, before: {}", names.join(", "));
    let step = tool.step(&description);
    plan.requirements.extend(step_requirements(&step));
    plan.requirements.sort();
    plan.requirements.dedup();
    plan.privileged = true;
    plan.steps.insert(0, step);
    // Every index moves up by one; nothing depends on the snapshot.
    let mut depends = vec![Vec::new()];
    depends.extend(
        plan.depends
            .iter()
            .map(|list| list.iter().map(|index| index + 1).collect()),
    );
    plan.depends = depends;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::shell_program;
    use std::process::Command;

    #[test]
    fn backup_names_give_the_original_and_the_time() {
        assert_eq!(
            parse_name("grub.commander-backup-20261015164340-4242"),
            Some(("grub", "20261015164340-4242"))
        );
        assert_eq!(
            parse_name("pacman.conf.helixforge-backup-20261015164340"),
            Some(("pacman.conf", "20261015164340"))
        );
        assert_eq!(
            parse_name("example.commander-backup-20261015142214095844"),
            Some(("example", "20261015142214095844"))
        );
        assert_eq!(parse_name("grub.backup"), None);
        assert_eq!(parse_name(".commander-backup-20261015164340"), None);
        assert_eq!(parse_name("x.commander-backup-2026"), None);
        assert_eq!(
            readable("20261015164340-1").as_deref(),
            Some("2026-10-15 16:43:40")
        );
    }

    #[test]
    fn backups_are_found_newest_first_and_links_are_left_alone() {
        let root = tempfile::tempdir().unwrap();
        let home = root.path().join("home");
        let config = home.join(".config");
        let etc = root.path().join("etc");
        let write = |path: &Path| {
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(path, "x").unwrap();
        };
        write(&config.join("ghostty/config.commander-backup-20261001000000-1"));
        write(&config.join("deep/a/b/c/d/e.helixforge-backup-20261003000000"));
        fs::create_dir_all(config.join("nvim.commander-backup-20261002000000-7/lua")).unwrap();
        write(&home.join(".bashrc.commander-backup-20260901000000-1"));
        write(&config.join("node_modules/x.commander-backup-20261005000000-1"));
        write(&etc.join("default/grub.commander-backup-20261004000000-9"));
        write(&etc.join("a/b/c/d/too-deep.commander-backup-20261006000000-1"));
        std::os::unix::fs::symlink(
            etc.join("default/grub.commander-backup-20261004000000-9"),
            config.join("linked.commander-backup-20261007000000-1"),
        )
        .unwrap();
        let roots = Roots {
            user: vec![config.clone()],
            home: Some(home.clone()),
            system: vec![(etc.clone(), 3)],
        };
        let found = discover(&roots);
        let shown: Vec<(String, bool, bool)> = found
            .iter()
            .map(|backup| {
                (
                    backup
                        .original
                        .strip_prefix(root.path())
                        .unwrap()
                        .display()
                        .to_string(),
                    backup.system,
                    backup.directory,
                )
            })
            .collect();
        assert_eq!(
            shown,
            [
                ("etc/default/grub".to_string(), true, false),
                ("home/.config/deep/a/b/c/d/e".to_string(), false, false),
                ("home/.config/nvim".to_string(), false, true),
                ("home/.config/ghostty/config".to_string(), false, false),
                ("home/.bashrc".to_string(), false, false),
            ]
        );
        assert_eq!(found[0].taken.as_deref(), Some("2026-10-04 00:00:00"));
    }

    #[test]
    fn a_restore_keeps_the_current_one_and_can_be_undone() {
        let root = tempfile::tempdir().unwrap();
        let original = root.path().join("config");
        let backup = root.path().join("config.commander-backup-20261001000000-1");
        fs::write(&original, "current").unwrap();
        fs::write(&backup, "older").unwrap();
        let found = Backup {
            backup: backup.clone(),
            original: original.clone(),
            taken: readable("20261001000000"),
            system: false,
            directory: false,
        };
        let plan = plan_restore(&found);
        assert_eq!(plan.risk, Risk::Low);
        assert!(!plan.privileged);
        assert!(plan.steps[0].display().starts_with("cp -a "));
        let run = |plan: &Plan| {
            let output = Command::new("sh")
                .arg("-c")
                .arg(shell_program(plan, Path::new("/lib"), Path::new("/hf")))
                .output()
                .unwrap();
            assert!(
                output.status.success(),
                "{}",
                String::from_utf8_lossy(&output.stderr)
            );
        };
        run(&plan);
        assert_eq!(fs::read_to_string(&original).unwrap(), "older");
        assert_eq!(
            fs::read_to_string(&backup).unwrap(),
            "older",
            "the backup stays"
        );
        let kept: Vec<PathBuf> = fs::read_dir(root.path())
            .unwrap()
            .flatten()
            .map(|item| item.path())
            .filter(|path| path.to_string_lossy().contains(".helixforge-backup-"))
            .collect();
        assert_eq!(kept.len(), 1);
        assert_eq!(fs::read_to_string(&kept[0]).unwrap(), "current");
        // A folder, with nothing in its place now.
        let folder = root.path().join("nvim");
        let saved = root.path().join("nvim.commander-backup-20261001000000-1");
        fs::create_dir_all(saved.join("lua")).unwrap();
        fs::write(saved.join("lua/init.lua"), "old").unwrap();
        let plan = plan_restore(&Backup {
            backup: saved,
            original: folder.clone(),
            taken: None,
            system: false,
            directory: true,
        });
        run(&plan);
        assert_eq!(
            fs::read_to_string(folder.join("lua/init.lua")).unwrap(),
            "old"
        );
        // A system file needs sudo.
        let system = plan_restore(&Backup {
            backup: PathBuf::from("/etc/default/grub.commander-backup-20261001000000-1"),
            original: PathBuf::from("/etc/default/grub"),
            taken: None,
            system: true,
            directory: false,
        });
        assert!(system.privileged);
        assert_eq!(system.requirements, ["sudo"]);
        assert!(system.steps[0].display().starts_with("sudo cp -a "));
    }

    #[test]
    fn a_strange_name_is_shown_safely_and_restored_as_it_is() {
        let backup = Backup {
            backup: PathBuf::from("/home/me/.config/a\u{1b}[2Jb.commander-backup-20261001000000-1"),
            original: PathBuf::from("/home/me/.config/a\u{1b}[2Jb"),
            taken: None,
            system: false,
            directory: false,
        };
        let step = restore_step(&backup);
        assert!(!step.display().contains('\u{1b}'));
        assert!(!step.name().contains('\u{1b}'));
        let Step::Shell { program, .. } = &step else {
            panic!("expected a shell step");
        };
        assert!(
            program.contains("'/home/me/.config/a\u{1b}[2Jb'"),
            "the real name, quoted"
        );
    }

    #[test]
    fn snapshots_go_first_when_the_system_changes() {
        let snapper = |path: &str| path == "/etc/snapper/configs/root";
        assert_eq!(
            Snapshots::detect(&snapper, &|_| true),
            Some(Snapshots::Snapper)
        );
        assert_eq!(Snapshots::detect(&|_| false, &|_| true), None);
        assert_eq!(
            Snapshots::detect(
                &|path| path == "/etc/timeshift/timeshift.json",
                &|tool| tool == "timeshift"
            ),
            Some(Snapshots::Timeshift)
        );
        let mut plan = Plan {
            steps: vec![
                Step::Command {
                    id: "grub".into(),
                    name: "GRUB Theme".into(),
                    line: "true".into(),
                },
                Step::Command {
                    id: "after".into(),
                    name: "After".into(),
                    line: "true".into(),
                },
            ],
            depends: vec![vec![], vec![0]],
            risk: Risk::Medium,
            ..Plan::default()
        };
        let flags = |id: &str| (id == "grub").then(|| "I PFM MP".to_string());
        assert!(worth_a_snapshot(&plan, &flags));
        assert!(!worth_a_snapshot(&plan, &|_| Some("I".to_string())));
        with_snapshot(&mut plan, Snapshots::Snapper);
        assert_eq!(plan.steps[0].name(), "Take a Snapper snapshot");
        assert_eq!(
            plan.steps[0].display(),
            "sudo snapper -c root create --cleanup-algorithm number --description HelixForge, before: GRUB Theme, After"
        );
        assert_eq!(plan.depends, [vec![], vec![], vec![1]]);
        assert!(plan.privileged);
        assert!(plan.requirements.contains(&"snapper".to_string()));
    }
}