upload tizen1.0 source
[pkgs/n/native-installer.git] / frontend / src / frontendcmdline.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
24
25
26   
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <getopt.h>
30 #include <stdio.h>
31 #include <appcore-efl.h>
32 #include "frontend.h"
33
34 #include "native_installer_util.h"
35 #include "nativeinstaller.h"
36 #include<pkgmgr_installer.h>
37 #include <security-server.h>
38
39 #define _FIX_POP_UP_
40 extern struct appdata ad;
41 extern pkgmgr_installer *pi;
42 struct frontenddata frontdata;
43 char scrolllabel[256];
44 int quiet;
45
46 static void __show_usage(char **arg);
47 static int __process_request(struct frontendcmdlinearg *pardata);
48
49
50 extern int main_of_backend(char *keyid, char *pkgname, char *reqcommand);
51
52 static void __show_usage(char **arg)
53 {
54         int i = 0;
55         char buffer[256];
56         char tBuff[256] = "";
57         while (arg[i] != NULL) {
58                 snprintf(buffer, 256, "%s %s", tBuff, arg[i]);
59                 strncpy(tBuff, buffer, 255);
60                 i++;
61         }
62
63         d_msg_frontend(DEBUG_INFO, "%s\n", buffer);
64         d_msg_frontend(DEBUG_INFO,
65                        "\nnative-backend usage\n   native-backend -k <keyid>  <command> <pkg_name | pkg_path> [-q] \n\n");
66         d_msg_frontend(DEBUG_INFO, "<Commands> \n");
67         d_msg_frontend(DEBUG_INFO,
68                 "\t -i <PackageFilePath>          : install package file \n");
69         d_msg_frontend(DEBUG_INFO,
70                 "\t -k <keyid>                  : key id file \n");
71         d_msg_frontend(DEBUG_INFO,
72                 "\t -r : (recover). Must ignore specific package name or path \n");
73         d_msg_frontend(DEBUG_INFO,
74                 "\t -d <PackageName>            : delete a package with Package Name \n");
75         d_msg_frontend(DEBUG_INFO,
76                 "\t -q : (quite) run in background without any user interaction \n");
77 }
78
79 int _parse_cmdline(int argc, char **argv, struct frontendcmdlinearg *pardata)
80 {
81         int reqcommand = INVALID_CMD;
82         const char *ppkgname = NULL;
83         int quiteopt = 0;
84         const char *pkeyid = NULL;
85         int ret = 0;
86         /*const char *licensePath = NULL;
87         FILE *fd = NULL;
88         char licenseBuf[1024*4] = {'\0'};
89         unsigned int licenseBuflen = 0;
90         char decLicenseBuf[1024*4] = {'\0'};
91         unsigned int decLicenseBuflen = 0;
92         char decPackageName[256] = {'\0'};
93         char temp[256] = {'\0'};
94         char *ptr = NULL;*/
95         pi = pkgmgr_installer_new();
96         if (!pi) {
97                 d_msg_frontend(DEBUG_ERR,
98                                "Failure in creating the pkgmgr_installer object \n");
99                 return NATIVEINSTALLER_ERR_WRONG_PARAM;
100         }
101         ret = pkgmgr_installer_receive_request(pi, argc, argv);
102         if (ret) {
103                 d_msg_frontend(DEBUG_ERR,
104                                "pkgmgr_installer_receive_request failed \n");
105                 return NATIVEINSTALLER_ERR_WRONG_PARAM;
106         }
107         ret = pkgmgr_installer_get_request_type(pi);
108         switch (ret) {
109         case PKGMGR_REQ_INSTALL:
110                 reqcommand = INSTALL_CMD;
111                 break;
112         case PKGMGR_REQ_UNINSTALL:
113                 reqcommand = DELETE_CMD;
114                 break;
115         case PKGMGR_REQ_RECOVER:
116                 reqcommand = RECOVER_CMD;
117                 break;
118         case PKGMGR_REQ_CLEAR:
119                 reqcommand = CLEAR_CMD;
120                 break;
121         case PKGMGR_REQ_PERM:
122                 goto PARSEERROR;
123         case PKGMGR_REQ_INVALID:
124                 reqcommand = INVALID_CMD;
125                 goto PARSEERROR;
126         default:
127                 goto PARSEERROR;
128         }
129         if (reqcommand != RECOVER_CMD) {
130                 ppkgname = pkgmgr_installer_get_request_info(pi);
131                 if (!ppkgname) {
132                         d_msg_frontend(DEBUG_ERR,
133                                        "pkgmgr_installer_get_request_info failed \n");
134                         return NATIVEINSTALLER_ERR_WRONG_PARAM;
135                 }
136                 pkeyid = pkgmgr_installer_get_session_id(pi);
137                 if (!pkeyid) {
138                         d_msg_frontend(DEBUG_ERR,
139                                        "pkgmgr_installer_get_session_id failed \n");
140                         return NATIVEINSTALLER_ERR_WRONG_PARAM;
141                 }
142
143                 quiteopt = pkgmgr_installer_is_quiet(pi);
144                 if (quiteopt != 0 && quiteopt != 1) {
145                         d_msg_frontend(DEBUG_ERR,
146                                        "pkgmgr_installer_is_quiet failed \n");
147                         return NATIVEINSTALLER_ERR_WRONG_PARAM;
148                 }
149         }
150         quiet = quiteopt;
151         if (reqcommand != INSTALL_CMD && reqcommand != DELETE_CMD
152             && reqcommand != RECOVER_CMD && reqcommand != CLEAR_CMD) {
153                 d_msg_frontend(DEBUG_ERR, "invalid command \n");
154                 goto PARSEERROR;
155         }
156         
157         pardata->reqcommand = reqcommand;
158         pardata->ppkgname = (char *)ppkgname;
159         pardata->quiteoptions = quiteopt;
160         pardata->pkeyid = (char *)pkeyid;
161         return NATIVEINSTALLER_SUCCESS;
162
163  PARSEERROR:
164         d_msg_frontend(DEBUG_ERR, "Error in parsing input parameter\n");
165         __show_usage(argv);
166         return NATIVEINSTALLER_ERR_WRONG_PARAM;
167
168 }
169
170 static int __process_request(struct frontendcmdlinearg *pardata)
171 {
172         int ret = 0;
173         if (!pardata)
174                 return NATIVEINSTALLER_ERR_WRONG_PARAM;
175         char *pkgname = NULL;
176         char *keyid = NULL;
177         if (pardata->reqcommand != RECOVER_CMD) {
178                 pkgname = strdup(pardata->ppkgname);
179                 if (PM_UNLIKELY(pkgname == NULL)) {
180                         d_msg_frontend(DEBUG_ERR, "strdup failed\n");
181                         return NATIVEINSTALLER_ERR_WRONG_PARAM;
182                 }
183                 keyid = strdup(pardata->pkeyid);
184                 if (PM_UNLIKELY(keyid == NULL)) {
185                         d_msg_frontend(DEBUG_ERR, "strdup failed\n");
186                         free(pkgname);
187                         return NATIVEINSTALLER_ERR_WRONG_PARAM;
188                 }
189         }
190         switch (pardata->reqcommand) {
191         case INSTALL_CMD:
192                 d_msg_frontend(DEBUG_INFO, "native-backend -i %s\n", pkgname);
193                 ret = main_of_backend(keyid, pkgname, "install");
194                 break;
195         case DELETE_CMD:
196                 d_msg_frontend(DEBUG_INFO, "native-backend -d %s\n", pkgname);
197                 ret = main_of_backend(keyid, pkgname, "remove");
198                 break;
199         case RECOVER_CMD:
200                 d_msg_frontend(DEBUG_INFO, "native-backend -r \n");
201                 ret = main_of_backend(keyid, pkgname, "recover");
202                 break;
203         case CLEAR_CMD:
204                 d_msg_frontend(DEBUG_INFO, "native-backend -c %s\n", pkgname);
205                 ret = main_of_backend(keyid, pkgname, "clear");
206                 break;
207         default:
208                 d_msg_frontend(DEBUG_ERR,
209                                "Error Never Come Here as Error is already checked\n");
210
211         }
212         if (keyid) {
213                 free(keyid);
214                 keyid = NULL;
215         }
216         if (pkgname) {
217                 free(pkgname);
218                 pkgname = NULL;
219         }
220
221         return ret;
222 }
223
224 void stat_cb(const char *pkg_name, const char *key, const char *val)
225 {
226
227 #ifdef _FIX_POP_UP_
228
229         if (NULL == pkg_name || NULL == key || NULL == val) {
230                 d_msg_frontend(DEBUG_ERR, "Either pkg_name/key/val is NULL\n");
231                 return;         /*TODO: handle error. */
232         }
233
234         char *pkg_name_modified = NULL;
235         char delims[] = "/";
236         char *result = NULL;
237         char *pkgname = NULL;
238         char *saveptr = NULL;
239
240         pkg_name_modified = (char *)malloc(strlen(pkg_name) + 1);
241         if (pkg_name_modified == NULL) {
242                 d_msg_frontend(DEBUG_ERR,
243                                "pkg_name_modified is NULL. Malloc failed\n");
244                 return;
245         }
246         memset(pkg_name_modified, '\0', strlen(pkg_name) + 1);
247         memcpy(pkg_name_modified, pkg_name, strlen(pkg_name));
248
249         result = strtok_r(pkg_name_modified, delims, &saveptr);
250         while (result != NULL) {
251                 pkgname = result;
252                 result = strtok_r(NULL, delims, &saveptr);
253         }
254
255 #else
256         char *pkgname = pkg_name;
257 #endif
258
259         if (strcmp(key, "install_percent") == 0) {
260                 return;
261         } else if (strcmp(key, "error") == 0) {
262                 /* Store the error to be display to the user */
263                 frontdata.perror_string = strdup(val);
264         } else if (strcmp(key, "end") == 0) {
265
266                 char requesttype[32];
267                 switch (frontdata.cmdlineargs->reqcommand) {
268                 case INSTALL_CMD:
269                         snprintf(requesttype, sizeof(requesttype), _("Installation"));
270                         break;
271                 case DELETE_CMD:
272                         snprintf(requesttype, sizeof(requesttype), _("Deletion"));
273                         break;
274                 case CLEAR_CMD:
275                         snprintf(requesttype, sizeof(requesttype), _("Clear Data"));
276                         break;
277                 default:
278                         snprintf(requesttype, sizeof(requesttype), _("Recovery"));
279                         break;
280                 }
281
282                 if (frontdata.perror_string) {
283                         /* Error Happened */
284                         snprintf(scrolllabel, sizeof(scrolllabel), "%s :: %s:: %s:: %s",
285                                 requesttype, pkgname,
286                                 dgettext("sys_string", "IDS_COM_POP_ERROR"),
287                                 frontdata.perror_string);
288                         d_msg_frontend(DEBUG_ERR, "%s\n", scrolllabel);
289                         greturn_main_value =
290                             _string_to_error_no(frontdata.perror_string);
291                         d_msg_frontend(DEBUG_ERR, "%d\n", greturn_main_value);
292                         /*gReturnMainValue = -1; */
293                 } else {
294                         snprintf(scrolllabel, sizeof(scrolllabel), " %s :: %s :: %s",
295                                 requesttype, pkgname,
296                                 dgettext("sys_string", "IDS_COM_POP_SUCCESS"));
297                         d_msg_frontend(DEBUG_INFO, "%s\n", scrolllabel);
298                         greturn_main_value = 0;
299                 }
300
301                 
302         }
303
304 }
305
306 int _cmdline_process(struct frontenddata *data)
307 {
308         char *cookie = NULL;
309         int cookie_size = 0;
310         int cookie_ret = 0;
311
312         int ret = 0;
313         struct frontendcmdlinearg *pardata = data->cmdlineargs;
314
315         cookie_size = security_server_get_cookie_size();
316         /* If security server is down or some other
317         error occured, raise failure */
318         if (0 >= cookie_size) {
319                 /* TODO: raise error */
320                 d_msg_frontend(DEBUG_ERR,
321                                "security_server_get_cookie_size: Security server down \n");
322         } else {
323                 cookie = calloc(cookie_size, sizeof(char));
324                 if(cookie == NULL)
325                         goto RETURN;
326                 
327                 cookie_ret =
328                     security_server_request_cookie(cookie, cookie_size);
329                 /* TODO: Check cookie_ret...
330                 (See security-server.h to check return code) */
331         }
332
333         if (cookie != NULL)
334                 d_msg_frontend(DEBUG_INFO, "Frontend Cookies is generated - size[%d]\n", cookie_size);
335
336         data->gptr_security_cookie = cookie;
337
338         ret = __process_request(pardata);
339         if (ret != NATIVEINSTALLER_SUCCESS) {
340                 d_msg_frontend(DEBUG_ERR, "__process_request: Error\n");
341                 goto RETURN;
342         }
343
344         return NATIVEINSTALLER_SUCCESS;
345
346  RETURN:
347
348         if (data->gptr_security_cookie) {
349                 free(data->gptr_security_cookie);
350                 data->gptr_security_cookie = NULL;
351         }
352
353         return ret;
354 }
355
356 int _cmdline_destroy(struct frontenddata *data)
357 {
358         if (data == NULL)
359                 return 0;
360
361         if (data->gptr_security_cookie)
362                 free(data->gptr_security_cookie);
363
364         return 0;
365
366 }
367