diff options
author | Bryan Drewery <bryan@shatow.net> | 2022-11-10 01:21:36 +0300 |
---|---|---|
committer | Bryan Drewery <bryan@shatow.net> | 2022-11-10 02:06:20 +0300 |
commit | bac8a649e2717554700b7e04f8b226db41d883dc (patch) | |
tree | f8b611661b8b5388deb23a0ec8ec2a360d7d2248 | |
parent | b6a430f0b30e6d8ffc5151951b41430870a9bfba (diff) |
mapfile sh: mapfile open for read should fail on missing file
-rw-r--r-- | src/share/poudriere/include/util.sh | 28 | ||||
-rw-r--r-- | test/mapfile.sh | 7 |
2 files changed, 33 insertions, 2 deletions
diff --git a/src/share/poudriere/include/util.sh b/src/share/poudriere/include/util.sh index 5d08f1c4..8debed3c 100644 --- a/src/share/poudriere/include/util.sh +++ b/src/share/poudriere/include/util.sh @@ -678,8 +678,20 @@ mapfile() { [ $# -eq 2 -o $# -eq 3 ] || eargs mapfile handle_name file modes local handle_name="$1" local _file="$2" - local mypid _hkey + local _modes="$3" + local mypid _hkey ret + + case " ${_modes} " in + *r*w*|*w*r*) ;; + *w*|*a*) ;; + *r*) + if [ ! -r "${_file}" ]; then + return 1 + fi + ;; + esac + ret=0 mypid=$(getpid) case "${_file}" in -|/dev/stdin) _file="/dev/fd/0" ;; @@ -716,9 +728,21 @@ mapfile() { hash_set mapfile_fd "${_mapfile_handle}" "${_file#/dev/fd/}" ;; *) - exec 8<> "${_file}" ;; + case " ${_modes} " in + *r*w*|*w*r*) + exec 8<> "${_file}" || ret="$?" + ;; + *r*) + exec 8< "${_file}" || ret="$?" + ;; + *w*|*a*) + exec 8> "${_file}" || ret="$?" + ;; + esac + ;; esac hash_set mapfile_file "${_mapfile_handle}" "${_file}" + return "${ret}" } mapfile_read() { diff --git a/test/mapfile.sh b/test/mapfile.sh index 06986ef7..0c9b7326 100644 --- a/test/mapfile.sh +++ b/test/mapfile.sh @@ -47,6 +47,13 @@ writer() { fi } +{ + TMP=$(mktemp -u) + set -x + assert_ret_not 0 mapfile handle "${TMP}" "re" + rm -f "${TMP}" +} + if mapfile_builtin; then # Test pipes { |