Cleanup pkgmgr-info
[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                 free(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                 return -1;
1234         xmlNode *cur_node = xmlFirstElementChild(rootElement);
1235         if (cur_node == NULL)
1236                 return -1;
1237         xmlNode *temp = xmlTextReaderExpand(reader);
1238         if (temp == NULL)
1239                 return -1;
1240         xmlNode *next_node = NULL;
1241         while (cur_node != NULL) {
1242                 if ((strcmp(ASCII(temp->name), ASCII(cur_node->name)) == 0) &&
1243                         (temp->line == cur_node->line)) {
1244                         break;
1245                 } else {
1246                         next_node = xmlNextElementSibling(cur_node);
1247                         xmlUnlinkNode(cur_node);
1248                         xmlFreeNode(cur_node);
1249                         cur_node = next_node;
1250                 }
1251         }
1252         if (cur_node == NULL)
1253                 return -1;
1254         next_node = xmlNextElementSibling(cur_node);
1255         if (next_node) {
1256                 cur_node->next = NULL;
1257                 next_node->prev = NULL;
1258                 xmlFreeNodeList(next_node);
1259                 xmlSetTreeDoc(cur_node, copyDocPtr);
1260         } else {
1261                 xmlSetTreeDoc(cur_node, copyDocPtr);
1262         }
1263
1264         ret = __ps_run_tag_parser(lib_handle, copyDocPtr, ASCII(name), action, pkgid);
1265  END:
1266
1267         return ret;
1268 }
1269
1270 static void __process_tag(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, char *tag, const char *pkgid)
1271 {
1272         switch (xmlTextReaderNodeType(reader)) {
1273         case XML_READER_TYPE_END_ELEMENT:
1274                 {
1275                         break;
1276                 }
1277         case XML_READER_TYPE_ELEMENT:
1278                 {
1279                         /* Elements without closing tag don't receive */
1280                         const xmlChar *elementName =
1281                             xmlTextReaderLocalName(reader);
1282                         if (elementName == NULL)
1283                                 break;
1284
1285                         if (strcmp(tag, ASCII(elementName)) == 0) {
1286                                 _LOGD("find : tag[%s] ACTION_TYPE[%d] pkg[%s]\n", tag, action, pkgid);
1287                                 __run_tag_parser_prestep(lib_handle, reader, action, pkgid);
1288                                 break;
1289                         }
1290                         break;
1291                 }
1292
1293         default:
1294                 break;
1295         }
1296 }
1297
1298 static void __str_trim(char *input)
1299 {
1300         char *trim_str = input;
1301
1302         if (input == NULL)
1303                 return;
1304
1305         while (*input != 0) {
1306                 if (!isspace(*input)) {
1307                         *trim_str = *input;
1308                         trim_str++;
1309                 }
1310                 input++;
1311         }
1312
1313         *trim_str = 0;
1314         return;
1315 }
1316
1317 static char *__get_parser_plugin(const char *type)
1318 {
1319         char temp_path[1024] = { 0 };
1320         int ret = -1;
1321
1322         if (type == NULL) {
1323                 _LOGE("invalid argument\n");
1324                 return NULL;
1325         }
1326
1327         ret = snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so",
1328                         PLUGIN_PATH_PREFIX, type);
1329         if (ret < 0 || ret > sizeof(temp_path) -1) {
1330                 _LOGE("snprintf fail");
1331                 return NULL;
1332         }
1333
1334         return strdup(temp_path);
1335 }
1336
1337 static void *__open_lib_handle(char *tag)
1338 {
1339         char *lib_path = NULL;
1340         void *lib_handle = NULL;
1341
1342         lib_path = __get_parser_plugin(tag);
1343         retvm_if(!lib_path, NULL, "lib_path get fail");
1344
1345         lib_handle = dlopen(lib_path, RTLD_LAZY);
1346         if (lib_handle == NULL)
1347                 _LOGE("dlopen is failed lib_path[%s]", lib_path);
1348
1349         free(lib_path);
1350
1351         return lib_handle;
1352 }
1353
1354 static void __close_lib_handle(void *lib_handle)
1355 {
1356         dlclose(lib_handle);
1357 }
1358
1359 static char * __get_tag_by_key(char *md_key)
1360 {
1361         char *md_tag = NULL;
1362
1363         if (md_key == NULL) {
1364                 _LOGD("md_key is NULL\n");
1365                 return NULL;
1366         }
1367
1368         md_tag = strrchr(md_key, 47) + 1;
1369
1370
1371         return strdup(md_tag);
1372 }
1373
1374 static int __parser_send_tag(void *lib_handle, ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1375 {
1376         int (*plugin_install) (const char *);
1377         int ret = -1;
1378         char *ac = NULL;
1379
1380         if (process == PLUGIN_PRE_PROCESS) {
1381                 switch (action) {
1382                 case ACTION_INSTALL:
1383                         ac = "PKGMGR_PARSER_PLUGIN_PRE_INSTALL";
1384                         break;
1385                 case ACTION_UPGRADE:
1386                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE";
1387                         break;
1388                 case ACTION_UNINSTALL:
1389                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL";
1390                         break;
1391                 default:
1392                         return -1;
1393                 }
1394         } else if (process == PLUGIN_POST_PROCESS) {
1395                 switch (action) {
1396                 case ACTION_INSTALL:
1397                         ac = "PKGMGR_PARSER_PLUGIN_POST_INSTALL";
1398                         break;
1399                 case ACTION_UPGRADE:
1400                         ac = "PKGMGR_PARSER_PLUGIN_POST_UPGRADE";
1401                         break;
1402                 case ACTION_UNINSTALL:
1403                         ac = "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL";
1404                         break;
1405                 default:
1406                         return -1;
1407                 }
1408         } else
1409                 return -1;
1410
1411         if ((plugin_install =
1412                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1413                 return -1;
1414         }
1415
1416         ret = plugin_install(pkgid);
1417         return ret;
1418 }
1419
1420 static int __ps_process_tag_parser(manifest_x *mfx, const char *filename, ACTION_TYPE action)
1421 {
1422         xmlTextReaderPtr reader;
1423         xmlDocPtr docPtr;
1424         int ret = -1;
1425         FILE *fp = NULL;
1426         void *lib_handle = NULL;
1427         char tag[PKG_STRING_LEN_MAX] = { 0 };
1428
1429         fp = fopen(TAG_PARSER_LIST, "r");
1430         retvm_if(fp == NULL, PMINFO_R_ERROR, "no preload list");
1431
1432         while (fgets(tag, sizeof(tag), fp) != NULL) {
1433                 __str_trim(tag);
1434
1435                 lib_handle = __open_lib_handle(tag);
1436                 if (lib_handle == NULL)
1437                         continue;
1438
1439                 ret = __parser_send_tag(lib_handle, action, PLUGIN_PRE_PROCESS, mfx->package);
1440                 _LOGD("PLUGIN_PRE_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
1441
1442                 docPtr = xmlReadFile(filename, NULL, 0);
1443                 reader = xmlReaderWalker(docPtr);
1444                 if (reader != NULL) {
1445                         ret = xmlTextReaderRead(reader);
1446                         while (ret == 1) {
1447                                 __process_tag(lib_handle, reader, action, tag, mfx->package);
1448                                 ret = xmlTextReaderRead(reader);
1449                         }
1450                         xmlFreeTextReader(reader);
1451
1452                         if (ret != 0)
1453                                 _LOGD("%s : failed to parse", filename);
1454                 } else {
1455                         _LOGD("Unable to open %s", filename);
1456                 }
1457
1458                 ret = __parser_send_tag(lib_handle, action, PLUGIN_POST_PROCESS, mfx->package);
1459                 _LOGD("PLUGIN_POST_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
1460
1461                 __close_lib_handle(lib_handle);
1462
1463                 memset(tag, 0x00, sizeof(tag));
1464         }
1465
1466         if (fp != NULL)
1467                 fclose(fp);
1468
1469         return 0;
1470 }
1471
1472 static void __metadata_parser_clear_dir_list(GList* dir_list)
1473 {
1474         GList *list = NULL;
1475         __metadata_t* detail = NULL;
1476
1477         if (dir_list) {
1478                 list = g_list_first(dir_list);
1479                 while (list) {
1480                         detail = (__metadata_t *)list->data;
1481                         if (detail) {
1482                                 if (detail->key)
1483                                         free((void *)detail->key);
1484                                 if (detail->value)
1485                                         free((void *)detail->value);
1486                                 free(detail);
1487                         }
1488                         list = g_list_next(list);
1489                 }
1490                 g_list_free(dir_list);
1491         }
1492 }
1493
1494 static char *__get_metadata_parser_plugin(const char *type)
1495 {
1496         char temp_path[1024] = { 0 };
1497
1498         if (type == NULL) {
1499                 _LOGE("invalid argument\n");
1500                 return NULL;
1501         }
1502
1503         snprintf(temp_path, sizeof(temp_path) - 1, "%smetadata/lib%s.so",
1504                         PLUGIN_PATH_PREFIX, type);
1505
1506         return strdup(temp_path);
1507 }
1508
1509 static int __ps_run_metadata_parser(GList *md_list, const char *tag,
1510                                 ACTION_TYPE action, const char *pkgid, const char *appid)
1511 {
1512         char *lib_path = NULL;
1513         void *lib_handle = NULL;
1514         int (*metadata_parser_plugin) (const char *, const char *, GList *);
1515         int ret = -1;
1516         char *ac = NULL;
1517
1518         switch (action) {
1519         case ACTION_INSTALL:
1520                 ac = "PKGMGR_MDPARSER_PLUGIN_INSTALL";
1521                 break;
1522         case ACTION_UPGRADE:
1523                 ac = "PKGMGR_MDPARSER_PLUGIN_UPGRADE";
1524                 break;
1525         case ACTION_UNINSTALL:
1526                 ac = "PKGMGR_MDPARSER_PLUGIN_UNINSTALL";
1527                 break;
1528         default:
1529                 goto END;
1530         }
1531
1532         lib_path = __get_metadata_parser_plugin(tag);
1533         if (!lib_path) {
1534                 _LOGE("get %s parser fail\n", tag);
1535                 goto END;
1536         }
1537
1538         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
1539                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
1540                 goto END;
1541         }
1542
1543         if ((metadata_parser_plugin =
1544                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1545                 _LOGE("can not find symbol[%s] \n", ac);
1546                 goto END;
1547         }
1548
1549         ret = metadata_parser_plugin(pkgid, appid, md_list);
1550         if (ret < 0)
1551                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
1552         else
1553                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
1554
1555 END:
1556         if (lib_path)
1557                 free(lib_path);
1558         if (lib_handle)
1559                 dlclose(lib_handle);
1560         return ret;
1561 }
1562
1563 static int __run_metadata_parser_prestep(manifest_x *mfx, char *md_key, ACTION_TYPE action)
1564 {
1565         int ret = -1;
1566         int tag_exist = 0;
1567         char buffer[1024] = { 0, };
1568         GList *app_tmp;
1569         application_x *app;
1570         GList *md_tmp = NULL;
1571         metadata_x *md;
1572         char *md_tag = NULL;
1573
1574         GList *md_list = NULL;
1575         __metadata_t *md_detail = NULL;
1576
1577         md_tag = __get_tag_by_key(md_key);
1578         if (md_tag == NULL) {
1579                 _LOGD("md_tag is NULL\n");
1580                 return -1;
1581         }
1582
1583         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1584                 app = (application_x *)app_tmp->data;
1585                 if (app == NULL)
1586                         continue;
1587                 for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
1588                         md = (metadata_x *)md_tmp->data;
1589                         if (md == NULL)
1590                                 continue;
1591                         /* get glist of metadata key and value combination */
1592                         memset(buffer, 0x00, 1024);
1593                         ret = snprintf(buffer, 1024, "%s/", md_key);
1594                         if (ret < 0 || ret > 1024) {
1595                                 _LOGD("snprintf fail\n");
1596                                 ret = -1;
1597                                 goto END;
1598                         }
1599                         if ((md->key && md->value) && (strncmp(md->key, md_key, strlen(md_key)) == 0) && (strncmp(buffer, md->key, strlen(buffer)) == 0)) {
1600                                 md_detail = (__metadata_t*) calloc(1, sizeof(__metadata_t));
1601                                 if (md_detail == NULL) {
1602                                         _LOGD("Memory allocation failed\n");
1603                                         goto END;
1604                                 }
1605
1606                                 md_detail->key = strdup(md->key);
1607                                 if (md_detail->key == NULL) {
1608                                         _LOGD("Memory allocation failed\n");
1609                                         free(md_detail);
1610                                         goto END;
1611                                 }
1612
1613                                 md_detail->value = strdup(md->value);
1614                                 if (md_detail->value == NULL) {
1615                                         _LOGD("Memory allocation failed\n");
1616                                         free((void *)md_detail->key);
1617                                         free(md_detail);
1618                                         goto END;
1619                                 }
1620
1621                                 md_list = g_list_append(md_list, (gpointer)md_detail);
1622                                 tag_exist = 1;
1623                         }
1624                 }
1625
1626                 /* send glist to parser when tags for metadata plugin parser exist. */
1627                 if (tag_exist) {
1628                         ret = __ps_run_metadata_parser(md_list, md_tag, action, mfx->package, app->appid);
1629                         if (ret < 0)
1630                                 _LOGD("metadata_parser failed[%d] for tag[%s]\n", ret, md_tag);
1631                         else
1632                                 _LOGD("metadata_parser success for tag[%s]\n", md_tag);
1633                 }
1634                 __metadata_parser_clear_dir_list(md_list);
1635                 md_list = NULL;
1636                 tag_exist = 0;
1637         }
1638
1639         free(md_tag);
1640
1641         return 0;
1642 END:
1643         __metadata_parser_clear_dir_list(md_list);
1644
1645         free(md_tag);
1646
1647         return ret;
1648 }
1649
1650 static int __ps_process_metadata_parser(manifest_x *mfx, ACTION_TYPE action)
1651 {
1652         fprintf(stdout, "__ps_process_metadata_parser\n");
1653         int ret = 0;
1654         FILE *fp = NULL;
1655         char md_key[PKG_STRING_LEN_MAX] = { 0 };
1656
1657         fp = fopen(METADATA_PARSER_LIST, "r");
1658         if (fp == NULL) {
1659                 _LOGD("no preload list\n");
1660                 return -1;
1661         }
1662
1663         while (fgets(md_key, sizeof(md_key), fp) != NULL) {
1664                 __str_trim(md_key);
1665                 ret = __run_metadata_parser_prestep(mfx, md_key, action);
1666                 if (ret < 0)
1667                         break;
1668         }
1669
1670         if (fp != NULL)
1671                 fclose(fp);
1672
1673         return ret;
1674 }
1675
1676 static void __category_parser_clear_dir_list(GList* dir_list)
1677 {
1678         GList *list = NULL;
1679         __category_t* detail = NULL;
1680
1681         if (dir_list) {
1682                 list = g_list_first(dir_list);
1683                 while (list) {
1684                         detail = (__category_t *)list->data;
1685                         if (detail) {
1686                                 if (detail->name)
1687                                         free((void *)detail->name);
1688
1689                                 free(detail);
1690                         }
1691                         list = g_list_next(list);
1692                 }
1693                 g_list_free(dir_list);
1694         }
1695 }
1696
1697 static char *__get_category_parser_plugin(const char *type)
1698 {
1699         char temp_path[1024] = { 0 };
1700
1701         if (type == NULL) {
1702                 _LOGE("invalid argument\n");
1703                 return NULL;
1704         }
1705
1706         snprintf(temp_path, sizeof(temp_path) - 1, "%s/category/lib%s.so",
1707                         PLUGIN_PATH_PREFIX, type);
1708
1709         return strdup(temp_path);
1710 }
1711
1712 static int __ps_run_category_parser(GList *category_list, const char *tag,
1713                                 ACTION_TYPE action, const char *pkgid, const char *appid)
1714 {
1715         char *lib_path = NULL;
1716         void *lib_handle = NULL;
1717         int (*category_parser_plugin) (const char *, const char *, GList *);
1718         int ret = -1;
1719         char *ac = NULL;
1720
1721         switch (action) {
1722         case ACTION_INSTALL:
1723                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL";
1724                 break;
1725         case ACTION_UPGRADE:
1726                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE";
1727                 break;
1728         case ACTION_UNINSTALL:
1729                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL";
1730                 break;
1731         default:
1732                 goto END;
1733         }
1734
1735         lib_path = __get_category_parser_plugin(tag);
1736         if (!lib_path) {
1737                 _LOGE("get %s parser fail\n", tag);
1738                 goto END;
1739         }
1740
1741         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
1742                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
1743                 goto END;
1744         }
1745
1746         if ((category_parser_plugin =
1747                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1748                 _LOGE("can not find symbol[%s] \n", ac);
1749                 goto END;
1750         }
1751
1752         ret = category_parser_plugin(pkgid, appid, category_list);
1753         if (ret < 0)
1754                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
1755         else
1756                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
1757
1758 END:
1759         if (lib_path)
1760                 free(lib_path);
1761         if (lib_handle)
1762                 dlclose(lib_handle);
1763         return ret;
1764 }
1765
1766 static int __run_category_parser_prestep(manifest_x *mfx, char *category_key, ACTION_TYPE action)
1767 {
1768         int ret = -1;
1769         int tag_exist = 0;
1770         char buffer[1024] = { 0, };
1771         GList *app_tmp;
1772         application_x *app;
1773         GList *category_tmp;
1774         const char *category;
1775         char *category_tag = NULL;
1776
1777         GList *category_list = NULL;
1778         __category_t *category_detail = NULL;
1779
1780         category_tag = __get_tag_by_key(category_key);
1781         if (category_tag == NULL) {
1782                 _LOGD("md_tag is NULL\n");
1783                 return -1;
1784         }
1785
1786         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1787                 app = (application_x *)app_tmp->data;
1788                 if (app == NULL)
1789                         continue;
1790                 for (category_tmp = app->category; category_tmp; category_tmp = category_tmp->next) {
1791                         category = (const char *)category_tmp->data;
1792                         /* get glist of category key and value combination */
1793                         memset(buffer, 0x00, 1024);
1794                         ret = snprintf(buffer, 1024, "%s/", category_key);
1795                         if (ret < 0 || ret > 1024) {
1796                                 _LOGD("snprintf fail\n");
1797                                 ret = -1;
1798                                 goto END;
1799                         }
1800                         if ((category) && (strncmp(category, category_key, strlen(category_key)) == 0)) {
1801                                 category_detail = (__category_t*) calloc(1, sizeof(__category_t));
1802                                 if (category_detail == NULL) {
1803                                         _LOGD("Memory allocation failed\n");
1804                                         goto END;
1805                                 }
1806
1807                                 category_detail->name = strdup(category);
1808                                 if (category_detail->name == NULL) {
1809                                         _LOGD("Memory allocation failed\n");
1810                                         free(category_detail);
1811                                         goto END;
1812                                 }
1813
1814                                 category_list = g_list_append(category_list, (gpointer)category_detail);
1815                                 tag_exist = 1;
1816                         }
1817                 }
1818
1819                 /* send glist to parser when tags for metadata plugin parser exist. */
1820                 if (tag_exist) {
1821                         ret = __ps_run_category_parser(category_list, category_tag, action, mfx->package, app->appid);
1822                         if (ret < 0)
1823                                 _LOGD("category_parser failed[%d] for tag[%s]\n", ret, category_tag);
1824                         else
1825                                 _LOGD("category_parser success for tag[%s]\n", category_tag);
1826                 }
1827                 __category_parser_clear_dir_list(category_list);
1828                 category_list = NULL;
1829                 tag_exist = 0;
1830         }
1831
1832         free(category_tag);
1833
1834         return 0;
1835 END:
1836         __category_parser_clear_dir_list(category_list);
1837
1838         free(category_tag);
1839
1840         return ret;
1841 }
1842
1843 static int __ps_process_category_parser(manifest_x *mfx, ACTION_TYPE action)
1844 {
1845         int ret = 0;
1846         FILE *fp = NULL;
1847         char category_key[PKG_STRING_LEN_MAX] = { 0 };
1848
1849         fp = fopen(CATEGORY_PARSER_LIST, "r");
1850         if (fp == NULL) {
1851                 _LOGD("no category parser list\n");
1852                 return -1;
1853         }
1854
1855         while (fgets(category_key, sizeof(category_key), fp) != NULL) {
1856                 __str_trim(category_key);
1857                 ret = __run_category_parser_prestep(mfx, category_key, action);
1858                 if (ret < 0)
1859                         break;
1860         }
1861
1862         if (fp != NULL)
1863                 fclose(fp);
1864
1865         return ret;
1866 }
1867
1868 DEPRECATED API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
1869 {
1870         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1871         _LOGD("parsing manifest for installation: %s\n", manifest);
1872
1873         manifest_x *mfx = NULL;
1874         int ret = -1;
1875
1876         xmlInitParser();
1877         mfx = pkgmgr_parser_process_manifest_xml(manifest);
1878         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1879
1880         _LOGD("Parsing Finished\n");
1881
1882         __ps_process_tag(mfx, tagv);
1883
1884         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
1885         if (ret == PMINFO_R_ERROR) {
1886                 _LOGE("DB Insert failed");
1887                 pkgmgr_parser_free_manifest_xml(mfx);
1888                 return PMINFO_R_ERROR;
1889         }
1890
1891         _LOGD("DB Insert Success\n");
1892
1893         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
1894         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
1895         if (ret == -1)
1896                 _LOGD("Creating metadata parser failed\n");
1897
1898         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
1899         if (ret == -1)
1900                 _LOGD("Creating category parser failed\n");
1901
1902         pkgmgr_parser_free_manifest_xml(mfx);
1903         _LOGD("Free Done\n");
1904         xmlCleanupParser();
1905
1906         return PMINFO_R_OK;
1907 }
1908
1909 DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_installation(const char *manifest, uid_t uid, char *const tagv[])
1910 {
1911         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1912         _LOGD("parsing manifest for installation: %s\n", manifest);
1913         manifest_x *mfx = NULL;
1914         int ret = -1;
1915
1916         xmlInitParser();
1917         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
1918         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1919
1920         _LOGD("Parsing Finished\n");
1921
1922         __ps_process_tag(mfx, tagv);
1923
1924         ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
1925         if (ret == PMINFO_R_ERROR) {
1926                 _LOGE("DB Insert failed");
1927                 pkgmgr_parser_free_manifest_xml(mfx);
1928                 return PMINFO_R_ERROR;
1929         }
1930
1931         _LOGD("DB Insert Success\n");
1932
1933         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
1934         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
1935         if (ret == -1)
1936                 _LOGD("Creating metadata parser failed\n");
1937         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
1938         if (ret == -1)
1939                 _LOGD("Creating category parser failed\n");
1940
1941         pkgmgr_parser_free_manifest_xml(mfx);
1942         _LOGD("Free Done\n");
1943         xmlCleanupParser();
1944
1945         return PMINFO_R_OK;
1946 }
1947
1948 static int __check_preload_updated(manifest_x * mfx, const char *manifest, uid_t uid)
1949 {
1950         if (!strstr(manifest, getUserManifestPath(uid,
1951                 strcmp(mfx->preload, "true") == 0))) {
1952                 /* if downloaded app is updated, then update tag set true*/
1953                 if (mfx->update)
1954                         free((void *)mfx->update);
1955                 mfx->update = strdup("true");
1956         }
1957
1958         return 0;
1959 }
1960
1961 DEPRECATED API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
1962 {
1963         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1964         _LOGD("pkgmgr_parser_parse_manifest_for_upgrade  parsing manifest for upgradation: %s\n", manifest);
1965         manifest_x *mfx = NULL;
1966         int ret = -1;
1967         bool preload = false;
1968         bool system = false;
1969         char *csc_path = NULL;
1970         pkgmgrinfo_pkginfo_h handle = NULL;
1971
1972         xmlInitParser();
1973         mfx = pkgmgr_parser_process_manifest_xml(manifest);
1974         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
1975
1976         _LOGD("Parsing Finished\n");
1977         __check_preload_updated(mfx, manifest, GLOBAL_USER);
1978
1979         ret = pkgmgrinfo_pkginfo_get_pkginfo(mfx->package, &handle);
1980         if (ret != PMINFO_R_OK)
1981                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
1982         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
1983         if (ret != PMINFO_R_OK)
1984                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
1985
1986         if (preload) {
1987                 free((void *)mfx->preload);
1988                 mfx->preload = strdup("true");
1989         }
1990
1991         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
1992         if (ret != PMINFO_R_OK)
1993                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
1994         if (system) {
1995                 free((void *)mfx->system);
1996                 mfx->system = strdup("true");
1997         }
1998
1999         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
2000         if (ret != PMINFO_R_OK)
2001                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
2002
2003         if (csc_path != NULL) {
2004                 if (mfx->csc_path)
2005                         free((void *)mfx->csc_path);
2006                 mfx->csc_path = strdup(csc_path);
2007         }
2008
2009         /*Delete from cert table*/
2010         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2011         if (ret != PMINFO_R_OK) {
2012                 _LOGD("Cert Info  DB Delete Failed\n");
2013                 pkgmgr_parser_free_manifest_xml(mfx);
2014                 return PMINFO_R_ERROR;
2015         }
2016
2017         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
2018         if (ret != PMINFO_R_OK) {
2019                 _LOGD("DB Insert failed\n");
2020                 pkgmgr_parser_free_manifest_xml(mfx);
2021                 return PMINFO_R_ERROR;
2022         }
2023
2024         _LOGD("DB Update Success\n");
2025
2026         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
2027         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
2028         if (ret == -1)
2029                 _LOGD("Upgrade metadata parser failed\n");
2030         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
2031         if (ret == -1)
2032                 _LOGD("Creating category parser failed\n");
2033         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2034         pkgmgr_parser_free_manifest_xml(mfx);
2035         _LOGD("Free Done\n");
2036         xmlCleanupParser();
2037
2038         return PMINFO_R_OK;
2039 }
2040
2041 DEPRECATED API int pkgmgr_parser_parse_usr_manifest_for_upgrade(const char *manifest, uid_t uid, char *const tagv[])
2042 {
2043         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2044         _LOGD(" pkgmgr_parser_parse_usr_manifest_for_upgrade parsing manifest for upgradation: %s\n", manifest);
2045         manifest_x *mfx = NULL;
2046         int ret = -1;
2047         bool preload = false;
2048         bool system = false;
2049         char *csc_path = NULL;
2050         pkgmgrinfo_pkginfo_h handle = NULL;
2051
2052         xmlInitParser();
2053         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
2054         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2055
2056         _LOGD("Parsing Finished\n");
2057         __check_preload_updated(mfx, manifest, uid);
2058
2059         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(mfx->package, uid, &handle);
2060         if (ret != PMINFO_R_OK)
2061                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
2062         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
2063         if (ret != PMINFO_R_OK)
2064                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
2065
2066         if (preload) {
2067                 free((void *)mfx->preload);
2068                 mfx->preload = strdup("true");
2069         }
2070
2071         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
2072         if (ret != PMINFO_R_OK)
2073                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
2074
2075         if (system) {
2076                 free((void *)mfx->system);
2077                 mfx->system = strdup("true");
2078         }
2079
2080         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
2081         if (ret != PMINFO_R_OK)
2082                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
2083         if (csc_path != NULL) {
2084                 if (mfx->csc_path)
2085                         free((void *)mfx->csc_path);
2086                 mfx->csc_path = strdup(csc_path);
2087         }
2088
2089         /*Delete from cert table*/
2090         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2091         if (ret != PMINFO_R_OK) {
2092                 _LOGD("Cert Info  DB Delete Failed\n");
2093                 pkgmgr_parser_free_manifest_xml(mfx);
2094                 return PMINFO_R_ERROR;
2095         }
2096
2097         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
2098         if (ret != PMINFO_R_OK) {
2099                 _LOGD("DB Insert failed\n");
2100                 pkgmgr_parser_free_manifest_xml(mfx);
2101                 return PMINFO_R_ERROR;
2102         }
2103
2104         _LOGD("DB Update Success\n");
2105
2106         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
2107         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
2108         if (ret == -1)
2109                 _LOGD("Upgrade metadata parser failed\n");
2110         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
2111         if (ret == -1)
2112                 _LOGD("Creating category parser failed\n");
2113         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2114         pkgmgr_parser_free_manifest_xml(mfx);
2115         _LOGD("Free Done\n");
2116         xmlCleanupParser();
2117
2118         return PMINFO_R_OK;
2119 }
2120
2121 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
2122 {
2123         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2124         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
2125
2126         manifest_x *mfx = NULL;
2127         int ret = -1;
2128         xmlInitParser();
2129         mfx = pkgmgr_parser_process_manifest_xml(manifest);
2130         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2131
2132         _LOGD("Parsing Finished\n");
2133
2134         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
2135
2136         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
2137         if (ret == -1)
2138                 _LOGD("Removing metadata parser failed\n");
2139
2140         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
2141         if (ret == -1)
2142                 _LOGD("Creating category parser failed\n");
2143
2144         /*Delete from cert table*/
2145         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2146         if (ret) {
2147                 _LOGD("Cert Info  DB Delete Failed\n");
2148                 pkgmgr_parser_free_manifest_xml(mfx);
2149                 return -1;
2150         }
2151
2152         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
2153         if (ret == -1)
2154                 _LOGD("DB Delete failed\n");
2155         else
2156                 _LOGD("DB Delete Success\n");
2157
2158         pkgmgr_parser_free_manifest_xml(mfx);
2159         _LOGD("Free Done\n");
2160         xmlCleanupParser();
2161
2162         return PMINFO_R_OK;
2163 }
2164
2165 #define LIBAPPSVC_PATH LIB_PATH "/libappsvc.so.0"
2166
2167 static int __ps_remove_appsvc_db(manifest_x *mfx, uid_t uid)
2168 {
2169         void *lib_handle = NULL;
2170         int (*appsvc_operation) (const char *, uid_t);
2171         int ret = 0;
2172         GList *tmp;
2173         application_x *application;
2174
2175         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
2176                 _LOGE("dlopen is failed LIBAPPSVC_PATH[%s]\n", LIBAPPSVC_PATH);
2177                 goto END;
2178         }
2179
2180         if ((appsvc_operation =
2181                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
2182                 _LOGE("can not find symbol \n");
2183                 goto END;
2184         }
2185
2186         for (tmp = mfx->application; tmp; tmp = tmp->next) {
2187                 application = (application_x *)tmp->data;
2188                 if (application == NULL)
2189                         continue;
2190                 ret = appsvc_operation(application->appid, uid);
2191                 if (ret < 0)
2192                         _LOGE("can not operation  symbol \n");
2193         }
2194
2195 END:
2196         if (lib_handle)
2197                 dlclose(lib_handle);
2198
2199         return ret;
2200 }
2201
2202 API int pkgmgr_parser_parse_usr_manifest_for_uninstallation(const char *manifest, uid_t uid, char *const tagv[])
2203 {
2204         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2205         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
2206
2207         manifest_x *mfx = NULL;
2208         int ret = -1;
2209         xmlInitParser();
2210         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
2211         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
2212
2213         _LOGD("Parsing Finished\n");
2214
2215         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
2216
2217         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
2218         if (ret == -1)
2219                 _LOGD("Removing metadata parser failed\n");
2220
2221         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
2222         if (ret == -1)
2223                 _LOGD("Creating category parser failed\n");
2224
2225         /*Delete from cert table*/
2226         ret = pkgmgrinfo_delete_certinfo(mfx->package);
2227         if (ret) {
2228                 _LOGD("Cert Info  DB Delete Failed\n");
2229                 pkgmgr_parser_free_manifest_xml(mfx);
2230                 return -1;
2231         }
2232
2233         ret = pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, uid);
2234         if (ret == -1)
2235                 _LOGD("DB Delete failed\n");
2236         else
2237                 _LOGD("DB Delete Success\n");
2238
2239         ret = __ps_remove_appsvc_db(mfx, uid);
2240         if (ret == -1)
2241                 _LOGD("Removing appsvc_db failed\n");
2242         else
2243                 _LOGD("Removing appsvc_db Success\n");
2244
2245         pkgmgr_parser_free_manifest_xml(mfx);
2246         _LOGD("Free Done\n");
2247         xmlCleanupParser();
2248
2249         return PMINFO_R_OK;
2250 }