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