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