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