Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
11QT_BEGIN_NAMESPACE
12
13using namespace Qt::StringLiterals;
14
15void forceStopBundle(const Hdc &hdc, const QString &bundleName)
16{
17 hdc.shell({ u"aa"_s, u"force-stop"_s, bundleName });
18}
19
20ProcessState 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))
29 if (pidList.match(output).hasMatch())
31
32 fprintf(stderr, "harmonyostestrunner: warning: cannot tell whether %s is running, "
33 "hdc returned \"%s\"\n", qPrintable(bundleName), qPrintable(output));
35}
36
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
58DeviceFileState 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)
67 if (output == absentMarker)
70}
71
72std::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
83BundleState 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)
89 if (dump->startsWith(bundleName))
92}
93
94QString readDeviceFile(const Hdc &hdc, const QString &devicePath)
95{
96 return hdc.shell({ u"cat"_s, devicePath });
97}
98
99QT_END_NAMESPACE
Definition hdc.h:17
std::optional< QString > answeredShell(const Hdc &hdc, const QStringList &command)
QString readDeviceFile(const Hdc &hdc, const QString &devicePath)
ProcessState
BundleState queryBundleState(const Hdc &hdc, const QString &bundleName)
DeviceFileState queryDeviceFileState(const Hdc &hdc, const QString &devicePath)
DeviceFileState
q23::expected< QByteArray, QString > copyDeviceFile(const Hdc &hdc, const QString &devicePath, const QString &localPath)
BundleState
ProcessState queryProcessState(const Hdc &hdc, const QString &bundleName)
void forceStopBundle(const Hdc &hdc, const QString &bundleName)