22//
33// SPDX-License-Identifier: LGPL-3.0-or-later
44
5- #include < numeric>
6- #include < unordered_map>
7- #include < vector>
8- #include < deque>
9- #include < memory>
10- #include < iostream>
11- #include < algorithm>
12- #include < cstdlib>
13- #include < map>
14- #include < list>
15- #include < thread>
16- #include < filesystem>
17- #include < cctype>
185#include " constant.h"
196#include " types.h"
207#include " variantValue.h"
8+ #include < algorithm>
9+ #include < cstdlib>
10+ #include < deque>
11+ #include < filesystem>
12+ #include < memory>
13+ #include < unordered_map>
14+ #include < vector>
2115
2216namespace {
2317
24- ExitCode fromString (const std::string & str)
18+ ExitCode fromString (std::string_view str)
2519{
2620 if (str == " done" ) {
2721 return ExitCode::Done;
2822 }
2923
30- if (str == " canceled" or str == " timeout" or str == " failed" or str == " dependency" or str == " skipped" ) {
24+ if (str == " canceled" || str == " timeout" || str == " failed" || str == " dependency" || str == " skipped" ) {
3125 return ExitCode::SystemdError;
3226 }
3327
@@ -44,10 +38,10 @@ ExitCode fromString(const std::string &str)
4438
4539ExitCode fromString (const char *str)
4640{
47- if (! str) {
41+ if (str == nullptr ) {
4842 return ExitCode::Waiting;
4943 }
50- std::string tmp{str};
44+ const std::string tmp{str};
5145 return fromString (tmp);
5246}
5347
@@ -62,7 +56,7 @@ ExitCode fromString(const char *str)
6256
6357int processExecStart (msg_ptr &msg, const std::deque<std::string_view> &execArgs)
6458{
65- int ret;
59+ int ret{ 0 } ;
6660
6761 if (ret = sd_bus_message_open_container (msg, SD_BUS_TYPE_STRUCT, " sv" ); ret < 0 ) {
6862 sd_journal_perror (" open struct of ExecStart failed." );
@@ -99,7 +93,7 @@ int processExecStart(msg_ptr &msg, const std::deque<std::string_view> &execArgs)
9993 return ret;
10094 }
10195
102- for (auto execArg : execArgs) {
96+ for (const auto & execArg : execArgs) {
10397 if (ret = sd_bus_message_append (msg, " s" , execArg.data ()); ret < 0 ) {
10498 sd_journal_perror (" append args of execStart failed." );
10599 return ret;
@@ -147,16 +141,16 @@ DBusValueType getPropType(std::string_view key)
147141 {" WorkingDirectory" , DBusValueType::String},
148142 {" ExecSearchPath" , DBusValueType::ArrayOfString}};
149143
150- if (auto it = map.find (key); it != map.cend ()) {
144+ if (const auto it = map.find (key); it != map.cend ()) {
151145 return it->second ;
152146 }
153147
154148 return DBusValueType::String; // fallback to string
155149}
156150
157- int appendPropValue (msg_ptr &msg, DBusValueType type, const std::list <std::string_view > &value)
151+ int appendPropValue (msg_ptr &msg, DBusValueType type, const std::vector <std::string > &value)
158152{
159- int ret;
153+ int ret{ 0 } ;
160154
161155 auto handler = creatValueHandler (msg, type);
162156 if (handler == nullptr ) {
@@ -184,47 +178,42 @@ int appendPropValue(msg_ptr &msg, DBusValueType type, const std::list<std::strin
184178 return 0 ;
185179}
186180
187- int processKVPair (msg_ptr &msg, const std::map <std::string_view, std::list <std::string_view >> &props)
181+ int processKVPair (msg_ptr &msg, std::unordered_map <std::string_view, std::vector <std::string >> &props)
188182{
189- int ret;
183+ int ret{ 0 } ;
190184 if (!props.empty ()) {
191- for (auto [key, value] : props) {
192- const std::list<std::string_view> *valuePtr = &value;
193- std::list<std::string_view> normalizedValue;
194- std::vector<std::string> normalizedStorage;
195-
185+ for (auto &[key, value] : props) {
196186 if (key == " ExecSearchPath" ) {
187+ std::vector<std::string> normalizedValue;
197188 for (const auto &v : value) {
198- std::filesystem::path p{std::string{v}};
189+ const std::filesystem::path p{std::string{v}};
199190 if (!p.is_absolute ()) {
200191 sd_journal_print (LOG_INFO, " ExecSearchPath ignoring relative path: %s" , std::string{v}.c_str ());
201192 continue ;
202193 }
203- normalizedStorage .emplace_back (p.lexically_normal ().string ());
194+ normalizedValue .emplace_back (p.lexically_normal ().string ());
204195 }
205196
206- normalizedValue.assign (normalizedStorage.begin (), normalizedStorage.end ());
207-
208197 if (normalizedValue.empty ()) {
209198 sd_journal_print (LOG_WARNING, " ExecSearchPath normalized to empty, skipping property" );
210199 continue ;
211200 }
212201
213- valuePtr = & normalizedValue;
202+ value = std::move ( normalizedValue) ;
214203 }
215204
216- std::string keyStr{key};
205+ const std::string keyStr{key};
217206 if (ret = sd_bus_message_open_container (msg, SD_BUS_TYPE_STRUCT, " sv" ); ret < 0 ) {
218207 sd_journal_perror (" open struct of properties failed." );
219208 return ret;
220209 }
221210
222- if (ret = sd_bus_message_append (msg, " s" , keyStr.data ()); ret < 0 ) {
211+ if (ret = sd_bus_message_append (msg, " s" , keyStr.c_str ()); ret < 0 ) {
223212 sd_journal_perror (" append key of property failed." );
224213 return ret;
225214 }
226215
227- if (ret = appendPropValue (msg, getPropType (key), *valuePtr ); ret < 0 ) {
216+ if (ret = appendPropValue (msg, getPropType (key), value ); ret < 0 ) {
228217 sd_journal_perror (" append value of property failed." );
229218 return ret;
230219 }
@@ -241,7 +230,7 @@ int processKVPair(msg_ptr &msg, const std::map<std::string_view, std::list<std::
241230std::string cmdParse (msg_ptr &msg, std::deque<std::string_view> cmdLines)
242231{
243232 std::string serviceName{" internalError" };
244- std::map <std::string_view, std::list <std::string_view >> props;
233+ std::unordered_map <std::string_view, std::vector <std::string >> props;
245234
246235 while (!cmdLines.empty ()) { // NOTE: avoid stl exception
247236 auto str = cmdLines.front ();
@@ -280,7 +269,7 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
280269 continue ;
281270 }
282271
283- props[key].push_back (kvStr.substr (splitIndex + 1 ));
272+ props[key].emplace_back (kvStr.substr (splitIndex + 1 ));
284273 cmdLines.pop_front ();
285274 continue ;
286275 }
@@ -290,7 +279,7 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
290279 }
291280
292281 // Processing of the binary file and its parameters that am want to launch
293- auto &execArgs = cmdLines;
282+ const auto &execArgs = cmdLines;
294283 if (execArgs.empty ()) {
295284 sd_journal_print (LOG_ERR, " param exec is empty." );
296285 serviceName = " invalidInput" ;
@@ -302,8 +291,8 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
302291 return serviceName;
303292 }
304293
305- int ret;
306- if (ret = sd_bus_message_append (msg, " s" , props[" unitName" ].front ().data ()); ret < 0 ) { // unitName
294+ int ret{ 0 } ;
295+ if (ret = sd_bus_message_append (msg, " s" , props[" unitName" ].front ().c_str ()); ret < 0 ) { // unitName
307296 sd_journal_perror (" append unitName failed." );
308297 return serviceName;
309298 }
@@ -368,7 +357,7 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
368357
369358int jobRemovedReceiver (sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
370359{
371- int ret;
360+ int ret{ 0 } ;
372361 if (ret = sd_bus_error_is_set (ret_error); ret != 0 ) {
373362 sd_journal_print (LOG_ERR, " JobRemoved error: [%s,%s]" , ret_error->name , ret_error->message );
374363 } else {
@@ -385,7 +374,7 @@ int jobRemovedReceiver(sd_bus_message *m, void *userdata, sd_bus_error *ret_erro
385374 }
386375 }
387376
388- if (ret_error and ret_error->_need_free ) {
377+ if (ret_error != nullptr && ret_error->_need_free != 0 ) {
389378 sd_bus_error_free (ret_error);
390379 }
391380
@@ -394,7 +383,7 @@ int jobRemovedReceiver(sd_bus_message *m, void *userdata, sd_bus_error *ret_erro
394383
395384int process_dbus_message (sd_bus *bus)
396385{
397- int ret;
386+ int ret{ 0 } ;
398387 ret = sd_bus_process (bus, nullptr );
399388 if (ret < 0 ) {
400389 sd_journal_print (LOG_ERR, " event loop error." );
@@ -422,7 +411,7 @@ int main(int argc, const char *argv[])
422411 sd_bus_message *msg{nullptr };
423412 sd_bus *bus{nullptr };
424413 std::string serviceId;
425- int ret;
414+ int ret{ 0 } ;
426415
427416 if (ret = sd_bus_open_user (&bus); ret < 0 ) {
428417 sd_journal_perror (" Failed to connect to user bus." );
@@ -476,12 +465,12 @@ int main(int argc, const char *argv[])
476465 sd_bus_message_unref (reply);
477466
478467 while (true ) { // wait for jobRemoved
479- int ret = process_dbus_message (bus);
468+ const auto ret = process_dbus_message (bus);
480469 if (ret < 0 ) {
481470 releaseRes (error, msg, bus, ExitCode::InternalError);
482471 }
483472
484- if (resultData.removedFlag ) {
473+ if (resultData.removedFlag != 0 ) {
485474 break ;
486475 }
487476 }
0 commit comments