From f10463b89c834f82740b9babf32f2cb46145491d Mon Sep 17 00:00:00 2001 From: David Young Date: Mon, 19 Oct 2026 14:01:00 -0600 Subject: [PATCH] More fixes for update --- install.sh | 10 +++++----- refresh | 6 ++++++ update | 18 +++++++++++++++--- updates.d/00-rename-migration.sh | 6 ++++-- utils.sh | 7 +++++++ 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/install.sh b/install.sh index 44d7381..b4761fb 100755 --- a/install.sh +++ b/install.sh @@ -48,16 +48,16 @@ else git clone https://gitea.federated.computer/david/coredesktop.git ~/.local/share/coredesktop fi -# Install the refresh and update management scripts to the user's bin -mkdir -p ~/.local/bin -sudo cp ~/.local/share/coredesktop/refresh ~/.local/bin/refresh -sudo cp ~/.local/share/coredesktop/update ~/.local/bin/update - cd ~/.local/share/coredesktop # Source utility functions (provides install_packages and other helpers) source utils.sh +# Install the refresh and update management scripts to the user's bin +mkdir -p ~/.local/bin +install_script ~/.local/share/coredesktop/refresh ~/.local/bin/refresh +install_script ~/.local/share/coredesktop/update ~/.local/bin/update + # Source the package list (defines SYSTEM_UTILS, DEV_TOOLS, DESKTOP, etc. arrays) if [ ! -f "packages.conf" ]; then echo "Error: packages.conf not found!" diff --git a/refresh b/refresh index 5fab2ca..4e52e7b 100755 --- a/refresh +++ b/refresh @@ -1,5 +1,9 @@ #!/bin/bash +# Wrapped in { ...; exit; } so bash reads the whole script up front; install.sh +# replaces this file while it runs. +{ + # Print the logo print_logo() { cat << "EOF" @@ -21,3 +25,5 @@ clear print_logo bash ~/.local/share/coredesktop/install.sh +exit +} diff --git a/update b/update index c2459cb..3b1324e 100755 --- a/update +++ b/update @@ -1,5 +1,16 @@ #!/bin/bash +# The whole script is wrapped in { ...; exit; } so bash parses all of it before +# running anything. That way, replacing this file mid-run (e.g. by git pull and +# the copy below) can't make the running copy read a mix of old and new text. +{ + +# Install a script atomically (copy to a temp file, then rename over the target) +install_script() { + local tmp + tmp="$(mktemp "$2.XXXXXX")" && cp "$1" "$tmp" && chmod 755 "$tmp" && mv -f "$tmp" "$2" +} + # Print the logo print_logo() { cat << "EOF" @@ -28,9 +39,8 @@ if [[ -z "$UPDATE_REEXECED" ]] && ! git -C "$COREDESKTOP" pull; then export UPDATE_PULL_FAILED=1 fi mkdir -p "$COREDESKTOP/updates.d" -# --remove-destination replaces copies left root-owned by older versions of this script -cp --remove-destination "$COREDESKTOP/update" ~/.local/bin/update -cp --remove-destination "$COREDESKTOP/refresh" ~/.local/bin/refresh +install_script "$COREDESKTOP/update" ~/.local/bin/update +install_script "$COREDESKTOP/refresh" ~/.local/bin/refresh if [[ -z "$UPDATE_REEXECED" ]]; then export UPDATE_REEXECED=1 @@ -63,3 +73,5 @@ if (( ${#FAILED[@]} > 0 )); then fi echo "Done." +exit 0 +} diff --git a/updates.d/00-rename-migration.sh b/updates.d/00-rename-migration.sh index 10e4d0f..ec289eb 100755 --- a/updates.d/00-rename-migration.sh +++ b/updates.d/00-rename-migration.sh @@ -2,14 +2,16 @@ COREDESKTOP="$HOME/.local/share/coredesktop" +source "$COREDESKTOP/utils.sh" + if [ -f ~/.local/bin/coreupdate ]; then echo "Migrating coreupdate -> update..." - cp --remove-destination "$COREDESKTOP/update" ~/.local/bin/update + install_script "$COREDESKTOP/update" ~/.local/bin/update sudo rm -f ~/.local/bin/coreupdate fi if [ -f ~/.local/bin/corerefresh ]; then echo "Migrating corerefresh -> refresh..." - cp --remove-destination "$COREDESKTOP/refresh" ~/.local/bin/refresh + install_script "$COREDESKTOP/refresh" ~/.local/bin/refresh sudo rm -f ~/.local/bin/corerefresh fi diff --git a/utils.sh b/utils.sh index ee0472e..58d4c7d 100755 --- a/utils.sh +++ b/utils.sh @@ -72,3 +72,10 @@ enable_services() { fi done } + +# Install a script atomically (copy to a temp file, then rename over the target) +# so a copy of it that is running right now keeps reading the old file +install_script() { + local tmp + tmp="$(mktemp "$2.XXXXXX")" && cp "$1" "$tmp" && chmod 755 "$tmp" && mv -f "$tmp" "$2" +}