2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "InstallerWrapper.h"
19 #include <dpl/log/secure_log.h>
25 const std::string params = "WRT_TEST_MODE=1 ";
26 const std::string installCmd = params + "wrt-installer -i ";
27 const std::string uninstallCmd = params + "wrt-installer -un ";
28 const std::string redirection = " 2>&1";
29 const std::string INSTALLER_MESSAGE_ID_LINE =
30 "## wrt-installer : %s installation was successful.\n";
31 const std::string INSTALLER_MESSAGE_ID_LINE_FAIL =
32 "## wrt-installer : %s installation has failed - package already installed";
33 const std::string INSTALLER_MESSSGE_START = "## wrt-installer : ";
35 std::string getAndCutInstallerLogLine(std::string &src)
37 size_t startIndex = src.find(INSTALLER_MESSSGE_START);
38 if (startIndex == std::string::npos)
40 _W("Installer message can not be found");
43 size_t newLineIndex = src.find("\n", startIndex);
44 std::string line = src.substr(startIndex, newLineIndex - startIndex + 1);
45 src.erase(0, newLineIndex + 1);
51 namespace InstallerWrapper
54 InstallResult install(
55 const std::string& path,
57 const std::string& user)
61 auto cmd = installCmd + path + redirection;
62 if(user.length()) //if other user should be used
64 cmd = "su " + user + " -c '" + cmd + "'";
66 auto filehandle = popen(cmd.c_str(), "r");
71 char buffer[1024] = "";
72 while ( fread_unlocked(buffer, sizeof(char),
73 sizeof(buffer)/sizeof(char), filehandle) > 0 )
77 _D("%s", msg.c_str());
78 auto err = pclose(filehandle);
79 if (!WIFEXITED(err)) {
86 if (0 != WEXITSTATUS(err)) {
87 while ((line = getAndCutInstallerLogLine(msg)) != "")
89 if(line.find("failed") != std::string::npos)
91 id = new char[line.length()];
92 int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE_FAIL.c_str(), id);
95 _W("Can not read widget ID from message: %s", line.c_str());
104 if (1 == WEXITSTATUS(err)) {
105 return WrongWidgetPackage;
110 while ((line = getAndCutInstallerLogLine(msg)) != "")
112 if (line.find("successful") != std::string::npos)
114 id = new char[line.length()];
115 int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE.c_str(), id);
119 _W("Can not read widget ID from message: %s", line.c_str());
125 if (tizenId != "plugin")
135 bool uninstall(const std::string& tizenId)
137 std::string cmd = uninstallCmd + tizenId + " > /dev/null 2>/dev/null";
138 _D("executing: %s", cmd.c_str());
139 return (system(cmd.c_str()) == EXIT_SUCCESS);
142 bool sigintWrtClients()
144 return (system("pkill -2 wrt-client") == 0);