diff --git a/yadm b/yadm index 4cae9eb..ec0a617 100755 --- a/yadm +++ b/yadm @@ -436,10 +436,7 @@ EOF "$awk_pgm" \ "$input" > "$temp_file" || rm -f "$temp_file" - if [ -f "$temp_file" ] ; then - copy_perms "$input" "$temp_file" - mv -f "$temp_file" "$output" - fi + move_file "$input" "$output" "$temp_file" } function template_j2cli() { @@ -455,10 +452,7 @@ function template_j2cli() { YADM_SOURCE="$input" \ "$J2CLI_PROGRAM" "$input" -o "$temp_file" - if [ -f "$temp_file" ] ; then - copy_perms "$input" "$temp_file" - mv -f "$temp_file" "$output" - fi + move_file "$input" "$output" "$temp_file" } function template_envtpl() { @@ -474,10 +468,7 @@ function template_envtpl() { YADM_SOURCE="$input" \ "$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file" - if [ -f "$temp_file" ] ; then - copy_perms "$input" "$temp_file" - mv -f "$temp_file" "$output" - fi + move_file "$input" "$output" "$temp_file" } function template_esh() { @@ -493,9 +484,27 @@ function template_esh() { YADM_DISTRO="$local_distro" \ YADM_SOURCE="$input" - if [ -f "$temp_file" ] ; then - copy_perms "$input" "$temp_file" - mv -f "$temp_file" "$output" + move_file "$input" "$output" "$temp_file" +} + +function move_file() { + local input=$1 + local output=$2 + local temp_file=$3 + + if [ ! -f "$temp_file" ] ; then + return 0 + fi + + local read_only + copy_perms "$input" "$temp_file" + if [[ -e "$output" && ! -w "$output" ]]; then + read_only=1 + chmod u+w "$output" + fi + mv -f "$temp_file" "$output" + if [ -n "$read_only" ]; then + chmod u-w "$output" fi }