From 216aed2f87555453db815033e840835f9c7cc859 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Sun, 28 Feb 2021 10:55:37 -0600 Subject: [PATCH] Adjust special handling of existing read-only output files Instead of duplicating the permissions on the temp file, the permissions are duplicated on the output file directly. If the output file exists as read-only, it is made writeable first. There are some environments which don't allow the mv to work if the file itself is read-only. --- test/test_unit_template_default.py | 6 ++++++ test/test_unit_template_esh.py | 6 ++++++ test/test_unit_template_j2.py | 6 ++++++ yadm | 19 +++++++------------ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/test/test_unit_template_default.py b/test/test_unit_template_default.py index 639cb29..ea2f904 100644 --- a/test/test_unit_template_default.py +++ b/test/test_unit_template_default.py @@ -127,6 +127,12 @@ def test_template_default(runner, yadm, tmpdir): input_file.chmod(FILE_MODE) output_file = tmpdir.join('output') + # ensure overwrite works when file exists as read-only (there is some + # special processing when this is encountered because some environments do + # not properly overwrite read-only files) + output_file.write('existing') + output_file.chmod(0o400) + script = f""" YADM_TEST=1 source {yadm} set_awk diff --git a/test/test_unit_template_esh.py b/test/test_unit_template_esh.py index e975152..23766c4 100644 --- a/test/test_unit_template_esh.py +++ b/test/test_unit_template_esh.py @@ -86,6 +86,12 @@ def test_template_esh(runner, yadm, tmpdir): input_file.chmod(FILE_MODE) output_file = tmpdir.join('output') + # ensure overwrite works when file exists as read-only (there is some + # special processing when this is encountered because some environments do + # not properly overwrite read-only files) + output_file.write('existing') + output_file.chmod(0o400) + script = f""" YADM_TEST=1 source {yadm} local_class="{LOCAL_CLASS}" diff --git a/test/test_unit_template_j2.py b/test/test_unit_template_j2.py index f81f4c6..981112b 100644 --- a/test/test_unit_template_j2.py +++ b/test/test_unit_template_j2.py @@ -88,6 +88,12 @@ def test_template_j2(runner, yadm, tmpdir, processor): input_file.chmod(FILE_MODE) output_file = tmpdir.join('output') + # ensure overwrite works when file exists as read-only (there is some + # special processing when this is encountered because some environments do + # not properly overwrite read-only files) + output_file.write('existing') + output_file.chmod(0o400) + script = f""" YADM_TEST=1 source {yadm} local_class="{LOCAL_CLASS}" diff --git a/yadm b/yadm index 7ba9f2f..52aab3f 100755 --- a/yadm +++ b/yadm @@ -489,20 +489,15 @@ function move_file() { local output=$2 local temp_file=$3 - if [ ! -f "$temp_file" ] ; then - return 0 - fi + [ ! -f "$temp_file" ] && return + + # if the output files already exists as read-only, change it to be writable. + # there are some environments in which a read-only file will prevent the move + # from being successful. + [[ -e "$output" && ! -w "$output" ]] && chmod u+w "$output" - 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 + copy_perms "$input" "$output" } # ****** yadm Commands ******