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