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