MCPcopy Create free account
hub / github.com/IJHack/QtPass / findBinaryInPath

Method findBinaryInPath

src/util.cpp:120–179  ·  view source on GitHub ↗

* @brief Finds the absolute path of a binary by searching the PATH environment * variable. * * Iterates through each PATH entry, checks whether the binary exists and is * executable, and returns the first matching absolute file path. On Windows, if * no local match is found, it may fall back to a WSL invocation when the binary * name is valid and WSL appears to support it. * * @example *

Source from the content-addressed store, hash-verified

118 * found.
119 */
120auto Util::findBinaryInPath(const QString &binary) -> QString {
121 if (binary.isEmpty())
122 return QString();
123
124 initialiseEnvironment();
125
126 QString ret;
127
128 const QString binaryWithSep = QDir::separator() + binary;
129
130 if (_env.contains("PATH")) {
131 QString path = _env.value("PATH");
132 const QChar delimiter = QDir::separator() == '\\' ? ';' : ':';
133 QStringList entries = path.split(delimiter);
134
135 for (const QString &entryConst : entries) {
136 QString fullPath = entryConst + binaryWithSep;
137 QFileInfo qfi(fullPath);
138#ifdef Q_OS_WIN
139 if (!qfi.exists()) {
140 QString fullPathExe = fullPath + ".exe";
141 qfi = QFileInfo(fullPathExe);
142 }
143#endif
144 if (!qfi.exists()) {
145 continue;
146 }
147 if (!qfi.isExecutable()) {
148 continue;
149 }
150
151 ret = qfi.absoluteFilePath();
152 break;
153 }
154 }
155#ifdef Q_OS_WIN
156 if (ret.isEmpty()) {
157 static const QRegularExpression whitespaceRegex(QStringLiteral("\\s"));
158 const bool hasWhitespace = binary.contains(whitespaceRegex);
159 if (!binary.isEmpty() && !hasWhitespace) {
160 QString wslCommand = QStringLiteral("wsl ") + binary;
161#ifdef QT_DEBUG
162 dbg() << "Util::findBinaryInPath(): falling back to WSL for binary"
163 << binary;
164#endif
165 QString out, err;
166 if (Executor::executeBlocking(wslCommand, {"--version"}, &out, &err) ==
167 0 &&
168 !out.isEmpty() && err.isEmpty()) {
169#ifdef QT_DEBUG
170 dbg() << "Util::findBinaryInPath(): using WSL binary" << wslCommand;
171#endif
172 ret = wslCommand;
173 }
174 }
175 }
176#endif
177

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected