Make file operations more permission-aware

This commit is contained in:
Juno Takano 2024-09-03 12:09:40 -03:00
parent de634f3749
commit e94e68540f
2 changed files with 23 additions and 11 deletions

View file

@ -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"
if [ -r "$path" ]; then
cp -f "$path" "$ephemeral_path"
else
$AUTHORIZE_COMMAND cp -f "$path" "$ephemeral_path"
fi
else
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")"
if [ -r "$path" ]; then
cp "$path" "$canonical_path"
else
$AUTHORIZE_COMMAND cp "$path" "$canonical_path"
fi
fi
done
}

View file

@ -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"