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/wrt_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 -i ";
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 WrtLogW("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 WrtLogD("executing: %s", cmd.c_str());
68 auto filehandle = popen(cmd.c_str(), "r");
73 char buffer[1024] = "";
74 while (fread_unlocked(buffer, sizeof(char), sizeof(buffer)/sizeof(char),
79 WrtLogD("%s", msg.c_str());
80 auto err = pclose(filehandle);
81 if (!WIFEXITED(err)) {
84 if (0 != WEXITSTATUS(err)) {
85 if (1 == WEXITSTATUS(err)) {
86 return WrongWidgetPackage;
94 while ((line = getAndCutInstallerLogLine(msg)) != "")
96 if (line.find("successful") != std::string::npos)
98 id = new char[line.length()];
99 int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE.c_str(), id);
103 WrtLogW("Can not read widget ID from message: %s", line.c_str());
109 if (tizenId != "plugin")
119 bool uninstall(const std::string& tizenId)
121 std::string cmd = uninstallCmd + tizenId + " > /dev/null 2>/dev/null";
122 WrtLogD("executing: %s", cmd.c_str());
123 return (system(cmd.c_str()) == EXIT_SUCCESS);
126 bool uninstallByGuid(const std::string& guid)
128 std::string cmd = uninstallByGuidCmd + guid + "\" > /dev/null 2>/dev/null";
129 WrtLogD("executing: %s", cmd.c_str());
130 return (system(cmd.c_str()) == EXIT_SUCCESS);
133 bool sigintWrtClients()
135 return (system("pkill -2 wrt-client") == 0);