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