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