fix(preflight): asset mode retries a move that fails, names the cause and rolls back (#197)
A part of the stack that will not move from the unpack folder into place is retried for about 30 seconds; each retry removes what reached the target and unpacks the entry afresh from the archive, so the entry ends up there once and whole. If it still fails, both scripts take back everything they moved and exit 1 with the system's message and "nothing was installed". New test hook CHEMENU_PREFLIGHT_MOVE_FAIL=<entry>:<n>[:partial]. Files changed: - CHANGES.md - INSTALL.md - VERSION - instructions/preflight.md - tools/CONTRACT.md - tools/chemenu/tests/conftest.py - tools/chemenu/tests/test_preflight.py - tools/chemenu/tests/test_preflight_pwsh.py - tools/preflight.ps1 - tools/preflight.sh Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SnAJ7Z3CpVD3PRbN73QtU2
This commit is contained in:
1 parent
5915b07383
commit
5c9be38aa8
10 files changed
+379
-47
No files matched your search
+115
-22
@@ -27,7 +27,8 @@
|
||||
# it and what happens next; show it verbatim and wait (AGENTS.md, Tool
|
||||
# error contract). Never install anything on the user's behalf.
|
||||
# Exit 1: this script was called wrongly, a download or unpack failed (nothing is
|
||||
# unpacked then), or the tree next to it is incomplete.
|
||||
# unpacked then - or, should even that fail, the message names what is
|
||||
# left), or the tree next to it is incomplete.
|
||||
#
|
||||
# The counterpart of tools/preflight.sh, and the two answer the same questions from the
|
||||
# same list, tools/prerequisites.txt. What only this one checks: the PowerShell execution
|
||||
@@ -35,11 +36,15 @@
|
||||
# tools/wikitool.ps1 from starting, and that exist only here. Windows PowerShell 5.1 is
|
||||
# not supported; `#Requires` turns it away.
|
||||
#
|
||||
# Four variables exist for the test suite and are read nowhere else:
|
||||
# Five variables exist for the test suite and are read nowhere else:
|
||||
# CHEMENU_PREFLIGHT_PLATFORM (windows|macos|linux), CHEMENU_PREFLIGHT_LONGPATHS (0|1),
|
||||
# CHEMENU_PREFLIGHT_POLICY (`Scope=Policy,...`, as `Get-ExecutionPolicy -List` names them)
|
||||
# and CHEMENU_PREFLIGHT_MARKED (comma-separated script names below tools/) stand in for
|
||||
# the operating system, the registry, the policy and the file streams.
|
||||
# CHEMENU_PREFLIGHT_MOVE_FAIL (<entry>:<n>[:partial]) stands in for a file some other
|
||||
# program holds open: moving the unpacked <entry> up fails the first <n> times (`always`:
|
||||
# every time), with :partial after moving a part of it, and the pauses between attempts
|
||||
# are skipped.
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = 'Stop'
|
||||
@@ -237,23 +242,100 @@ function Resolve-AssetPath {
|
||||
return [IO.Path]::GetFullPath($Path).TrimEnd('\', '/')
|
||||
}
|
||||
|
||||
# Whether the install folder holds anything besides this script and a .git - the two
|
||||
# things an empty folder may already carry: the script was downloaded into it, and the
|
||||
# user may have cloned the instance's own, still empty repository there.
|
||||
function Test-TargetOccupied {
|
||||
param([string]$Target)
|
||||
$selfName = Split-Path -Leaf $PSCommandPath
|
||||
$isOwnFolder = [IO.Path]::GetFullPath($Target).TrimEnd('\', '/') -eq [IO.Path]::GetFullPath($Dir).TrimEnd('\', '/')
|
||||
foreach ($entry in @(Get-ChildItem -LiteralPath $Target -Force)) {
|
||||
if ($entry.Name -eq '.git') {
|
||||
continue
|
||||
}
|
||||
if ($isOwnFolder -and $entry.Name -eq $selfName) {
|
||||
continue
|
||||
}
|
||||
# This script and a .git are the two things an empty install folder may already carry:
|
||||
# the script was downloaded into it, and the user may have cloned the instance's own,
|
||||
# still empty repository there. Neither counts as occupying the folder, and neither is
|
||||
# ever removed from it.
|
||||
function Test-ProtectedName {
|
||||
param([string]$Target, [string]$Name)
|
||||
if ($Name -eq '.git') {
|
||||
return $true
|
||||
}
|
||||
return $false
|
||||
$isOwnFolder = [IO.Path]::GetFullPath($Target).TrimEnd('\', '/') -eq [IO.Path]::GetFullPath($Dir).TrimEnd('\', '/')
|
||||
return $isOwnFolder -and $Name -eq (Split-Path -Leaf $PSCommandPath)
|
||||
}
|
||||
|
||||
# The names in the install folder besides those two.
|
||||
function Get-TargetOccupant {
|
||||
param([string]$Target)
|
||||
return @(Get-ChildItem -LiteralPath $Target -Force |
|
||||
Where-Object { -not (Test-ProtectedName $Target $_.Name) } |
|
||||
ForEach-Object { $_.Name })
|
||||
}
|
||||
|
||||
# How long an entry that will not move up is retried: the pauses between attempts, in
|
||||
# seconds - 30 in all. On Windows a folder cannot be renamed while any file in it is
|
||||
# held open, and a virus scanner or indexer looking at a freshly unpacked tree holds one
|
||||
# for a moment; waiting is what gets past it.
|
||||
$MovePauses = @(1, 2, 3, 4, 5, 6, 9)
|
||||
|
||||
# One attempt at moving the unpacked entry <Name> up into the target; the system's
|
||||
# message when it fails, $null when it worked. A failed attempt can leave part of the
|
||||
# entry on either side - a move that falls back to copying and deleting stops wherever
|
||||
# the lock is - so every attempt after the first starts over: what reached the target is
|
||||
# removed, and the entry is unpacked afresh from the archive. Only then is the entry in
|
||||
# the target, once and whole, after the attempt that works.
|
||||
function Move-AssetEntry {
|
||||
param([string]$Name, [int]$Attempt, [string]$Target, [string]$Unpack, [string]$Top, [string]$Tar, [string]$ArchivePath)
|
||||
$source = Join-Path (Join-Path $Unpack $Top) $Name
|
||||
$landed = Join-Path $Target $Name
|
||||
try {
|
||||
if ($Attempt -gt 1) {
|
||||
if ((Test-Path -LiteralPath $landed) -and -not (Test-ProtectedName $Target $Name)) {
|
||||
Remove-Item -LiteralPath $landed -Recurse -Force
|
||||
}
|
||||
$again = Invoke-NativeVerbose $Tar @('-xzf', $ArchivePath, '-C', $Unpack, "$Top/$Name")
|
||||
if ($again.Code -ne 0) {
|
||||
return "unpacking it again failed: $($again.Output)"
|
||||
}
|
||||
}
|
||||
$hook = "$env:CHEMENU_PREFLIGHT_MOVE_FAIL".Split(':')
|
||||
if ($hook[0] -eq $Name -and $hook.Count -gt 1 -and ($hook[1] -eq 'always' -or $Attempt -le [int]$hook[1])) {
|
||||
if ($hook.Count -gt 2 -and $hook[2] -eq 'partial') {
|
||||
if (Test-Path -LiteralPath $source -PathType Container) {
|
||||
$null = New-Item -ItemType Directory -Path $landed -Force
|
||||
$part = Get-ChildItem -LiteralPath $source -Force | Select-Object -First 1
|
||||
if ($part) {
|
||||
Move-Item -LiteralPath $part.FullName -Destination $landed
|
||||
}
|
||||
} else {
|
||||
$null = New-Item -ItemType File -Path $landed -Force
|
||||
}
|
||||
}
|
||||
return "Access to the path '$source' is denied. (CHEMENU_PREFLIGHT_MOVE_FAIL, attempt $Attempt)"
|
||||
}
|
||||
Move-Item -LiteralPath $source -Destination $Target
|
||||
return $null
|
||||
} catch {
|
||||
return $_.Exception.Message
|
||||
}
|
||||
}
|
||||
|
||||
# An entry would not move even after waiting: take back everything this run moved into
|
||||
# the target, so that it holds what it held before and running this again is all it
|
||||
# takes. Only names that came out of the archive are removed, never a protected one.
|
||||
function Undo-AssetMove {
|
||||
param([string]$Target, [string]$Unpack, [string[]]$Moved, [string]$Name, [string]$Message)
|
||||
foreach ($entry in $Moved + $Name) {
|
||||
$path = Join-Path $Target $entry
|
||||
if (-not (Test-ProtectedName $Target $entry) -and (Test-Path -LiteralPath $path)) {
|
||||
try {
|
||||
Remove-Item -LiteralPath $path -Recurse -Force
|
||||
} catch {
|
||||
$null = $_
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
Remove-Item -LiteralPath $Unpack -Recurse -Force
|
||||
} catch {
|
||||
$null = $_
|
||||
}
|
||||
$left = @(Get-TargetOccupant $Target)
|
||||
if ($left.Count -eq 0) {
|
||||
Exit-Asset "could not move $Name into $Target ($Message) - nothing was installed, the folder is as it was before. Run this again."
|
||||
}
|
||||
Exit-Asset "could not move $Name into $Target ($Message), and could not take back what was already there: $($left -join ', ') - empty the folder (keep .git), then run this again."
|
||||
}
|
||||
|
||||
function Invoke-AssetMode {
|
||||
@@ -262,7 +344,7 @@ function Invoke-AssetMode {
|
||||
if ($null -ne $existing -and -not $existing.PSIsContainer) {
|
||||
Exit-Asset "$target exists and is not a folder - nothing was unpacked."
|
||||
}
|
||||
if ($null -ne $existing -and (Test-TargetOccupied $target)) {
|
||||
if ($null -ne $existing -and @(Get-TargetOccupant $target).Count -gt 0) {
|
||||
Exit-Asset "$target is not empty - nothing was unpacked. The wiki installs into an empty folder (an empty git clone is fine): put this script into one and run it there, or pass --into <path>."
|
||||
}
|
||||
|
||||
@@ -367,12 +449,23 @@ function Invoke-AssetMode {
|
||||
if (-not (Test-Path -LiteralPath $unpacked -PathType Container)) {
|
||||
Exit-Asset 'unpacking failed - nothing was installed.'
|
||||
}
|
||||
$moved = @()
|
||||
foreach ($entry in @(Get-ChildItem -LiteralPath $unpacked -Force)) {
|
||||
try {
|
||||
Move-Item -LiteralPath $entry.FullName -Destination $target
|
||||
} catch {
|
||||
Exit-Asset "could not move $($entry.Name) into $target - the folder holds part of the stack now; empty it (keep .git) and run this again."
|
||||
$attempt = 1
|
||||
while ($true) {
|
||||
$failure = Move-AssetEntry $entry.Name $attempt $target $unpack $top $tar.Source $archivePath
|
||||
if ($null -eq $failure) {
|
||||
break
|
||||
}
|
||||
if ($attempt -gt $MovePauses.Count) {
|
||||
Undo-AssetMove $target $unpack $moved $entry.Name $failure
|
||||
}
|
||||
if (-not $env:CHEMENU_PREFLIGHT_MOVE_FAIL) {
|
||||
Start-Sleep -Seconds $MovePauses[$attempt - 1]
|
||||
}
|
||||
$attempt++
|
||||
}
|
||||
$moved += $entry.Name
|
||||
}
|
||||
} finally {
|
||||
foreach ($leftover in @($work, $unpack)) {
|
||||
|
||||
Reference in new issue
Block a user