Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
shellhelpers.cpp
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4
#
include
"shellhelpers.h"
5
6
#
include
<
QtCore
/
qfile
.
h
>
7
#
include
<
QtCore
/
qregularexpression
.
h
>
8
9
#
include
<
cstdio
>
10
11
QT_BEGIN_NAMESPACE
12
13
using
namespace
Qt::StringLiterals;
14
15
void
forceStopBundle
(
const
Hdc
&hdc,
const
QString &bundleName)
16
{
17
hdc.shell({ u"aa"_s, u"force-stop"_s, bundleName });
18
}
19
20
ProcessState
queryProcessState
(
const
Hdc
&hdc,
const
QString &bundleName)
21
{
22
const
QString notRunningMarker = u"HARMONYOSTESTRUNNER_NOT_RUNNING"_s;
23
static
const
QRegularExpression pidList(u"^\\d+(\\s+\\d+)*$"_s);
24
25
const
QString output =
26
hdc.shell({ u"pidof"_s, bundleName, u"||"_s, u"echo"_s, notRunningMarker }).trimmed();
27
if
(output.contains(notRunningMarker))
28
return
ProcessState
::
NotRunning
;
29
if
(pidList.match(output).hasMatch())
30
return
ProcessState
::
Running
;
31
32
fprintf(stderr,
"harmonyostestrunner: warning: cannot tell whether %s is running, "
33
"hdc returned \"%s\"\n"
, qPrintable(bundleName), qPrintable(output));
34
return
ProcessState
::
Unknown
;
35
}
36
37
q23
::
expected
<
QByteArray
,
QString
>
copyDeviceFile
(
38
const
Hdc
&hdc,
const
QString &devicePath,
const
QString &localPath)
39
{
40
static
const
QRegularExpression transferFinishedPattern(u"FileTransfer finish, Size:(\\d+),"_s);
41
42
const
QString hdcAnswer = hdc.receiveFile(devicePath, localPath);
43
const
QRegularExpressionMatch sizeMatch = transferFinishedPattern.match(hdcAnswer);
44
if
(!sizeMatch.hasMatch())
45
return
q23::unexpected(u"hdc file recv answered \"%1\""_s.arg(hdcAnswer.trimmed()));
46
QFile localCopy(localPath);
47
if
(!localCopy.open(QIODevice::ReadOnly))
48
return
q23::unexpected(localCopy.errorString());
49
QByteArray contents = localCopy.readAll();
50
const
qint64 reportedSize = sizeMatch.captured(1).toLongLong();
51
if
(contents.size() != reportedSize) {
52
return
q23::unexpected(u"hdc reported %1 bytes, the local copy holds %2"_s
53
.arg(reportedSize).arg(contents.size()));
54
}
55
return
contents;
56
}
57
58
DeviceFileState
queryDeviceFileState
(
const
Hdc
&hdc,
const
QString &devicePath)
59
{
60
const
QString presentMarker = u"HARMONYOSTESTRUNNER_FILE_PRESENT"_s;
61
const
QString absentMarker = u"HARMONYOSTESTRUNNER_FILE_ABSENT"_s;
62
63
const
QString output = hdc.shell({ u"test"_s, u"-e"_s, devicePath, u"&&"_s, u"echo"_s,
64
presentMarker, u"||"_s, u"echo"_s, absentMarker }).trimmed();
65
if
(output == presentMarker)
66
return
DeviceFileState
::
Present
;
67
if
(output == absentMarker)
68
return
DeviceFileState
::
Absent
;
69
return
DeviceFileState
::
Unknown
;
70
}
71
72
std
::
optional
<
QString
>
answeredShell
(
const
Hdc
&hdc,
const
QStringList &command)
73
{
74
const
QString answeredMarker = u"HARMONYOSTESTRUNNER_ANSWERED"_s;
75
76
const
QString output =
77
hdc.shell(command + QStringList{ u";"_s, u"echo"_s, answeredMarker }).trimmed();
78
if
(!output.endsWith(answeredMarker))
79
return
std
::nullopt;
80
return
output.chopped(answeredMarker.size()).trimmed();
81
}
82
83
BundleState
queryBundleState
(
const
Hdc
&hdc,
const
QString &bundleName)
84
{
85
const
std::optional<QString> dump =
86
answeredShell(hdc, { u"bm"_s, u"dump"_s, u"-n"_s, bundleName });
87
if
(!dump)
88
return
BundleState
::
Unknown
;
89
if
(dump->startsWith(bundleName))
90
return
BundleState
::
Installed
;
91
return
BundleState
::
NotInstalled
;
92
}
93
94
QString
readDeviceFile
(
const
Hdc
&hdc,
const
QString &devicePath)
95
{
96
return
hdc.shell({ u"cat"_s, devicePath });
97
}
98
99
QT_END_NAMESPACE
Hdc
Definition
hdc.h:17
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
answeredShell
std::optional< QString > answeredShell(const Hdc &hdc, const QStringList &command)
Definition
shellhelpers.cpp:72
readDeviceFile
QString readDeviceFile(const Hdc &hdc, const QString &devicePath)
Definition
shellhelpers.cpp:94
ProcessState
ProcessState
Definition
shellhelpers.h:19
ProcessState::Running
@ Running
Definition
shellhelpers.h:19
ProcessState::Unknown
@ Unknown
Definition
shellhelpers.h:19
ProcessState::NotRunning
@ NotRunning
Definition
shellhelpers.h:19
queryBundleState
BundleState queryBundleState(const Hdc &hdc, const QString &bundleName)
Definition
shellhelpers.cpp:83
queryDeviceFileState
DeviceFileState queryDeviceFileState(const Hdc &hdc, const QString &devicePath)
Definition
shellhelpers.cpp:58
DeviceFileState
DeviceFileState
Definition
shellhelpers.h:18
DeviceFileState::Unknown
@ Unknown
Definition
shellhelpers.h:18
DeviceFileState::Present
@ Present
Definition
shellhelpers.h:18
DeviceFileState::Absent
@ Absent
Definition
shellhelpers.h:18
copyDeviceFile
q23::expected< QByteArray, QString > copyDeviceFile(const Hdc &hdc, const QString &devicePath, const QString &localPath)
Definition
shellhelpers.cpp:37
BundleState
BundleState
Definition
shellhelpers.h:17
BundleState::NotInstalled
@ NotInstalled
Definition
shellhelpers.h:17
BundleState::Unknown
@ Unknown
Definition
shellhelpers.h:17
BundleState::Installed
@ Installed
Definition
shellhelpers.h:17
queryProcessState
ProcessState queryProcessState(const Hdc &hdc, const QString &bundleName)
Definition
shellhelpers.cpp:20
forceStopBundle
void forceStopBundle(const Hdc &hdc, const QString &bundleName)
Definition
shellhelpers.cpp:15
qtbase
src
tools
harmonyostestrunner
shellhelpers.cpp
Generated on
for Qt by
1.18.0