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