Tizen 2.1 base
[platform/core/base/rpm-installer.git] / frontend / src / rpm-cmdline.c
1 /*
2  * rpm-installer
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <getopt.h>
26 #include <pthread.h>
27 #include <stdio.h>
28 #include <appcore-efl.h>
29 #include <pkgmgr_installer.h>
30 #include <security-server.h>
31 #include "rpm-frontend.h"
32 #include "rpm-installer-util.h"
33 #include "rpm-installer.h"
34 #include "rpm-homeview.h"
35
36 #define _FIX_POP_UP_
37 extern struct appdata ad;
38 extern int ret_val;
39 extern pkgmgr_installer *pi;
40 ri_frontend_data front_data;
41 char scrolllabel[256];
42 int move_type;
43
44 static void __ri_show_usage(char **arg);
45 static int __ri_process_request(ri_frontend_cmdline_arg *fdata);
46
47 static void __ri_show_usage(char **arg)
48 {
49
50         int i = 0;
51         char buffer[256];
52         char buff[256] = "";
53         while (arg[i] != NULL) {
54                 snprintf(buffer, 256, "%s %s", buff, arg[i]);
55                 strncpy(buff, buffer, 255);
56                 i++;
57         }
58
59         _d_msg(DEBUG_INFO, "%s\n", buffer);
60         _d_msg(DEBUG_INFO,
61                "\nrpm-backend usage\n   rpm-backend -k <keyid>  <command> <pkgid | pkg_path> [-q] \n\n");
62         _d_msg(DEBUG_INFO, "<Commands> \n");
63         _d_msg(DEBUG_INFO,
64                "\t -i <package file path>         : install package file \n");
65         _d_msg(DEBUG_INFO,
66                "\t -k <keyid>                   : key id file \n");
67         _d_msg(DEBUG_INFO,
68                "\t -r : (recover). Must ignore specific package name or path \n");
69         _d_msg(DEBUG_INFO,
70                "\t -d <package name>            : delete a package with package name \n");
71         _d_msg(DEBUG_INFO,
72                "\t -q : (quiet) run in background without any user interaction \n");
73 }
74
75 int _ri_parse_cmdline(int argc, char **argv, ri_frontend_cmdline_arg *data)
76 {
77         int req_cmd = INVALID_CMD;
78         const char *pkgid = NULL;
79         int quiet = 0;
80         const char *pkeyid = NULL;
81         int ret = 0;
82         int move_type = -1;
83         pi = pkgmgr_installer_new();
84         if (!pi) {
85                 _d_msg(DEBUG_ERR,
86                        "Failure in creating the pkgmgr_installer object \n");
87                 return RPM_INSTALLER_ERR_WRONG_PARAM;
88         }
89         ret = pkgmgr_installer_receive_request(pi, argc, argv);
90         if (ret) {
91                 _d_msg(DEBUG_ERR, "pkgmgr_installer_receive_request failed \n");
92                 return RPM_INSTALLER_ERR_WRONG_PARAM;
93         }
94         ret = pkgmgr_installer_get_request_type(pi);
95         switch (ret) {
96         case PKGMGR_REQ_INSTALL:
97                 req_cmd = INSTALL_CMD;
98                 break;
99         case PKGMGR_REQ_UNINSTALL:
100                 req_cmd = DELETE_CMD;
101                 break;
102         case PKGMGR_REQ_RECOVER:
103                 req_cmd = RECOVER_CMD;
104                 break;
105         case PKGMGR_REQ_CLEAR:
106                 req_cmd = CLEARDATA_CMD;
107                 break;
108         case PKGMGR_REQ_MOVE:
109                 req_cmd = MOVE_CMD;
110                 break;
111         case PKGMGR_REQ_PERM:
112                 goto PARSEERROR;
113         case PKGMGR_REQ_INVALID:
114                 req_cmd = INVALID_CMD;
115                 goto PARSEERROR;
116         default:
117                 goto PARSEERROR;
118         }
119         if (req_cmd != RECOVER_CMD) {
120                 pkgid = pkgmgr_installer_get_request_info(pi);
121                 if (!pkgid) {
122                         _d_msg(DEBUG_ERR,
123                                "pkgmgr_installer_get_request_info failed \n");
124                         return RPM_INSTALLER_ERR_WRONG_PARAM;
125                 }
126                 pkeyid = pkgmgr_installer_get_session_id(pi);
127                 if (!pkeyid) {
128                         _d_msg(DEBUG_ERR, "pkgmgr_installer_get_session_id failed \n");
129                         return RPM_INSTALLER_ERR_WRONG_PARAM;
130                 }
131
132                 quiet = pkgmgr_installer_is_quiet(pi);
133                 if (quiet != 0 && quiet != 1) {
134                         _d_msg(DEBUG_ERR, "pkgmgr_installer_is_quiet failed \n");
135                         return RPM_INSTALLER_ERR_WRONG_PARAM;
136                 }
137
138                 move_type = pkgmgr_installer_get_move_type(pi);
139         }
140         if ((req_cmd < INSTALL_CMD) ||(req_cmd > MOVE_CMD)) {
141                 _d_msg(DEBUG_ERR, "invalid command \n");
142                 goto PARSEERROR;
143         }
144
145         data->req_cmd = req_cmd;
146         data->pkgid = (char *)pkgid;
147         data->quiet = quiet;
148         data->keyid = (char *)pkeyid;
149         data->move_type = move_type;
150         return RPM_INSTALLER_SUCCESS;
151
152  PARSEERROR:
153         _d_msg(DEBUG_ERR, "Error in parsing input parameter\n");
154         __ri_show_usage(argv);
155         return RPM_INSTALLER_ERR_WRONG_PARAM;
156
157 }
158
159 static int __ri_process_request(ri_frontend_cmdline_arg *data)
160 {
161         int ret = 0;
162         if (!data)
163                 return RPM_INSTALLER_ERR_WRONG_PARAM;
164         char *pkgid = NULL;
165         char *keyid = NULL;
166         if (data->req_cmd != RECOVER_CMD) {
167                 pkgid = strdup(data->pkgid);
168                 if (PM_UNLIKELY(pkgid == NULL)) {
169                         _d_msg(DEBUG_ERR, "strdup failed\n");
170                         return RPM_INSTALLER_ERR_WRONG_PARAM;
171                 }
172                 keyid = strdup(data->keyid);
173                 if (PM_UNLIKELY(keyid == NULL)) {
174                         _d_msg(DEBUG_ERR, "strdup failed\n");
175                         free(pkgid);
176                         return RPM_INSTALLER_ERR_WRONG_PARAM;
177                 }
178         }
179         switch (data->req_cmd) {
180         case INSTALL_CMD:
181                 _d_msg(DEBUG_INFO, "rpm-backend -i %s\n", pkgid);
182                 ret = _rpm_backend_interface(keyid, pkgid, "install");
183                 break;
184         case DELETE_CMD:
185                 _d_msg(DEBUG_INFO, "rpm-backend -d %s\n", pkgid);
186                 ret = _rpm_backend_interface(keyid, pkgid, "remove");
187                 break;
188         case CLEARDATA_CMD:
189                 _d_msg(DEBUG_INFO, "rpm-backend -c %s\n", pkgid);
190                 ret = _rpm_backend_interface(keyid, pkgid, "cleardata");
191                 break;
192         case MOVE_CMD:
193                 _d_msg(DEBUG_INFO, "rpm-backend -m %s -t %d\n", pkgid, data->move_type);
194                 move_type = data->move_type;
195                 ret = _rpm_backend_interface(keyid, pkgid, "move");
196                 break;
197         case RECOVER_CMD:
198                 _d_msg(DEBUG_INFO, "rpm-backend -r \n");
199                 ret = _rpm_backend_interface(keyid, pkgid, "recover");
200                 break;
201         default:
202                 _d_msg(DEBUG_ERR,
203                        "Error Never Come Here as Error is already checked\n");
204
205         }
206         if (keyid) {
207                 free(keyid);
208                 keyid = NULL;
209         }
210         if (pkgid) {
211                 free(pkgid);
212                 pkgid = NULL;
213         }
214
215         return ret;
216 }
217
218 void _ri_stat_cb(const char *pkgid, const char *key, const char *val)
219 {
220
221         if (NULL == pkgid || NULL == key || NULL == val) {
222                 _d_msg(DEBUG_ERR, "Either pkgid/key/val is NULL\n");
223                 return;         /*TODO: handle error. */
224         }
225
226         char *pkgid_modified = NULL;
227         char delims[] = "/";
228         char *result = NULL;
229         char *pkgid_tmp = NULL;
230         char *saveptr = NULL;
231
232         pkgid_modified = (char *)malloc(strlen(pkgid) + 1);
233         if (pkgid_modified == NULL) {
234                 _d_msg(DEBUG_ERR, "pkgid_modified is NULL. Malloc failed\n");
235                 return;
236         }
237         memset(pkgid_modified, '\0', strlen(pkgid) + 1);
238         memcpy(pkgid_modified, pkgid, strlen(pkgid));
239
240         result = strtok_r(pkgid_modified, delims, &saveptr);
241         while (result != NULL) {
242                 pkgid_tmp = result;
243                 result = strtok_r(NULL, delims, &saveptr);
244         }
245
246         if (strcmp(key, "install_percent") == 0) {
247                 return;
248         } else if (strcmp(key, "error") == 0) {
249                 /* Store the error to be display to the user */
250                 front_data.error = strdup(val);
251         } else if (strcmp(key, "end") == 0) {
252
253                 char requesttype[32];
254                 switch (front_data.args->req_cmd) {
255                 case INSTALL_CMD:
256                         snprintf(requesttype, sizeof(requesttype),
257                                 _("Installation"));
258                         break;
259                 case DELETE_CMD:
260                         snprintf(requesttype, sizeof(requesttype), _("Deletion"));
261                         break;
262                 case CLEARDATA_CMD:
263                         snprintf(requesttype, sizeof(requesttype),
264                                  _("Clear Data"));
265                         break;
266                 case MOVE_CMD:
267                         snprintf(requesttype, sizeof(requesttype),
268                                  _("Move"));
269                         break;
270                 default:
271                         snprintf(requesttype, sizeof(requesttype), _("Recovery"));
272                         break;
273                 }
274
275                 if (front_data.error) {
276                         /* Error Happened */
277                         snprintf(scrolllabel, sizeof(scrolllabel),
278                                  "%s :: %s:: %s:: %s", requesttype, pkgid_tmp,
279                                  dgettext("sys_string", "IDS_COM_POP_ERROR"),
280                                  front_data.error);
281                         _d_msg(DEBUG_ERR, "%s\n", scrolllabel);
282                         ret_val = _ri_string_to_error_no(front_data.error);
283                         _d_msg(DEBUG_ERR, "%d\n", ret_val);
284
285                 } else {
286                         snprintf(scrolllabel, sizeof(scrolllabel),
287                                  " %s :: %s :: %s", requesttype, pkgid_tmp,
288                                  dgettext("sys_string", "IDS_COM_POP_SUCCESS"));
289                         _d_msg(DEBUG_INFO, "%s\n", scrolllabel);
290                         ret_val = 0;
291                 }
292         }
293 }
294
295 int _ri_cmdline_process(ri_frontend_data *data)
296 {
297         char *cookie = NULL;
298         int cookie_size = 0;
299         int cookie_ret = 0;
300
301         int ret = 0;
302         ri_frontend_cmdline_arg *fdata = data->args;
303
304         cookie_size = security_server_get_cookie_size();
305         /* If security server is down or some other
306            error occured, raise failure */
307         if (0 >= cookie_size) {
308                 /* TODO: raise error */
309                 _d_msg(DEBUG_ERR,
310                        "security_server_get_cookie_size: Security server down \n");
311         } else {
312                 cookie = calloc(cookie_size, sizeof(char));
313                 cookie_ret =
314                     security_server_request_cookie(cookie, cookie_size);
315                 /* TODO: Check cookie_ret...
316                    (See security-server.h to check return code) */
317         }
318
319         if (cookie != NULL)
320                 _d_msg(DEBUG_INFO, "Got Cookie with size = %d\n", cookie_size);
321
322         data->security_cookie = cookie;
323
324         ret = __ri_process_request(fdata);
325         if (ret != RPM_INSTALLER_SUCCESS) {
326                 _d_msg(DEBUG_ERR, "__ri_process_request: Error\n");
327                 goto RETURN;
328         }
329
330         return RPM_INSTALLER_SUCCESS;
331
332  RETURN:
333
334         if (data->security_cookie) {
335                 free(data->security_cookie);
336                 data->security_cookie = NULL;
337         }
338
339         return ret;
340 }
341
342 int _ri_cmdline_destroy(ri_frontend_data *data)
343 {
344         if (data == NULL)
345                 return 0;
346
347         if (data->security_cookie)
348                 free(data->security_cookie);
349
350         return 0;
351
352 }