Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/freebsd/poudriere.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Drewery <bryan@shatow.net>2021-11-17 06:37:08 +0300
committerBryan Drewery <bryan@shatow.net>2021-11-18 16:13:45 +0300
commitcefb5070136ccb7de0f9c65becaafe18763d3bed (patch)
tree88bd53de13bbec3626ad26b034c5719c78937755
parent80ec59d8e523fc116f80a8555a5d38bb60f09510 (diff)
mapfile_write (from stdin): Fix writing newline when none is read
-rw-r--r--src/poudriere-sh/mapfile.c6
-rw-r--r--src/share/poudriere/include/util.sh8
-rw-r--r--test/mapfile.sh26
3 files changed, 36 insertions, 4 deletions
diff --git a/src/poudriere-sh/mapfile.c b/src/poudriere-sh/mapfile.c
index 33cd358a..fe14009d 100644
--- a/src/poudriere-sh/mapfile.c
+++ b/src/poudriere-sh/mapfile.c
@@ -543,8 +543,12 @@ mapfile_writecmd(int argc, char **argv)
* XXX: Using shell mapfile_cat until some changes from
* copool branch make it in to avoid massive conflicts
*/
- const char *cmd = "__mapfile_write_cat=$(mapfile_cat)";
+ /* Avoid adding our own newline by keeping any read. */
+ const char *cmd =
+ "__mapfile_write_cat=\"$(mapfile_cat; echo .)\";"
+ "__mapfile_write_cat=\"${__mapfile_write_cat%.}\"";
+ nflag = 1;
evalstring(cmd, 0);
if (exitstatus != 0) {
ret = exitstatus;
diff --git a/src/share/poudriere/include/util.sh b/src/share/poudriere/include/util.sh
index cf5e2d43..8b039948 100644
--- a/src/share/poudriere/include/util.sh
+++ b/src/share/poudriere/include/util.sh
@@ -701,12 +701,14 @@ _mapfile_write_from_stdin() {
[ $# -eq 1 ] || eargs _mapfile_write_from_stdin handle
local data
- data="$(cat)"
- _mapfile_write "$@" "${data}"
+ # . is to preserve newline
+ data="$(cat; echo .)"
+ data="${data%.}"
+ _mapfile_write "$@" -n "${data}"
}
_mapfile_write() {
- [ $# -eq 2 ] || eargs mapfile_write handle data
+ [ $# -ge 2 ] || eargs mapfile_write handle [-n] data
local handle="$1"
shift
local fd
diff --git a/test/mapfile.sh b/test/mapfile.sh
index 8c27f723..3f8e5599 100644
--- a/test/mapfile.sh
+++ b/test/mapfile.sh
@@ -415,6 +415,32 @@ fi
TMP=$(mktemp -t mapfile)
TMP2=$(mktemp -t mapfile)
+ :>"${TMP}"
+ assert_ret 0 mapfile handle "${TMP2}" "we"
+ cat "${TMP}" | mapfile_write "${handle}"
+ assert_ret 0 mapfile_close "${handle}"
+ [ ! -s "${TMP2}" ]
+ assert 0 "$?" "'cat <empty file> | mapfile_write' should not write anything"
+ rm -f "${TMP}" "${TMP2}"
+}
+
+{
+ TMP=$(mktemp -t mapfile)
+ TMP2=$(mktemp -t mapfile)
+
+ :>"${TMP}"
+ assert_ret 0 mapfile handle "${TMP2}" "we"
+ mapfile_cat "${TMP}" | mapfile_write "${handle}"
+ assert_ret 0 mapfile_close "${handle}"
+ [ ! -s "${TMP2}" ]
+ assert 0 "$?" "'mapfile_cat <empty file> | mapfile_write' should not write anything"
+ rm -f "${TMP}" "${TMP2}"
+}
+
+{
+ TMP=$(mktemp -t mapfile)
+ TMP2=$(mktemp -t mapfile)
+
ps uaxwd > "${TMP}"
:>"${TMP2}"