Tizen 2.1 base
[platform/framework/native/installer.git] / src / backend / backend.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        backend.cpp
19  */
20
21 #include <new>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include <pkgmgr_installer.h>
29
30 #include <FBase_StringConverter.h>
31
32 #include "InstallerManager.h"
33 #include "InstallerDefs.h"
34 #include "InstallerUtil.h"
35
36 using namespace Tizen::Base;
37
38 extern "C" void Osp_Initialize();
39 static bool __osp_installer_report_result(const String& appId, int errorType);
40
41 static pkgmgr_installer *_pi = null;
42
43 int
44 main(int argc, char **argv)
45 {
46         int ret = 0;
47         const char *pkg_info = NULL;
48         char* pkg_path = NULL;
49         String path;
50         String appId;
51         int errorType = 0;
52         InstallerManager manager;
53         pkgmgr_installer *pi = null;
54         int req_type = 0;
55
56         Osp_Initialize();
57
58         AppLogTag(OSP_INSTALLER, "--\n\n\n");
59         AppLogTag(OSP_INSTALLER, "==========================================");
60         AppLogTag(OSP_INSTALLER, " # %s", OSP_INSTALLER_VERSION);
61         AppLogTag(OSP_INSTALLER, " # argc = %d", argc);
62
63         if (argc == TEST_ARG_COUNT)
64         {
65                 errorType = manager.ReqeustByTest();
66                 manager.PrintResult();
67                 goto CATCH;
68         }
69         else if (argc == COMMAND_ARG_COUNT)
70         {
71                 errorType = manager.RequestByCommand(argc, argv);
72                 goto CATCH;
73         }
74
75         pi = pkgmgr_installer_new();
76         _pi = pi;
77
78         pkgmgr_installer_receive_request(pi, argc, argv);
79
80         req_type = pkgmgr_installer_get_request_type(pi);
81         if (PKGMGR_REQ_INVALID >= req_type)
82         {
83                 goto CATCH;
84         }
85
86         pkg_info = pkgmgr_installer_get_request_info(pi);
87         pkg_path = (const_cast<char*>(pkg_info));
88         AppLogTag(OSP_INSTALLER, " # path = [%s]", pkg_path);
89
90         path = pkg_path;
91
92         switch (req_type)
93         {
94         case PKGMGR_REQ_INSTALL:
95                 {
96                         errorType = manager.Request(path, INSTALLER_OPERATION_INSTALL, REQUESTER_TYPE_NORMAL);
97                         appId = manager.GetId();
98                         if (_pi != 0)
99                         {
100                                 char resultBuf[128] = {0};
101                                 snprintf(resultBuf, sizeof(resultBuf), "%ls", appId.GetPointer());
102                                 pkgmgr_installer_send_signal(_pi, "tpk", resultBuf, "start", "install");
103                         }
104                         else
105                         {
106                                 AppLogTag(OSP_INSTALLER, "_pi is null");
107                         }
108
109 //                      if (errorType != 0)
110 //                      {
111 //                              manager.RemoveGarbage(path);
112 //                      }
113
114                         __osp_installer_report_result(appId, errorType);
115                         manager.PrintResult();
116                 }
117                 break;
118
119         case PKGMGR_REQ_UNINSTALL:
120                 {
121                         String requestAppId;
122
123                         if (path.Contains(PACKAGE_NAME_PREFIX_ORG) == true)
124                         {
125                                 String prefixPackage(PACKAGE_NAME_PREFIX_ORG);
126                                 path.SubString(prefixPackage.GetLength(), APPID_LENGTH, requestAppId);
127                         }
128                         else
129                         {
130                                 path.SubString(0, APPID_LENGTH, requestAppId);
131                         }
132
133                         AppLogTag(OSP_INSTALLER, "requestAppId = %ls", requestAppId.GetPointer());
134                         ret = manager.Request(requestAppId, INSTALLER_OPERATION_UNINSTALL, REQUESTER_TYPE_NORMAL);
135                         appId = manager.GetId();
136
137                         if (_pi != 0)
138                         {
139                                 char resultBuf[128] = {0};
140                                 snprintf(resultBuf, sizeof(resultBuf), "%ls", appId.GetPointer());
141                                 pkgmgr_installer_send_signal(_pi, "tpk", resultBuf, "start", "uninstall");
142                         }
143                         else
144                         {
145                                 AppLogTag(OSP_INSTALLER, "_pi is null");
146                         }
147
148                         __osp_installer_report_result(appId, errorType);
149                         manager.PrintResult();
150                 }
151                 break;
152
153         case PKGMGR_REQ_MOVE:
154                 {
155                         if (_pi != 0)
156                         {
157                                 int moveType = -1;
158                                 moveType = pkgmgr_installer_get_move_type(_pi);
159
160                                 manager.RequestMove(path, moveType);
161                         }
162                         else
163                         {
164                                 AppLog("_pi is null");
165                         }
166
167                 }
168                 break;
169
170         default:
171                 ret = -1;
172                 break;
173         }
174
175         if (ret == -1)
176         {
177                 goto CATCH;
178         }
179
180 CATCH:
181         return errorType;
182 }
183
184 bool
185 __osp_installer_send_error(int errorType)
186 {
187         char errorMsg[11] = {0};
188         int error = 0;
189
190         if (errorType >= INSTALLER_ERROR_USER_CANCEL)
191         {
192                 error = INSTALLER_ERROR_FATAL_ERROR;
193         }
194
195         snprintf(errorMsg, sizeof(errorMsg) - 1, "%d", error);
196         pkgmgr_installer_send_signal(_pi, "tpk", "", "error", errorMsg);
197
198         return true;
199 }
200
201 bool
202 __osp_installer_report_result(const String& appId, int errorType)
203 {
204         int ret = 0;
205         char resultBuf[128] = {0};
206         InstallerManager manager;
207         const char* pPkgType = "tpk";
208         const char* pKey = "end";
209         const char* pValue = null;
210
211         AppLogTag(OSP_INSTALLER, "------------------------------------------");
212         AppLogTag(OSP_INSTALLER, "osp_installer_report_result");
213         AppLogTag(OSP_INSTALLER, " # request_type = [%d]", pkgmgr_installer_get_request_type(_pi));
214         AppLogTag(OSP_INSTALLER, " # request_info = [%s]", pkgmgr_installer_get_request_info(_pi));
215         AppLogTag(OSP_INSTALLER, " # session_id = [%s]", pkgmgr_installer_get_session_id(_pi));
216         AppLogTag(OSP_INSTALLER, "------------------------------------------");
217
218         if (_pi == 0)
219         {
220                 AppLogTag(OSP_INSTALLER, "_pi is null");
221                 return false;
222         }
223
224         if (errorType == 0)
225         {
226                 if (appId.IsEmpty() == true)
227                 {
228                         snprintf(resultBuf, sizeof(resultBuf), "0000000000");
229                 }
230                 else
231                 {
232                         snprintf(resultBuf, sizeof(resultBuf), "%ls", appId.GetPointer());
233                 }
234
235                 pValue = "ok";
236         }
237         else
238         {
239                 snprintf(resultBuf, sizeof(resultBuf), "0000000000");
240                 pValue = "fail";
241                 __osp_installer_send_error(errorType);
242         }
243
244         ret = pkgmgr_installer_send_signal(_pi, pPkgType, resultBuf, pKey, pValue);
245         AppLogTag(OSP_INSTALLER, "------------------------------------------");
246         AppLogTag(OSP_INSTALLER, "pkgmgr_installer_send_signal");
247         AppLogTag(OSP_INSTALLER, " # type   = [%s]", pPkgType);
248         AppLogTag(OSP_INSTALLER, " # pkg id = [%s]", resultBuf);
249         AppLogTag(OSP_INSTALLER, " # key    = [%s]", pKey);
250         AppLogTag(OSP_INSTALLER, " # val    = [%s]", pValue);
251         AppLogTag(OSP_INSTALLER, " # ret    = [%s]", (ret == 0)?"success":"failure");
252         AppLogTag(OSP_INSTALLER, "------------------------------------------");
253
254         pkgmgr_installer_free(_pi);
255         _pi = null;
256
257         return true;
258 }