diff options
author | Bryan Drewery <bryan@shatow.net> | 2021-11-20 02:36:11 +0300 |
---|---|---|
committer | Bryan Drewery <bryan@shatow.net> | 2022-11-10 02:06:20 +0300 |
commit | 03b9a770d3583bfaf9a596ce340f3d1bc5506e4b (patch) | |
tree | 1de4a216915ddd5978a8154b6da2d78e6e27a144 | |
parent | 874616addcc7eafe407fb57fe4354721e5901d4b (diff) |
mapfile_cat: Rework
- Rename this to mapfile_cat_file(file)
- Add mapfile_cat(handle)
- Add more tests
- Remove useless flags
- sh compat: Don't fork to cat(1)
-rw-r--r-- | src/poudriere-sh/mapfile.c | 4 | ||||
-rw-r--r-- | src/share/poudriere/include/display.sh | 2 | ||||
-rw-r--r-- | src/share/poudriere/include/shared_hash.sh | 2 | ||||
-rw-r--r-- | src/share/poudriere/include/util.sh | 47 | ||||
-rw-r--r-- | test/mapfile.sh | 65 |
5 files changed, 85 insertions, 35 deletions
diff --git a/src/poudriere-sh/mapfile.c b/src/poudriere-sh/mapfile.c index fe14009d..44053756 100644 --- a/src/poudriere-sh/mapfile.c +++ b/src/poudriere-sh/mapfile.c @@ -540,12 +540,12 @@ mapfile_writecmd(int argc, char **argv) /* Read from TTY */ char *value; /* - * XXX: Using shell mapfile_cat until some changes from + * XXX: Using shell mapfile_cat_file until some changes from * copool branch make it in to avoid massive conflicts */ /* Avoid adding our own newline by keeping any read. */ const char *cmd = - "__mapfile_write_cat=\"$(mapfile_cat; echo .)\";" + "__mapfile_write_cat=\"$(mapfile_cat_file; echo .)\";" "__mapfile_write_cat=\"${__mapfile_write_cat%.}\""; nflag = 1; diff --git a/src/share/poudriere/include/display.sh b/src/share/poudriere/include/display.sh index 819c465c..b4d480a7 100644 --- a/src/share/poudriere/include/display.sh +++ b/src/share/poudriere/include/display.sh @@ -251,7 +251,7 @@ display_output() { if [ "${quiet}" -eq 0 ]; then echo "${_DISPLAY_HEADER}" fi - mapfile_cat "${_DISPLAY_TMP}.filtered" + mapfile_cat_file "${_DISPLAY_TMP}.filtered" if [ -n "${_DISPLAY_FOOTER}" ]; then echo "${_DISPLAY_FOOTER}" fi diff --git a/src/share/poudriere/include/shared_hash.sh b/src/share/poudriere/include/shared_hash.sh index 6ca2876e..f4e33f84 100644 --- a/src/share/poudriere/include/shared_hash.sh +++ b/src/share/poudriere/include/shared_hash.sh @@ -128,7 +128,7 @@ shash_read() { local _shash_varkey_file _shash_varkey_file "${var}" "${key}" - mapfile_cat "${_shash_varkey_file}" + mapfile_cat_file "${_shash_varkey_file}" } shash_read_mapfile() { diff --git a/src/share/poudriere/include/util.sh b/src/share/poudriere/include/util.sh index a4fc4d34..9c278a42 100644 --- a/src/share/poudriere/include/util.sh +++ b/src/share/poudriere/include/util.sh @@ -821,48 +821,37 @@ mapfile_read_loop_redir() { read -r "$@" } -# Basically an optimized loop of mapfile_read_loop_redir, or read_file +# Pipe to STDOUT from handle. mapfile_cat() { - local -; set +x - [ $# -ge 0 ] || eargs mapfile_cat [-u] file... - local _handle ret _line _file ret flag - local nflag lines - local IFS + [ $# -ge 1 ] || eargs mapfile_cat handle... + local IFS handle line - if ! mapfile_builtin; then - ret=0 - cat "$@" || ret="$?" - return "${ret}" - fi - nflag= - while getopts "n" flag; do - case "${flag}" in - n) - nflag=1 - ;; - esac - shift $((OPTIND-1)) + for handle in "$@"; do + while IFS= mapfile_read "${handle}" line; do + echo "${line}" + done done +} + +# Pipe to STDOUT from a file. +# Basically an optimized loop of mapfile_read_loop_redir, or read_file +mapfile_cat_file() { + local -; set +x + [ $# -ge 0 ] || eargs mapfile_cat_file file... + local _handle ret _file + if [ $# -eq 0 ]; then # Read from stdin set -- "-" fi ret=0 - lines=0 for _file in "$@"; do case "${_file}" in -) _file="/dev/fd/0" ;; esac if mapfile _handle "${_file}" "re"; then - while IFS= mapfile_read "${_handle}" _line; do - lines=$((lines + 1)) - case "${nflag}" in - "") ;; - *) printf "%6d\t" "${lines}" ;; - esac - echo "${_line}" - done - mapfile_close "${_handle}" + mapfile_cat "${_handle}" || ret="$?" + mapfile_close "${_handle}" || ret="$?" else ret="$?" fi diff --git a/test/mapfile.sh b/test/mapfile.sh index 0d96c6ce..084acb63 100644 --- a/test/mapfile.sh +++ b/test/mapfile.sh @@ -5,6 +5,8 @@ set -e . ${SCRIPTPREFIX}/include/util.sh set +e +set_pipefail + JAILED=$(sysctl -n security.jail.jailed 2>/dev/null || echo 0) LINES=20 @@ -505,6 +507,7 @@ fi :>"${TMP}" assert_ret 0 mapfile handle "${TMP2}" "we" cat "${TMP}" | mapfile_write "${handle}" + assert 0 "$?" "pipe exit status" assert_ret 0 mapfile_close "${handle}" [ ! -s "${TMP2}" ] assert 0 "$?" "'cat <empty file> | mapfile_write' should not write anything" @@ -517,10 +520,67 @@ fi :>"${TMP}" assert_ret 0 mapfile handle "${TMP2}" "we" - mapfile_cat "${TMP}" | mapfile_write "${handle}" + assert_ret 0 mapfile_cat_file "${TMP}" | + assert_ret 0 mapfile_write "${handle}" + assert 0 "$?" "pipe exit status" assert_ret 0 mapfile_close "${handle}" [ ! -s "${TMP2}" ] - assert 0 "$?" "'mapfile_cat <empty file> | mapfile_write' should not write anything" + assert 0 "$?" "'mapfile_cat_file <empty file> | mapfile_write' should not write anything" + rm -f "${TMP}" "${TMP2}" +} + +{ + TMP=$(mktemp -t mapfile) + TMP2=$(mktemp -t mapfile) + + ps uaxwd > "${TMP}" + + :>"${TMP2}" + assert_ret 0 mapfile handle "${TMP2}" "we" + assert_ret 0 mapfile_cat_file "${TMP}" | + assert_ret 0 mapfile_write "${handle}" + assert 0 "$?" "pipe exit status" + assert_ret 0 mapfile_close "${handle}" + assert_ret 0 diff -u "${TMP}" "${TMP2}" + + rm -f "${TMP2}" + :>"${TMP2}" + assert_ret 0 mapfile handle "${TMP2}" "we" + mapfile_write "${handle}" <<-EOF + $(cat "${TMP}") + EOF + assert_ret 0 mapfile_close "${handle}" + assert_ret 0 diff -u "${TMP}" "${TMP2}" + + rm -f "${TMP}" "${TMP2}" +} + +{ + TMP=$(mktemp -t mapfile) + TMP2=$(mktemp -t mapfile) + + ps uaxwd > "${TMP}" + + :>"${TMP2}" + assert_ret 0 mapfile read_handle "${TMP}" "re" + assert_ret 0 mapfile_cat "${read_handle}" | ( + assert_ret 0 mapfile handle "${TMP2}" "we" + assert_ret 0 mapfile_write "${handle}" + assert_ret 0 mapfile_close "${handle}" + ) + assert 0 "$?" "pipe exit status" + assert_ret 0 mapfile_close "${read_handle}" + assert_ret 0 diff -u "${TMP}" "${TMP2}" + + rm -f "${TMP2}" + :>"${TMP2}" + assert_ret 0 mapfile handle "${TMP2}" "we" + mapfile_write "${handle}" <<-EOF + $(cat "${TMP}") + EOF + assert_ret 0 mapfile_close "${handle}" + assert_ret 0 diff -u "${TMP}" "${TMP2}" + rm -f "${TMP}" "${TMP2}" } @@ -533,6 +593,7 @@ fi :>"${TMP2}" assert_ret 0 mapfile handle "${TMP2}" "we" cat "${TMP}" | mapfile_write "${handle}" + assert 0 "$?" "pipe exit status" assert_ret 0 mapfile_close "${handle}" assert_ret 0 diff -u "${TMP}" "${TMP2}" |