Remove unused variable
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_deprecated.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <ctype.h>
19 #include <dlfcn.h>
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <glib.h>
28 #include <libxml/parser.h>
29 #include <libxml/xmlreader.h>
30 #include <libxml/xmlschemas.h>
31
32 #include <tzplatform_config.h>
33
34 #include "pkgmgrinfo_basic.h"
35 #include "pkgmgr-info.h"
36 #include "pkgmgr_parser.h"
37 #include "pkgmgr_parser_db.h"
38 #include "pkgmgr_parser_debug.h"
39 #include "pkgmgr_parser_internal.h"
40
41 #define ASCII(s) (char *)s
42 #define XMLCHAR(s) (const xmlChar *)s
43
44 #define METADATA_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/metadata/mdparser_list.txt"
45 #define CATEGORY_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/category/category_parser_list.txt"
46 #define TAG_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/tag_parser_list.txt"
47
48 #define PLUGIN_PATH_PREFIX SYSCONFDIR "/package-manager/parserlib/"
49
50 #define OWNER_ROOT 0
51 #define BUFSIZE 4096
52
53 /* plugin process_type */
54 typedef enum {
55         PLUGIN_PRE_PROCESS = 0,
56         PLUGIN_POST_PROCESS
57 } PLUGIN_PROCESS_TYPE;
58
59 const char *package;
60
61 static void __save_xml_attribute(xmlTextReaderPtr reader, char *attribute, char **xml_attribute, char *default_value)
62 {
63         xmlChar *attrib_val = xmlTextReaderGetAttribute(reader, XMLCHAR(attribute));
64         if (attrib_val) {
65                 *xml_attribute = strdup((const char *)attrib_val);
66                 xmlFree(attrib_val);
67         } else {
68                 if (default_value != NULL)
69                         *xml_attribute = strdup(default_value);
70         }
71 }
72
73 static void __save_xml_value(xmlTextReaderPtr reader, char **xml_attribute)
74 {
75         int ret;
76         const xmlChar *attrib_val;
77
78         ret = xmlTextReaderRead(reader);
79         if (ret == -1)
80                 return;
81         attrib_val = xmlTextReaderConstValue(reader);
82
83         if (attrib_val)
84                 *xml_attribute = strdup((const char *)attrib_val);
85 }
86
87 static void __save_xml_lang(xmlTextReaderPtr reader, char **xml_attribute)
88 {
89         const xmlChar *attrib_val = xmlTextReaderConstXmlLang(reader);
90         if (attrib_val != NULL)
91                 *xml_attribute = strdup(ASCII(attrib_val));
92         else
93                 *xml_attribute = strdup(DEFAULT_LOCALE);
94 }
95
96 static void __save_xml_installed_time(manifest_x *mfx)
97 {
98         char buf[PKG_STRING_LEN_MAX] = {'\0'};
99         char *val = NULL;
100         time_t current_time;
101         time(&current_time);
102         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", (int)current_time);
103         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
104         mfx->installed_time = val;
105 }
106
107 static void __save_xml_root_path(manifest_x *mfx, uid_t uid)
108 {
109         char root[PKG_STRING_LEN_MAX] = { '\0' };
110         const char *path;
111
112         if (mfx->root_path)
113                 return;
114
115         tzplatform_set_user(uid);
116         path = tzplatform_getenv((uid == OWNER_ROOT || uid == GLOBAL_USER) ? TZ_SYS_RO_APP : TZ_USER_APP);
117         snprintf(root, PKG_STRING_LEN_MAX - 1, "%s/%s", path, mfx->package);
118
119         mfx->root_path = strdup(root);
120
121         tzplatform_reset_user();
122 }
123
124 static void __save_xml_default_value(manifest_x * mfx)
125 {
126         mfx->preload = strdup("False");
127         mfx->removable = strdup("True");
128         mfx->readonly = strdup("False");
129         mfx->update = strdup("False");
130         mfx->system = strdup("False");
131         mfx->installed_storage = strdup("installed_internal");
132         package = mfx->package;
133 }
134
135 static int __next_child_element(xmlTextReaderPtr reader, int depth)
136 {
137         int ret = xmlTextReaderRead(reader);
138         int cur = xmlTextReaderDepth(reader);
139         while (ret == 1) {
140
141                 switch (xmlTextReaderNodeType(reader)) {
142                 case XML_READER_TYPE_ELEMENT:
143                         if (cur == depth + 1)
144                                 return 1;
145                         break;
146                 case XML_READER_TYPE_TEXT:
147                         /*text is handled by each function separately*/
148                         if (cur == depth + 1)
149                                 return 0;
150                         break;
151                 case XML_READER_TYPE_END_ELEMENT:
152                         if (cur == depth)
153                                 return 0;
154                         break;
155                 default:
156                         if (cur <= depth)
157                                 return 0;
158                         break;
159                 }
160                 ret = xmlTextReaderRead(reader);
161                 cur = xmlTextReaderDepth(reader);
162         }
163         return ret;
164 }
165
166 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
167 {
168         __save_xml_attribute(reader, "name", &label->name, NULL);
169         __save_xml_lang(reader, &label->lang);
170         __save_xml_value(reader, &label->text);
171         return 0;
172
173 }
174
175 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
176 {
177         __save_xml_attribute(reader, "email", &author->email, NULL);
178         __save_xml_attribute(reader, "href", &author->href, NULL);
179         __save_xml_value(reader, &author->text);
180         return 0;
181 }
182
183 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
184 {
185         __save_xml_lang(reader, &description->lang);
186         __save_xml_value(reader, &description->text);
187         return 0;
188 }
189
190 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
191 {
192         __save_xml_lang(reader, &license->lang);
193         __save_xml_value(reader, &license->text);
194         return 0;
195 }
196
197 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege)
198 {
199         __save_xml_attribute(reader, "type", &privilege->type, "tpk");
200         __save_xml_value(reader, &privilege->value);
201         return 0;
202 }
203 static int __ps_process_privileges(xmlTextReaderPtr reader, GList **privileges)
204 {
205         const xmlChar *node;
206         int ret = -1;
207         int depth = -1;
208
209         depth = xmlTextReaderDepth(reader);
210         while ((ret = __next_child_element(reader, depth))) {
211                 node = xmlTextReaderConstName(reader);
212                 if (!node) {
213                         _LOGD("xmlTextReaderConstName value is NULL\n");
214                         return -1;
215                 }
216
217                 if (strcmp(ASCII(node), "privilege") == 0) {
218                         privilege_x *privilege = calloc(1, sizeof(privilege_x));
219                         if (privilege == NULL) {
220                                 _LOGD("Malloc Failed\n");
221                                 return -1;
222                         }
223                         ret = __ps_process_privilege(reader, privilege);
224                         if (ret < 0)
225                                 free(privilege);
226                         else
227                                 *privileges = g_list_append(*privileges, (gpointer)privilege);
228                 } else {
229                         return -1;
230                 }
231                 if (ret < 0) {
232                         _LOGD("Processing privileges failed\n");
233                         return ret;
234                 }
235         }
236         return ret;
237 }
238
239 static char *__get_icon_with_path(const char *icon, uid_t uid)
240 {
241         char icon_with_path[BUFSIZE];
242         const char *app_path;
243
244         if (!icon || !package)
245                 return NULL;
246
247         /* just use absolute path */
248         if (index(icon, '/'))
249                 return strdup(icon);
250
251         do {
252                 if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
253                         snprintf(icon_with_path, sizeof(icon_with_path),
254                                 "%s%s", getIconPath(uid, true), icon);
255                         if (access(icon_with_path, F_OK) == 0)
256                                 break;
257
258                         snprintf(icon_with_path, sizeof(icon_with_path),
259                                 "%s%s", getIconPath(uid, false), icon);
260                         if (access(icon_with_path, F_OK) == 0)
261                                 break;
262
263                         /* for backward compatibility (.../default/small/...)
264                          * this should be removed
265                          */
266                         snprintf(icon_with_path, sizeof(icon_with_path),
267                                 "%sdefault/small/%s",
268                                 getIconPath(uid, true), icon);
269                         if (access(icon_with_path, F_OK) == 0)
270                                 break;
271
272                         snprintf(icon_with_path, sizeof(icon_with_path),
273                                 "%sdefault/small/%s",
274                                 getIconPath(uid, false), icon);
275                         if (access(icon_with_path, F_OK) == 0)
276                                 break;
277
278                         /* If doesn't exist in case of Global app,
279                          * try to get icon directly into app's directory
280                          */
281                         app_path = tzplatform_getenv(TZ_SYS_RO_APP);
282
283                         snprintf(icon_with_path, sizeof(icon_with_path),
284                                 "%s/%s/%s", app_path, package, icon);
285                         if (access(icon_with_path, F_OK) == 0)
286                                 break;
287
288                         app_path = tzplatform_getenv(TZ_SYS_RW_APP);
289
290                         snprintf(icon_with_path, sizeof(icon_with_path),
291                                 "%s/%s/%s", app_path, package, icon);
292                         if (access(icon_with_path, F_OK) == 0)
293                                 break;
294                 } else {
295                         tzplatform_set_user(uid);
296                         app_path = tzplatform_getenv(TZ_USER_APP);
297                         tzplatform_reset_user();
298
299                         snprintf(icon_with_path, sizeof(icon_with_path),
300                                 "%s/%s/%s", app_path, package, icon);
301                         if (access(icon_with_path, F_OK) == 0)
302                                 break;
303                 }
304
305                 /* some preload package has icons at below path */
306                 snprintf(icon_with_path, sizeof(icon_with_path),
307                                 "%s/%s/res/icons/%s", app_path, package, icon);
308                 if (access(icon_with_path, F_OK) == 0)
309                         break;
310
311                 /* since 2.3 tpk package */
312                 snprintf(icon_with_path, sizeof(icon_with_path),
313                                 "%s/%s/shared/res/%s", app_path, package, icon);
314                 if (access(icon_with_path, F_OK) == 0)
315                         break;
316
317                 _LOGE("cannot find icon path for [%s]", icon);
318                 return NULL;
319         } while (0);
320
321         _LOGD("Icon path : %s ---> %s", icon, icon_with_path);
322
323         return strdup(icon_with_path);
324 }
325
326 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid)
327 {
328         int ret;
329         char *text;
330
331         __save_xml_attribute(reader, "section", &icon->section, NULL);
332         __save_xml_attribute(reader, "size", &icon->size, NULL);
333         __save_xml_attribute(reader, "resolution", &icon->resolution, NULL);
334         __save_xml_lang(reader, &icon->lang);
335
336         ret = xmlTextReaderRead(reader);
337         if (ret == -1)
338                 return 0;
339         text  = ASCII(xmlTextReaderValue(reader));
340         if (text) {
341                 icon->text = __get_icon_with_path(text, uid);
342                 xmlFree(text);
343         }
344
345         return 0;
346 }
347
348 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
349 {
350         __save_xml_attribute(reader, "name", &compatibility->name, NULL);
351         __save_xml_value(reader, &compatibility->text);
352         return 0;
353 }
354
355 static int __ps_process_image(xmlTextReaderPtr reader, image_x *image)
356 {
357         __save_xml_attribute(reader, "section", &image->section, NULL);
358         __save_xml_lang(reader, &image->lang);
359         __save_xml_value(reader, &image->text);
360         return 0;
361 }
362
363 static int __ps_process_category(xmlTextReaderPtr reader, char **category)
364 {
365         __save_xml_attribute(reader, "name", category, NULL);
366         return 0;
367 }
368
369 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata)
370 {
371         __save_xml_attribute(reader, "key", &metadata->key, NULL);
372         __save_xml_attribute(reader, "value", &metadata->value, NULL);
373         return 0;
374 }
375
376 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission)
377 {
378         __save_xml_attribute(reader, "type", &permission->type, NULL);
379         __save_xml_value(reader, &permission->value);
380         return 0;
381 }
382
383 struct appcontrol_data {
384         GList *operations;
385         GList *uris;
386         GList *mimes;
387         GList *appcontrols;
388         char operation[BUFSIZE];
389         char uri[BUFSIZE];
390         char mime[BUFSIZE];
391 };
392
393 static void __ps_process_mime(gpointer data, gpointer user_data)
394 {
395         char *mime = (char *)data;
396         struct appcontrol_data *ad = (struct appcontrol_data *)user_data;
397         appcontrol_x *appcontrol;
398
399         snprintf(ad->mime, sizeof(ad->mime), "%s", mime);
400
401         appcontrol = calloc(1, sizeof(appcontrol_x));
402         if (appcontrol == NULL) {
403                 _LOGD("Malloc Failed\n");
404                 return;
405         }
406         if (strlen(ad->operation))
407                 appcontrol->operation = strdup(ad->operation);
408         if (strlen(ad->uri))
409                 appcontrol->uri = strdup(ad->uri);
410         appcontrol->mime = strdup(ad->mime);
411         ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
412 }
413
414 static void __ps_process_uri(gpointer data, gpointer user_data)
415 {
416         char *uri = (char *)data;
417         struct appcontrol_data *ad = (struct appcontrol_data *)user_data;
418         appcontrol_x *appcontrol;
419
420         snprintf(ad->uri, sizeof(ad->uri), "%s", uri);
421
422         if (ad->mimes != NULL) {
423                 g_list_foreach(ad->mimes, __ps_process_mime, user_data);
424         } else {
425                 appcontrol = calloc(1, sizeof(appcontrol_x));
426                 if (appcontrol == NULL) {
427                         _LOGD("Malloc Failed\n");
428                         return;
429                 }
430                 if (strlen(ad->operation))
431                         appcontrol->operation = strdup(ad->operation);
432                 appcontrol->uri = strdup(ad->uri);
433                 ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
434         }
435 }
436
437 static void __ps_process_operation(gpointer data, gpointer user_data)
438 {
439         char *operation = (char *)data;
440         struct appcontrol_data *ad = (struct appcontrol_data *)user_data;
441         appcontrol_x *appcontrol;
442
443         snprintf(ad->operation, sizeof(ad->operation), "%s", operation);
444
445         if (ad->uris != NULL) {
446                 g_list_foreach(ad->uris, __ps_process_uri, user_data);
447         } else if (ad->mimes != NULL) {
448                 g_list_foreach(ad->mimes, __ps_process_mime, user_data);
449         } else {
450                 appcontrol = calloc(1, sizeof(appcontrol_x));
451                 if (appcontrol == NULL) {
452                         _LOGD("Malloc Failed\n");
453                         return;
454                 }
455                 appcontrol->operation = strdup(ad->operation);
456                 ad->appcontrols = g_list_append(ad->appcontrols, appcontrol);
457         }
458 }
459
460 static GList *__make_appcontrol_list(GList *operations, GList *uris, GList *mimes)
461 {
462         struct appcontrol_data ad = {0, };
463
464         ad.operations = operations;
465         ad.uris = uris;
466         ad.mimes = mimes;
467
468         if (ad.operations == NULL)
469                 return NULL;
470
471         g_list_foreach(ad.operations, __ps_process_operation, (gpointer)&ad);
472
473         return ad.appcontrols;
474 }
475
476 static int __ps_process_appcontrol(xmlTextReaderPtr reader, GList **appcontrol)
477 {
478         const xmlChar *node;
479         int ret = -1;
480         int depth = -1;
481         char *val;
482         GList *operations = NULL;
483         GList *uris = NULL;
484         GList *mimes = NULL;
485         GList *result;
486
487         depth = xmlTextReaderDepth(reader);
488         while ((ret = __next_child_element(reader, depth)) > 0) {
489                 node = xmlTextReaderConstName(reader);
490                 if (!node) {
491                         _LOGD("xmlTextReaderConstName value is NULL\n");
492                         g_list_free_full(operations, free);
493                         g_list_free_full(uris, free);
494                         g_list_free_full(mimes, free);
495                         return -1;
496                 }
497
498                 val = NULL;
499                 if (!strcmp(ASCII(node), "operation")) {
500                         __save_xml_attribute(reader, "name", &val, NULL);
501                         if (val)
502                                 operations = g_list_append(operations, (gpointer)val);
503                         _LOGD("operation processing\n");
504                 } else if (!strcmp(ASCII(node), "uri")) {
505                         __save_xml_attribute(reader, "name", &val, NULL);
506                         if (val)
507                                 uris = g_list_append(uris, (gpointer)val);
508                         _LOGD("uri processing\n");
509                 } else if (!strcmp(ASCII(node), "mime")) {
510                         __save_xml_attribute(reader, "name", &val, NULL);
511                         if (val)
512                                 mimes = g_list_append(mimes, (gpointer)val);
513                         _LOGD("mime processing\n");
514                 } else if (!strcmp(ASCII(node), "subapp")) {
515                         continue;
516                 } else {
517                         ret = -1;
518                 }
519         }
520
521         if (ret < 0) {
522                 _LOGD("Processing appcontrol failed\n");
523                 g_list_free_full(operations, free);
524                 g_list_free_full(uris, free);
525                 g_list_free_full(mimes, free);
526                 return ret;
527         }
528
529         result = __make_appcontrol_list(operations, uris, mimes);
530         if (result)
531                 *appcontrol = g_list_concat(*appcontrol, result);
532         else
533                 ret = -1;
534
535         g_list_free_full(operations, free);
536         g_list_free_full(uris, free);
537         g_list_free_full(mimes, free);
538
539         return ret;
540 }
541
542 static int __ps_process_allowed(xmlTextReaderPtr reader, char **allowed)
543 {
544         __save_xml_value(reader, allowed);
545         return 0;
546 }
547
548 static int __ps_process_request(xmlTextReaderPtr reader, char **request)
549 {
550         __save_xml_value(reader, request);
551         return 0;
552 }
553
554 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
555 {
556         const xmlChar *node;
557         int ret = -1;
558         int depth = -1;
559         char *val;
560
561         __save_xml_attribute(reader, "path", &define->path, NULL);
562
563         depth = xmlTextReaderDepth(reader);
564         while ((ret = __next_child_element(reader, depth))) {
565                 node = xmlTextReaderConstName(reader);
566                 if (!node) {
567                         _LOGD("xmlTextReaderConstName value is NULL\n");
568                         return -1;
569                 }
570
571                 if (!strcmp(ASCII(node), "allowed")) {
572                         val = NULL;
573                         ret = __ps_process_allowed(reader, &val);
574                         if (val)
575                                 define->allowed = g_list_append(define->allowed, (gpointer)val);
576                 } else if (!strcmp(ASCII(node), "request")) {
577                         val = NULL;
578                         ret = __ps_process_request(reader, &val);
579                         if (val)
580                                 define->request = g_list_append(define->request, (gpointer)val);
581                 } else {
582                         return -1;
583                 }
584                 if (ret < 0) {
585                         _LOGD("Processing define failed\n");
586                         return ret;
587                 }
588         }
589         return ret;
590 }
591
592 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
593 {
594         const xmlChar *node;
595         int ret = -1;
596         int depth = -1;
597         char *val;
598         depth = xmlTextReaderDepth(reader);
599         while ((ret = __next_child_element(reader, depth))) {
600                 node = xmlTextReaderConstName(reader);
601                 if (!node) {
602                         _LOGD("xmlTextReaderConstName value is NULL\n");
603                         return -1;
604                 }
605
606                 if (!strcmp(ASCII(node), "define")) {
607                         define_x *define = calloc(1, sizeof(define_x));
608                         if (define == NULL) {
609                                 _LOGD("Malloc Failed\n");
610                                 return -1;
611                         }
612                         datashare->define = g_list_append(datashare->define, define);
613                         ret = __ps_process_define(reader, define);
614                 } else if (!strcmp(ASCII(node), "request")) {
615                         val = NULL;
616                         ret = __ps_process_request(reader, &val);
617                         if (val)
618                                 datashare->request = g_list_append(datashare->request, (gpointer)val);
619                 } else
620                         return -1;
621                 if (ret < 0) {
622                         _LOGD("Processing data-share failed\n");
623                         return ret;
624                 }
625         }
626         return ret;
627 }
628
629 static int __ps_process_condition(xmlTextReaderPtr reader, char **condition)
630 {
631         __save_xml_attribute(reader, "name", condition, NULL);
632         return 0;
633 }
634
635 static int __ps_process_launchconditions(xmlTextReaderPtr reader, GList **launchconditions)
636 {
637         const xmlChar *node;
638         int ret = -1;
639         int depth = -1;
640         char *val;
641
642         depth = xmlTextReaderDepth(reader);
643         while ((ret = __next_child_element(reader, depth))) {
644                 node = xmlTextReaderConstName(reader);
645                 if (!node) {
646                         _LOGD("xmlTextReaderConstName value is NULL\n");
647                         return -1;
648                 }
649
650                 if (strcmp(ASCII(node), "condition") == 0) {
651                         val = NULL;
652                         ret = __ps_process_condition(reader, &val);
653                         if (val)
654                                 *launchconditions = g_list_append(*launchconditions, (gpointer)val);
655                 } else
656                         return -1;
657                 if (ret < 0) {
658                         _LOGD("Processing launchconditions failed\n");
659                         return ret;
660                 }
661         }
662
663         return ret;
664 }
665
666 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
667 {
668         __save_xml_attribute(reader, "name", &notification->name, NULL);
669         __save_xml_value(reader, &notification->text);
670         return 0;
671 }
672
673 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
674 {
675         __save_xml_attribute(reader, "providerid", &datacontrol->providerid, NULL);
676         __save_xml_attribute(reader, "access", &datacontrol->access, NULL);
677         __save_xml_attribute(reader, "type", &datacontrol->type, NULL);
678         return 0;
679 }
680
681 static int __ps_process_splashscreen(xmlTextReaderPtr reader, splashscreen_x *splashscreen)
682 {
683         __save_xml_attribute(reader, "src", &splashscreen->src, NULL);
684         __save_xml_attribute(reader, "type", &splashscreen->type, NULL);
685         __save_xml_attribute(reader, "dpi", &splashscreen->dpi, NULL);
686         __save_xml_attribute(reader, "orientation", &splashscreen->orientation, NULL);
687         __save_xml_attribute(reader, "indicator-display", &splashscreen->indicatordisplay, NULL);
688         __save_xml_attribute(reader, "app-control-operation", &splashscreen->operation, NULL);
689         __save_xml_attribute(reader, "color-depth", &splashscreen->color_depth, NULL);
690         return 0;
691 }
692
693 static int __ps_process_splashscreens(xmlTextReaderPtr reader, GList **splashscreens)
694 {
695         const xmlChar *node;
696         int ret = -1;
697         int depth = -1;
698         splashscreen_x *splashscreen;
699
700         depth = xmlTextReaderDepth(reader);
701         while ((ret = __next_child_element(reader, depth))) {
702                 node = xmlTextReaderConstName(reader);
703                 if (!node) {
704                         _LOGD("xmlTextReaderConstName value is NULL\n");
705                         return -1;
706                 }
707
708                 if (strcmp(ASCII(node), "splash-screen") == 0) {
709                         splashscreen = calloc(1, sizeof(splashscreen_x));
710                         if (splashscreen == NULL) {
711                                 _LOGD("Malloc Failed\n");
712                                 return -1;
713                         }
714                         *splashscreens = g_list_append(*splashscreens, splashscreen);
715                         ret = __ps_process_splashscreen(reader, splashscreen);
716                 } else {
717                         return -1;
718                 }
719
720                 if (ret < 0) {
721                         _LOGD("Processing splash-screen failed\n");
722                         return ret;
723                 }
724         }
725         return 0;
726 }
727
728 static int __ps_process_application(xmlTextReaderPtr reader, application_x *application, int type, uid_t uid)
729 {
730         const xmlChar *node;
731         int ret = -1;
732         int depth = -1;
733         char *val;
734
735         __save_xml_attribute(reader, "appid", &application->appid, NULL);
736         retvm_if(application->appid == NULL, PM_PARSER_R_ERROR, "appid cant be NULL, appid field is mandatory\n");
737         __save_xml_attribute(reader, "exec", &application->exec, NULL);
738         __save_xml_attribute(reader, "nodisplay", &application->nodisplay, "false");
739         __save_xml_attribute(reader, "multiple", &application->multiple, "false");
740         __save_xml_attribute(reader, "type", &application->type, NULL);
741         __save_xml_attribute(reader, "categories", &application->categories, NULL);
742         __save_xml_attribute(reader, "extraid", &application->extraid, NULL);
743         __save_xml_attribute(reader, "taskmanage", &application->taskmanage, "true");
744         __save_xml_attribute(reader, "hw-acceleration", &application->hwacceleration, "default");
745         __save_xml_attribute(reader, "screen-reader", &application->screenreader, "use-system-setting");
746         __save_xml_attribute(reader, "mainapp", &application->mainapp, "false");
747         __save_xml_attribute(reader, "recentimage", &application->recentimage, "false");
748         __save_xml_attribute(reader, "launchcondition", &application->launchcondition, "false");
749         __save_xml_attribute(reader, "indicatordisplay", &application->indicatordisplay, "true");
750         __save_xml_attribute(reader, "portrait-effectimage", &application->portraitimg, NULL);
751         __save_xml_attribute(reader, "landscape-effectimage", &application->landscapeimg, NULL);
752         __save_xml_attribute(reader, "guestmode-visibility", &application->guestmode_visibility, "true");
753         __save_xml_attribute(reader, "permission-type", &application->permission_type, "normal");
754         __save_xml_attribute(reader, "component-type", &application->component_type, type == PMINFO_UI_APP ? "uiapp" : type == PMINFO_SVC_APP ? "svcapp" : "widgetapp");
755         /*component_type has "svcapp" or "uiapp", if it is not, parsing manifest is fail*/
756         retvm_if(((strcmp(application->component_type, "svcapp") != 0) && (strcmp(application->component_type, "uiapp") != 0) && (strcmp(application->component_type, "widgetapp") != 0)), PM_PARSER_R_ERROR, "invalid component_type[%s]", application->component_type);
757         __save_xml_attribute(reader, "submode", &application->submode, "false");
758         __save_xml_attribute(reader, "submode-mainid", &application->submode_mainid, NULL);
759         __save_xml_attribute(reader, "process-pool", &application->process_pool, "false");
760         __save_xml_attribute(reader, "launch_mode", &application->launch_mode, "caller");
761         __save_xml_attribute(reader, "ui-gadget", &application->ui_gadget, "false");
762         __save_xml_attribute(reader, "auto-restart", &application->autorestart, "false");
763         __save_xml_attribute(reader, "on-boot", &application->onboot, "false");
764         __save_xml_attribute(reader, "splash-screen-display", &application->splash_screen_display, "true");
765
766         application->package = strdup(package);
767         /* overwrite some attributes if the app is widgetapp */
768         if (type == PMINFO_WIDGET_APP || type == PMINFO_WATCH_APP) {
769                 free((void *)application->nodisplay);
770                 application->nodisplay = strdup("true");
771                 free((void *)application->multiple);
772                 application->multiple = strdup("true");
773                 free((void *)application->type);
774                 application->type = strdup("capp");
775                 free((void *)application->taskmanage);
776                 application->taskmanage = strdup("false");
777                 free((void *)application->indicatordisplay);
778                 application->indicatordisplay = strdup("false");
779         }
780
781         /* hw-acceleration values are changed from use-GL/not-use-GL/use-system-setting to on/off/default */
782         if (strcmp(application->hwacceleration, "use-GL") == 0) {
783                 free((void *)application->hwacceleration);
784                 application->hwacceleration = strdup("on");
785         } else if (strcmp(application->hwacceleration, "not-use-GL") == 0) {
786                 free((void *)application->hwacceleration);
787                 application->hwacceleration = strdup("off");
788         } else if (strcmp(application->hwacceleration, "use-system-setting") == 0) {
789                 free((void *)application->hwacceleration);
790                 application->hwacceleration = strdup("default");
791         }
792
793         depth = xmlTextReaderDepth(reader);
794         while ((ret = __next_child_element(reader, depth))) {
795                 node = xmlTextReaderConstName(reader);
796                 if (!node) {
797                         _LOGD("xmlTextReaderConstName value is NULL\n");
798                         return -1;
799                 }
800                 if (!strcmp(ASCII(node), "label")) {
801                         label_x *label = calloc(1, sizeof(label_x));
802                         if (label == NULL) {
803                                 _LOGD("Malloc Failed\n");
804                                 return -1;
805                         }
806                         application->label = g_list_append(application->label, label);
807                         ret = __ps_process_label(reader, label);
808                 } else if (!strcmp(ASCII(node), "icon")) {
809                         icon_x *icon = calloc(1, sizeof(icon_x));
810                         if (icon == NULL) {
811                                 _LOGD("Malloc Failed\n");
812                                 return -1;
813                         }
814                         application->icon = g_list_append(application->icon, icon);
815                         ret = __ps_process_icon(reader, icon, uid);
816                 } else if (!strcmp(ASCII(node), "image")) {
817                         image_x *image = calloc(1, sizeof(image_x));
818                         if (image == NULL) {
819                                 _LOGD("Malloc Failed\n");
820                                 return -1;
821                         }
822                         application->image = g_list_append(application->image, image);
823                         ret = __ps_process_image(reader, image);
824                 } else if (!strcmp(ASCII(node), "category")) {
825                         val = NULL;
826                         ret = __ps_process_category(reader, &val);
827                         if (val)
828                                 application->category = g_list_append(application->category, (gpointer)val);
829                 } else if (!strcmp(ASCII(node), "metadata")) {
830                         metadata_x *metadata = calloc(1, sizeof(metadata_x));
831                         if (metadata == NULL) {
832                                 _LOGD("Malloc Failed\n");
833                                 return -1;
834                         }
835                         application->metadata = g_list_append(application->metadata, metadata);
836                         ret = __ps_process_metadata(reader, metadata);
837                 } else if (!strcmp(ASCII(node), "permission")) {
838                         permission_x *permission = calloc(1, sizeof(permission_x));
839                         if (permission == NULL) {
840                                 _LOGD("Malloc Failed\n");
841                                 return -1;
842                         }
843                         application->permission = g_list_append(application->permission, permission);
844                         ret = __ps_process_permission(reader, permission);
845                 } else if (!strcmp(ASCII(node), "app-control")) {
846                         ret = __ps_process_appcontrol(reader, &application->appcontrol);
847                 } else if (!strcmp(ASCII(node), "application-service")) {
848                         ret = __ps_process_appcontrol(reader, &application->appcontrol);
849                 } else if (!strcmp(ASCII(node), "data-share")) {
850                         datashare_x *datashare = calloc(1, sizeof(datashare_x));
851                         if (datashare == NULL) {
852                                 _LOGD("Malloc Failed\n");
853                                 return -1;
854                         }
855                         application->datashare = g_list_append(application->datashare, datashare);
856                         ret = __ps_process_datashare(reader, datashare);
857                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
858                         ret = __ps_process_launchconditions(reader, &application->launchconditions);
859                 } else if (!strcmp(ASCII(node), "notification")) {
860                         notification_x *notification = calloc(1, sizeof(notification_x));
861                         if (notification == NULL) {
862                                 _LOGD("Malloc Failed\n");
863                                 return -1;
864                         }
865                         application->notification = g_list_append(application->notification, notification);
866                         ret = __ps_process_notification(reader, notification);
867                 } else if (!strcmp(ASCII(node), "datacontrol")) {
868                         datacontrol_x *datacontrol = calloc(1, sizeof(datacontrol_x));
869                         if (datacontrol == NULL) {
870                                 _LOGD("Malloc Failed\n");
871                                 return -1;
872                         }
873                         application->datacontrol = g_list_append(application->datacontrol, datacontrol);
874                         ret = __ps_process_datacontrol(reader, datacontrol);
875                 } else if (!strcmp(ASCII(node), "splash-screens") == 0) {
876                         ret = __ps_process_splashscreens(reader, &application->splashscreens);
877                 } else
878                         continue;
879                 if (ret < 0) {
880                         _LOGD("Processing application failed\n");
881                         return ret;
882                 }
883         }
884
885         return ret;
886 }
887
888 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid)
889 {
890         _LOGD("__start_process\n");
891         const xmlChar *node;
892         int ret = -1;
893         int depth = -1;
894
895         depth = xmlTextReaderDepth(reader);
896         while ((ret = __next_child_element(reader, depth))) {
897                 node = xmlTextReaderConstName(reader);
898                 if (!node) {
899                         _LOGD("xmlTextReaderConstName value is NULL\n");
900                         return -1;
901                 }
902
903                 if (!strcmp(ASCII(node), "label")) {
904                         label_x *label = calloc(1, sizeof(label_x));
905                         if (label == NULL) {
906                                 _LOGD("Malloc Failed\n");
907                                 return -1;
908                         }
909                         mfx->label = g_list_append(mfx->label, label);
910                         ret = __ps_process_label(reader, label);
911                 } else if (!strcmp(ASCII(node), "author")) {
912                         author_x *author = calloc(1, sizeof(author_x));
913                         if (author == NULL) {
914                                 _LOGD("Malloc Failed\n");
915                                 return -1;
916                         }
917                         mfx->author = g_list_append(mfx->author, author);
918                         ret = __ps_process_author(reader, author);
919                 } else if (!strcmp(ASCII(node), "description")) {
920                         description_x *description = calloc(1, sizeof(description_x));
921                         if (description == NULL) {
922                                 _LOGD("Malloc Failed\n");
923                                 return -1;
924                         }
925                         mfx->description = g_list_append(mfx->description, description);
926                         ret = __ps_process_description(reader, description);
927                 } else if (!strcmp(ASCII(node), "license")) {
928                         license_x *license = calloc(1, sizeof(license_x));
929                         if (license == NULL) {
930                                 _LOGD("Malloc Failed\n");
931                                 return -1;
932                         }
933                         mfx->license = g_list_append(mfx->license, license);
934                         ret = __ps_process_license(reader, license);
935                 } else if (!strcmp(ASCII(node), "privileges")) {
936                         ret = __ps_process_privileges(reader, &mfx->privileges);
937                 } else if (!strcmp(ASCII(node), "ui-application")) {
938                         application_x *application = calloc(1, sizeof(application_x));
939                         if (application == NULL) {
940                                 _LOGD("Malloc Failed\n");
941                                 return -1;
942                         }
943                         mfx->application = g_list_append(mfx->application, application);
944                         ret = __ps_process_application(reader, application, PMINFO_UI_APP, uid);
945                 } else if (!strcmp(ASCII(node), "service-application")) {
946                         application_x *application = calloc(1, sizeof(application_x));
947                         if (application == NULL) {
948                                 _LOGD("Malloc Failed\n");
949                                 return -1;
950                         }
951                         mfx->application = g_list_append(mfx->application, application);
952                         ret = __ps_process_application(reader, application, PMINFO_SVC_APP, uid);
953                 } else if (!strcmp(ASCII(node), "widget-application")) {
954                         application_x *application = calloc(1, sizeof(application_x));
955                         if (application == NULL) {
956                                 _LOGD("Malloc Failed\n");
957                                 return -1;
958                         }
959                         mfx->application = g_list_append(mfx->application, application);
960                         ret = __ps_process_application(reader, application, PMINFO_WIDGET_APP, uid);
961                 } else if (!strcmp(ASCII(node), "watch-application")) {
962                         application_x *application = calloc(1, sizeof(application_x));
963                         if (application == NULL) {
964                                 _LOGD("Malloc Failed\n");
965                                 return -1;
966                         }
967                         mfx->application = g_list_append(mfx->application, application);
968                         ret = __ps_process_application(reader, application, PMINFO_WATCH_APP, uid);
969                 } else if (!strcmp(ASCII(node), "icon")) {
970                         icon_x *icon = calloc(1, sizeof(icon_x));
971                         if (icon == NULL) {
972                                 _LOGD("Malloc Failed\n");
973                                 return -1;
974                         }
975                         mfx->icon = g_list_append(mfx->icon, icon);
976                         ret = __ps_process_icon(reader, icon, uid);
977                 } else if (!strcmp(ASCII(node), "compatibility")) {
978                         compatibility_x *compatibility = calloc(1, sizeof(compatibility_x));
979                         if (compatibility == NULL) {
980                                 _LOGD("Malloc Failed\n");
981                                 return -1;
982                         }
983                         mfx->compatibility = g_list_append(mfx->compatibility, compatibility);
984                         ret = __ps_process_compatibility(reader, compatibility);
985                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
986                         continue;
987                 } else if (!strcmp(ASCII(node), "livebox")) {
988                         continue;
989                 } else if (!strcmp(ASCII(node), "account")) {
990                         continue;
991                 } else if (!strcmp(ASCII(node), "notifications")) {
992                         continue;
993                 } else if (!strcmp(ASCII(node), "ime")) {
994                         continue;
995                 } else if (!strcmp(ASCII(node), "feature")) {
996                         continue;
997                 } else {
998                         _LOGI("Unknown element: %s", ASCII(node));
999                         continue;
1000                 }
1001
1002                 if (ret < 0) {
1003                         _LOGD("Processing manifest failed\n");
1004                         return ret;
1005                 }
1006         }
1007         return ret;
1008 }
1009
1010 static int __process_manifest(xmlTextReaderPtr reader, manifest_x *mfx, uid_t uid)
1011 {
1012         const xmlChar *node;
1013         int ret = -1;
1014
1015         if ((ret = __next_child_element(reader, -1))) {
1016                 node = xmlTextReaderConstName(reader);
1017                 if (!node) {
1018                         _LOGD("xmlTextReaderConstName value is NULL\n");
1019                         return -1;
1020                 }
1021
1022                 if (!strcmp(ASCII(node), "manifest")) {
1023                         __save_xml_attribute(reader, "xmlns", &mfx->ns, NULL);
1024                         __save_xml_attribute(reader, "package", &mfx->package, NULL);
1025                         retvm_if(mfx->package == NULL, PM_PARSER_R_ERROR, "package cant be NULL, package field is mandatory\n");
1026                         __save_xml_attribute(reader, "version", &mfx->version, NULL);
1027                         __save_xml_attribute(reader, "size", &mfx->package_size, NULL);
1028                         __save_xml_attribute(reader, "install-location", &mfx->installlocation, "internal-only");
1029                         __save_xml_attribute(reader, "type", &mfx->type, "tpk");
1030                         __save_xml_attribute(reader, "root_path", &mfx->root_path, NULL);
1031                         __save_xml_attribute(reader, "csc_path", &mfx->csc_path, NULL);
1032                         __save_xml_attribute(reader, "appsetting", &mfx->appsetting, "false");
1033                         __save_xml_attribute(reader, "storeclient-id", &mfx->storeclient_id, NULL);
1034                         __save_xml_attribute(reader, "nodisplay-setting", &mfx->nodisplay_setting, "false");
1035                         __save_xml_attribute(reader, "url", &mfx->package_url, NULL);
1036                         __save_xml_attribute(reader, "api-version", &mfx->api_version, NULL);
1037                         __save_xml_attribute(reader, "support-disable", &mfx->support_disable, "false");
1038
1039                         __save_xml_installed_time(mfx);
1040                         __save_xml_root_path(mfx, uid);
1041                         /*Assign default values. If required it will be overwritten in __add_preload_info()*/
1042                         __save_xml_default_value(mfx);
1043
1044                         ret = __start_process(reader, mfx, uid);
1045                 } else {
1046                         _LOGD("No Manifest element found\n");
1047                         return -1;
1048                 }
1049         }
1050         return ret;
1051 }
1052
1053 DEPRECATED API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
1054 {
1055         _LOGD("parsing start pkgmgr_parser_process_manifest_xml\n");
1056         xmlTextReaderPtr reader;
1057         manifest_x *mfx = NULL;
1058
1059         reader = xmlReaderForFile(manifest, NULL, 0);
1060         if (reader) {
1061                 mfx = malloc(sizeof(manifest_x));
1062                 if (mfx) {
1063                         memset(mfx, '\0', sizeof(manifest_x));
1064                         if (__process_manifest(reader, mfx, GLOBAL_USER) < 0) {
1065                                 _LOGD("Parsing Failed\n");
1066                                 pkgmgr_parser_free_manifest_xml(mfx);
1067                                 mfx = NULL;
1068                         } else
1069                                 _LOGD("Parsing Success\n");
1070                 } else {
1071                         _LOGD("Memory allocation error\n");
1072                 }
1073                 xmlFreeTextReader(reader);
1074         } else {
1075                 _LOGD("Unable to create xml reader\n");
1076         }
1077         return mfx;
1078 }
1079
1080 DEPRECATED API manifest_x *pkgmgr_parser_usr_process_manifest_xml(const char *manifest, uid_t uid)
1081 {
1082         _LOGD("parsing start pkgmgr_parser_usr_process_manifest_xml\n");
1083         xmlTextReaderPtr reader;
1084         manifest_x *mfx = NULL;
1085
1086         reader = xmlReaderForFile(manifest, NULL, 0);
1087         if (reader) {
1088                 mfx = malloc(sizeof(manifest_x));
1089                 if (mfx) {
1090                         memset(mfx, '\0', sizeof(manifest_x));
1091                         if (__process_manifest(reader, mfx, uid) < 0) {
1092                                 _LOGD("Parsing Failed\n");
1093                                 pkgmgr_parser_free_manifest_xml(mfx);
1094                                 mfx = NULL;
1095                         } else
1096                                 _LOGD("Parsing Success\n");
1097                 } else {
1098                         _LOGD("Memory allocation error\n");
1099                 }
1100                 xmlFreeTextReader(reader);
1101         } else {
1102                 _LOGD("Unable to create xml reader\n");
1103         }
1104         return mfx;
1105 }
1106
1107 static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
1108 {
1109         int i = 0;
1110         char delims[] = "=";
1111         char *ret_result = NULL;
1112         char *tag = NULL;
1113         char *ptr = NULL;
1114
1115         if (tagv == NULL)
1116                 return;
1117
1118         for (tag = strdup(tagv[0]); tag != NULL; ) {
1119                 ret_result = strtok_r(tag, delims, &ptr);
1120                 if (ret_result != NULL) {
1121                         /*check tag :  preload */
1122                         if (strcmp(ret_result, "preload") == 0) {
1123                                 ret_result = strtok_r(NULL, delims, &ptr);
1124                                 if (ret_result && strcmp(ret_result, "true") == 0) {
1125                                         free((void *)mfx->preload);
1126                                         mfx->preload = strdup("true");
1127                                 } else if (ret_result && strcmp(ret_result, "false") == 0) {
1128                                         free((void *)mfx->preload);
1129                                         mfx->preload = strdup("false");
1130                                 }
1131                         /*check tag :  removable*/
1132                         } else if (strcmp(ret_result, "removable") == 0) {
1133                                 ret_result = strtok_r(NULL, delims, &ptr);
1134                                 if (ret_result && strcmp(ret_result, "true") == 0) {
1135                                         free((void *)mfx->removable);
1136                                         mfx->removable = strdup("true");
1137                                 } else if (ret_result && strcmp(ret_result, "false") == 0) {
1138                                         free((void *)mfx->removable);
1139                                         mfx->removable = strdup("false");
1140                                 }
1141                         /*check tag :  not matched*/
1142                         } else
1143                                 _LOGD("tag process [%s]is not defined\n", ret_result);
1144                 }
1145                 free(tag);
1146
1147                 /*check next value*/
1148                 if (tagv[++i] != NULL)
1149                         tag = strdup(tagv[i]);
1150                 else {
1151                         _LOGD("tag process success...\n");
1152                         return;
1153                 }
1154         }
1155 }
1156
1157 static int __ps_run_tag_parser(void *lib_handle, xmlDocPtr docPtr, const char *tag,
1158                            ACTION_TYPE action, const char *pkgid)
1159 {
1160         int (*plugin_install) (xmlDocPtr, const char *);
1161         int ret = -1;
1162         char *ac = NULL;
1163
1164         switch (action) {
1165         case ACTION_INSTALL:
1166                 ac = "PKGMGR_PARSER_PLUGIN_INSTALL";
1167                 break;
1168         case ACTION_UPGRADE:
1169                 ac = "PKGMGR_PARSER_PLUGIN_UPGRADE";
1170                 break;
1171         case ACTION_UNINSTALL:
1172                 ac = "PKGMGR_PARSER_PLUGIN_UNINSTALL";
1173                 break;
1174         default:
1175                 goto END;
1176         }
1177
1178         if ((plugin_install =
1179                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1180                 _LOGE("can not find symbol[%s] \n", ac);
1181                 goto END;
1182         }
1183
1184         ret = plugin_install(docPtr, pkgid);
1185         _LOGD("tag parser[%s, %s] ACTION_TYPE[%d] result[%d]\n", pkgid, tag, action, ret);
1186
1187 END:
1188         return ret;
1189 }
1190
1191 static int __run_tag_parser_prestep(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid)
1192 {
1193         int ret = -1;
1194         const xmlChar *name;
1195
1196         if (xmlTextReaderDepth(reader) != 1) {
1197                 _LOGE("Node depth is not 1");
1198                 goto END;
1199         }
1200
1201         if (xmlTextReaderNodeType(reader) != 1) {
1202                 _LOGE("Node type is not 1");
1203                 goto END;
1204         }
1205
1206         const xmlChar *value;
1207         name = xmlTextReaderConstName(reader);
1208         if (name == NULL) {
1209                 _LOGE("TEST TEST TES\n");
1210                 name = BAD_CAST "--";
1211         }
1212
1213         value = xmlTextReaderConstValue(reader);
1214         if (value != NULL) {
1215                 if (xmlStrlen(value) > 40)
1216                         _LOGD(" %.40s...", value);
1217                 else
1218                         _LOGD(" %s", value);
1219         }
1220
1221         name = xmlTextReaderConstName(reader);
1222         if (name == NULL) {
1223                 _LOGE("TEST TEST TES\n");
1224                 name = BAD_CAST "--";
1225         }
1226
1227         xmlDocPtr docPtr = xmlTextReaderCurrentDoc(reader);
1228         xmlDocPtr copyDocPtr = xmlCopyDoc(docPtr, 1);
1229         if (copyDocPtr == NULL)
1230                 return -1;
1231         xmlNode *rootElement = xmlDocGetRootElement(copyDocPtr);
1232         if (rootElement == NULL) {
1233                 xmlFreeDoc(copyDocPtr);
1234                 return -1;
1235         }
1236         xmlNode *cur_node = xmlFirstElementChild(rootElement);
1237         if (cur_node == NULL) {
1238                 xmlFreeDoc(copyDocPtr);
1239                 return -1;
1240         }
1241         xmlNode *temp = xmlTextReaderExpand(reader);
1242         if (temp == NULL) {
1243                 xmlFreeDoc(copyDocPtr);
1244                 return -1;
1245         }
1246         xmlNode *next_node = NULL;
1247         while (cur_node != NULL) {
1248                 if ((strcmp(ASCII(temp->name), ASCII(cur_node->name)) == 0) &&
1249                         (temp->line == cur_node->line)) {
1250                         break;
1251                 } else {
1252                         next_node = xmlNextElementSibling(cur_node);
1253                         xmlUnlinkNode(cur_node);
1254                         xmlFreeNode(cur_node);
1255                         cur_node = next_node;
1256                 }
1257         }
1258         if (cur_node == NULL) {
1259                 xmlFreeDoc(copyDocPtr);
1260                 return -1;
1261         }
1262         next_node = xmlNextElementSibling(cur_node);
1263         if (next_node) {
1264                 cur_node->next = NULL;
1265                 next_node->prev = NULL;
1266                 xmlFreeNodeList(next_node);
1267                 xmlSetTreeDoc(cur_node, copyDocPtr);
1268         } else {
1269                 xmlSetTreeDoc(cur_node, copyDocPtr);
1270         }
1271
1272         ret = __ps_run_tag_parser(lib_handle, copyDocPtr, ASCII(name), action, pkgid);
1273  END:
1274
1275         return ret;
1276 }
1277
1278 static void __process_tag(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, char *tag, const char *pkgid)
1279 {
1280         xmlChar *elementName;
1281
1282         switch (xmlTextReaderNodeType(reader)) {
1283         case XML_READER_TYPE_END_ELEMENT:
1284                 {
1285                         break;
1286                 }
1287         case XML_READER_TYPE_ELEMENT:
1288                 {
1289                         /* Elements without closing tag don't receive */
1290                         elementName = xmlTextReaderLocalName(reader);
1291                         if (elementName == NULL)
1292                                 break;
1293
1294                         if (strcmp(tag, ASCII(elementName)) == 0) {
1295                                 _LOGD("find : tag[%s] ACTION_TYPE[%d] pkg[%s]\n", tag, action, pkgid);
1296                                 __run_tag_parser_prestep(lib_handle, reader, action, pkgid);
1297                         }
1298                         xmlFree(elementName);
1299                         break;
1300                 }
1301
1302         default:
1303                 break;
1304         }
1305 }
1306
1307 static void __str_trim(char *input)
1308 {
1309         char *trim_str = input;
1310
1311         if (input == NULL)
1312                 return;
1313
1314         while (*input != 0) {
1315                 if (!isspace(*input)) {
1316                         *trim_str = *input;
1317                         trim_str++;
1318                 }
1319                 input++;
1320         }
1321
1322         *trim_str = 0;
1323         return;
1324 }
1325
1326 static char *__get_parser_plugin(const char *type)
1327 {
1328         char temp_path[1024] = { 0 };
1329         int ret = -1;
1330
1331         if (type == NULL) {
1332                 _LOGE("invalid argument\n");
1333                 return NULL;
1334         }
1335
1336         ret = snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so",
1337                         PLUGIN_PATH_PREFIX, type);
1338         if (ret < 0 || ret > sizeof(temp_path) -1) {
1339                 _LOGE("snprintf fail");
1340                 return NULL;
1341         }
1342
1343         return strdup(temp_path);
1344 }
1345
1346 static void *__open_lib_handle(char *tag)
1347 {
1348         char *lib_path = NULL;
1349         void *lib_handle = NULL;
1350
1351         lib_path = __get_parser_plugin(tag);
1352         retvm_if(!lib_path, NULL, "lib_path get fail");
1353
1354         lib_handle = dlopen(lib_path, RTLD_LAZY);
1355         if (lib_handle == NULL)
1356                 _LOGE("dlopen is failed lib_path[%s]", lib_path);
1357
1358         free(lib_path);
1359
1360         return lib_handle;
1361 }
1362
1363 static void __close_lib_handle(void *lib_handle)
1364 {
1365         dlclose(lib_handle);
1366 }
1367
1368 static char * __get_tag_by_key(char *md_key)
1369 {
1370         char *md_tag = NULL;
1371
1372         if (md_key == NULL) {
1373                 _LOGD("md_key is NULL\n");
1374                 return NULL;
1375         }
1376
1377         md_tag = strrchr(md_key, 47) + 1;
1378
1379
1380         return strdup(md_tag);
1381 }
1382
1383 static int __parser_send_tag(void *lib_handle, ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1384 {
1385         int (*plugin_install) (const char *);
1386         int ret = -1;
1387         char *ac = NULL;
1388
1389         if (process == PLUGIN_PRE_PROCESS) {
1390                 switch (action) {
1391                 case ACTION_INSTALL:
1392                         ac = "PKGMGR_PARSER_PLUGIN_PRE_INSTALL";
1393                         break;
1394                 case ACTION_UPGRADE:
1395                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE";
1396                         break;
1397                 case ACTION_UNINSTALL:
1398                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL";
1399                         break;
1400                 default:
1401                         return -1;
1402                 }
1403         } else if (process == PLUGIN_POST_PROCESS) {
1404                 switch (action) {
1405                 case ACTION_INSTALL:
1406                         ac = "PKGMGR_PARSER_PLUGIN_POST_INSTALL";
1407                         break;
1408                 case ACTION_UPGRADE:
1409                         ac = "PKGMGR_PARSER_PLUGIN_POST_UPGRADE";
1410                         break;
1411                 case ACTION_UNINSTALL:
1412                         ac = "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL";
1413                         break;
1414                 default:
1415                         return -1;
1416                 }
1417         } else
1418                 return -1;
1419
1420         if ((plugin_install =
1421                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1422                 return -1;
1423         }
1424
1425         ret = plugin_install(pkgid);
1426         return ret;
1427 }
1428
1429 static int __ps_process_tag_parser(manifest_x *mfx, const char *filename, ACTION_TYPE action)
1430 {
1431         xmlTextReaderPtr reader;
1432         xmlDocPtr docPtr;
1433         int ret = -1;
1434         FILE *fp = NULL;
1435         void *lib_handle = NULL;
1436         char tag[PKG_STRING_LEN_MAX] = { 0 };
1437
1438         fp = fopen(TAG_PARSER_LIST, "r");
1439         retvm_if(fp == NULL, PMINFO_R_ERROR, "no preload list");
1440
1441         while (fgets(tag, sizeof(tag), fp) != NULL) {
1442                 __str_trim(tag);
1443
1444                 lib_handle = __open_lib_handle(tag);
1445                 if (lib_handle == NULL)
1446                         continue;
1447
1448                 ret = __parser_send_tag(lib_handle, action, PLUGIN_PRE_PROCESS, mfx->package);
1449                 _LOGD("PLUGIN_PRE_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
1450
1451                 docPtr = xmlReadFile(filename, NULL, 0);
1452                 reader = xmlReaderWalker(docPtr);
1453                 if (reader != NULL) {
1454                         ret = xmlTextReaderRead(reader);
1455                         while (ret == 1) {
1456                                 __process_tag(lib_handle, reader, action, tag, mfx->package);
1457                                 ret = xmlTextReaderRead(reader);
1458                         }
1459                         xmlFreeTextReader(reader);
1460
1461                         if (ret != 0)
1462                                 _LOGD("%s : failed to parse", filename);
1463                 } else {
1464                         _LOGD("Unable to open %s", filename);
1465                 }
1466
1467                 ret = __parser_send_tag(lib_handle, action, PLUGIN_POST_PROCESS, mfx->package);
1468                 _LOGD("PLUGIN_POST_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
1469
1470                 __close_lib_handle(lib_handle);
1471
1472                 memset(tag, 0x00, sizeof(tag));
1473         }
1474
1475         if (fp != NULL)
1476                 fclose(fp);
1477
1478         return 0;
1479 }
1480
1481 static void __metadata_parser_clear_dir_list(GList* dir_list)
1482 {
1483         GList *list = NULL;
1484         __metadata_t* detail = NULL;
1485
1486         if (dir_list) {
1487                 list = g_list_first(dir_list);
1488                 while (list) {
1489                         detail = (__metadata_t *)list->data;
1490                         if (detail) {
1491                                 if (detail->key)
1492                                         free((void *)detail->key);
1493                                 if (detail->value)
1494                                         free((void *)detail->value);
1495                                 free(detail);
1496                         }
1497                         list = g_list_next(list);
1498                 }
1499                 g_list_free(dir_list);
1500         }
1501 }
1502
1503 static char *__get_metadata_parser_plugin(const char *type)
1504 {
1505         char temp_path[1024] = { 0 };
1506
1507         if (type == NULL) {
1508                 _LOGE("invalid argument\n");
1509                 return NULL;
1510         }
1511
1512         snprintf(temp_path, sizeof(temp_path) - 1, "%smetadata/lib%s.so",
1513                         PLUGIN_PATH_PREFIX, type);
1514
1515         return strdup(temp_path);
1516 }
1517
1518 static int __ps_run_metadata_parser(GList *md_list, const char *tag,
1519                                 ACTION_TYPE action, const char *pkgid, const char *appid)
1520 {
1521         char *lib_path = NULL;
1522         void *lib_handle = NULL;
1523         int (*metadata_parser_plugin) (const char *, const char *, GList *);
1524         int ret = -1;
1525         char *ac = NULL;
1526
1527         switch (action) {
1528         case ACTION_INSTALL:
1529                 ac = "PKGMGR_MDPARSER_PLUGIN_INSTALL";
1530                 break;
1531         case ACTION_UPGRADE:
1532                 ac = "PKGMGR_MDPARSER_PLUGIN_UPGRADE";
1533                 break;
1534         case ACTION_UNINSTALL:
1535                 ac = "PKGMGR_MDPARSER_PLUGIN_UNINSTALL";
1536                 break;
1537         default:
1538                 goto END;
1539         }
1540
1541         lib_path = __get_metadata_parser_plugin(tag);
1542         if (!lib_path) {
1543                 _LOGE("get %s parser fail\n", tag);
1544                 goto END;
1545         }
1546
1547         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
1548                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
1549                 goto END;
1550         }
1551
1552         if ((metadata_parser_plugin =
1553                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1554                 _LOGE("can not find symbol[%s] \n", ac);
1555                 goto END;
1556         }
1557
1558         ret = metadata_parser_plugin(pkgid, appid, md_list);
1559         if (ret < 0)
1560                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
1561         else
1562                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
1563
1564 END:
1565         if (lib_path)
1566                 free(lib_path);
1567         if (lib_handle)
1568                 dlclose(lib_handle);
1569         return ret;
1570 }
1571
1572 static int __run_metadata_parser_prestep(manifest_x *mfx, char *md_key, ACTION_TYPE action)
1573 {
1574         int ret = -1;
1575         int tag_exist = 0;
1576         char buffer[1024] = { 0, };
1577         GList *app_tmp;
1578         application_x *app;
1579         GList *md_tmp = NULL;
1580         metadata_x *md;
1581         char *md_tag = NULL;
1582
1583         GList *md_list = NULL;
1584         __metadata_t *md_detail = NULL;
1585
1586         md_tag = __get_tag_by_key(md_key);
1587         if (md_tag == NULL) {
1588                 _LOGD("md_tag is NULL\n");
1589                 return -1;
1590         }
1591
1592         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1593                 app = (application_x *)app_tmp->data;
1594                 if (app == NULL)
1595                         continue;
1596                 for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
1597                         md = (metadata_x *)md_tmp->data;
1598                         if (md == NULL)
1599                                 continue;
1600                         /* get glist of metadata key and value combination */
1601                         memset(buffer, 0x00, 1024);
1602                         ret = snprintf(buffer, 1024, "%s/", md_key);
1603                         if (ret < 0 || ret > 1024) {
1604                                 _LOGD("snprintf fail\n");
1605                                 ret = -1;
1606                                 goto END;
1607                         }
1608                         if ((md->key && md->value) && (strncmp(md->key, md_key, strlen(md_key)) == 0) && (strncmp(buffer, md->key, strlen(buffer)) == 0)) {
1609                                 md_detail = (__metadata_t*) calloc(1, sizeof(__metadata_t));
1610                                 if (md_detail == NULL) {
1611                                         _LOGD("Memory allocation failed\n");
1612                                         goto END;
1613                                 }
1614
1615                                 md_detail->key = strdup(md->key);
1616                                 if (md_detail->key == NULL) {
1617                                         _LOGD("Memory allocation failed\n");
1618                                         free(md_detail);
1619                                         goto END;
1620                                 }
1621
1622                                 md_detail->value = strdup(md->value);
1623                                 if (md_detail->value == NULL) {
1624                                         _LOGD("Memory allocation failed\n");
1625                                         free((void *)md_detail->key);
1626                                         free(md_detail);
1627                                         goto END;
1628                                 }
1629
1630                                 md_list = g_list_append(md_list, (gpointer)md_detail);
1631                                 tag_exist = 1;
1632                         }
1633                 }
1634
1635                 /* send glist to parser when tags for metadata plugin parser exist. */
1636                 if (tag_exist) {
1637                         ret = __ps_run_metadata_parser(md_list, md_tag, action, mfx->package, app->appid);
1638                         if (ret < 0)
1639                                 _LOGD("metadata_parser failed[%d] for tag[%s]\n", ret, md_tag);
1640                         else
1641                                 _LOGD("metadata_parser success for tag[%s]\n", md_tag);
1642                 }
1643                 __metadata_parser_clear_dir_list(md_list);
1644                 md_list = NULL;
1645                 tag_exist = 0;
1646         }
1647
1648         free(md_tag);
1649
1650         return 0;
1651 END:
1652         __metadata_parser_clear_dir_list(md_list);
1653
1654         free(md_tag);
1655
1656         return ret;
1657 }
1658
1659 static int __ps_process_metadata_parser(manifest_x *mfx, ACTION_TYPE action)
1660 {
1661         fprintf(stdout, "__ps_process_metadata_parser\n");
1662         int ret = 0;
1663         FILE *fp = NULL;
1664         char md_key[PKG_STRING_LEN_MAX] = { 0 };
1665
1666         fp = fopen(METADATA_PARSER_LIST, "r");
1667         if (fp == NULL) {
1668                 _LOGD("no preload list\n");
1669                 return -1;
1670         }
1671
1672         while (fgets(md_key, sizeof(md_key), fp) != NULL) {
1673                 __str_trim(md_key);
1674                 ret = __run_metadata_parser_prestep(mfx, md_key, action);
1675                 if (ret < 0)
1676                         break;
1677         }
1678
1679         if (fp != NULL)
1680                 fclose(fp);
1681
1682         return ret;
1683 }
1684
1685 static void __category_parser_clear_dir_list(GList* dir_list)
1686 {
1687         GList *list = NULL;
1688         __category_t* detail = NULL;
1689
1690         if (dir_list) {
1691                 list = g_list_first(dir_list);
1692                 while (list) {
1693                         detail = (__category_t *)list->data;
1694                         if (detail) {
1695                                 if (detail->name)
1696                                         free((void *)detail->name);
1697
1698                                 free(detail);
1699                         }
1700                         list = g_list_next(list);
1701                 }
1702                 g_list_free(dir_list);
1703         }
1704 }
1705
1706 static char *__get_category_parser_plugin(const char *type)
1707 {
1708         char temp_path[1024] = { 0 };
1709
1710         if (type == NULL) {
1711                 _LOGE("invalid argument\n");
1712                 return NULL;
1713         }
1714
1715         snprintf(temp_path, sizeof(temp_path) - 1, "%s/category/lib%s.so",
1716                         PLUGIN_PATH_PREFIX, type);
1717
1718         return strdup(temp_path);
1719 }
1720
1721 static int __ps_run_category_parser(GList *category_list, const char *tag,
1722                                 ACTION_TYPE action, const char *pkgid, const char *appid)
1723 {
1724         char *lib_path = NULL;
1725         void *lib_handle = NULL;
1726         int (*category_parser_plugin) (const char *, const char *, GList *);
1727         int ret = -1;
1728         char *ac = NULL;
1729
1730         switch (action) {
1731         case ACTION_INSTALL:
1732                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL";
1733                 break;
1734         case ACTION_UPGRADE:
1735                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE";
1736                 break;
1737         case ACTION_UNINSTALL:
1738                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL";
1739                 break;
1740         default:
1741                 goto END;
1742         }
1743
1744         lib_path = __get_category_parser_plugin(tag);
1745         if (!lib_path) {
1746                 _LOGE("get %s parser fail\n", tag);
1747                 goto END;
1748         }
1749
1750         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
1751                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
1752                 goto END;
1753         }
1754
1755         if ((category_parser_plugin =
1756                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1757                 _LOGE("can not find symbol[%s] \n", ac);
1758                 goto END;
1759         }
1760
1761         ret = category_parser_plugin(pkgid, appid, category_list);
1762         if (ret < 0)
1763                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
1764         else
1765                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
1766
1767 END:
1768         if (lib_path)
1769                 free(lib_path);
1770         if (lib_handle)
1771                 dlclose(lib_handle);
1772         return ret;
1773 }
1774
1775 static int __run_category_parser_prestep(manifest_x *mfx, char *category_key, ACTION_TYPE action)
1776 {
1777         int ret = -1;
1778         int tag_exist = 0;
1779         char buffer[1024] = { 0, };
1780         GList *app_tmp;
1781         application_x *app;
1782         GList *category_tmp;
1783         const char *category;
1784         char *category_tag = NULL;
1785
1786         GList *category_list = NULL;
1787         __category_t *category_detail = NULL;
1788
1789         category_tag = __get_tag_by_key(category_key);
1790         if (category_tag == NULL) {
1791                 _LOGD("md_tag is NULL\n");
1792                 return -1;
1793         }
1794
1795         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1796                 app = (application_x *)app_tmp->data;
1797                 if (app == NULL)
1798                         continue;
1799                 for (category_tmp = app->category; category_tmp; category_tmp = category_tmp->next) {
1800                         category = (const char *)category_tmp->data;
1801                         /* get glist of category key and value combination */
1802                         memset(buffer, 0x00, 1024);
1803                         ret = snprintf(buffer, 1024, "%s/", category_key);
1804                         if (ret < 0 || ret > 1024) {
1805                                 _LOGD("snprintf fail\n");
1806                                 ret = -1;
1807                                 goto END;
1808                         }
1809                         if ((category) && (strncmp(category, category_key, strlen(category_key)) == 0)) {
1810                                 category_detail = (__category_t*) calloc(1, sizeof(__category_t));
1811                                 if (category_detail == NULL) {
1812                                         _LOGD("Memory allocation failed\n");
1813                                         goto END;
1814                                 }
1815
1816                                 category_detail->name = strdup(category);
1817                                 if (category_detail->name == NULL) {
1818                                         _LOGD("Memory allocation failed\n");
1819                                         free(category_detail);
1820                                         goto END;
1821                                 }
1822
1823                                 category_list = g_list_append(category_list, (gpointer)category_detail);
1824                                 tag_exist = 1;
1825                         }
1826                 }
1827
1828                 /* send glist to parser when tags for metadata plugin parser exist. */
1829                 if (tag_exist) {
1830                         ret = __ps_run_category_parser(category_list, category_tag, action, mfx->package, app->appid);
1831                         if (ret < 0)
1832                                 _LOGD("category_parser failed[%d] for tag[%s]\n", ret, category_tag);
1833                         else
1834                                 _LOGD("category_parser success for tag[%s]\n", category_tag);
1835                 }
1836                 __category_parser_clear_dir_list(category_list);
1837                 category_list = NULL;
1838                 tag_exist = 0;
1839         }
1840
1841         free(category_tag);
1842
1843         return 0;
1844 END:
1845         __category_parser_clear_dir_list(category_list);
1846
1847         free(category_tag);
1848
1849         return ret;
1850 }
1851
1852 static int __ps_process_category_parser(manifest_x *mfx, ACTION_TYPE action)
1853 {
1854         int ret = 0;
1855         FILE *fp = NULL;
1856         char category_key[PKG_STRING_LEN_MAX] = { 0 };
1857
1858         fp = fopen(CATEGORY_PARSER_LIST, "r");
1859         if (fp == NULL) {
1860                 _LOGD("no category parser list\n");
1861                 return -1;
1862         }
1863
1864         while (fgets(category_key, sizeof(category_key), fp) != NULL) {
1865                 __str_trim(category_key);
1866                 ret = __run_category_parser_prestep(mfx, category_key, action);
1867                 if (ret < 0)
1868                         break;
1869         }
1870
1871         if (fp != NULL)
1872                 fclose(fp);
1873
1874         return ret;
1875 }
1876
1877 DEPRECATED API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
1878 {
1879         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1880         _LOGD("parsing manifest for installation: %s\n", manifest);
1881
1882         manifest_x *mfx = NULL;
1883         int ret = -1;
1884
1885         xmlInitParser();
1886         mfx = pkgmgr_parser_process_manifest_xml(manifest);
1887         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1888
1889         _LOGD("Parsing Finished\n");
1890
1891         __ps_process_tag(mfx, tagv);
1892
1893         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
1894         if (ret == PMINFO_R_ERROR) {
1895                 _LOGE("DB Insert failed");
1896                 pkgmgr_parser_free_manifest_xml(mfx);
1897                 return PMINFO_R_ERROR;
1898         }
1899
1900         _LOGD("DB Insert Success\n");
1901
1902         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
1903         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
1904         if (ret == -1)
1905                 _LOGD("Creating metadata parser failed\n");
1906
1907         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
1908         if (ret == -1)
1909                 _LOGD("Creating category parser failed\n");
1910
1911         pkgmgr_parser_free_manifest_xml(mfx);
1912         _LOGD("Free Done\n");
1913         xmlCleanupParser();
1914
1915         return PMINFO_R_OK;
1916 }
1917
1918 DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_installation(const char *manifest, uid_t uid, char *const tagv[])
1919 {
1920         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1921         _LOGD("parsing manifest for installation: %s\n", manifest);
1922         manifest_x *mfx = NULL;
1923         int ret = -1;
1924
1925         xmlInitParser();
1926         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
1927         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1928
1929         _LOGD("Parsing Finished\n");
1930
1931         __ps_process_tag(mfx, tagv);
1932
1933         ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
1934         if (ret == PMINFO_R_ERROR) {
1935                 _LOGE("DB Insert failed");
1936                 pkgmgr_parser_free_manifest_xml(mfx);
1937                 return PMINFO_R_ERROR;
1938         }
1939
1940         _LOGD("DB Insert Success\n");
1941
1942         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
1943         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
1944         if (ret == -1)
1945                 _LOGD("Creating metadata parser failed\n");
1946         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
1947         if (ret == -1)
1948                 _LOGD("Creating category parser failed\n");
1949
1950         pkgmgr_parser_free_manifest_xml(mfx);
1951         _LOGD("Free Done\n");
1952         xmlCleanupParser();
1953
1954         return PMINFO_R_OK;
1955 }
1956
1957 static int __check_preload_updated(manifest_x * mfx, const char *manifest, uid_t uid)
1958 {
1959         if (!strstr(manifest, getUserManifestPath(uid,
1960                 strcmp(mfx->preload, "true") == 0))) {
1961                 /* if downloaded app is updated, then update tag set true*/
1962                 if (mfx->update)
1963                         free((void *)mfx->update);
1964                 mfx->update = strdup("true");
1965         }
1966
1967         return 0;
1968 }
1969
1970 DEPRECATED API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
1971 {
1972         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1973         _LOGD("pkgmgr_parser_parse_manifest_for_upgrade  parsing manifest for upgradation: %s\n", manifest);
1974         manifest_x *mfx = NULL;
1975         int ret = -1;
1976         bool preload = false;
1977         bool system = false;
1978         char *csc_path = NULL;
1979         pkgmgrinfo_pkginfo_h handle = NULL;
1980
1981         xmlInitParser();
1982         mfx = pkgmgr_parser_process_manifest_xml(manifest);
1983         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1984
1985         _LOGD("Parsing Finished\n");
1986         __check_preload_updated(mfx, manifest, GLOBAL_USER);
1987
1988         ret = pkgmgrinfo_pkginfo_get_pkginfo(mfx->package, &handle);
1989         if (ret != PMINFO_R_OK)
1990                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
1991         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
1992         if (ret != PMINFO_R_OK)
1993                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
1994
1995         if (preload) {
1996                 free((void *)mfx->preload);
1997                 mfx->preload = strdup("true");
1998         }
1999
2000         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
2001         if (ret != PMINFO_R_OK)
2002                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
2003         if (system) {
2004                 free((void *)mfx->system);
2005                 mfx->system = strdup("true");
2006         }
2007
2008         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
2009         if (ret != PMINFO_R_OK)
2010                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
2011
2012         if (csc_path != NULL) {
2013                 if (mfx->csc_path)
2014                         free((void *)mfx->csc_path);
2015                 mfx->csc_path = strdup(csc_path);
2016         }
2017
2018         /*Delete from cert table*/
2019         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2020         if (ret != PMINFO_R_OK) {
2021                 _LOGD("Cert Info  DB Delete Failed\n");
2022                 pkgmgr_parser_free_manifest_xml(mfx);
2023                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2024                 return PMINFO_R_ERROR;
2025         }
2026
2027         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
2028         if (ret != PMINFO_R_OK) {
2029                 _LOGD("DB Insert failed\n");
2030                 pkgmgr_parser_free_manifest_xml(mfx);
2031                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2032                 return PMINFO_R_ERROR;
2033         }
2034
2035         _LOGD("DB Update Success\n");
2036
2037         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
2038         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
2039         if (ret == -1)
2040                 _LOGD("Upgrade metadata parser failed\n");
2041         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
2042         if (ret == -1)
2043                 _LOGD("Creating category parser failed\n");
2044         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2045         pkgmgr_parser_free_manifest_xml(mfx);
2046         _LOGD("Free Done\n");
2047         xmlCleanupParser();
2048
2049         return PMINFO_R_OK;
2050 }
2051
2052 DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_upgrade(const char *manifest, uid_t uid, char *const tagv[])
2053 {
2054         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2055         _LOGD(" pkgmgr_parser_parse_usr_manifest_for_upgrade parsing manifest for upgradation: %s\n", manifest);
2056         manifest_x *mfx = NULL;
2057         int ret = -1;
2058         bool preload = false;
2059         bool system = false;
2060         char *csc_path = NULL;
2061         pkgmgrinfo_pkginfo_h handle = NULL;
2062
2063         xmlInitParser();
2064         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
2065         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2066
2067         _LOGD("Parsing Finished\n");
2068         __check_preload_updated(mfx, manifest, uid);
2069
2070         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(mfx->package, uid, &handle);
2071         if (ret != PMINFO_R_OK)
2072                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
2073         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
2074         if (ret != PMINFO_R_OK)
2075                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
2076
2077         if (preload) {
2078                 free((void *)mfx->preload);
2079                 mfx->preload = strdup("true");
2080         }
2081
2082         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
2083         if (ret != PMINFO_R_OK)
2084                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
2085
2086         if (system) {
2087                 free((void *)mfx->system);
2088                 mfx->system = strdup("true");
2089         }
2090
2091         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
2092         if (ret != PMINFO_R_OK)
2093                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
2094         if (csc_path != NULL) {
2095                 if (mfx->csc_path)
2096                         free((void *)mfx->csc_path);
2097                 mfx->csc_path = strdup(csc_path);
2098         }
2099
2100         /*Delete from cert table*/
2101         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2102         if (ret != PMINFO_R_OK) {
2103                 _LOGD("Cert Info  DB Delete Failed\n");
2104                 pkgmgr_parser_free_manifest_xml(mfx);
2105                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2106                 return PMINFO_R_ERROR;
2107         }
2108
2109         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
2110         if (ret != PMINFO_R_OK) {
2111                 _LOGD("DB Insert failed\n");
2112                 pkgmgr_parser_free_manifest_xml(mfx);
2113                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2114                 return PMINFO_R_ERROR;
2115         }
2116
2117         _LOGD("DB Update Success\n");
2118
2119         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
2120         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
2121         if (ret == -1)
2122                 _LOGD("Upgrade metadata parser failed\n");
2123         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
2124         if (ret == -1)
2125                 _LOGD("Creating category parser failed\n");
2126         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2127         pkgmgr_parser_free_manifest_xml(mfx);
2128         _LOGD("Free Done\n");
2129         xmlCleanupParser();
2130
2131         return PMINFO_R_OK;
2132 }
2133
2134 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
2135 {
2136         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2137         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
2138
2139         manifest_x *mfx = NULL;
2140         int ret = -1;
2141         xmlInitParser();
2142         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2143         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2144
2145         _LOGD("Parsing Finished\n");
2146
2147         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
2148
2149         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
2150         if (ret == -1)
2151                 _LOGD("Removing metadata parser failed\n");
2152
2153         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
2154         if (ret == -1)
2155                 _LOGD("Creating category parser failed\n");
2156
2157         /*Delete from cert table*/
2158         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2159         if (ret) {
2160                 _LOGD("Cert Info  DB Delete Failed\n");
2161                 pkgmgr_parser_free_manifest_xml(mfx);
2162                 return -1;
2163         }
2164
2165         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
2166         if (ret == -1)
2167                 _LOGD("DB Delete failed\n");
2168         else
2169                 _LOGD("DB Delete Success\n");
2170
2171         pkgmgr_parser_free_manifest_xml(mfx);
2172         _LOGD("Free Done\n");
2173         xmlCleanupParser();
2174
2175         return PMINFO_R_OK;
2176 }
2177
2178 #define LIBAPPSVC_PATH LIB_PATH "/libappsvc.so.0"
2179
2180 static int __ps_remove_appsvc_db(manifest_x *mfx, uid_t uid)
2181 {
2182         void *lib_handle = NULL;
2183         int (*appsvc_operation) (const char *, uid_t);
2184         int ret = 0;
2185         GList *tmp;
2186         application_x *application;
2187
2188         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
2189                 _LOGE("dlopen is failed LIBAPPSVC_PATH[%s]\n", LIBAPPSVC_PATH);
2190                 goto END;
2191         }
2192
2193         if ((appsvc_operation =
2194                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
2195                 _LOGE("can not find symbol \n");
2196                 goto END;
2197         }
2198
2199         for (tmp = mfx->application; tmp; tmp = tmp->next) {
2200                 application = (application_x *)tmp->data;
2201                 if (application == NULL)
2202                         continue;
2203                 ret = appsvc_operation(application->appid, uid);
2204                 if (ret < 0)
2205                         _LOGE("can not operation  symbol \n");
2206         }
2207
2208 END:
2209         if (lib_handle)
2210                 dlclose(lib_handle);
2211
2212         return ret;
2213 }
2214
2215 API int pkgmgr_parser_parse_usr_manifest_for_uninstallation(const char *manifest, uid_t uid, char *const tagv[])
2216 {
2217         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2218         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
2219
2220         manifest_x *mfx = NULL;
2221         int ret = -1;
2222         xmlInitParser();
2223         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
2224         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2225
2226         _LOGD("Parsing Finished\n");
2227
2228         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
2229
2230         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
2231         if (ret == -1)
2232                 _LOGD("Removing metadata parser failed\n");
2233
2234         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
2235         if (ret == -1)
2236                 _LOGD("Creating category parser failed\n");
2237
2238         /*Delete from cert table*/
2239         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2240         if (ret) {
2241                 _LOGD("Cert Info  DB Delete Failed\n");
2242                 pkgmgr_parser_free_manifest_xml(mfx);
2243                 return -1;
2244         }
2245
2246         ret = pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, uid);
2247         if (ret == -1)
2248                 _LOGD("DB Delete failed\n");
2249         else
2250                 _LOGD("DB Delete Success\n");
2251
2252         ret = __ps_remove_appsvc_db(mfx, uid);
2253         if (ret == -1)
2254                 _LOGD("Removing appsvc_db failed\n");
2255         else
2256                 _LOGD("Removing appsvc_db Success\n");
2257
2258         pkgmgr_parser_free_manifest_xml(mfx);
2259         _LOGD("Free Done\n");
2260         xmlCleanupParser();
2261
2262         return PMINFO_R_OK;
2263 }