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/log.h>
26 const std::string params = "DPL_USE_OLD_STYLE_LOGS=0 "
27 "DPL_USE_OLD_STYLE_PEDANTIC_LOGS=0 WRT_TEST_MODE=1 ";
28 const std::string installCmd = params + "wrt-installer -if ";
29 const std::string uninstallCmd = params + "wrt-installer -un ";
30 const std::string uninstallByGuidCmd = params + "wrt-installer -ug \"";
31 const std::string redirection = " 2>&1";
32 const std::string INSTALLER_MESSAGE_ID_LINE =
33 "## wrt-installer : %s installation was successful.\n";
34 const std::string INSTALLER_MESSSGE_START = "## wrt-installer : ";
36 std::string getAndCutInstallerLogLine(std::string &src)
38 size_t startIndex = src.find(INSTALLER_MESSSGE_START);
39 if (startIndex == std::string::npos)
41 LogWarning("Installer message can not be found");
44 size_t newLineIndex = src.find("\n", startIndex);
45 std::string line = src.substr(startIndex, newLineIndex - startIndex + 1);
46 src.erase(0, newLineIndex + 1);
52 namespace InstallerWrapper
55 InstallResult install(
56 const std::string& path,
58 const std::string& user)
62 auto cmd = installCmd + path + redirection;
63 if(user.length()) //if other user should be used
65 cmd = "su " + user + " -c '" + cmd + "'";
67 auto filehandle = popen(cmd.c_str(), "r");
72 char buffer[1024] = "";
74 while ((ret = fread_unlocked(buffer,
76 sizeof(buffer)/sizeof(char),
82 auto err = pclose(filehandle);
83 if (!WIFEXITED(err)) {
86 if (0 != WEXITSTATUS(err)) {
87 if (1 == WEXITSTATUS(err)) {
88 return WrongWidgetPackage;
96 while ((line = getAndCutInstallerLogLine(msg)) != "")
98 if (line.find("successful") != std::string::npos)
100 id = new char[line.length()];
101 int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE.c_str(), id);
105 LogWarning("Can not read widget ID from message: " << line);
111 if (tizenId != "plugin")
121 bool uninstall(const std::string& tizenId)
123 std::string cmd = uninstallCmd + tizenId + " > /dev/null 2>/dev/null";
124 LogDebug("executing: " << cmd);
125 return (system(cmd.c_str()) == EXIT_SUCCESS);
128 bool uninstallByGuid(const std::string& guid)
130 std::string cmd = uninstallByGuidCmd + guid + "\" > /dev/null 2>/dev/null";
131 LogDebug("executing: " << cmd);
132 return (system(cmd.c_str()) == EXIT_SUCCESS);
135 bool sigintWrtClients()
137 return (system("pkill -2 wrt-client") == 0);