]> arthur.barton.de Git - ax-zsh.git/commitdiff
ax.zsh: Put new cache files in place only after they are complete
authorAlexander Barton <alex@barton.de>
Mon, 29 Mar 2021 17:48:29 +0000 (19:48 +0200)
committerAlexander Barton <alex@barton.de>
Mon, 29 Mar 2021 17:48:29 +0000 (19:48 +0200)
ax.zsh

diff --git a/ax.zsh b/ax.zsh
index 12fab140a3a589ac114524a214d0ba094326e115..c88c1920fd507fca123c0b51c1c8df54961be039 100644 (file)
--- a/ax.zsh
+++ b/ax.zsh
@@ -49,6 +49,8 @@ function axzsh_handle_stage {
                unfunction ax_plugin_init
        else
                # No cache file available.
+               local new_cache_file="$cache_file.NEW"
+
                # Setup list of plugins to load:
                local plugin_list
                typeset -U plugin_list
@@ -59,10 +61,12 @@ function axzsh_handle_stage {
                )
 
                # Create new cache file:
-               if [[ -n "$cache_file" ]]; then
+               if [[ -n "$cache_file" && -w "$new_cache_file" ]]; then
                        [[ -n "$AXZSH_DEBUG" ]] \
-                               && echo "   (Writing new cache file to \"$cache_file\" ...)"
-                       printf "# %s\n\n" "$(LC_ALL=C date)" >"$cache_file"
+                               && echo "   (Writing new cache file to \"$new_cache_file\" ...)"
+                       if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
+                               unset new_cache_file
+                       fi
                fi
 
                # Read in all the plugins for the current "type":
@@ -72,16 +76,21 @@ function axzsh_handle_stage {
                        if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
                                if [[ -r "$AXZSH_THEME" ]]; then
                                        source "$AXZSH_THEME"
-                                       if [[ -n "$cache_file" ]]; then
+                                       if [[ -n "$new_cache_file" ]]; then
                                                # Source the theme in the new cache file:
-                                               echo "# BEGIN Theme" >>"$cache_file"
-                                               echo 'source "$AXZSH_THEME"' >>"$cache_file"
-                                               echo "# END Theme" >>"$cache_file"
+                                               echo "# BEGIN Theme" >>"$new_cache_file"
+                                               echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
+                                               echo "# END Theme" >>"$new_cache_file"
                                        fi
                                fi
                        fi
-                       axzsh_load_plugin "$plugin" "$type" "$cache_file"
+                       axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
                done
+
+               if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
+                       # Move newly created cache file in place:
+                       mv "$new_cache_file" "$cache_file"
+               fi
        fi
 }