176f76f4eff83b120bb142f43379d5a697875c7b
[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 <FIoFile.h>
31 #include <FAppPkgPackageInfo.h>
32 #include <FAppPkg_PackageManagerImpl.h>
33
34 #include "InstallerManager.h"
35
36 using namespace Tizen::App;
37 using namespace Tizen::App::Package;
38 using namespace Tizen::Base;
39 using namespace Tizen::Io;
40
41 extern "C" void Osp_Initialize();
42 static bool __osp_installer_report_result(const PackageId& packageId, int errorType);
43
44 static pkgmgr_installer *_pi = null;
45
46 int
47 main(int argc, char **argv)
48 {
49         int ret = 0;
50         const char *pkg_info = null;
51         char* pkg_path = null;
52         const char* pOptional = null;
53         String path;
54         PackageId packageId;
55         int errorType = 0;
56         pkgmgr_installer *pi = null;
57         int req_type = 0;
58
59         Osp_Initialize();
60
61         String buf;
62         for (int i = 0; i < argc; i++)
63         {
64                 const char* pStr = argv[i];
65                 String cmd(pStr);
66                 buf.Append(cmd);
67                 if (i != argc - 1)
68                 {
69                         buf.Append(" ");
70                 }
71         }
72
73         int emul = 0;
74
75 #ifdef _OSP_EMUL_
76         emul = 1;
77 #endif
78         fprintf(stderr, "  ## osp-installer: %s[%s] cmd=[%ls]\n", OSP_INSTALLER_VERSION, emul?"Emulator":"Target", buf.GetPointer());
79
80         if (argc == TEST_ARG_COUNT)
81         {
82                 errorType = InstallerManager::ReqeustByTest();
83                 goto CATCH;
84         }
85         else if (argc == COMMAND_ARG_COUNT)
86         {
87                 errorType = InstallerManager::RequestByCommand(argc, argv);
88                 goto CATCH;
89         }
90
91         pi = pkgmgr_installer_new();
92         _pi = pi;
93
94         pkgmgr_installer_receive_request(pi, argc, argv);
95
96         req_type = pkgmgr_installer_get_request_type(pi);
97         if (PKGMGR_REQ_INVALID >= req_type)
98         {
99                 goto CATCH;
100         }
101
102         pkg_info = pkgmgr_installer_get_request_info(pi);
103         pkg_path = (const_cast<char*>(pkg_info));
104         AppLog(" # path = [%s]", pkg_path);
105
106         path = pkg_path;
107
108         switch (req_type)
109         {
110         case PKGMGR_REQ_INSTALL:
111                 {
112                         pOptional = pkgmgr_installer_get_optional_data(pi);
113                         if (pOptional)
114                         {
115                                 packageId = pOptional;
116                                 AppLog(" # optional = [%s]", pOptional);
117                         }
118                         else
119                         {
120                                 FileAttributes attr;
121                                 result r = File::GetAttributes(path, attr);
122                                 if (IsFailed(r))
123                                 {
124                                         AppLog("GetAttributes() failed. [%ls]", path.GetPointer());
125                                         goto CATCH;
126                                 }
127
128                                 if (attr.IsDirectory())
129                                 {
130                                         if (path.EndsWith("/") == true)
131                                         {
132                                                 int length = path.GetLength();
133                                                 path.Remove(length - 1, 1);
134                                         }
135
136                                         path.SubString(path.GetLength() - PACKAGE_ID_LENGTH, PACKAGE_ID_LENGTH, packageId);
137                                 }
138                                 else
139                                 {
140                                         std::unique_ptr< PackageInfo > pPackageInfo(_PackageManagerImpl::GetInstance()->GetPackageInfoFromFileN(path));
141                                         if (pPackageInfo)
142                                         {
143                                                 packageId = pPackageInfo->GetId();
144                                         }
145                                 }
146
147                                 AppLog(" # path = [%ls] -> packageId = [%ls]", path.GetPointer(), packageId.GetPointer());
148                         }
149
150                         errorType = InstallerManager::Request(path, INSTALLER_OPERATION_INSTALL, INSTALLER_OPTION_NORMAL, pi, &packageId);
151
152 //              if (errorType != 0)
153 //                      {
154 //                              manager.RemoveGarbage(path);
155 //                      }
156
157                         __osp_installer_report_result(packageId, errorType);
158                 }
159                 break;
160
161         case PKGMGR_REQ_UNINSTALL:
162                 {
163                         PackageId reqeustPackageId;
164
165                         path.SubString(0, PACKAGE_ID_LENGTH, reqeustPackageId);
166
167                         AppLog("reqeustPackageId = %ls", reqeustPackageId.GetPointer());
168                         ret = InstallerManager::Request(reqeustPackageId, INSTALLER_OPERATION_UNINSTALL, INSTALLER_OPTION_NORMAL, _pi);
169
170                         __osp_installer_report_result(reqeustPackageId, errorType);
171                 }
172                 break;
173
174         case PKGMGR_REQ_REINSTALL:
175                 {
176                         PackageId rdsPackageId;
177
178                         path.SubString(0, PACKAGE_ID_LENGTH, rdsPackageId);
179
180                         AppLog("rdsPackageId = %ls", rdsPackageId.GetPointer());
181                         ret = InstallerManager::Request(rdsPackageId, INSTALLER_OPERATION_REINSTALL, INSTALLER_OPTION_NORMAL, _pi, &rdsPackageId);
182
183                         __osp_installer_report_result(rdsPackageId, errorType);
184                 }
185                 break;
186
187         case PKGMGR_REQ_MOVE:
188                 {
189                         if (_pi != 0)
190                         {
191                                 int moveType = -1;
192                                 moveType = pkgmgr_installer_get_move_type(_pi);
193
194                                 InstallerManager::RequestMove(path, moveType);
195                         }
196                         else
197                         {
198                                 AppLog("_pi is null");
199                         }
200                 }
201                 break;
202
203         default:
204                 ret = -1;
205                 break;
206         }
207
208         if (ret == -1)
209         {
210                 goto CATCH;
211         }
212
213 CATCH:
214
215         return errorType;
216 }
217
218 bool
219 __osp_installer_report_result(const PackageId& packageId, int errorType)
220 {
221         AppLog("------------------------------------------");
222         AppLog("osp_installer_report_result");
223         AppLog(" # request_type = [%d]", pkgmgr_installer_get_request_type(_pi));
224         AppLog(" # request_info = [%s]", pkgmgr_installer_get_request_info(_pi));
225         AppLog(" # session_id = [%s]", pkgmgr_installer_get_session_id(_pi));
226         if (packageId.IsEmpty() == false)
227         {
228                 AppLog(" # packageId = [%ls]", packageId.GetPointer());
229         }
230         AppLog(" # errorType = [%d]", errorType);
231         AppLog("------------------------------------------");
232
233         if (_pi == 0)
234         {
235                 AppLog("_pi is null");
236                 return false;
237         }
238
239         pkgmgr_installer_free(_pi);
240         _pi = null;
241
242         return true;
243 }