From e94e68540f7395359b0ede4f21f5b6b4769828ab Mon Sep 17 00:00:00 2001 From: jutty Date: Tue, 3 Sep 2024 12:09:40 -0300 Subject: [PATCH] Make file operations more permission-aware --- src/file/backup.sh | 20 ++++++++++++++++---- src/file/file_merge.sh | 14 +++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/file/backup.sh b/src/file/backup.sh index 7a05c2c..7f992a7 100644 --- a/src/file/backup.sh +++ b/src/file/backup.sh @@ -1,4 +1,4 @@ -# takes a list of space-separated absolute paths +# takes a list of newline-separated absolute paths # backs each path up, creating canonical or ephemeral copies as needed backup_paths() { local paths="$1" @@ -15,14 +15,26 @@ backup_paths() { mkdir -p "$(dirname "$ephemeral_path")" if [ -f "$ephemeral_path" ]; then log debug "[backup] Overwriting ephemeral copy for $path" - cp -f "$path" "$ephemeral_path" + if [ -r "$path" ]; then + cp -f "$path" "$ephemeral_path" + else + $AUTHORIZE_COMMAND cp -f "$path" "$ephemeral_path" + fi else - cp "$path" "$ephemeral_path" + if [ -r "$path" ]; then + cp "$path" "$ephemeral_path" + else + $AUTHORIZE_COMMAND cp "$path" "$ephemeral_path" + fi fi else log debug "[backup] Creating canonical copy for $path" mkdir -p "$(dirname "$canonical_path")" - cp "$path" "$canonical_path" + if [ -r "$path" ]; then + cp "$path" "$canonical_path" + else + $AUTHORIZE_COMMAND cp "$path" "$canonical_path" + fi fi done } diff --git a/src/file/file_merge.sh b/src/file/file_merge.sh index c938431..ffd61f4 100644 --- a/src/file/file_merge.sh +++ b/src/file/file_merge.sh @@ -28,7 +28,7 @@ file_scan_tree() { file_merge_tree() { local base_files="$1" - local strategy= + local overwrite_choice= for file in $base_files; do log debug "[merge_tree] Processing $file" @@ -47,12 +47,12 @@ file_merge_tree() { local prompt_verb="In configuration only" local prompt_options="Copy to system" fi - strategy="$(ask "$prompt_verb: $(tildify "$absolute_path")" "$prompt_options")" - log debug "[merge_tree] Chosen strategy: $strategy" + overwrite_choice="$(ask "$prompt_verb: $(tildify "$absolute_path")" "$prompt_options")" + log debug "[merge_tree] Overwrite choice: $overwrite_choice" - if [ "$strategy" -eq 0 ]; then + if [ "$overwrite_choice" -eq 0 ]; then return 0 - elif [ "$strategy" -eq 1 ]; then + elif [ "$overwrite_choice" -eq 1 ]; then backup_paths "$absolute_path" if [ -r "$config_path" ] && [ -w "$absolute_path" ]; then cp -vi "$config_path" "$absolute_path" @@ -60,7 +60,7 @@ file_merge_tree() { $AUTHORIZE_COMMAND cp -vi "$config_path" "$absolute_path" fi return 1 - elif [ "$strategy" -eq 2 ]; then + elif [ "$overwrite_choice" -eq 2 ]; then backup_paths "$config_path" if [ -r "$absolute_path" ] && [ -w "$config_path" ]; then cp -vi "$absolute_path" "$config_path" @@ -68,7 +68,7 @@ file_merge_tree() { $AUTHORIZE_COMMAND cp -vi "$absolute_path" "$config_path" fi return 1 - elif [ "$strategy" -eq 3 ]; then + elif [ "$overwrite_choice" -eq 3 ]; then echo "< $(tildify "$absolute_path") | $(echo "$config_path" | sed "s*$CONFIG_ROOT/**") >" if [ -r "$absolute_path" ] && [ -r "$config_path" ]; then diff "$absolute_path" "$config_path"