diff options
author | Bryan Drewery <bryan@shatow.net> | 2021-08-23 22:33:38 +0300 |
---|---|---|
committer | Bryan Drewery <bryan@shatow.net> | 2021-08-27 22:46:48 +0300 |
commit | 04be581581ff4af3468db1bde8abaecdb4dba9f6 (patch) | |
tree | 853ff69a21ca947f523cd22e5fcc2e40236b4d75 /src | |
parent | 8ef1c97fdd8d83006110dbbcc6869ee1fb2e2e38 (diff) |
write_atomic* can use common code
Diffstat (limited to 'src')
-rw-r--r-- | src/share/poudriere/include/util.sh | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/src/share/poudriere/include/util.sh b/src/share/poudriere/include/util.sh index 5298d952..4c6b059b 100644 --- a/src/share/poudriere/include/util.sh +++ b/src/share/poudriere/include/util.sh @@ -1126,56 +1126,55 @@ calculate_duration() { setvar "${var_return}" "${_duration}" } -write_atomic_cmp() { +_write_atomic() { local -; set +x - [ $# -eq 1 ] || eargs write_atomic_cmp destfile "< content" - local dest="$1" + [ $# -eq 2 ] || eargs _write_atomic cmp destfile "< content" + local cmp="$1" + local dest="$2" local tmpfile_handle tmpfile ret TMPDIR="${dest%/*}" mapfile_mktemp tmpfile_handle tmpfile \ -ut ".tmp-${dest##*/}" || - err $? "write_atomic_cmp unable to create tmpfile in ${dest%/*}" + err $? "write_atomic unable to create tmpfile in ${dest%/*}" ret=0 mapfile_write "${tmpfile_handle}" || ret="$?" if [ "${ret}" -ne 0 ]; then - unlink "${tmpfile}" 2>/dev/null || : + unlink "${tmpfile}" || : return "${ret}" fi ret=0 mapfile_close "${tmpfile_handle}" || ret="$?" if [ "${ret}" -ne 0 ]; then - unlink "${tmpfile}" 2>/dev/null || : + unlink "${tmpfile}" || : return "${ret}" fi - if ! cmp -s "${dest}" "${tmpfile}"; then - rename "${tmpfile}" "${dest}" - else - unlink "${tmpfile}" + if [ "${cmp}" -eq 1 ] && cmp -s "${dest}" "${tmpfile}"; then + unlink "${tmpfile}" || : + return 0 + fi + ret=0 + rename "${tmpfile}" "${dest}" || ret="$?" + if [ "${ret}" -ne 0 ]; then + unlink "${tmpfile}" || : + return "${ret}" fi } + +write_atomic_cmp() { + local -; set +x + [ $# -eq 1 ] || eargs write_atomic_cmp destfile "< content" + local dest="$1" + + _write_atomic 1 "${dest}" || return +} + write_atomic() { local -; set +x [ $# -eq 1 ] || eargs write_atomic destfile "< content" local dest="$1" - local tmpfile_handle tmpfile ret - TMPDIR="${dest%/*}" mapfile_mktemp tmpfile_handle tmpfile \ - -ut ".tmp-${dest##*/}" || - err $? "write_atomic unable to create tmpfile in ${dest%/*}" - ret=0 - mapfile_write "${tmpfile_handle}" || ret="$?" - if [ "${ret}" -ne 0 ]; then - unlink "${tmpfile}" 2>/dev/null || : - return "${ret}" - fi - ret=0 - mapfile_close "${tmpfile_handle}" || ret="$?" - if [ "${ret}" -ne 0 ]; then - unlink "${tmpfile}" 2>/dev/null || : - return "${ret}" - fi - rename "${tmpfile}" "${dest}" + _write_atomic 0 "${dest}" || return } # Place environment requirements on entering a function |