Skip to content

Commit e891ccc

Browse files
committed
fix: changed the way how addons get updated
updates for installed addons now get staged, rollback is made easier
1 parent 15a084a commit e891ccc

2 files changed

Lines changed: 186 additions & 27 deletions

File tree

‎crates/core/src/addons/install.rs‎

Lines changed: 178 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,27 @@ pub struct Extracted {
300300
pub bytes: u64,
301301
}
302302

303+
/// Staging folders are named this, beside `AddOns` in `Interface`.
304+
const STAGING_PREFIX: &str = ".lightforge-staging-";
305+
306+
/// A staging folder this old was left by a run that died, not by one still
307+
/// going: no package takes an hour to unpack.
308+
const STALE_STAGING: std::time::Duration = std::time::Duration::from_secs(60 * 60);
309+
303310
/// Extract an addon package into `addons_dir`.
304311
///
305312
/// Replaces the folders it contains: an update must not leave a removed file
306313
/// from the previous version behind, since a stale `.lua` can break an addon
307-
/// far more confusingly than a missing one.
308-
pub fn extract(package: &[u8], addons_dir: &Path) -> Result<Extracted, InstallError> {
314+
/// far more confusingly than a missing one. `retire` names more folders to
315+
/// take away in the same step, the ones a repair replaces that this build no
316+
/// longer ships.
317+
///
318+
/// Nothing installed is touched until the whole package is on disk. It is
319+
/// written to a staging folder beside `AddOns`, on the same filesystem, and
320+
/// only then swapped in folder by folder with renames. A full disk or an I/O
321+
/// error while writing leaves the old addon exactly as it was, and a failed
322+
/// rename puts back every folder already swapped.
323+
pub fn extract(package: &[u8], addons_dir: &Path, retire: &[String]) -> Result<Extracted, InstallError> {
309324
let mut archive =
310325
zip::ZipArchive::new(Cursor::new(package)).map_err(InstallError::Archive)?;
311326

@@ -356,22 +371,36 @@ pub fn extract(package: &[u8], addons_dir: &Path) -> Result<Extracted, InstallEr
356371
return Err(InstallError::NoAddonFolders);
357372
}
358373

359-
// Clear the previous copy of each folder this package owns.
360-
for folder in &folders {
361-
let target = addons_dir.join(folder);
362-
if target.exists() {
363-
std::fs::remove_dir_all(&target).map_err(|source| InstallError::Io {
364-
path: target,
365-
source,
366-
})?;
374+
// A retired folder is replaced wholesale like the others, so it has to be
375+
// one plain name inside `AddOns`, the same rule the ledger's are held to.
376+
for folder in retire {
377+
if Path::new(folder).components().count() != 1 || safe_relative(Path::new(folder)).is_none() {
378+
return Err(InstallError::UnsafePath(folder.clone()));
367379
}
368380
}
369381

382+
let io = |path: &Path| {
383+
let path = path.to_path_buf();
384+
move |source| InstallError::Io { path, source }
385+
};
386+
std::fs::create_dir_all(addons_dir).map_err(io(addons_dir))?;
387+
// `Interface`, not `AddOns`: neither the client nor `scan` ever looks
388+
// at a folder there that is not an addon.
389+
let interface = addons_dir.parent().unwrap_or(addons_dir);
390+
sweep_stale_staging(interface);
391+
// Dropped at the end either way, taking the replaced copies with it.
392+
let staging = tempfile::Builder::new()
393+
.prefix(STAGING_PREFIX)
394+
.tempdir_in(interface)
395+
.map_err(io(interface))?;
396+
let fresh = staging.path().join("new");
397+
let old = staging.path().join("old");
398+
370399
let mut files = 0usize;
371400
let mut bytes = 0u64;
372401
for (index, relative, _) in &planned {
373402
let mut entry = archive.by_index(*index).map_err(InstallError::Archive)?;
374-
let dest = addons_dir.join(relative);
403+
let dest = fresh.join(relative);
375404
if let Some(parent) = dest.parent() {
376405
std::fs::create_dir_all(parent).map_err(|source| InstallError::Io {
377406
path: parent.to_path_buf(),
@@ -393,9 +422,84 @@ pub fn extract(package: &[u8], addons_dir: &Path) -> Result<Extracted, InstallEr
393422
bytes += contents.len() as u64;
394423
}
395424

425+
let retired: Vec<&String> = retire.iter().filter(|f| !folders.contains(f)).collect();
426+
swap(addons_dir, &fresh, &old, &folders, &retired)?;
396427
Ok(Extracted { folders, files, bytes })
397428
}
398429

430+
/// Move the installed folders out to `old` and the staged ones in, putting
431+
/// everything back if any rename fails.
432+
fn swap(
433+
addons_dir: &Path,
434+
fresh: &Path,
435+
old: &Path,
436+
folders: &[String],
437+
retired: &[&String],
438+
) -> Result<(), InstallError> {
439+
std::fs::create_dir_all(old).map_err(|source| InstallError::Io { path: old.to_path_buf(), source })?;
440+
let mut moved_out: Vec<&str> = Vec::new();
441+
let mut moved_in: Vec<&str> = Vec::new();
442+
443+
let result = swap_steps(addons_dir, fresh, old, folders, retired, &mut moved_out, &mut moved_in);
444+
if result.is_err() {
445+
for name in moved_in.iter().rev() {
446+
let _ = std::fs::rename(addons_dir.join(name), fresh.join(name));
447+
}
448+
for name in moved_out.iter().rev() {
449+
let _ = std::fs::rename(old.join(name), addons_dir.join(name));
450+
}
451+
}
452+
result
453+
}
454+
455+
fn swap_steps<'a>(
456+
addons_dir: &Path,
457+
fresh: &Path,
458+
old: &Path,
459+
folders: &'a [String],
460+
retired: &[&'a String],
461+
moved_out: &mut Vec<&'a str>,
462+
moved_in: &mut Vec<&'a str>,
463+
) -> Result<(), InstallError> {
464+
for name in folders.iter().chain(retired.iter().copied()) {
465+
let target = addons_dir.join(name);
466+
match std::fs::rename(&target, old.join(name)) {
467+
Ok(()) => moved_out.push(name),
468+
// Not installed yet, or already removed by hand.
469+
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
470+
Err(source) => return Err(InstallError::Io { path: target, source }),
471+
}
472+
}
473+
for name in folders {
474+
let target = addons_dir.join(name);
475+
std::fs::rename(fresh.join(name), &target)
476+
.map_err(|source| InstallError::Io { path: target, source })?;
477+
moved_in.push(name);
478+
}
479+
Ok(())
480+
}
481+
482+
/// Clear staging folders a crashed run left behind. Best effort: a leftover
483+
/// costs disk space, never correctness, so failing here is not a failed
484+
/// install.
485+
fn sweep_stale_staging(interface: &Path) {
486+
let Ok(entries) = std::fs::read_dir(interface) else { return };
487+
for entry in entries.filter_map(Result::ok) {
488+
if !entry.file_name().to_string_lossy().starts_with(STAGING_PREFIX) {
489+
continue;
490+
}
491+
let stale = entry
492+
.metadata()
493+
.and_then(|m| m.modified())
494+
.ok()
495+
.and_then(|t| t.elapsed().ok())
496+
.is_some_and(|age| age > STALE_STAGING);
497+
if stale {
498+
let _ = std::fs::remove_dir_all(entry.path());
499+
}
500+
}
501+
}
502+
399503
/// Delete the folders an install wrote, and forget it.
400504
///
401505
/// Only folders the ledger records are removed, and each is checked to be
@@ -538,7 +642,7 @@ mod tests {
538642
]);
539643
let dir = tmpdir("multi");
540644

541-
let extracted = extract(&package, &dir).unwrap();
645+
let extracted = extract(&package, &dir, &[]).unwrap();
542646
assert_eq!(extracted.folders, vec!["BigWigs", "BigWigs_Options"]);
543647
assert_eq!(extracted.files, 3);
544648
assert!(dir.join("BigWigs/BigWigs.toc").is_file());
@@ -553,7 +657,7 @@ mod tests {
553657
("../../../evil.txt", b"pwned"),
554658
]);
555659

556-
let err = extract(&package, &dir).unwrap_err();
660+
let err = extract(&package, &dir, &[]).unwrap_err();
557661
assert!(
558662
matches!(err, InstallError::UnsafePath(_)),
559663
"expected a path refusal, got {err:?}"
@@ -566,27 +670,84 @@ mod tests {
566670
#[test]
567671
fn an_update_replaces_a_folder_rather_than_merging_into_it() {
568672
let dir = tmpdir("replace");
569-
extract(&zip_with(&[("Addon/old.lua", b"old"), ("Addon/keep.toc", b"v1")]), &dir).unwrap();
673+
extract(&zip_with(&[("Addon/old.lua", b"old"), ("Addon/keep.toc", b"v1")]), &dir, &[]).unwrap();
570674
assert!(dir.join("Addon/old.lua").is_file());
571675

572676
// The new version dropped old.lua. A stale file left behind is worse
573677
// than a missing one: it still loads.
574-
extract(&zip_with(&[("Addon/keep.toc", b"v2")]), &dir).unwrap();
678+
extract(&zip_with(&[("Addon/keep.toc", b"v2")]), &dir, &[]).unwrap();
575679
assert!(!dir.join("Addon/old.lua").exists());
576680
assert_eq!(std::fs::read(dir.join("Addon/keep.toc")).unwrap(), b"v2");
577681
}
578682

683+
/// An `AddOns` inside its own `Interface`, so the staging folder an
684+
/// extract makes is this test's alone to look for.
685+
fn addons_dir(name: &str) -> PathBuf {
686+
let dir = tmpdir(name).join("AddOns");
687+
std::fs::create_dir_all(&dir).unwrap();
688+
dir
689+
}
690+
691+
fn staging_left(addons: &Path) -> bool {
692+
std::fs::read_dir(addons.parent().unwrap())
693+
.unwrap()
694+
.filter_map(Result::ok)
695+
.any(|e| e.file_name().to_string_lossy().starts_with(STAGING_PREFIX))
696+
}
697+
698+
#[test]
699+
fn a_failed_write_leaves_the_installed_addon_untouched() {
700+
let dir = addons_dir("failed-write");
701+
extract(&zip_with(&[("Addon/Addon.toc", b"v1"), ("Addon/core.lua", b"v1")]), &dir, &[]).unwrap();
702+
703+
// `x` as a file and then as a folder: the second write fails halfway
704+
// through the package, the way a full disk would.
705+
let broken = zip_with(&[("Addon/Addon.toc", b"v2"), ("Addon/x", b"file"), ("Addon/x/y", b"dir")]);
706+
assert!(matches!(extract(&broken, &dir, &[]), Err(InstallError::Io { .. })));
707+
708+
assert_eq!(std::fs::read(dir.join("Addon/Addon.toc")).unwrap(), b"v1");
709+
assert_eq!(std::fs::read(dir.join("Addon/core.lua")).unwrap(), b"v1");
710+
assert!(!staging_left(&dir));
711+
}
712+
713+
#[test]
714+
fn a_retired_folder_goes_in_the_same_swap() {
715+
let dir = addons_dir("retire");
716+
extract(&zip_with(&[("Main/Main.toc", b"v1"), ("Main_Old/Main_Old.toc", b"v1")]), &dir, &[])
717+
.unwrap();
718+
std::fs::create_dir_all(dir.join("Someone_Else")).unwrap();
719+
720+
let retire = ["Main".to_owned(), "Main_Old".to_owned()];
721+
extract(&zip_with(&[("Main/Main.toc", b"v2")]), &dir, &retire).unwrap();
722+
723+
assert_eq!(std::fs::read(dir.join("Main/Main.toc")).unwrap(), b"v2");
724+
assert!(!dir.join("Main_Old").exists());
725+
assert!(dir.join("Someone_Else").exists());
726+
assert!(!staging_left(&dir));
727+
}
728+
729+
#[test]
730+
fn a_retired_folder_must_be_one_plain_name() {
731+
let dir = addons_dir("retire-unsafe");
732+
let package = zip_with(&[("Main/Main.toc", b"v1")]);
733+
for bad in ["../Elsewhere", "Main/Sub"] {
734+
let err = extract(&package, &dir, &[bad.to_owned()]).unwrap_err();
735+
assert!(matches!(err, InstallError::UnsafePath(_)), "{bad}: {err:?}");
736+
}
737+
assert!(!dir.join("Main").exists());
738+
}
739+
579740
#[test]
580741
fn a_package_with_only_top_level_files_has_no_addon_to_install() {
581742
let dir = tmpdir("flat");
582743
let package = zip_with(&[("readme.txt", b"hello")]);
583-
assert!(matches!(extract(&package, &dir), Err(InstallError::NoAddonFolders)));
744+
assert!(matches!(extract(&package, &dir, &[]), Err(InstallError::NoAddonFolders)));
584745
}
585746

586747
#[test]
587748
fn removal_deletes_only_what_the_ledger_recorded() {
588749
let dir = tmpdir("remove");
589-
extract(&zip_with(&[("Mine/Mine.toc", b"x"), ("Mine_Extra/Mine_Extra.toc", b"y")]), &dir)
750+
extract(&zip_with(&[("Mine/Mine.toc", b"x"), ("Mine_Extra/Mine_Extra.toc", b"y")]), &dir, &[])
590751
.unwrap();
591752
std::fs::create_dir_all(dir.join("HandInstalled")).unwrap();
592753
std::fs::write(dir.join("HandInstalled/thing.toc"), b"not ours").unwrap();

‎crates/core/src/addons/manage.rs‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,17 @@ async fn fetch_and_install(
245245

246246
verify_md5(&package, entry.md5.as_deref(), name)?;
247247

248-
// A repair replaces the folders rather than writing over them, so a file
249-
// an older build shipped and this one does not goes too. Deliberately
250-
// after the download: until the package is in hand there is nothing to
251-
// put back, and a failed fetch must not leave the addon gone.
252-
if options.reinstall {
253-
if let Some(record) = ledger.find(provider, &entry.id) {
254-
install::delete_folders(&options.addons_dir, &record.folders)?;
255-
}
256-
}
248+
// A repair replaces every folder the record names, so one an older build
249+
// shipped and this one does not goes too. It happens inside the extract's
250+
// swap, so a failure leaves the addon as it was rather than half gone.
251+
let retire = match options.reinstall {
252+
true => ledger.find(provider, &entry.id).map(|r| r.folders.clone()).unwrap_or_default(),
253+
false => Vec::new(),
254+
};
257255

258256
// Trust what the package contains over what the catalog said it contains:
259257
// the folders are what removal will delete, so they have to be real.
260-
let extracted = install::extract(&package, &options.addons_dir)?;
258+
let extracted = install::extract(&package, &options.addons_dir, &retire)?;
261259
// An install replaces the record wholesale, so everything the user chose
262260
// about this addon is read off the old one first and carried across.
263261
let chosen = ledger.find(provider, &entry.id).cloned().unwrap_or_default();

0 commit comments

Comments
 (0)