Utils: Move deviceshell script into .qrc

Change-Id: I6acb542163ebe39aaea8a1f1c1b84d355f25eccc
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Marcus Tillmanns
2022-10-17 12:59:12 +02:00
parent bdf3c2d576
commit 5b3efd61c7
3 changed files with 118 additions and 122 deletions

View File

@@ -14,8 +14,6 @@ Q_LOGGING_CATEGORY(deviceShellLog, "qtc.utils.deviceshell", QtWarningMsg)
namespace Utils { namespace Utils {
namespace {
/*! /*!
* The multiplex script waits for input via stdin. * 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". * 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) DeviceShell::DeviceShell(bool forceFailScriptInstallation)
: m_forceFailScriptInstallation(forceFailScriptInstallation) : m_forceFailScriptInstallation(forceFailScriptInstallation)
@@ -381,7 +260,7 @@ bool DeviceShell::installShellScript()
} }
const static QByteArray shellScriptBase64 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 const QByteArray scriptCmd = "(scriptData=$(echo " + shellScriptBase64
+ " | base64 -d 2>/dev/null ) && /bin/sh -c \"$scriptData\") || " + " | base64 -d 2>/dev/null ) && /bin/sh -c \"$scriptData\") || "
"echo ERROR_INSTALL_SCRIPT >&2\n"; "echo ERROR_INSTALL_SCRIPT >&2\n";

View File

@@ -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

View File

@@ -239,6 +239,7 @@
<file>images/message@2x.png</file> <file>images/message@2x.png</file>
<file>images/help.png</file> <file>images/help.png</file>
<file alias="mimetypes/freedesktop.org.xml" compression-algorithm="best">../3rdparty/xdg/freedesktop.org.xml</file> <file alias="mimetypes/freedesktop.org.xml" compression-algorithm="best">../3rdparty/xdg/freedesktop.org.xml</file>
<file>scripts/deviceshell.sh</file>
</qresource> </qresource>
<qresource prefix="/codemodel"> <qresource prefix="/codemodel">
<file>images/enum.png</file> <file>images/enum.png</file>