Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating of readonly files #320

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions yadm
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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
}

Expand Down