From 5b3efd61c7f5de81d01b737481c9d6fc2113c303 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Mon, 17 Oct 2022 12:59:12 +0200 Subject: [PATCH] Utils: Move deviceshell script into .qrc Change-Id: I6acb542163ebe39aaea8a1f1c1b84d355f25eccc Reviewed-by: Jarek Kobus --- src/libs/utils/deviceshell.cpp | 123 +------------------------- src/libs/utils/scripts/deviceshell.sh | 116 ++++++++++++++++++++++++ src/libs/utils/utils.qrc | 1 + 3 files changed, 118 insertions(+), 122 deletions(-) create mode 100644 src/libs/utils/scripts/deviceshell.sh diff --git a/src/libs/utils/deviceshell.cpp b/src/libs/utils/deviceshell.cpp index 5e43d020219..b3133a66c14 100644 --- a/src/libs/utils/deviceshell.cpp +++ b/src/libs/utils/deviceshell.cpp @@ -14,8 +14,6 @@ Q_LOGGING_CATEGORY(deviceShellLog, "qtc.utils.deviceshell", QtWarningMsg) namespace Utils { -namespace { - /*! * The multiplex script waits for input via stdin. * @@ -31,125 +29,6 @@ namespace { * Once the process exits its exit code is send to stdout with the command-id and the type "R". * */ -const QLatin1String r_execScript = QLatin1String(R"SCRIPT( -#!/bin/sh -FINAL_OUT=$(mktemp -u) -mkfifo "$FINAL_OUT" - -finalOutput() { - local fileInputBuffer - while read fileInputBuffer - do - if test -f "$fileInputBuffer.err"; then - cat $fileInputBuffer.err - fi - cat $fileInputBuffer - rm -f $fileInputBuffer.err $fileInputBuffer - done -} - -finalOutput < $FINAL_OUT & - -readAndMark() { - local buffer - while read buffer - do - printf '%s:%s:%s\n' "$1" "$2" "$buffer" - done -} - -base64decode() -{ - base64 -d 2>/dev/null -} - -base64encode() -{ - base64 2>/dev/null -} - -executeAndMark() -{ - PID="$1" - INDATA="$2" - shift - shift - CMD="$@" - - # LogFile - TMPFILE=$(mktemp) - - # Output Streams - stdoutenc=$(mktemp -u) - stderrenc=$(mktemp -u) - mkfifo "$stdoutenc" "$stderrenc" - - # app output streams - stdoutraw=$(mktemp -u) - stderrraw=$(mktemp -u) - mkfifo "$stdoutraw" "$stderrraw" - - # Cleanup - trap 'rm -f "$stdoutenc" "$stderrenc" "$stdoutraw" "$stderrraw"' EXIT - - # Pipe all app output through base64, and then into the output streams - cat $stdoutraw | base64encode > "$stdoutenc" & - cat $stderrraw | base64encode > "$stderrenc" & - - # Mark the app's output streams - readAndMark $PID 'O' < "$stdoutenc" >> $TMPFILE & - readAndMark $PID 'E' < "$stderrenc" >> $TMPFILE.err & - - # Start the app ... - if [ -z "$INDATA" ] - then - eval $CMD 1> "$stdoutraw" 2> "$stderrraw" - else - echo $INDATA | base64decode | eval "$CMD" 1> "$stdoutraw" 2> "$stderrraw" - fi - - exitcode=$(echo $? | base64encode) - wait - echo "$PID:R:$exitcode" >> $TMPFILE - echo $TMPFILE -} - -execute() -{ - PID="$1" - - if [ "$#" -lt "3" ]; then - TMPFILE=$(mktemp) - echo "$PID:R:MjU1Cg==" > $TMPFILE - echo $TMPFILE - else - INDATA=$(eval echo "$2") - shift - shift - CMD=$@ - executeAndMark $PID "$INDATA" "$CMD" - fi -} - -cleanup() -{ - kill -- -$$ - exit 1 -} - -trap cleanup 1 2 3 6 - -echo SCRIPT_INSTALLED >&2 - -(while read -r id inData cmd; do - if [ "$id" = "exit" ]; then - exit - fi - execute $id $inData $cmd || echo "$id:R:255" & -done) > $FINAL_OUT -)SCRIPT"); - -} // namespace DeviceShell::DeviceShell(bool forceFailScriptInstallation) : m_forceFailScriptInstallation(forceFailScriptInstallation) @@ -381,7 +260,7 @@ bool DeviceShell::installShellScript() } const static QByteArray shellScriptBase64 - = QByteArray(r_execScript.begin(), r_execScript.size()).toBase64(); + = FilePath(":/utils/scripts/deviceshell.sh").fileContents().value().toBase64(); const QByteArray scriptCmd = "(scriptData=$(echo " + shellScriptBase64 + " | base64 -d 2>/dev/null ) && /bin/sh -c \"$scriptData\") || " "echo ERROR_INSTALL_SCRIPT >&2\n"; diff --git a/src/libs/utils/scripts/deviceshell.sh b/src/libs/utils/scripts/deviceshell.sh new file mode 100644 index 00000000000..86f096b256e --- /dev/null +++ b/src/libs/utils/scripts/deviceshell.sh @@ -0,0 +1,116 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 +FINAL_OUT=$(mktemp -u) +mkfifo "$FINAL_OUT" + +finalOutput() { + local fileInputBuffer + while read fileInputBuffer + do + if test -f "$fileInputBuffer.err"; then + cat $fileInputBuffer.err + fi + cat $fileInputBuffer + rm -f $fileInputBuffer.err $fileInputBuffer + done +} + +finalOutput < $FINAL_OUT & + +readAndMark() { + local buffer + while read buffer + do + printf '%s:%s:%s\n' "$1" "$2" "$buffer" + done +} + +base64decode() +{ + base64 -d 2>/dev/null +} + +base64encode() +{ + base64 2>/dev/null +} + +executeAndMark() +{ + PID="$1" + INDATA="$2" + shift + shift + CMD="$@" + + # LogFile + TMPFILE=$(mktemp) + + # Output Streams + stdoutenc=$(mktemp -u) + stderrenc=$(mktemp -u) + mkfifo "$stdoutenc" "$stderrenc" + + # app output streams + stdoutraw=$(mktemp -u) + stderrraw=$(mktemp -u) + mkfifo "$stdoutraw" "$stderrraw" + + # Cleanup + trap 'rm -f "$stdoutenc" "$stderrenc" "$stdoutraw" "$stderrraw"' EXIT + + # Pipe all app output through base64, and then into the output streams + cat $stdoutraw | base64encode > "$stdoutenc" & + cat $stderrraw | base64encode > "$stderrenc" & + + # Mark the app's output streams + readAndMark $PID 'O' < "$stdoutenc" >> $TMPFILE & + readAndMark $PID 'E' < "$stderrenc" >> $TMPFILE.err & + + # Start the app ... + if [ -z "$INDATA" ] + then + eval $CMD 1> "$stdoutraw" 2> "$stderrraw" + else + echo $INDATA | base64decode | eval "$CMD" 1> "$stdoutraw" 2> "$stderrraw" + fi + + exitcode=$(echo $? | base64encode) + wait + echo "$PID:R:$exitcode" >> $TMPFILE + echo $TMPFILE +} + +execute() +{ + PID="$1" + + if [ "$#" -lt "3" ]; then + TMPFILE=$(mktemp) + echo "$PID:R:MjU1Cg==" > $TMPFILE + echo $TMPFILE + else + INDATA=$(eval echo "$2") + shift + shift + CMD=$@ + executeAndMark $PID "$INDATA" "$CMD" + fi +} + +cleanup() +{ + kill -- -$$ + exit 1 +} + +trap cleanup 1 2 3 6 + +echo SCRIPT_INSTALLED >&2 + +(while read -r id inData cmd; do + if [ "$id" = "exit" ]; then + exit + fi + execute $id $inData $cmd || echo "$id:R:255" & +done) > $FINAL_OUT diff --git a/src/libs/utils/utils.qrc b/src/libs/utils/utils.qrc index 8685c9be9f2..1f2ef64898e 100644 --- a/src/libs/utils/utils.qrc +++ b/src/libs/utils/utils.qrc @@ -239,6 +239,7 @@ images/message@2x.png images/help.png ../3rdparty/xdg/freedesktop.org.xml + scripts/deviceshell.sh images/enum.png