Revise parser, insert db
[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_parser.h"
39 #include "pkgmgr_parser_internal.h"
40 #include "pkgmgr_parser_db.h"
41 #include "pkgmgr-info.h"
42 #include "pkgmgr_parser_signature.h"
43 #include "pkgmgr-info-debug.h"
44
45 #ifdef LOG_TAG
46 #undef LOG_TAG
47 #endif
48 #define LOG_TAG "PKGMGR_PARSER"
49
50 #define ASCII(s) (const char *)s
51 #define XMLCHAR(s) (const xmlChar *)s
52
53 //#define METADATA_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/metadata/metadata_parser_list.txt"
54 #define METADATA_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/metadata/mdparser_list.txt"
55 #define METADATA_PARSER_NAME    "metadataparser:"
56
57 #define CATEGORY_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/category/category_parser_list.txt"
58 #define CATEGORY_PARSER_NAME    "categoryparser:"
59
60 #define TAG_PARSER_LIST SYSCONFDIR "/package-manager/parserlib/tag_parser_list.txt"
61 #define TAG_PARSER_NAME "parserlib:"
62
63 #define PKG_TAG_LEN_MAX 128
64 #define OWNER_ROOT 0
65 #define BUFSIZE 4096
66 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
67
68 /* operation_type */
69 typedef enum {
70         ACTION_INSTALL = 0,
71         ACTION_UPGRADE,
72         ACTION_UNINSTALL,
73         ACTION_FOTA,
74         ACTION_MAX
75 } ACTION_TYPE;
76
77 /* plugin process_type */
78 typedef enum {
79         PLUGIN_PRE_PROCESS = 0,
80         PLUGIN_POST_PROCESS
81 } PLUGIN_PROCESS_TYPE;
82
83 typedef struct {
84         const char *key;
85         const char *value;
86 } __metadata_t;
87
88 typedef struct {
89         const char *name;
90 } __category_t;
91
92 const char *package;
93
94 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label);
95 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege);
96 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges);
97 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile);
98 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed);
99 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation);
100 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri);
101 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime);
102 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp);
103 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition);
104 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notifiation);
105 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category);
106 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata);
107 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission);
108 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility);
109 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request);
110 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define);
111 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc);
112 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions);
113 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare);
114 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid);
115 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author);
116 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description);
117 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license);
118 static int __ps_process_appcontrol(xmlTextReaderPtr reader, appcontrol_x *appcontrol);
119 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol);
120 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication, uid_t uid);
121 static int __ps_process_serviceapplication(xmlTextReaderPtr reader, serviceapplication_x *serviceapplication, uid_t uid);
122 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font);
123 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme);
124 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon);
125 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime);
126 static void __ps_free_label(label_x *label);
127 static void __ps_free_privilege(privilege_x *privilege);
128 static void __ps_free_privileges(privileges_x *privileges);
129 static void __ps_free_deviceprofile(deviceprofile_x * deviceprofile);
130 static void __ps_free_allowed(allowed_x *allowed);
131 static void __ps_free_operation(operation_x *operation);
132 static void __ps_free_uri(uri_x *uri);
133 static void __ps_free_mime(mime_x *mime);
134 static void __ps_free_subapp(subapp_x *subapp);
135 static void __ps_free_condition(condition_x *condition);
136 static void __ps_free_notification(notification_x *notifiation);
137 static void __ps_free_category(category_x *category);
138 static void __ps_free_metadata(metadata_x *metadata);
139 static void __ps_free_permission(permission_x *permission);
140 static void __ps_free_compatibility(compatibility_x *compatibility);
141 static void __ps_free_resolution(resolution_x *resolution);
142 static void __ps_free_request(request_x *request);
143 static void __ps_free_define(define_x *define);
144 static void __ps_free_appsvc(appsvc_x *appsvc);
145 static void __ps_free_launchconditions(launchconditions_x *launchconditions);
146 static void __ps_free_datashare(datashare_x *datashare);
147 static void __ps_free_icon(icon_x *icon);
148 static void __ps_free_author(author_x *author);
149 static void __ps_free_description(description_x *description);
150 static void __ps_free_capability(capability_x *capability);
151 static void __ps_free_license(license_x *license);
152 static void __ps_free_appcontrol(appcontrol_x *appcontrol);
153 static void __ps_free_datacontrol(datacontrol_x *datacontrol);
154 static void __ps_free_uiapplication(uiapplication_x *uiapplication);
155 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication);
156 static void __ps_free_font(font_x *font);
157 static void __ps_free_theme(theme_x *theme);
158 static void __ps_free_daemon(daemon_x *daemon);
159 static void __ps_free_ime(ime_x *ime);
160 static char *__pkgid_to_manifest(const char *pkgid, uid_t uid);
161 static int __next_child_element(xmlTextReaderPtr reader, int depth);
162 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid);
163 static int __process_manifest(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid);
164 static void __str_trim(char *input);
165 static char *__get_parser_plugin(const char *type);
166 static int __ps_run_parser(xmlDocPtr docPtr, const char *tag, ACTION_TYPE action, const char *pkgid);
167 static int __run_parser_prestep(xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid);
168 static void __processNode(xmlTextReaderPtr reader, ACTION_TYPE action, char *const tagv[], const char *pkgid);
169 static void __streamFile(const char *filename, ACTION_TYPE action, char *const tagv[], const char *pkgid);
170 API int __is_admin();
171
172 static void __save_xml_attribute(xmlTextReaderPtr reader, char *attribute, const char **xml_attribute, char *default_value)
173 {
174         xmlChar *attrib_val = xmlTextReaderGetAttribute(reader, XMLCHAR(attribute));
175         if (attrib_val) {
176                 *xml_attribute = strdup((const char *)attrib_val);
177                 xmlFree(attrib_val);
178         } else {
179                 if (default_value != NULL) {
180                         *xml_attribute = strdup(default_value);
181                 }
182         }
183 }
184
185 static void __save_xml_lang(xmlTextReaderPtr reader, const char **xml_attribute)
186 {
187         const xmlChar *attrib_val = xmlTextReaderConstXmlLang(reader);
188         if (attrib_val != NULL)
189                 *xml_attribute = strdup(ASCII(attrib_val));
190         else
191                 *xml_attribute = strdup(DEFAULT_LOCALE);
192 }
193
194 static void __save_xml_value(xmlTextReaderPtr reader, const char **xml_attribute)
195 {
196         xmlTextReaderRead(reader);
197         const xmlChar *attrib_val = xmlTextReaderConstValue(reader);
198
199         if (attrib_val)
200                 *xml_attribute = strdup((const char *)attrib_val);
201 }
202
203 static void __save_xml_installed_time(manifest_x *mfx)
204 {
205         char buf[PKG_STRING_LEN_MAX] = {'\0'};
206         char *val = NULL;
207         time_t current_time;
208         time(&current_time);
209         snprintf(buf, PKG_STRING_LEN_MAX - 1, "%d", (int)current_time);
210         val = strndup(buf, PKG_STRING_LEN_MAX - 1);
211         mfx->installed_time = val;
212 }
213
214 static void __save_xml_root_path(manifest_x *mfx, uid_t uid)
215 {
216         char root[PKG_STRING_LEN_MAX] = { '\0' };
217         const char *path;
218
219         if (mfx->root_path)
220                 return;
221
222         tzplatform_set_user(uid);
223         path = tzplatform_getenv((uid == OWNER_ROOT || uid == GLOBAL_USER) ? TZ_SYS_RO_APP : TZ_USER_APP);
224         snprintf(root, PKG_STRING_LEN_MAX - 1, "%s/%s", path, mfx->package);
225
226         mfx->root_path = strdup(root);
227
228         tzplatform_reset_user();
229 }
230
231 static void __save_xml_default_value(manifest_x * mfx)
232 {
233         mfx->preload = strdup("False");
234         mfx->removable = strdup("True");
235         mfx->readonly = strdup("False");
236         mfx->update = strdup("False");
237         mfx->system = strdup("False");
238         mfx->installed_storage= strdup("installed_internal");
239         package = mfx->package;
240 }
241
242 void *__open_lib_handle(char *tag)
243 {
244         char *lib_path = NULL;
245         void *lib_handle = NULL;
246
247         lib_path = __get_parser_plugin(tag);
248         retvm_if(!lib_path, NULL, "lib_path get fail");
249
250         lib_handle = dlopen(lib_path, RTLD_LAZY);
251         retvm_if(lib_handle == NULL, NULL, "dlopen is failed lib_path[%s]", lib_path);
252
253         return lib_handle;
254 }
255
256 void __close_lib_handle(void *lib_handle)
257 {
258         dlclose(lib_handle);
259 }
260
261 static void __str_trim(char *input)
262 {
263         char *trim_str = input;
264
265         if (input == NULL)
266                 return;
267
268         while (*input != 0) {
269                 if (!isspace(*input)) {
270                         *trim_str = *input;
271                         trim_str++;
272                 }
273                 input++;
274         }
275
276         *trim_str = 0;
277         return;
278 }
279
280 API int __is_admin()
281 {
282         uid_t uid = getuid();
283         if ((uid_t) 0 == uid )
284                 return 1;
285         else
286                 return 0;
287 }
288
289
290
291 static char * __get_tag_by_key(char *md_key)
292 {
293         char *md_tag = NULL;
294
295         if (md_key == NULL) {
296                 _LOGD("md_key is NULL\n");
297                 return NULL;
298         }
299
300         md_tag = strrchr(md_key, 47) + 1;
301
302
303         return strdup(md_tag);
304 }
305
306 static char *__get_metadata_parser_plugin(const char *type)
307 {
308         FILE *fp = NULL;
309         char buffer[1024] = { 0 };
310         char temp_path[1024] = { 0 };
311         char *path = NULL;
312
313         if (type == NULL) {
314                 _LOGE("invalid argument\n");
315                 return NULL;
316         }
317
318         fp = fopen(PKG_PARSER_CONF_PATH, "r");
319         if (fp == NULL) {
320                 _LOGE("no matching metadata parser\n");
321                 return NULL;
322         }
323
324         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
325                 if (buffer[0] == '#')
326                         continue;
327
328                 __str_trim(buffer);
329
330                 if ((path = strstr(buffer, METADATA_PARSER_NAME)) != NULL) {
331                         path = path + strlen(METADATA_PARSER_NAME);
332
333                         break;
334                 }
335
336                 memset(buffer, 0x00, 1024);
337         }
338
339         if (fp != NULL)
340                 fclose(fp);
341
342         if (path == NULL) {
343                 _LOGE("no matching [%s] [%s]\n", METADATA_PARSER_NAME,type);
344                 return NULL;
345         }
346
347         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
348
349         return strdup(temp_path);
350 }
351
352 static char *__get_category_parser_plugin(const char *type)
353 {
354         FILE *fp = NULL;
355         char buffer[1024] = { 0 };
356         char temp_path[1024] = { 0 };
357         char *path = NULL;
358
359         if (type == NULL) {
360                 _LOGE("invalid argument\n");
361                 return NULL;
362         }
363
364         fp = fopen(PKG_PARSER_CONF_PATH, "r");
365         if (fp == NULL) {
366                 _LOGE("no matching metadata parser\n");
367                 return NULL;
368         }
369
370         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
371                 if (buffer[0] == '#')
372                         continue;
373
374                 __str_trim(buffer);
375
376                 if ((path = strstr(buffer, CATEGORY_PARSER_NAME)) != NULL) {
377                         path = path + strlen(CATEGORY_PARSER_NAME);
378
379                         break;
380                 }
381
382                 memset(buffer, 0x00, 1024);
383         }
384
385         if (fp != NULL)
386                 fclose(fp);
387
388         if (path == NULL) {
389                 _LOGE("no matching [%s] [%s]\n", CATEGORY_PARSER_NAME,type);
390                 return NULL;
391         }
392
393         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
394
395         return strdup(temp_path);
396 }
397
398 static char *__get_parser_plugin(const char *type)
399 {
400         FILE *fp = NULL;
401         char buffer[1024] = { 0 };
402         char temp_path[1024] = { 0 };
403         char *path = NULL;
404
405         if (type == NULL) {
406                 _LOGE("invalid argument\n");
407                 return NULL;
408         }
409
410         fp = fopen(PKG_PARSER_CONF_PATH, "r");
411         if (fp == NULL) {
412                 _LOGE("no matching backendlib\n");
413                 return NULL;
414         }
415
416         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
417                 if (buffer[0] == '#')
418                         continue;
419
420                 __str_trim(buffer);
421
422                 if ((path = strstr(buffer, PKG_PARSERLIB)) != NULL) {
423                         path = path + strlen(PKG_PARSERLIB);
424                         break;
425                 }
426
427                 memset(buffer, 0x00, 1024);
428         }
429
430         if (fp != NULL)
431                 fclose(fp);
432
433         if (path == NULL) {
434                 _LOGE("no matching backendlib\n");
435                 return NULL;
436         }
437
438         snprintf(temp_path, sizeof(temp_path) - 1, "%slib%s.so", path, type);
439
440         return strdup(temp_path);
441 }
442
443 static int __ps_run_tag_parser(void *lib_handle, xmlDocPtr docPtr, const char *tag,
444                            ACTION_TYPE action, const char *pkgid)
445 {
446         int (*plugin_install) (xmlDocPtr, const char *);
447         int ret = -1;
448         char *ac = NULL;
449
450         switch (action) {
451         case ACTION_INSTALL:
452                 ac = "PKGMGR_PARSER_PLUGIN_INSTALL";
453                 break;
454         case ACTION_UPGRADE:
455                 ac = "PKGMGR_PARSER_PLUGIN_UPGRADE";
456                 break;
457         case ACTION_UNINSTALL:
458                 ac = "PKGMGR_PARSER_PLUGIN_UNINSTALL";
459                 break;
460         default:
461                 goto END;
462         }
463
464         if ((plugin_install =
465                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
466                 _LOGE("can not find symbol[%s] \n", ac);
467                 goto END;
468         }
469
470         ret = plugin_install(docPtr, pkgid);
471         _LOGD("tag parser[%s, %s] ACTION_TYPE[%d] result[%d]\n", pkgid, tag, action, ret);
472
473 END:
474         return ret;
475 }
476
477 static int __ps_run_metadata_parser(GList *md_list, const char *tag,
478                                 ACTION_TYPE action, const char *pkgid, const char *appid)
479 {
480         char *lib_path = NULL;
481         void *lib_handle = NULL;
482         int (*metadata_parser_plugin) (const char *, const char *, GList *);
483         int ret = -1;
484         char *ac = NULL;
485
486         switch (action) {
487         case ACTION_INSTALL:
488                 ac = "PKGMGR_MDPARSER_PLUGIN_INSTALL";
489                 break;
490         case ACTION_UPGRADE:
491                 ac = "PKGMGR_MDPARSER_PLUGIN_UPGRADE";
492                 break;
493         case ACTION_UNINSTALL:
494                 ac = "PKGMGR_MDPARSER_PLUGIN_UNINSTALL";
495                 break;
496         default:
497                 goto END;
498         }
499
500         lib_path = __get_metadata_parser_plugin(tag);
501         if (!lib_path) {
502                 _LOGE("get %s parser fail\n", tag);
503                 goto END;
504         }
505
506         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
507                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
508                 goto END;
509         }
510
511         if ((metadata_parser_plugin =
512                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
513                 _LOGE("can not find symbol[%s] \n",ac);
514                 goto END;
515         }
516
517         ret = metadata_parser_plugin(pkgid, appid, md_list);
518         if (ret < 0)
519                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
520         else
521                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
522
523 END:
524         if (lib_path)
525                 free(lib_path);
526         if (lib_handle)
527                 dlclose(lib_handle);
528         return ret;
529 }
530
531 static int __ps_run_category_parser(GList *category_list, const char *tag,
532                                 ACTION_TYPE action, const char *pkgid, const char *appid)
533 {
534         char *lib_path = NULL;
535         void *lib_handle = NULL;
536         int (*category_parser_plugin) (const char *, const char *, GList *);
537         int ret = -1;
538         char *ac = NULL;
539
540         switch (action) {
541         case ACTION_INSTALL:
542                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL";
543                 break;
544         case ACTION_UPGRADE:
545                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE";
546                 break;
547         case ACTION_UNINSTALL:
548                 ac = "PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL";
549                 break;
550         default:
551                 goto END;
552         }
553
554         lib_path = __get_category_parser_plugin(tag);
555         if (!lib_path) {
556                 _LOGE("get %s parser fail\n", tag);
557                 goto END;
558         }
559
560         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
561                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
562                 goto END;
563         }
564
565         if ((category_parser_plugin =
566                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
567                 _LOGE("can not find symbol[%s] \n",ac);
568                 goto END;
569         }
570
571         ret = category_parser_plugin(pkgid, appid, category_list);
572         if (ret < 0)
573                 _LOGD("[appid = %s, libpath = %s plugin fail\n", appid, lib_path);
574         else
575                 _LOGD("[appid = %s, libpath = %s plugin success\n", appid, lib_path);
576
577 END:
578         if (lib_path)
579                 free(lib_path);
580         if (lib_handle)
581                 dlclose(lib_handle);
582         return ret;
583 }
584
585 static int __ps_run_parser(xmlDocPtr docPtr, const char *tag,
586                            ACTION_TYPE action, const char *pkgid)
587 {
588         char *lib_path = NULL;
589         void *lib_handle = NULL;
590         int (*plugin_install) (xmlDocPtr, const char *);
591         int ret = -1;
592         char *ac = NULL;
593
594         switch (action) {
595         case ACTION_INSTALL:
596                 ac = "PKGMGR_PARSER_PLUGIN_INSTALL";
597                 break;
598         case ACTION_UPGRADE:
599                 ac = "PKGMGR_PARSER_PLUGIN_UPGRADE";
600                 break;
601         case ACTION_UNINSTALL:
602                 ac = "PKGMGR_PARSER_PLUGIN_UNINSTALL";
603                 break;
604         default:
605                 goto END;
606         }
607
608         lib_path = __get_parser_plugin(tag);
609         if (!lib_path) {
610                 goto END;
611         }
612
613         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
614                 _LOGE("dlopen is failed lib_path[%s]\n", lib_path);
615                 goto END;
616         }
617         if ((plugin_install =
618                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
619                 _LOGE("can not find symbol[%s] \n", ac);
620                 goto END;
621         }
622
623         ret = plugin_install(docPtr, pkgid);
624         if (ret < 0)
625                 _LOGD("[pkgid = %s, libpath = %s plugin fail\n", pkgid, lib_path);
626         else
627                 _LOGD("[pkgid = %s, libpath = %s plugin success\n", pkgid, lib_path);
628
629 END:
630         if (lib_path)
631                 free(lib_path);
632         if (lib_handle)
633                 dlclose(lib_handle);
634         return ret;
635 }
636
637 static char *__pkgid_to_manifest(const char *pkgid, uid_t uid)
638 {
639         char *manifest;
640         int size;
641
642         if (pkgid == NULL) {
643                 _LOGE("pkgid is NULL");
644                 return NULL;
645         }
646
647         size = strlen(getUserManifestPath(uid)) + strlen(pkgid) + 10;
648         manifest = malloc(size);
649         if (manifest == NULL) {
650                 _LOGE("No memory");
651                 return NULL;
652         }
653         memset(manifest, '\0', size);
654         snprintf(manifest, size, "%s%s.xml", getUserManifestPath(uid), pkgid);
655
656         if (access(manifest, F_OK)) {
657                 snprintf(manifest, size, "%s%s.xml", getUserManifestPath(uid), pkgid);
658         }
659
660         return manifest;
661 }
662
663 static void __metadata_parser_clear_dir_list(GList* dir_list)
664 {
665         GList *list = NULL;
666         __metadata_t* detail = NULL;
667
668         if (dir_list) {
669                 list = g_list_first(dir_list);
670                 while (list) {
671                         detail = (__metadata_t *)list->data;
672                         if (detail) {
673                                 if (detail->key)
674                                         free(detail->key);
675                                 if (detail->value)
676                                         free(detail->value);
677                                 free(detail);
678                         }
679                         list = g_list_next(list);
680                 }
681                 g_list_free(dir_list);
682         }
683 }
684
685 static void __category_parser_clear_dir_list(GList* dir_list)
686 {
687         GList *list = NULL;
688         __category_t* detail = NULL;
689
690         if (dir_list) {
691                 list = g_list_first(dir_list);
692                 while (list) {
693                         detail = (__category_t *)list->data;
694                         if (detail) {
695                                 if (detail->name)
696                                         free(detail->name);
697
698                                 free(detail);
699                         }
700                         list = g_list_next(list);
701                 }
702                 g_list_free(dir_list);
703         }
704 }
705
706 static int __run_tag_parser_prestep(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid)
707 {
708         int ret = -1;
709         const xmlChar *name;
710
711         if (xmlTextReaderDepth(reader) != 1) {
712                 _LOGE("Node depth is not 1");
713                 goto END;
714         }
715
716         if (xmlTextReaderNodeType(reader) != 1) {
717                 _LOGE("Node type is not 1");
718                 goto END;
719         }
720
721         const xmlChar *value;
722         name = xmlTextReaderConstName(reader);
723         if (name == NULL) {
724                 _LOGE("TEST TEST TES\n");
725                 name = BAD_CAST "--";
726         }
727
728         value = xmlTextReaderConstValue(reader);
729         if (value != NULL) {
730                 if (xmlStrlen(value) > 40) {
731                         _LOGD(" %.40s...", value);
732                 } else {
733                         _LOGD(" %s", value);
734                 }
735         }
736
737         name = xmlTextReaderConstName(reader);
738         if (name == NULL) {
739                 _LOGE("TEST TEST TES\n");
740                 name = BAD_CAST "--";
741         }
742
743         xmlDocPtr docPtr = xmlTextReaderCurrentDoc(reader);
744         xmlDocPtr copyDocPtr = xmlCopyDoc(docPtr, 1);
745         if (copyDocPtr == NULL)
746                 return -1;
747         xmlNode *rootElement = xmlDocGetRootElement(copyDocPtr);
748         if (rootElement == NULL)
749                 return -1;
750         xmlNode *cur_node = xmlFirstElementChild(rootElement);
751         if (cur_node == NULL)
752                 return -1;
753         xmlNode *temp = xmlTextReaderExpand(reader);
754         if (temp == NULL)
755                 return -1;
756         xmlNode *next_node = NULL;
757         while(cur_node != NULL) {
758                 if ( (strcmp(ASCII(temp->name), ASCII(cur_node->name)) == 0) &&
759                         (temp->line == cur_node->line) ) {
760                         break;
761                 }
762                 else {
763                         next_node = xmlNextElementSibling(cur_node);
764                         xmlUnlinkNode(cur_node);
765                         xmlFreeNode(cur_node);
766                         cur_node = next_node;
767                 }
768         }
769         if (cur_node == NULL)
770                 return -1;
771         next_node = xmlNextElementSibling(cur_node);
772         if (next_node) {
773                 cur_node->next = NULL;
774                 next_node->prev = NULL;
775                 xmlFreeNodeList(next_node);
776                 xmlSetTreeDoc(cur_node, copyDocPtr);
777         } else {
778                 xmlSetTreeDoc(cur_node, copyDocPtr);
779         }
780
781         ret = __ps_run_tag_parser(lib_handle, copyDocPtr, ASCII(name), action, pkgid);
782  END:
783
784         return ret;
785 }
786
787 static int __run_metadata_parser_prestep (manifest_x *mfx, char *md_key, ACTION_TYPE action)
788 {
789         int ret = -1;
790         int tag_exist = 0;
791         char buffer[1024] = { 0, };
792         uiapplication_x *up = mfx->uiapplication;
793         metadata_x *md = NULL;
794         char *md_tag = NULL;
795
796         GList *md_list = NULL;
797         __metadata_t *md_detail = NULL;
798
799         md_tag = __get_tag_by_key(md_key);
800         if (md_tag == NULL) {
801                 _LOGD("md_tag is NULL\n");
802                 return -1;
803         }
804
805         while(up != NULL)
806         {
807                 md = up->metadata;
808                 while (md != NULL)
809                 {
810                         //get glist of metadata key and value combination
811                         memset(buffer, 0x00, 1024);
812                         snprintf(buffer, 1024, "%s/", md_key);
813                         if ((md->key && md->value) && (strncmp(md->key, md_key, strlen(md_key)) == 0) && (strncmp(buffer, md->key, strlen(buffer)) == 0)) {
814                                 md_detail = (__metadata_t*) calloc(1, sizeof(__metadata_t));
815                                 if (md_detail == NULL) {
816                                         _LOGD("Memory allocation failed\n");
817                                         goto END;
818                                 }
819
820                                 md_detail->key = (char*) calloc(1, sizeof(char)*(strlen(md->key)+2));
821                                 if (md_detail->key == NULL) {
822                                         _LOGD("Memory allocation failed\n");
823                                         free(md_detail);
824                                         goto END;
825                                 }
826                                 snprintf(md_detail->key, (strlen(md->key)+1), "%s", md->key);
827
828                                 md_detail->value = (char*) calloc(1, sizeof(char)*(strlen(md->value)+2));
829                                 if (md_detail->value == NULL) {
830                                         _LOGD("Memory allocation failed\n");
831                                         free(md_detail->key);
832                                         free(md_detail);
833                                         goto END;
834                                 }
835                                 snprintf(md_detail->value, (strlen(md->value)+1), "%s", md->value);
836
837                                 md_list = g_list_append(md_list, (gpointer)md_detail);
838                                 tag_exist = 1;
839                         }
840                         md = md->next;
841                 }
842
843                 //send glist to parser when tags for metadata plugin parser exist.
844                 if (tag_exist) {
845                         ret = __ps_run_metadata_parser(md_list, md_tag, action, mfx->package, up->appid);
846                         if (ret < 0){
847                                 _LOGD("metadata_parser failed[%d] for tag[%s]\n", ret, md_tag);
848                         }
849                         else{
850                                 _LOGD("metadata_parser success for tag[%s]\n", md_tag);
851                         }
852                 }
853                 __metadata_parser_clear_dir_list(md_list);
854                 md_list = NULL;
855                 tag_exist = 0;
856                 up = up->next;
857         }
858
859         return 0;
860 END:
861         __metadata_parser_clear_dir_list(md_list);
862
863         if (md_tag)
864                 free(md_tag);
865
866         return ret;
867 }
868
869 static int __run_category_parser_prestep (manifest_x *mfx, char *category_key, ACTION_TYPE action)
870 {
871         int ret = -1;
872         int tag_exist = 0;
873         char buffer[1024] = { 0, };
874         uiapplication_x *up = mfx->uiapplication;
875         category_x *category = NULL;
876         char *category_tag = NULL;
877
878         GList *category_list = NULL;
879         __category_t *category_detail = NULL;
880
881         category_tag = __get_tag_by_key(category_key);
882         if (category_tag == NULL) {
883                 _LOGD("md_tag is NULL\n");
884                 return -1;
885         }
886
887         while(up != NULL)
888         {
889                 category = up->category;
890                 while (category != NULL)
891                 {
892                         //get glist of category key and value combination
893                         memset(buffer, 0x00, 1024);
894                         snprintf(buffer, 1024, "%s/", category_key);
895                         if ((category->name) && (strncmp(category->name, category_key, strlen(category_key)) == 0)) {
896                                 category_detail = (__category_t*) calloc(1, sizeof(__category_t));
897                                 if (category_detail == NULL) {
898                                         _LOGD("Memory allocation failed\n");
899                                         goto END;
900                                 }
901
902                                 category_detail->name = (char*) calloc(1, sizeof(char)*(strlen(category->name)+2));
903                                 if (category_detail->name == NULL) {
904                                         _LOGD("Memory allocation failed\n");
905                                         free(category_detail);
906                                         goto END;
907                                 }
908                                 snprintf(category_detail->name, (strlen(category->name)+1), "%s", category->name);
909
910                                 category_list = g_list_append(category_list, (gpointer)category_detail);
911                                 tag_exist = 1;
912                         }
913                         category = category->next;
914                 }
915
916                 //send glist to parser when tags for metadata plugin parser exist.
917                 if (tag_exist) {
918                         ret = __ps_run_category_parser(category_list, category_tag, action, mfx->package, up->appid);
919                         if (ret < 0)
920                                 _LOGD("category_parser failed[%d] for tag[%s]\n", ret, category_tag);
921                         else
922                                 _LOGD("category_parser success for tag[%s]\n", category_tag);
923                 }
924                 __category_parser_clear_dir_list(category_list);
925                 category_list = NULL;
926                 tag_exist = 0;
927                 up = up->next;
928         }
929
930         return 0;
931 END:
932         __category_parser_clear_dir_list(category_list);
933
934         if (category_tag)
935                 free(category_tag);
936
937         return ret;
938 }
939
940 static int __run_parser_prestep(xmlTextReaderPtr reader, ACTION_TYPE action, const char *pkgid)
941 {
942         int ret = -1;
943         const xmlChar *name;
944
945 //      _LOGD("__run_parser_prestep");
946
947         if (xmlTextReaderDepth(reader) != 1) {
948                 _LOGE("Node depth is not 1");
949                 goto END;
950         }
951
952         if (xmlTextReaderNodeType(reader) != 1) {
953                 _LOGE("Node type is not 1");
954                 goto END;
955         }
956
957         const xmlChar *value;
958         name = xmlTextReaderConstName(reader);
959         if (name == NULL) {
960                 _LOGE("TEST TEST TES\n");
961                 name = BAD_CAST "--";
962         }
963
964         value = xmlTextReaderConstValue(reader);
965         _LOGD("%d %d %s %d %d",
966             xmlTextReaderDepth(reader),
967             xmlTextReaderNodeType(reader),
968             name,
969             xmlTextReaderIsEmptyElement(reader), xmlTextReaderHasValue(reader));
970
971         if (value != NULL) {
972                 if (xmlStrlen(value) > 40) {
973                         _LOGD(" %.40s...", value);
974                 } else {
975                         _LOGD(" %s", value);
976                 }
977         }
978
979         name = xmlTextReaderConstName(reader);
980         if (name == NULL) {
981                 _LOGE("TEST TEST TES\n");
982                 name = BAD_CAST "--";
983         }
984
985         xmlDocPtr docPtr = xmlTextReaderCurrentDoc(reader);
986         _LOGD("docPtr->URL %s\n", (char *)docPtr->URL);
987         xmlDocPtr copyDocPtr = xmlCopyDoc(docPtr, 1);
988         if (copyDocPtr == NULL)
989                 return -1;
990         xmlNode *rootElement = xmlDocGetRootElement(copyDocPtr);
991         if (rootElement == NULL)
992                 return -1;
993         xmlNode *cur_node = xmlFirstElementChild(rootElement);
994         if (cur_node == NULL)
995                 return -1;
996         xmlNode *temp = xmlTextReaderExpand(reader);
997         if (temp == NULL)
998                 return -1;
999         xmlNode *next_node = NULL;
1000         while(cur_node != NULL) {
1001                 if ( (strcmp(ASCII(temp->name), ASCII(cur_node->name)) == 0) &&
1002                         (temp->line == cur_node->line) ) {
1003                         break;
1004                 }
1005                 else {
1006                         next_node = xmlNextElementSibling(cur_node);
1007                         xmlUnlinkNode(cur_node);
1008                         xmlFreeNode(cur_node);
1009                         cur_node = next_node;
1010                 }
1011         }
1012         if (cur_node == NULL)
1013                 return -1;
1014         next_node = xmlNextElementSibling(cur_node);
1015         if (next_node) {
1016                 cur_node->next = NULL;
1017                 next_node->prev = NULL;
1018                 xmlFreeNodeList(next_node);
1019                 xmlSetTreeDoc(cur_node, copyDocPtr);
1020         } else {
1021                 xmlSetTreeDoc(cur_node, copyDocPtr);
1022         }
1023
1024 #ifdef __DEBUG__
1025
1026 //#else
1027         _LOGD("node type: %d, name: %s children->name: %s last->name: %s\n"
1028             "parent->name: %s next->name: %s prev->name: %s\n",
1029             cur_node->type, cur_node->name,
1030             cur_node->children ? cur_node->children->name : "NULL",
1031             cur_node->last ? cur_node->last->name : "NULL",
1032             cur_node->parent ? cur_node->parent->name : "NULL",
1033             cur_node->next ? cur_node->next->name : "NULL",
1034             cur_node->prev ? cur_node->prev->name : "NULL");
1035
1036         FILE *fp = fopen(tzplatform_mkpath(TZ_SYS_SHARE, "test.xml"), "a");
1037         xmlDocDump(fp, copyDocPtr);
1038         fclose(fp);
1039 #endif
1040
1041         ret = __ps_run_parser(copyDocPtr, ASCII(name), action, pkgid);
1042  END:
1043
1044         return ret;
1045 }
1046
1047 static void
1048 __processNode(xmlTextReaderPtr reader, ACTION_TYPE action, char *const tagv[], const char *pkgid)
1049 {
1050         char *tag = NULL;
1051         int i = 0;
1052
1053         switch (xmlTextReaderNodeType(reader)) {
1054         case XML_READER_TYPE_END_ELEMENT:
1055                 {
1056                         //            _LOGD("XML_READER_TYPE_END_ELEMENT");
1057                         break;
1058                 }
1059
1060         case XML_READER_TYPE_ELEMENT:
1061                 {
1062                         // Elements without closing tag don't receive
1063                         // XML_READER_TYPE_END_ELEMENT event.
1064
1065                         const xmlChar *elementName =
1066                             xmlTextReaderLocalName(reader);
1067                         if (elementName == NULL) {
1068 //                              _LOGD("elementName %s\n", (char *)elementName);
1069                                 break;
1070                         }
1071
1072                         const xmlChar *nameSpace =
1073                             xmlTextReaderConstNamespaceUri(reader);
1074                         if (nameSpace) {
1075 //                              _LOGD("nameSpace %s\n", (char *)nameSpace);
1076                         }
1077 /*
1078                         _LOGD("XML_READER_TYPE_ELEMENT %s, %s\n",
1079                             elementName ? elementName : "NULL",
1080                             nameSpace ? nameSpace : "NULL");
1081 */
1082                         if (tagv == NULL) {
1083                                 _LOGD("__run_parser_prestep pkgid[%s]\n", pkgid);
1084                                 __run_parser_prestep(reader, action, pkgid);
1085                         }
1086                         else {
1087                                 i = 0;
1088                                 for (tag = tagv[0]; tag; tag = tagv[++i])
1089                                         if (strcmp(tag, ASCII(elementName)) == 0) {
1090                                                 _LOGD("__run_parser_prestep tag[%s] pkgid[%s]\n", tag, pkgid);
1091                                                 __run_parser_prestep(reader,
1092                                                                      action, pkgid);
1093                                                 break;
1094                                         }
1095                         }
1096
1097                         break;
1098                 }
1099         case XML_READER_TYPE_TEXT:
1100         case XML_READER_TYPE_CDATA:
1101                 {
1102                         const xmlChar *value = xmlTextReaderConstValue(reader);
1103                         if (value) {
1104 //                              _LOGD("value %s\n", value);
1105                         }
1106
1107                         const xmlChar *lang = xmlTextReaderConstXmlLang(reader);
1108                         if (lang) {
1109 //                              _LOGD("lang\n", lang);
1110                         }
1111
1112 /*                      _LOGD("XML_READER_TYPE_TEXT %s, %s\n",
1113                             value ? value : "NULL", lang ? lang : "NULL");
1114 */
1115                         break;
1116                 }
1117         default:
1118 //              _LOGD("Ignoring Node of Type: %d", xmlTextReaderNodeType(reader));
1119                 break;
1120         }
1121 }
1122
1123 static void
1124 __processTag(void *lib_handle, xmlTextReaderPtr reader, ACTION_TYPE action, char *tag, const char *pkgid)
1125 {
1126         switch (xmlTextReaderNodeType(reader)) {
1127         case XML_READER_TYPE_END_ELEMENT:
1128                 {
1129                         break;
1130                 }
1131         case XML_READER_TYPE_ELEMENT:
1132                 {
1133                         // Elements without closing tag don't receive
1134                         const xmlChar *elementName =
1135                             xmlTextReaderLocalName(reader);
1136                         if (elementName == NULL) {
1137                                 break;
1138                         }
1139
1140                         if (strcmp(tag, ASCII(elementName)) == 0) {
1141                                 _LOGD("find : tag[%s] ACTION_TYPE[%d] pkg[%s]\n", tag, action, pkgid);
1142                                 __run_tag_parser_prestep(lib_handle, reader, action, pkgid);
1143                                 break;
1144                         }
1145                         break;
1146                 }
1147
1148         default:
1149                 break;
1150         }
1151 }
1152
1153 static int __parser_send_tag(void *lib_handle, ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1154 {
1155         int (*plugin_install) (const char *);
1156         int ret = -1;
1157         char *ac = NULL;
1158
1159         if (process == PLUGIN_PRE_PROCESS) {
1160                 switch (action) {
1161                 case ACTION_INSTALL:
1162                         ac = "PKGMGR_PARSER_PLUGIN_PRE_INSTALL";
1163                         break;
1164                 case ACTION_UPGRADE:
1165                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE";
1166                         break;
1167                 case ACTION_UNINSTALL:
1168                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL";
1169                         break;
1170                 default:
1171                         return -1;
1172                 }
1173         } else if (process == PLUGIN_POST_PROCESS) {
1174                 switch (action) {
1175                 case ACTION_INSTALL:
1176                         ac = "PKGMGR_PARSER_PLUGIN_POST_INSTALL";
1177                         break;
1178                 case ACTION_UPGRADE:
1179                         ac = "PKGMGR_PARSER_PLUGIN_POST_UPGRADE";
1180                         break;
1181                 case ACTION_UNINSTALL:
1182                         ac = "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL";
1183                         break;
1184                 default:
1185                         return -1;
1186                 }
1187         } else
1188                 return -1;
1189
1190         if ((plugin_install =
1191                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1192                 return -1;
1193         }
1194
1195         ret = plugin_install(pkgid);
1196         return ret;
1197 }
1198
1199 static void __plugin_send_tag(const char *tag, ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1200 {
1201         char *lib_path = NULL;
1202         void *lib_handle = NULL;
1203         int (*plugin_install) (const char *);
1204         int ret = -1;
1205         char *ac = NULL;
1206
1207         if (process == PLUGIN_PRE_PROCESS) {
1208                 switch (action) {
1209                 case ACTION_INSTALL:
1210                         ac = "PKGMGR_PARSER_PLUGIN_PRE_INSTALL";
1211                         break;
1212                 case ACTION_UPGRADE:
1213                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UPGRADE";
1214                         break;
1215                 case ACTION_UNINSTALL:
1216                         ac = "PKGMGR_PARSER_PLUGIN_PRE_UNINSTALL";
1217                         break;
1218                 default:
1219                         goto END;
1220                 }
1221         } else if (process == PLUGIN_POST_PROCESS) {
1222                 switch (action) {
1223                 case ACTION_INSTALL:
1224                         ac = "PKGMGR_PARSER_PLUGIN_POST_INSTALL";
1225                         break;
1226                 case ACTION_UPGRADE:
1227                         ac = "PKGMGR_PARSER_PLUGIN_POST_UPGRADE";
1228                         break;
1229                 case ACTION_UNINSTALL:
1230                         ac = "PKGMGR_PARSER_PLUGIN_POST_UNINSTALL";
1231                         break;
1232                 default:
1233                         goto END;
1234                 }
1235         } else
1236                 goto END;
1237
1238         lib_path = __get_parser_plugin(tag);
1239         if (!lib_path) {
1240                 goto END;
1241         }
1242
1243         if ((lib_handle = dlopen(lib_path, RTLD_LAZY)) == NULL) {
1244                 _LOGE("dlopen is failed lib_path[%s] for tag[%s]\n", lib_path, tag);
1245                 goto END;
1246         }
1247         if ((plugin_install =
1248                 dlsym(lib_handle, ac)) == NULL || dlerror() != NULL) {
1249 //              _LOGE("can not find symbol[%s] for tag[%s] \n", ac, tag);
1250                 goto END;
1251         }
1252
1253         ret = plugin_install(pkgid);
1254         if (ret < 0)
1255                 _LOGD("[PLUGIN_PROCESS_TYPE[%d] pkgid=%s, tag=%s plugin fail\n", process, pkgid, tag);
1256         else
1257                 _LOGD("[PLUGIN_PROCESS_TYPE[%d] pkgid=%s, tag=%s plugin success\n", process, pkgid, tag);
1258
1259 END:
1260         if (lib_path)
1261                 free(lib_path);
1262         if (lib_handle)
1263                 dlclose(lib_handle);
1264 }
1265
1266 static void
1267 __plugin_process_tag(char *const tag_list[], ACTION_TYPE action, PLUGIN_PROCESS_TYPE process, const char *pkgid)
1268 {
1269         char *tag = NULL;
1270         int i = 0;
1271
1272         for (tag = tag_list[0]; tag; tag = tag_list[++i])
1273                 __plugin_send_tag(tag, action, process, pkgid);
1274
1275 }
1276
1277 static void
1278 __plugin_save_tag(xmlTextReaderPtr reader, char *const tagv[], char *tag_list[])
1279 {
1280         char *tag = NULL;
1281         int i = 0;
1282         static int pre_cnt=0;
1283
1284         switch (xmlTextReaderNodeType(reader)) {
1285         case XML_READER_TYPE_ELEMENT:
1286                 {
1287                         const xmlChar *elementName = xmlTextReaderLocalName(reader);
1288                         if (elementName == NULL) {
1289                                 break;
1290                         }
1291                         i = 0;
1292                         for (tag = tag_list[0]; tag; tag = tag_list[++i])
1293                                 if (strcmp(ASCII(elementName), tag) == 0) {
1294                                         return;
1295                                 }
1296                         i = 0;
1297                         for (tag = tagv[0]; tag; tag = tagv[++i])
1298                                 if (strcmp(tag, ASCII(elementName)) == 0) {
1299                                         tag_list[pre_cnt++] = tag;
1300                                         break;
1301                                 }
1302                         break;
1303                 }
1304         default:
1305 //              _LOGD("Ignoring Node of Type: %d", xmlTextReaderNodeType(reader));
1306                 break;
1307         }
1308 }
1309
1310 static void
1311 __streamFile(const char *filename, ACTION_TYPE action, char *const tagv[], const char *pkgid)
1312 {
1313         xmlTextReaderPtr reader;
1314         xmlDocPtr docPtr;
1315         int ret;
1316         __plugin_process_tag(tagv, action, PLUGIN_PRE_PROCESS, pkgid);
1317
1318         docPtr = xmlReadFile(filename, NULL, 0);
1319         reader = xmlReaderWalker(docPtr);
1320         if (reader != NULL) {
1321                 ret = xmlTextReaderRead(reader);
1322                 while (ret == 1) {
1323                         __processNode(reader, action, tagv, pkgid);
1324                         ret = xmlTextReaderRead(reader);
1325                 }
1326                 xmlFreeTextReader(reader);
1327
1328                 if (ret != 0) {
1329                         _LOGD("%s : failed to parse", filename);
1330                 }
1331         } else {
1332                 _LOGD("Unable to open %s", filename);
1333         }
1334
1335         __plugin_process_tag(tagv, action, PLUGIN_POST_PROCESS, pkgid);
1336 }
1337
1338 static int __next_child_element(xmlTextReaderPtr reader, int depth)
1339 {
1340         int ret = xmlTextReaderRead(reader);
1341         int cur = xmlTextReaderDepth(reader);
1342         while (ret == 1) {
1343
1344                 switch (xmlTextReaderNodeType(reader)) {
1345                 case XML_READER_TYPE_ELEMENT:
1346                         if (cur == depth + 1)
1347                                 return 1;
1348                         break;
1349                 case XML_READER_TYPE_TEXT:
1350                         /*text is handled by each function separately*/
1351                         if (cur == depth + 1)
1352                                 return 0;
1353                         break;
1354                 case XML_READER_TYPE_END_ELEMENT:
1355                         if (cur == depth)
1356                                 return 0;
1357                         break;
1358                 default:
1359                         if (cur <= depth)
1360                                 return 0;
1361                         break;
1362                 }
1363                 ret = xmlTextReaderRead(reader);
1364                 cur = xmlTextReaderDepth(reader);
1365         }
1366         return ret;
1367 }
1368
1369 static bool __check_action_fota(char *const tagv[])
1370 {
1371         int i = 0;
1372         char delims[] = "=";
1373         char *ret_result = NULL;
1374         char *tag = NULL;
1375         int ret = false;
1376
1377         if (tagv == NULL)
1378                 return ret;
1379
1380         for (tag = strdup(tagv[0]); tag != NULL; ) {
1381                 ret_result = strtok(tag, delims);
1382
1383                 /*check tag :  fota is true */
1384                 if (strcmp(ret_result, "fota") == 0) {
1385                         ret_result = strtok(NULL, delims);
1386                         if (strcmp(ret_result, "true") == 0) {
1387                                 ret = true;
1388                         }
1389                 } else
1390                         _LOGD("tag process [%s]is not defined\n", ret_result);
1391
1392                 free(tag);
1393
1394                 /*check next value*/
1395                 if (tagv[++i] != NULL)
1396                         tag = strdup(tagv[i]);
1397                 else {
1398                         _LOGD("tag process success...%d\n" , ret);
1399                         return ret;
1400                 }
1401         }
1402
1403         return ret;
1404 }
1405
1406 static void __ps_free_category(category_x *category)
1407 {
1408         if (category == NULL)
1409                 return;
1410         if (category->name) {
1411                 free((void *)category->name);
1412                 category->name = NULL;
1413         }
1414         free((void*)category);
1415         category = NULL;
1416 }
1417
1418 static void __ps_free_privilege(privilege_x *privilege)
1419 {
1420         if (privilege == NULL)
1421                 return;
1422         if (privilege->text) {
1423                 free((void *)privilege->text);
1424                 privilege->text = NULL;
1425         }
1426         free((void*)privilege);
1427         privilege = NULL;
1428 }
1429
1430 static void __ps_free_privileges(privileges_x *privileges)
1431 {
1432         if (privileges == NULL)
1433                 return;
1434         /*Free Privilege*/
1435         if (privileges->privilege) {
1436                 privilege_x *privilege = privileges->privilege;
1437                 privilege_x *tmp = NULL;
1438                 while(privilege != NULL) {
1439                         tmp = privilege->next;
1440                         __ps_free_privilege(privilege);
1441                         privilege = tmp;
1442                 }
1443         }
1444         free((void*)privileges);
1445         privileges = NULL;
1446 }
1447
1448 static void __ps_free_metadata(metadata_x *metadata)
1449 {
1450         if (metadata == NULL)
1451                 return;
1452         if (metadata->key) {
1453                 free((void *)metadata->key);
1454                 metadata->key = NULL;
1455         }
1456         if (metadata->value) {
1457                 free((void *)metadata->value);
1458                 metadata->value = NULL;
1459         }
1460         free((void*)metadata);
1461         metadata = NULL;
1462 }
1463
1464 static void __ps_free_permission(permission_x *permission)
1465 {
1466         if (permission == NULL)
1467                 return;
1468         if (permission->type) {
1469                 free((void *)permission->type);
1470                 permission->type = NULL;
1471         }
1472         if (permission->value) {
1473                 free((void *)permission->value);
1474                 permission->value = NULL;
1475         }
1476         free((void*)permission);
1477         permission = NULL;
1478 }
1479
1480 static void __ps_free_icon(icon_x *icon)
1481 {
1482         if (icon == NULL)
1483                 return;
1484         if (icon->text) {
1485                 free((void *)icon->text);
1486                 icon->text = NULL;
1487         }
1488         if (icon->lang) {
1489                 free((void *)icon->lang);
1490                 icon->lang = NULL;
1491         }
1492         if (icon->name) {
1493                 free((void *)icon->name);
1494                 icon->name= NULL;
1495         }
1496         if (icon->section) {
1497                 free((void *)icon->section);
1498                 icon->section = NULL;
1499         }
1500         if (icon->size) {
1501                 free((void *)icon->size);
1502                 icon->size = NULL;
1503         }
1504         if (icon->resolution) {
1505                 free((void *)icon->resolution);
1506                 icon->resolution = NULL;
1507         }
1508         free((void*)icon);
1509         icon = NULL;
1510 }
1511
1512 static void __ps_free_image(image_x *image)
1513 {
1514         if (image == NULL)
1515                 return;
1516         if (image->text) {
1517                 free((void *)image->text);
1518                 image->text = NULL;
1519         }
1520         if (image->lang) {
1521                 free((void *)image->lang);
1522                 image->lang = NULL;
1523         }
1524         if (image->name) {
1525                 free((void *)image->name);
1526                 image->name= NULL;
1527         }
1528         if (image->section) {
1529                 free((void *)image->section);
1530                 image->section = NULL;
1531         }
1532         free((void*)image);
1533         image = NULL;
1534 }
1535
1536 static void __ps_free_operation(operation_x *operation)
1537 {
1538         if (operation == NULL)
1539                 return;
1540         if (operation->text) {
1541                 free((void *)operation->text);
1542                 operation->text = NULL;
1543         }
1544         free((void*)operation);
1545         operation = NULL;
1546 }
1547
1548 static void __ps_free_uri(uri_x *uri)
1549 {
1550         if (uri == NULL)
1551                 return;
1552         if (uri->text) {
1553                 free((void *)uri->text);
1554                 uri->text = NULL;
1555         }
1556         free((void*)uri);
1557         uri = NULL;
1558 }
1559
1560 static void __ps_free_mime(mime_x *mime)
1561 {
1562         if (mime == NULL)
1563                 return;
1564         if (mime->text) {
1565                 free((void *)mime->text);
1566                 mime->text = NULL;
1567         }
1568         free((void*)mime);
1569         mime = NULL;
1570 }
1571
1572 static void __ps_free_subapp(subapp_x *subapp)
1573 {
1574         if (subapp == NULL)
1575                 return;
1576         if (subapp->text) {
1577                 free((void *)subapp->text);
1578                 subapp->text = NULL;
1579         }
1580         free((void*)subapp);
1581         subapp = NULL;
1582 }
1583
1584 static void __ps_free_condition(condition_x *condition)
1585 {
1586         if (condition == NULL)
1587                 return;
1588         if (condition->text) {
1589                 free((void *)condition->text);
1590                 condition->text = NULL;
1591         }
1592         if (condition->name) {
1593                 free((void *)condition->name);
1594                 condition->name = NULL;
1595         }
1596         free((void*)condition);
1597         condition = NULL;
1598 }
1599
1600 static void __ps_free_notification(notification_x *notification)
1601 {
1602         if (notification == NULL)
1603                 return;
1604         if (notification->text) {
1605                 free((void *)notification->text);
1606                 notification->text = NULL;
1607         }
1608         if (notification->name) {
1609                 free((void *)notification->name);
1610                 notification->name = NULL;
1611         }
1612         free((void*)notification);
1613         notification = NULL;
1614 }
1615
1616 static void __ps_free_compatibility(compatibility_x *compatibility)
1617 {
1618         if (compatibility == NULL)
1619                 return;
1620         if (compatibility->text) {
1621                 free((void *)compatibility->text);
1622                 compatibility->text = NULL;
1623         }
1624         if (compatibility->name) {
1625                 free((void *)compatibility->name);
1626                 compatibility->name = NULL;
1627         }
1628         free((void*)compatibility);
1629         compatibility = NULL;
1630 }
1631
1632 static void __ps_free_resolution(resolution_x *resolution)
1633 {
1634         if (resolution == NULL)
1635                 return;
1636         if (resolution->mimetype) {
1637                 free((void *)resolution->mimetype);
1638                 resolution->mimetype = NULL;
1639         }
1640         if (resolution->urischeme) {
1641                 free((void *)resolution->urischeme);
1642                 resolution->urischeme = NULL;
1643         }
1644         free((void*)resolution);
1645         resolution = NULL;
1646 }
1647
1648 static void __ps_free_capability(capability_x *capability)
1649 {
1650         if (capability == NULL)
1651                 return;
1652         if (capability->operationid) {
1653                 free((void *)capability->operationid);
1654                 capability->operationid = NULL;
1655         }
1656         /*Free Resolution*/
1657         if (capability->resolution) {
1658                 resolution_x *resolution = capability->resolution;
1659                 resolution_x *tmp = NULL;
1660                 while(resolution != NULL) {
1661                         tmp = resolution->next;
1662                         __ps_free_resolution(resolution);
1663                         resolution = tmp;
1664                 }
1665         }
1666         free((void*)capability);
1667         capability = NULL;
1668 }
1669
1670 static void __ps_free_allowed(allowed_x *allowed)
1671 {
1672         if (allowed == NULL)
1673                 return;
1674         if (allowed->name) {
1675                 free((void *)allowed->name);
1676                 allowed->name = NULL;
1677         }
1678         if (allowed->text) {
1679                 free((void *)allowed->text);
1680                 allowed->text = NULL;
1681         }
1682         free((void*)allowed);
1683         allowed = NULL;
1684 }
1685
1686 static void __ps_free_request(request_x *request)
1687 {
1688         if (request == NULL)
1689                 return;
1690         if (request->text) {
1691                 free((void *)request->text);
1692                 request->text = NULL;
1693         }
1694         free((void*)request);
1695         request = NULL;
1696 }
1697
1698 static void __ps_free_datacontrol(datacontrol_x *datacontrol)
1699 {
1700         if (datacontrol == NULL)
1701                 return;
1702         if (datacontrol->providerid) {
1703                 free((void *)datacontrol->providerid);
1704                 datacontrol->providerid = NULL;
1705         }
1706         if (datacontrol->access) {
1707                 free((void *)datacontrol->access);
1708                 datacontrol->access = NULL;
1709         }
1710         if (datacontrol->type) {
1711                 free((void *)datacontrol->type);
1712                 datacontrol->type = NULL;
1713         }
1714         free((void*)datacontrol);
1715         datacontrol = NULL;
1716 }
1717
1718 static void __ps_free_launchconditions(launchconditions_x *launchconditions)
1719 {
1720         if (launchconditions == NULL)
1721                 return;
1722         if (launchconditions->text) {
1723                 free((void *)launchconditions->text);
1724                 launchconditions->text = NULL;
1725         }
1726         /*Free Condition*/
1727         if (launchconditions->condition) {
1728                 condition_x *condition = launchconditions->condition;
1729                 condition_x *tmp = NULL;
1730                 while(condition != NULL) {
1731                         tmp = condition->next;
1732                         __ps_free_condition(condition);
1733                         condition = tmp;
1734                 }
1735         }
1736         free((void*)launchconditions);
1737         launchconditions = NULL;
1738 }
1739
1740 static void __ps_free_appcontrol(appcontrol_x *appcontrol)
1741 {
1742         if (appcontrol == NULL)
1743                 return;
1744         /*Free Operation*/
1745         if (appcontrol->operation)
1746                 free(appcontrol->operation);
1747         /*Free Uri*/
1748         if (appcontrol->uri)
1749                 free(appcontrol->uri);
1750         /*Free Mime*/
1751         if (appcontrol->mime)
1752                 free(appcontrol->mime);
1753         free((void*)appcontrol);
1754         appcontrol = NULL;
1755 }
1756
1757 static void __ps_free_appsvc(appsvc_x *appsvc)
1758 {
1759         if (appsvc == NULL)
1760                 return;
1761         if (appsvc->text) {
1762                 free((void *)appsvc->text);
1763                 appsvc->text = NULL;
1764         }
1765         /*Free Operation*/
1766         if (appsvc->operation) {
1767                 operation_x *operation = appsvc->operation;
1768                 operation_x *tmp = NULL;
1769                 while(operation != NULL) {
1770                         tmp = operation->next;
1771                         __ps_free_operation(operation);
1772                         operation = tmp;
1773                 }
1774         }
1775         /*Free Uri*/
1776         if (appsvc->uri) {
1777                 uri_x *uri = appsvc->uri;
1778                 uri_x *tmp = NULL;
1779                 while(uri != NULL) {
1780                         tmp = uri->next;
1781                         __ps_free_uri(uri);
1782                         uri = tmp;
1783                 }
1784         }
1785         /*Free Mime*/
1786         if (appsvc->mime) {
1787                 mime_x *mime = appsvc->mime;
1788                 mime_x *tmp = NULL;
1789                 while(mime != NULL) {
1790                         tmp = mime->next;
1791                         __ps_free_mime(mime);
1792                         mime = tmp;
1793                 }
1794         }
1795         /*Free subapp*/
1796         if (appsvc->subapp) {
1797                 subapp_x *subapp = appsvc->subapp;
1798                 subapp_x *tmp = NULL;
1799                 while(subapp != NULL) {
1800                         tmp = subapp->next;
1801                         __ps_free_subapp(subapp);
1802                         subapp = tmp;
1803                 }
1804         }
1805         free((void*)appsvc);
1806         appsvc = NULL;
1807 }
1808
1809 static void __ps_free_deviceprofile(deviceprofile_x *deviceprofile)
1810 {
1811         return;
1812 }
1813
1814 static void __ps_free_define(define_x *define)
1815 {
1816         if (define == NULL)
1817                 return;
1818         if (define->path) {
1819                 free((void *)define->path);
1820                 define->path = NULL;
1821         }
1822         /*Free Request*/
1823         if (define->request) {
1824                 request_x *request = define->request;
1825                 request_x *tmp = NULL;
1826                 while(request != NULL) {
1827                         tmp = request->next;
1828                         __ps_free_request(request);
1829                         request = tmp;
1830                 }
1831         }
1832         /*Free Allowed*/
1833         if (define->allowed) {
1834                 allowed_x *allowed = define->allowed;
1835                 allowed_x *tmp = NULL;
1836                 while(allowed != NULL) {
1837                         tmp = allowed->next;
1838                         __ps_free_allowed(allowed);
1839                         allowed = tmp;
1840                 }
1841         }
1842         free((void*)define);
1843         define = NULL;
1844 }
1845
1846 static void __ps_free_datashare(datashare_x *datashare)
1847 {
1848         if (datashare == NULL)
1849                 return;
1850         /*Free Define*/
1851         if (datashare->define) {
1852                 define_x *define =  datashare->define;
1853                 define_x *tmp = NULL;
1854                 while(define != NULL) {
1855                         tmp = define->next;
1856                         __ps_free_define(define);
1857                         define = tmp;
1858                 }
1859         }
1860         /*Free Request*/
1861         if (datashare->request) {
1862                 request_x *request = datashare->request;
1863                 request_x *tmp = NULL;
1864                 while(request != NULL) {
1865                         tmp = request->next;
1866                         __ps_free_request(request);
1867                         request = tmp;
1868                 }
1869         }
1870         free((void*)datashare);
1871         datashare = NULL;
1872 }
1873
1874 static void __ps_free_label(label_x *label)
1875 {
1876         if (label == NULL)
1877                 return;
1878         if (label->name) {
1879                 free((void *)label->name);
1880                 label->name = NULL;
1881         }
1882         if (label->text) {
1883                 free((void *)label->text);
1884                 label->text = NULL;
1885         }
1886         if (label->lang) {
1887                 free((void *)label->lang);
1888                 label->lang= NULL;
1889         }
1890         free((void*)label);
1891         label = NULL;
1892 }
1893
1894 static void __ps_free_author(author_x *author)
1895 {
1896         if (author == NULL)
1897                 return;
1898         if (author->email) {
1899                 free((void *)author->email);
1900                 author->email = NULL;
1901         }
1902         if (author->text) {
1903                 free((void *)author->text);
1904                 author->text = NULL;
1905         }
1906         if (author->href) {
1907                 free((void *)author->href);
1908                 author->href = NULL;
1909         }
1910         if (author->lang) {
1911                 free((void *)author->lang);
1912                 author->lang = NULL;
1913         }
1914         free((void*)author);
1915         author = NULL;
1916 }
1917
1918 static void __ps_free_description(description_x *description)
1919 {
1920         if (description == NULL)
1921                 return;
1922         if (description->name) {
1923                 free((void *)description->name);
1924                 description->name = NULL;
1925         }
1926         if (description->text) {
1927                 free((void *)description->text);
1928                 description->text = NULL;
1929         }
1930         if (description->lang) {
1931                 free((void *)description->lang);
1932                 description->lang = NULL;
1933         }
1934         free((void*)description);
1935         description = NULL;
1936 }
1937
1938 static void __ps_free_license(license_x *license)
1939 {
1940         if (license == NULL)
1941                 return;
1942         if (license->text) {
1943                 free((void *)license->text);
1944                 license->text = NULL;
1945         }
1946         if (license->lang) {
1947                 free((void *)license->lang);
1948                 license->lang = NULL;
1949         }
1950         free((void*)license);
1951         license = NULL;
1952 }
1953
1954 static void __ps_free_uiapplication(uiapplication_x *uiapplication)
1955 {
1956         if (uiapplication == NULL)
1957                 return;
1958         if (uiapplication->exec) {
1959                 free((void *)uiapplication->exec);
1960                 uiapplication->exec = NULL;
1961         }
1962         if (uiapplication->appid) {
1963                 free((void *)uiapplication->appid);
1964                 uiapplication->appid = NULL;
1965         }
1966         if (uiapplication->nodisplay) {
1967                 free((void *)uiapplication->nodisplay);
1968                 uiapplication->nodisplay = NULL;
1969         }
1970         if (uiapplication->multiple) {
1971                 free((void *)uiapplication->multiple);
1972                 uiapplication->multiple = NULL;
1973         }
1974         if (uiapplication->type) {
1975                 free((void *)uiapplication->type);
1976                 uiapplication->type = NULL;
1977         }
1978         if (uiapplication->categories) {
1979                 free((void *)uiapplication->categories);
1980                 uiapplication->categories = NULL;
1981         }
1982         if (uiapplication->extraid) {
1983                 free((void *)uiapplication->extraid);
1984                 uiapplication->extraid = NULL;
1985         }
1986         if (uiapplication->taskmanage) {
1987                 free((void *)uiapplication->taskmanage);
1988                 uiapplication->taskmanage = NULL;
1989         }
1990         if (uiapplication->enabled) {
1991                 free((void *)uiapplication->enabled);
1992                 uiapplication->enabled = NULL;
1993         }
1994         if (uiapplication->hwacceleration) {
1995                 free((void *)uiapplication->hwacceleration);
1996                 uiapplication->hwacceleration = NULL;
1997         }
1998         if (uiapplication->screenreader) {
1999                 free((void *)uiapplication->screenreader);
2000                 uiapplication->screenreader = NULL;
2001         }
2002         if (uiapplication->mainapp) {
2003                 free((void *)uiapplication->mainapp);
2004                 uiapplication->mainapp = NULL;
2005         }
2006         if (uiapplication->recentimage) {
2007                 free((void *)uiapplication->recentimage);
2008                 uiapplication->recentimage = NULL;
2009         }
2010         if (uiapplication->package) {
2011                 free((void *)uiapplication->package);
2012                 uiapplication->package = NULL;
2013         }
2014         if (uiapplication->launchcondition) {
2015                 free((void *)uiapplication->launchcondition);
2016                 uiapplication->launchcondition = NULL;
2017         }
2018         /*Free Label*/
2019         if (uiapplication->label) {
2020                 label_x *label = uiapplication->label;
2021                 label_x *tmp = NULL;
2022                 while(label != NULL) {
2023                         tmp = label->next;
2024                         __ps_free_label(label);
2025                         label = tmp;
2026                 }
2027         }
2028         /*Free Icon*/
2029         if (uiapplication->icon) {
2030                 icon_x *icon = uiapplication->icon;
2031                 icon_x *tmp = NULL;
2032                 while(icon != NULL) {
2033                         tmp = icon->next;
2034                         __ps_free_icon(icon);
2035                         icon = tmp;
2036                 }
2037         }
2038         /*Free image*/
2039         if (uiapplication->image) {
2040                 image_x *image = uiapplication->image;
2041                 image_x *tmp = NULL;
2042                 while(image != NULL) {
2043                         tmp = image->next;
2044                         __ps_free_image(image);
2045                         image = tmp;
2046                 }
2047         }
2048         /*Free AppControl*/
2049         if (uiapplication->appcontrol) {
2050                 appcontrol_x *appcontrol = uiapplication->appcontrol;
2051                 appcontrol_x *tmp = NULL;
2052                 while(appcontrol != NULL) {
2053                         tmp = appcontrol->next;
2054                         __ps_free_appcontrol(appcontrol);
2055                         appcontrol = tmp;
2056                 }
2057         }
2058         /*Free LaunchConditions*/
2059         if (uiapplication->launchconditions) {
2060                 launchconditions_x *launchconditions = uiapplication->launchconditions;
2061                 launchconditions_x *tmp = NULL;
2062                 while(launchconditions != NULL) {
2063                         tmp = launchconditions->next;
2064                         __ps_free_launchconditions(launchconditions);
2065                         launchconditions = tmp;
2066                 }
2067         }
2068         /*Free Notification*/
2069         if (uiapplication->notification) {
2070                 notification_x *notification = uiapplication->notification;
2071                 notification_x *tmp = NULL;
2072                 while(notification != NULL) {
2073                         tmp = notification->next;
2074                         __ps_free_notification(notification);
2075                         notification = tmp;
2076                 }
2077         }
2078         /*Free DataShare*/
2079         if (uiapplication->datashare) {
2080                 datashare_x *datashare = uiapplication->datashare;
2081                 datashare_x *tmp = NULL;
2082                 while(datashare != NULL) {
2083                         tmp = datashare->next;
2084                         __ps_free_datashare(datashare);
2085                         datashare = tmp;
2086                 }
2087         }
2088         /*Free AppSvc*/
2089         if (uiapplication->appsvc) {
2090                 appsvc_x *appsvc = uiapplication->appsvc;
2091                 appsvc_x *tmp = NULL;
2092                 while(appsvc != NULL) {
2093                         tmp = appsvc->next;
2094                         __ps_free_appsvc(appsvc);
2095                         appsvc = tmp;
2096                 }
2097         }
2098         /*Free Category*/
2099         if (uiapplication->category) {
2100                 category_x *category = uiapplication->category;
2101                 category_x *tmp = NULL;
2102                 while(category != NULL) {
2103                         tmp = category->next;
2104                         __ps_free_category(category);
2105                         category = tmp;
2106                 }
2107         }
2108         /*Free Metadata*/
2109         if (uiapplication->metadata) {
2110                 metadata_x *metadata = uiapplication->metadata;
2111                 metadata_x *tmp = NULL;
2112                 while(metadata != NULL) {
2113                         tmp = metadata->next;
2114                         __ps_free_metadata(metadata);
2115                         metadata = tmp;
2116                 }
2117         }
2118         /*Free permission*/
2119         if (uiapplication->permission) {
2120                 permission_x *permission = uiapplication->permission;
2121                 permission_x *tmp = NULL;
2122                 while(permission != NULL) {
2123                         tmp = permission->next;
2124                         __ps_free_permission(permission);
2125                         permission = tmp;
2126                 }
2127         }
2128         /*Free DataControl*/
2129         if (uiapplication->datacontrol) {
2130                 datacontrol_x *datacontrol = uiapplication->datacontrol;
2131                 datacontrol_x *tmp = NULL;
2132                 while(datacontrol != NULL) {
2133                         tmp = datacontrol->next;
2134                         __ps_free_datacontrol(datacontrol);
2135                         datacontrol = tmp;
2136                 }
2137         }
2138         /* _PRODUCT_LAUNCHING_ENHANCED_ START */
2139         if (uiapplication->indicatordisplay) {
2140                 free((void *)uiapplication->indicatordisplay);
2141                 uiapplication->indicatordisplay = NULL;
2142         }
2143         if (uiapplication->portraitimg) {
2144                 free((void *)uiapplication->portraitimg);
2145                 uiapplication->portraitimg = NULL;
2146         }
2147         if (uiapplication->landscapeimg) {
2148                 free((void *)uiapplication->landscapeimg);
2149                 uiapplication->landscapeimg = NULL;
2150         }
2151         /* _PRODUCT_LAUNCHING_ENHANCED_ END */
2152         if (uiapplication->guestmode_visibility) {
2153                 free((void *)uiapplication->guestmode_visibility);
2154                 uiapplication->guestmode_visibility = NULL;
2155         }
2156         if (uiapplication->app_component) {
2157                 free((void *)uiapplication->app_component);
2158                 uiapplication->app_component = NULL;
2159         }
2160         if (uiapplication->permission_type) {
2161                 free((void *)uiapplication->permission_type);
2162                 uiapplication->permission_type = NULL;
2163         }
2164         if (uiapplication->component_type) {
2165                 free((void *)uiapplication->component_type);
2166                 uiapplication->component_type = NULL;
2167         }
2168         if (uiapplication->preload) {
2169                 free((void *)uiapplication->preload);
2170                 uiapplication->preload = NULL;
2171         }
2172         if (uiapplication->submode) {
2173                 free((void *)uiapplication->submode);
2174                 uiapplication->submode = NULL;
2175         }
2176         if (uiapplication->submode_mainid) {
2177                 free((void *)uiapplication->submode_mainid);
2178                 uiapplication->submode_mainid = NULL;
2179         }
2180
2181         free((void*)uiapplication);
2182         uiapplication = NULL;
2183 }
2184
2185 static void __ps_free_serviceapplication(serviceapplication_x *serviceapplication)
2186 {
2187         if (serviceapplication == NULL)
2188                 return;
2189         if (serviceapplication->exec) {
2190                 free((void *)serviceapplication->exec);
2191                 serviceapplication->exec = NULL;
2192         }
2193         if (serviceapplication->appid) {
2194                 free((void *)serviceapplication->appid);
2195                 serviceapplication->appid = NULL;
2196         }
2197         if (serviceapplication->onboot) {
2198                 free((void *)serviceapplication->onboot);
2199                 serviceapplication->onboot = NULL;
2200         }
2201         if (serviceapplication->autorestart) {
2202                 free((void *)serviceapplication->autorestart);
2203                 serviceapplication->autorestart = NULL;
2204         }
2205         if (serviceapplication->type) {
2206                 free((void *)serviceapplication->type);
2207                 serviceapplication->type = NULL;
2208         }
2209         if (serviceapplication->enabled) {
2210                 free((void *)serviceapplication->enabled);
2211                 serviceapplication->enabled = NULL;
2212         }
2213         if (serviceapplication->package) {
2214                 free((void *)serviceapplication->package);
2215                 serviceapplication->package = NULL;
2216         }
2217         if (serviceapplication->permission_type) {
2218                 free((void *)serviceapplication->permission_type);
2219                 serviceapplication->permission_type = NULL;
2220         }
2221         /*Free Label*/
2222         if (serviceapplication->label) {
2223                 label_x *label = serviceapplication->label;
2224                 label_x *tmp = NULL;
2225                 while(label != NULL) {
2226                         tmp = label->next;
2227                         __ps_free_label(label);
2228                         label = tmp;
2229                 }
2230         }
2231         /*Free Icon*/
2232         if (serviceapplication->icon) {
2233                 icon_x *icon = serviceapplication->icon;
2234                 icon_x *tmp = NULL;
2235                 while(icon != NULL) {
2236                         tmp = icon->next;
2237                         __ps_free_icon(icon);
2238                         icon = tmp;
2239                 }
2240         }
2241         /*Free AppControl*/
2242         if (serviceapplication->appcontrol) {
2243                 appcontrol_x *appcontrol = serviceapplication->appcontrol;
2244                 appcontrol_x *tmp = NULL;
2245                 while(appcontrol != NULL) {
2246                         tmp = appcontrol->next;
2247                         __ps_free_appcontrol(appcontrol);
2248                         appcontrol = tmp;
2249                 }
2250         }
2251         /*Free DataControl*/
2252         if (serviceapplication->datacontrol) {
2253                 datacontrol_x *datacontrol = serviceapplication->datacontrol;
2254                 datacontrol_x *tmp = NULL;
2255                 while(datacontrol != NULL) {
2256                         tmp = datacontrol->next;
2257                         __ps_free_datacontrol(datacontrol);
2258                         datacontrol = tmp;
2259                 }
2260         }
2261         /*Free LaunchConditions*/
2262         if (serviceapplication->launchconditions) {
2263                 launchconditions_x *launchconditions = serviceapplication->launchconditions;
2264                 launchconditions_x *tmp = NULL;
2265                 while(launchconditions != NULL) {
2266                         tmp = launchconditions->next;
2267                         __ps_free_launchconditions(launchconditions);
2268                         launchconditions = tmp;
2269                 }
2270         }
2271         /*Free Notification*/
2272         if (serviceapplication->notification) {
2273                 notification_x *notification = serviceapplication->notification;
2274                 notification_x *tmp = NULL;
2275                 while(notification != NULL) {
2276                         tmp = notification->next;
2277                         __ps_free_notification(notification);
2278                         notification = tmp;
2279                 }
2280         }
2281         /*Free DataShare*/
2282         if (serviceapplication->datashare) {
2283                 datashare_x *datashare = serviceapplication->datashare;
2284                 datashare_x *tmp = NULL;
2285                 while(datashare != NULL) {
2286                         tmp = datashare->next;
2287                         __ps_free_datashare(datashare);
2288                         datashare = tmp;
2289                 }
2290         }
2291         /*Free AppSvc*/
2292         if (serviceapplication->appsvc) {
2293                 appsvc_x *appsvc = serviceapplication->appsvc;
2294                 appsvc_x *tmp = NULL;
2295                 while(appsvc != NULL) {
2296                         tmp = appsvc->next;
2297                         __ps_free_appsvc(appsvc);
2298                         appsvc = tmp;
2299                 }
2300         }
2301         /*Free Category*/
2302         if (serviceapplication->category) {
2303                 category_x *category = serviceapplication->category;
2304                 category_x *tmp = NULL;
2305                 while(category != NULL) {
2306                         tmp = category->next;
2307                         __ps_free_category(category);
2308                         category = tmp;
2309                 }
2310         }
2311         /*Free Metadata*/
2312         if (serviceapplication->metadata) {
2313                 metadata_x *metadata = serviceapplication->metadata;
2314                 metadata_x *tmp = NULL;
2315                 while(metadata != NULL) {
2316                         tmp = metadata->next;
2317                         __ps_free_metadata(metadata);
2318                         metadata = tmp;
2319                 }
2320         }
2321         /*Free permission*/
2322         if (serviceapplication->permission) {
2323                 permission_x *permission = serviceapplication->permission;
2324                 permission_x *tmp = NULL;
2325                 while(permission != NULL) {
2326                         tmp = permission->next;
2327                         __ps_free_permission(permission);
2328                         permission = tmp;
2329                 }
2330         }
2331         free((void*)serviceapplication);
2332         serviceapplication = NULL;
2333 }
2334
2335 static void __ps_free_font(font_x *font)
2336 {
2337         if (font == NULL)
2338                 return;
2339         if (font->name) {
2340                 free((void *)font->name);
2341                 font->name = NULL;
2342         }
2343         if (font->text) {
2344                 free((void *)font->text);
2345                 font->text = NULL;
2346         }
2347         free((void*)font);
2348         font = NULL;
2349 }
2350
2351 static void __ps_free_theme(theme_x *theme)
2352 {
2353         if (theme == NULL)
2354                 return;
2355         if (theme->name) {
2356                 free((void *)theme->name);
2357                 theme->name = NULL;
2358         }
2359         if (theme->text) {
2360                 free((void *)theme->text);
2361                 theme->text = NULL;
2362         }
2363         free((void*)theme);
2364         theme = NULL;
2365 }
2366
2367 static void __ps_free_daemon(daemon_x *daemon)
2368 {
2369         if (daemon == NULL)
2370                 return;
2371         if (daemon->name) {
2372                 free((void *)daemon->name);
2373                 daemon->name = NULL;
2374         }
2375         if (daemon->text) {
2376                 free((void *)daemon->text);
2377                 daemon->text = NULL;
2378         }
2379         free((void*)daemon);
2380         daemon = NULL;
2381 }
2382
2383 static void __ps_free_ime(ime_x *ime)
2384 {
2385         if (ime == NULL)
2386                 return;
2387         if (ime->name) {
2388                 free((void *)ime->name);
2389                 ime->name = NULL;
2390         }
2391         if (ime->text) {
2392                 free((void *)ime->text);
2393                 ime->text = NULL;
2394         }
2395         free((void*)ime);
2396         ime = NULL;
2397 }
2398
2399 int __ps_process_tag_parser(manifest_x *mfx, const char *filename, ACTION_TYPE action)
2400 {
2401         xmlTextReaderPtr reader;
2402         xmlDocPtr docPtr;
2403         int ret = -1;
2404         FILE *fp = NULL;
2405         void *lib_handle = NULL;
2406         char tag[PKG_STRING_LEN_MAX] = { 0 };
2407
2408         fp = fopen(TAG_PARSER_LIST, "r");
2409         retvm_if(fp == NULL, PMINFO_R_ERROR, "no preload list");
2410
2411         while (fgets(tag, sizeof(tag), fp) != NULL) {
2412                 __str_trim(tag);
2413
2414                 lib_handle = __open_lib_handle(tag);
2415                 if (lib_handle == NULL)
2416                         continue;
2417
2418                 ret = __parser_send_tag(lib_handle, action, PLUGIN_PRE_PROCESS, mfx->package);
2419                 _LOGD("PLUGIN_PRE_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
2420
2421                 docPtr = xmlReadFile(filename, NULL, 0);
2422                 reader = xmlReaderWalker(docPtr);
2423                 if (reader != NULL) {
2424                         ret = xmlTextReaderRead(reader);
2425                         while (ret == 1) {
2426                                 __processTag(lib_handle, reader, action, tag, mfx->package);
2427                                 ret = xmlTextReaderRead(reader);
2428                         }
2429                         xmlFreeTextReader(reader);
2430
2431                         if (ret != 0) {
2432                                 _LOGD("%s : failed to parse", filename);
2433                         }
2434                 } else {
2435                         _LOGD("Unable to open %s", filename);
2436                 }
2437
2438                 ret = __parser_send_tag(lib_handle, action, PLUGIN_POST_PROCESS, mfx->package);
2439                 _LOGD("PLUGIN_POST_PROCESS[%s, %s] ACTION_TYPE[%d] result[%d]\n", mfx->package, tag, action, ret);
2440
2441                 __close_lib_handle(lib_handle);
2442
2443                 memset(tag, 0x00, sizeof(tag));
2444         }
2445
2446         if (fp != NULL)
2447                 fclose(fp);
2448
2449         return 0;
2450 }
2451
2452 int __ps_process_metadata_parser(manifest_x *mfx, ACTION_TYPE action)
2453 {
2454         fprintf(stdout,"__ps_process_metadata_parser\n");
2455         int ret = -1;
2456         FILE *fp = NULL;
2457         char md_key[PKG_STRING_LEN_MAX] = { 0 };
2458
2459         fp = fopen(METADATA_PARSER_LIST, "r");
2460         if (fp == NULL) {
2461                 _LOGD("no preload list\n");
2462                 return -1;
2463         }
2464         
2465         while (fgets(md_key, sizeof(md_key), fp) != NULL) {
2466                 __str_trim(md_key);
2467                 ret = __run_metadata_parser_prestep(mfx, md_key, action);
2468
2469                 memset(md_key, 0x00, sizeof(md_key));
2470         }
2471
2472         if (fp != NULL)
2473                 fclose(fp);
2474
2475         return 0;
2476 }
2477
2478 int __ps_process_category_parser(manifest_x *mfx, ACTION_TYPE action)
2479 {
2480         int ret = -1;
2481         FILE *fp = NULL;
2482         char category_key[PKG_STRING_LEN_MAX] = { 0 };
2483
2484         fp = fopen(CATEGORY_PARSER_LIST, "r");
2485         if (fp == NULL) {
2486                 _LOGD("no category parser list\n");
2487                 return -1;
2488         }
2489
2490         while (fgets(category_key, sizeof(category_key), fp) != NULL) {
2491                 __str_trim(category_key);
2492                 ret = __run_category_parser_prestep(mfx, category_key, action);
2493
2494                 memset(category_key, 0x00, sizeof(category_key));
2495         }
2496
2497         if (fp != NULL)
2498                 fclose(fp);
2499
2500         return 0;
2501 }
2502
2503 static int __ps_process_allowed(xmlTextReaderPtr reader, allowed_x *allowed)
2504 {
2505         __save_xml_value(reader, &allowed->text);
2506         return 0;
2507 }
2508
2509 static int __ps_process_operation(xmlTextReaderPtr reader, operation_x *operation)
2510 {
2511         __save_xml_attribute(reader, "name", &operation->name, NULL);
2512         return 0;
2513 }
2514
2515 static int __ps_process_uri(xmlTextReaderPtr reader, uri_x *uri)
2516 {
2517         __save_xml_attribute(reader, "name", &uri->name, NULL);
2518         return 0;
2519 }
2520
2521 static int __ps_process_mime(xmlTextReaderPtr reader, mime_x *mime)
2522 {
2523         __save_xml_attribute(reader, "name", &mime->name, NULL);
2524         return 0;
2525 }
2526
2527 static int __ps_process_subapp(xmlTextReaderPtr reader, subapp_x *subapp)
2528 {
2529         __save_xml_attribute(reader, "name", &subapp->name, NULL);
2530         return 0;
2531 }
2532
2533 static int __ps_process_condition(xmlTextReaderPtr reader, condition_x *condition)
2534 {
2535         __save_xml_attribute(reader, "name", &condition->name, NULL);
2536         return 0;
2537 }
2538
2539 static int __ps_process_notification(xmlTextReaderPtr reader, notification_x *notification)
2540 {
2541         __save_xml_attribute(reader, "name", &notification->name, NULL);
2542         __save_xml_value(reader, &notification->text);
2543         return 0;
2544 }
2545
2546 static int __ps_process_category(xmlTextReaderPtr reader, category_x *category)
2547 {
2548         __save_xml_attribute(reader, "name", &category->name, NULL);
2549         return 0;
2550 }
2551
2552 static int __ps_process_privilege(xmlTextReaderPtr reader, privilege_x *privilege)
2553 {
2554         __save_xml_value(reader, &privilege->text);
2555         return 0;
2556 }
2557
2558 static int __ps_process_metadata(xmlTextReaderPtr reader, metadata_x *metadata)
2559 {
2560         __save_xml_attribute(reader, "key", &metadata->key, NULL);
2561         __save_xml_attribute(reader, "value", &metadata->value, NULL);
2562         return 0;
2563 }
2564
2565 static int __ps_process_permission(xmlTextReaderPtr reader, permission_x *permission)
2566 {
2567         __save_xml_attribute(reader, "type", &permission->type, NULL);
2568         __save_xml_value(reader, &permission->value);
2569         return 0;
2570 }
2571
2572 static int __ps_process_compatibility(xmlTextReaderPtr reader, compatibility_x *compatibility)
2573 {
2574         __save_xml_attribute(reader, "name", &compatibility->name, NULL);
2575         __save_xml_value(reader, &compatibility->text);
2576         return 0;
2577 }
2578
2579 static int __ps_process_request(xmlTextReaderPtr reader, request_x *request)
2580 {
2581         __save_xml_value(reader, &request->text);
2582         return 0;
2583 }
2584
2585 static int __ps_process_define(xmlTextReaderPtr reader, define_x *define)
2586 {
2587         const xmlChar *node;
2588         int ret = -1;
2589         int depth = -1;
2590         allowed_x *tmp1 = NULL;
2591         request_x *tmp2 = NULL;
2592
2593         __save_xml_attribute(reader, "path", &define->path, NULL);
2594
2595         depth = xmlTextReaderDepth(reader);
2596         while ((ret = __next_child_element(reader, depth))) {
2597                 node = xmlTextReaderConstName(reader);
2598                 if (!node) {
2599                         _LOGD("xmlTextReaderConstName value is NULL\n");
2600                         return -1;
2601                 }
2602
2603                 if (!strcmp(ASCII(node), "allowed")) {
2604                         allowed_x *allowed= malloc(sizeof(allowed_x));
2605                         if (allowed == NULL) {
2606                                 _LOGD("Malloc Failed\n");
2607                                 return -1;
2608                         }
2609                         memset(allowed, '\0', sizeof(allowed_x));
2610                         LISTADD(define->allowed, allowed);
2611                         ret = __ps_process_allowed(reader, allowed);
2612                 } else if (!strcmp(ASCII(node), "request")) {
2613                         request_x *request = malloc(sizeof(request_x));
2614                         if (request == NULL) {
2615                                 _LOGD("Malloc Failed\n");
2616                                 return -1;
2617                         }
2618                         memset(request, '\0', sizeof(request_x));
2619                         LISTADD(define->request, request);
2620                         ret = __ps_process_request(reader, request);
2621                 } else
2622                         return -1;
2623                 if (ret < 0) {
2624                         _LOGD("Processing define failed\n");
2625                         return ret;
2626                 }
2627         }
2628         if (define->allowed) {
2629                 LISTHEAD(define->allowed, tmp1);
2630                 define->allowed = tmp1;
2631         }
2632         if (define->request) {
2633                 LISTHEAD(define->request, tmp2);
2634                 define->request = tmp2;
2635         }
2636         return ret;
2637 }
2638
2639 static int __ps_process_appcontrol(xmlTextReaderPtr reader, appcontrol_x *appcontrol)
2640 {
2641         const xmlChar *node;
2642         int ret = -1;
2643         int depth = -1;
2644
2645         depth = xmlTextReaderDepth(reader);
2646         while ((ret = __next_child_element(reader, depth))) {
2647                 node = xmlTextReaderConstName(reader);
2648                 if (!node) {
2649                         _LOGD("xmlTextReaderConstName value is NULL\n");
2650                         return -1;
2651                 }
2652
2653                 if (!strcmp(ASCII(node), "operation")) {
2654                         __save_xml_attribute(reader, "name", &appcontrol->operation, NULL);
2655                         _LOGD("operation processing\n");
2656                 } else if (!strcmp(ASCII(node), "uri")) {
2657                         __save_xml_attribute(reader, "name", &appcontrol->uri, NULL);
2658                         _LOGD("uri processing\n");
2659                 } else if (!strcmp(ASCII(node), "mime")) {
2660                         __save_xml_attribute(reader, "name", &appcontrol->mime, NULL);
2661                         _LOGD("mime processing\n");
2662                 } else
2663                         return -1;
2664                 if (ret < 0) {
2665                         _LOGD("Processing appcontrol failed\n");
2666                         return ret;
2667                 }
2668         }
2669
2670         return ret;
2671 }
2672
2673 static int __ps_process_appsvc(xmlTextReaderPtr reader, appsvc_x *appsvc)
2674 {
2675         const xmlChar *node;
2676         int ret = -1;
2677         int depth = -1;
2678         operation_x *tmp1 = NULL;
2679         uri_x *tmp2 = NULL;
2680         mime_x *tmp3 = NULL;
2681         subapp_x *tmp4 = NULL;
2682
2683         depth = xmlTextReaderDepth(reader);
2684         while ((ret = __next_child_element(reader, depth))) {
2685                 node = xmlTextReaderConstName(reader);
2686                 if (!node) {
2687                         _LOGD("xmlTextReaderConstName value is NULL\n");
2688                         return -1;
2689                 }
2690
2691                 if (!strcmp(ASCII(node), "operation")) {
2692                         operation_x *operation = malloc(sizeof(operation_x));
2693                         if (operation == NULL) {
2694                                 _LOGD("Malloc Failed\n");
2695                                 return -1;
2696                         }
2697                         memset(operation, '\0', sizeof(operation_x));
2698                         LISTADD(appsvc->operation, operation);
2699                         ret = __ps_process_operation(reader, operation);
2700                         _LOGD("operation processing\n");
2701                 } else if (!strcmp(ASCII(node), "uri")) {
2702                         uri_x *uri= malloc(sizeof(uri_x));
2703                         if (uri == NULL) {
2704                                 _LOGD("Malloc Failed\n");
2705                                 return -1;
2706                         }
2707                         memset(uri, '\0', sizeof(uri_x));
2708                         LISTADD(appsvc->uri, uri);
2709                         ret = __ps_process_uri(reader, uri);
2710                         _LOGD("uri processing\n");
2711                 } else if (!strcmp(ASCII(node), "mime")) {
2712                         mime_x *mime = malloc(sizeof(mime_x));
2713                         if (mime == NULL) {
2714                                 _LOGD("Malloc Failed\n");
2715                                 return -1;
2716                         }
2717                         memset(mime, '\0', sizeof(mime_x));
2718                         LISTADD(appsvc->mime, mime);
2719                         ret = __ps_process_mime(reader, mime);
2720                         _LOGD("mime processing\n");
2721                 } else if (!strcmp(ASCII(node), "subapp")) {
2722                         subapp_x *subapp = malloc(sizeof(subapp_x));
2723                         if (subapp == NULL) {
2724                                 _LOGD("Malloc Failed\n");
2725                                 return -1;
2726                         }
2727                         memset(subapp, '\0', sizeof(subapp_x));
2728                         LISTADD(appsvc->subapp, subapp);
2729                         ret = __ps_process_subapp(reader, subapp);
2730                         _LOGD("subapp processing\n");
2731                 } else
2732                         return -1;
2733                 if (ret < 0) {
2734                         _LOGD("Processing appsvc failed\n");
2735                         return ret;
2736                 }
2737         }
2738         if (appsvc->operation) {
2739                 LISTHEAD(appsvc->operation, tmp1);
2740                 appsvc->operation = tmp1;
2741         }
2742         if (appsvc->uri) {
2743                 LISTHEAD(appsvc->uri, tmp2);
2744                 appsvc->uri = tmp2;
2745         }
2746         if (appsvc->mime) {
2747                 LISTHEAD(appsvc->mime, tmp3);
2748                 appsvc->mime = tmp3;
2749         }
2750         if (appsvc->subapp) {
2751                 LISTHEAD(appsvc->subapp, tmp4);
2752                 appsvc->subapp = tmp4;
2753         }
2754
2755         xmlTextReaderRead(reader);
2756         if (xmlTextReaderValue(reader))
2757                 appsvc->text = ASCII(xmlTextReaderValue(reader));
2758
2759         return ret;
2760 }
2761
2762
2763 static int __ps_process_privileges(xmlTextReaderPtr reader, privileges_x *privileges)
2764 {
2765         const xmlChar *node;
2766         int ret = -1;
2767         int depth = -1;
2768         privilege_x *tmp1 = NULL;
2769
2770         depth = xmlTextReaderDepth(reader);
2771         while ((ret = __next_child_element(reader, depth))) {
2772                 node = xmlTextReaderConstName(reader);
2773                 if (!node) {
2774                         _LOGD("xmlTextReaderConstName value is NULL\n");
2775                         return -1;
2776                 }
2777
2778                 if (strcmp(ASCII(node), "privilege") == 0) {
2779                         privilege_x *privilege = malloc(sizeof(privilege_x));
2780                         if (privilege == NULL) {
2781                                 _LOGD("Malloc Failed\n");
2782                                 return -1;
2783                         }
2784                         memset(privilege, '\0', sizeof(privilege_x));
2785                         LISTADD(privileges->privilege, privilege);
2786                         ret = __ps_process_privilege(reader, privilege);
2787                 } else
2788                         return -1;
2789                 if (ret < 0) {
2790                         _LOGD("Processing privileges failed\n");
2791                         return ret;
2792                 }
2793         }
2794         if (privileges->privilege) {
2795                 LISTHEAD(privileges->privilege, tmp1);
2796                 privileges->privilege = tmp1;
2797         }
2798         return ret;
2799 }
2800
2801 static int __ps_process_launchconditions(xmlTextReaderPtr reader, launchconditions_x *launchconditions)
2802 {
2803         const xmlChar *node;
2804         int ret = -1;
2805         int depth = -1;
2806         condition_x *tmp1 = NULL;
2807
2808         depth = xmlTextReaderDepth(reader);
2809         while ((ret = __next_child_element(reader, depth))) {
2810                 node = xmlTextReaderConstName(reader);
2811                 if (!node) {
2812                         _LOGD("xmlTextReaderConstName value is NULL\n");
2813                         return -1;
2814                 }
2815
2816                 if (strcmp(ASCII(node), "condition") == 0) {
2817                         condition_x *condition = malloc(sizeof(condition_x));
2818                         if (condition == NULL) {
2819                                 _LOGD("Malloc Failed\n");
2820                                 return -1;
2821                         }
2822                         memset(condition, '\0', sizeof(condition_x));
2823                         LISTADD(launchconditions->condition, condition);
2824                         ret = __ps_process_condition(reader, condition);
2825                 } else
2826                         return -1;
2827                 if (ret < 0) {
2828                         _LOGD("Processing launchconditions failed\n");
2829                         return ret;
2830                 }
2831         }
2832         if (launchconditions->condition) {
2833                 LISTHEAD(launchconditions->condition, tmp1);
2834                 launchconditions->condition = tmp1;
2835         }
2836
2837         __save_xml_value(reader, &launchconditions->text);
2838
2839         return ret;
2840 }
2841
2842 static int __ps_process_datashare(xmlTextReaderPtr reader, datashare_x *datashare)
2843 {
2844         const xmlChar *node;
2845         int ret = -1;
2846         int depth = -1;
2847         define_x *tmp1 = NULL;
2848         request_x *tmp2 = NULL;
2849         depth = xmlTextReaderDepth(reader);
2850         while ((ret = __next_child_element(reader, depth))) {
2851                 node = xmlTextReaderConstName(reader);
2852                 if (!node) {
2853                         _LOGD("xmlTextReaderConstName value is NULL\n");
2854                         return -1;
2855                 }
2856
2857                 if (!strcmp(ASCII(node), "define")) {
2858                         define_x *define= malloc(sizeof(define_x));
2859                         if (define == NULL) {
2860                                 _LOGD("Malloc Failed\n");
2861                                 return -1;
2862                         }
2863                         memset(define, '\0', sizeof(define_x));
2864                         LISTADD(datashare->define, define);
2865                         ret = __ps_process_define(reader, define);
2866                 } else if (!strcmp(ASCII(node), "request")) {
2867                         request_x *request= malloc(sizeof(request_x));
2868                         if (request == NULL) {
2869                                 _LOGD("Malloc Failed\n");
2870                                 return -1;
2871                         }
2872                         memset(request, '\0', sizeof(request_x));
2873                         LISTADD(datashare->request, request);
2874                         ret = __ps_process_request(reader, request);
2875                 } else
2876                         return -1;
2877                 if (ret < 0) {
2878                         _LOGD("Processing data-share failed\n");
2879                         return ret;
2880                 }
2881         }
2882         if (datashare->define) {
2883                 LISTHEAD(datashare->define, tmp1);
2884                 datashare->define = tmp1;
2885         }
2886         if (datashare->request) {
2887                 LISTHEAD(datashare->request, tmp2);
2888                 datashare->request = tmp2;
2889         }
2890         return ret;
2891 }
2892
2893 static char*
2894 __get_icon_with_path(const char* icon, uid_t uid)
2895 {
2896         if (!icon)
2897                 return NULL;
2898
2899         if (index(icon, '/') == NULL) {
2900                 char* theme = NULL;
2901                 char* icon_with_path = NULL;
2902                 char *app_path = NULL;
2903                 int len;
2904
2905                 if (!package)
2906                         return NULL;
2907
2908 /* "db/setting/theme" is not exist */
2909 #if 0
2910                 theme = vconf_get_str("db/setting/theme");
2911                 if (!theme) {
2912                         theme = strdup("default");
2913                         if(!theme) {
2914                                 return NULL;
2915                         }
2916                 }
2917 #else
2918                 theme = strdup("default");
2919 #endif
2920
2921                 len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
2922                 icon_with_path = malloc(len);
2923                 if(icon_with_path == NULL) {
2924                         _LOGD("(icon_with_path == NULL) return\n");
2925                         free(theme);
2926                         return NULL;
2927                 }
2928
2929                 memset(icon_with_path, 0, len);
2930                 if (uid != GLOBAL_USER)
2931                         snprintf(icon_with_path, len, "%s%s", getIconPath(uid), icon);
2932                 else {
2933                         snprintf(icon_with_path, len, "%s%s/small/%s", getIconPath(GLOBAL_USER), theme, icon);
2934                         if (access (icon_with_path, F_OK)) { //If doesn't exist in case of Global app, try to get icon directly into app's directory
2935                                 app_path = tzplatform_getenv(TZ_SYS_RW_APP);
2936                                 if (app_path)
2937                                         snprintf(icon_with_path, len, "%s/%q/res/icons/%q/small/%q", app_path, package, theme, icon);
2938                                 if (access (icon_with_path, F_OK))
2939                                         _LOGE("Cannot find icon path");
2940                         }
2941                 }
2942                 free(theme);
2943                 _LOGD("Icon path : %s ---> %s", icon, icon_with_path);
2944                 return icon_with_path;
2945         } else {
2946                 char* confirmed_icon = NULL;
2947
2948                 confirmed_icon = strdup(icon);
2949                 if (!confirmed_icon)
2950                         return NULL;
2951                 return confirmed_icon;
2952         }
2953 }
2954
2955 static void __ps_process_tag(manifest_x * mfx, char *const tagv[])
2956 {
2957         int i = 0;
2958         char delims[] = "=";
2959         char *ret_result = NULL;
2960         char *tag = NULL;
2961
2962         if (tagv == NULL)
2963                 return;
2964
2965         for (tag = strdup(tagv[0]); tag != NULL; ) {
2966                 ret_result = strtok(tag, delims);
2967
2968                 /*check tag :  preload */
2969                 if (strcmp(ret_result, "preload") == 0) {
2970                         ret_result = strtok(NULL, delims);
2971                         if (strcmp(ret_result, "true") == 0) {
2972                                 free((void *)mfx->preload);
2973                                 mfx->preload = strdup("true");
2974                         } else if (strcmp(ret_result, "false") == 0) {
2975                                 free((void *)mfx->preload);
2976                                 mfx->preload = strdup("false");
2977                         }
2978                 /*check tag :  removable*/
2979                 } else if (strcmp(ret_result, "removable") == 0) {
2980                         ret_result = strtok(NULL, delims);
2981                         if (strcmp(ret_result, "true") == 0){
2982                                 free((void *)mfx->removable);
2983                                 mfx->removable = strdup("true");
2984                         } else if (strcmp(ret_result, "false") == 0) {
2985                                 free((void *)mfx->removable);
2986                                 mfx->removable = strdup("false");
2987                         }
2988                 /*check tag :  not matched*/
2989                 } else
2990                         _LOGD("tag process [%s]is not defined\n", ret_result);
2991
2992                 free(tag);
2993
2994                 /*check next value*/
2995                 if (tagv[++i] != NULL)
2996                         tag = strdup(tagv[i]);
2997                 else {
2998                         _LOGD("tag process success...\n");
2999                         return;
3000                 }
3001         }
3002 }
3003
3004 static int __ps_process_icon(xmlTextReaderPtr reader, icon_x *icon, uid_t uid)
3005 {
3006         __save_xml_attribute(reader, "name", &icon->name, NULL);
3007         __save_xml_attribute(reader, "section", &icon->section, NULL);
3008         __save_xml_attribute(reader, "size", &icon->size, NULL);
3009         __save_xml_attribute(reader, "resolution", &icon->resolution, NULL);
3010         __save_xml_lang(reader, &icon->lang);
3011
3012         xmlTextReaderRead(reader);
3013         const char *text  = ASCII(xmlTextReaderValue(reader));
3014         if (text) {
3015                 icon->text = (const char *)__get_icon_with_path(text, uid);
3016                 free((void *)text);
3017         }
3018
3019         return 0;
3020 }
3021
3022 static int __ps_process_image(xmlTextReaderPtr reader, image_x *image)
3023 {
3024         __save_xml_attribute(reader, "name", &image->name, NULL);
3025         __save_xml_attribute(reader, "section", &image->section, NULL);
3026         __save_xml_lang(reader, &image->lang);
3027         __save_xml_value(reader, &image->text);
3028         return 0;
3029 }
3030
3031 static int __ps_process_label(xmlTextReaderPtr reader, label_x *label)
3032 {
3033         __save_xml_attribute(reader, "name", &label->name, NULL);
3034         __save_xml_lang(reader, &label->lang);
3035         __save_xml_value(reader, &label->text);
3036         return 0;
3037
3038 }
3039
3040 static int __ps_process_author(xmlTextReaderPtr reader, author_x *author)
3041 {
3042         __save_xml_attribute(reader, "email", &author->email, NULL);
3043         __save_xml_attribute(reader, "href", &author->href, NULL);
3044         __save_xml_lang(reader, &author->lang);
3045         __save_xml_value(reader, &author->text);
3046         return 0;
3047 }
3048
3049 static int __ps_process_description(xmlTextReaderPtr reader, description_x *description)
3050 {
3051         __save_xml_lang(reader, &description->lang);
3052         __save_xml_value(reader, &description->text);
3053         return 0;
3054 }
3055
3056 static int __ps_process_license(xmlTextReaderPtr reader, license_x *license)
3057 {
3058         __save_xml_lang(reader, &license->lang);
3059         __save_xml_value(reader, &license->text);
3060         return 0;
3061 }
3062
3063 static int __ps_process_datacontrol(xmlTextReaderPtr reader, datacontrol_x *datacontrol)
3064 {
3065         __save_xml_attribute(reader, "providerid", &datacontrol->providerid, NULL);
3066         __save_xml_attribute(reader, "access", &datacontrol->access, NULL);
3067         __save_xml_attribute(reader, "type", &datacontrol->type, NULL);
3068         return 0;
3069 }
3070
3071 static int __ps_process_uiapplication(xmlTextReaderPtr reader, uiapplication_x *uiapplication, uid_t uid)
3072 {
3073         const xmlChar *node;
3074         int ret = -1;
3075         int depth = -1;
3076         label_x *tmp1 = NULL;
3077         icon_x *tmp2 = NULL;
3078         appsvc_x *tmp3 = NULL;
3079         appcontrol_x *tmp4 = NULL;
3080         launchconditions_x *tmp5 = NULL;
3081         notification_x *tmp6 = NULL;
3082         datashare_x *tmp7 = NULL;
3083         category_x *tmp8 = NULL;
3084         metadata_x *tmp9 = NULL;
3085         image_x *tmp10 = NULL;
3086         permission_x *tmp11 = NULL;
3087         datacontrol_x *tmp12 = NULL;
3088
3089         __save_xml_attribute(reader, "appid", &uiapplication->appid, NULL);
3090         retvm_if(uiapplication->appid == NULL, PM_PARSER_R_ERROR, "appid cant be NULL, appid field is mandatory\n");
3091         __save_xml_attribute(reader, "exec", &uiapplication->exec, NULL);
3092         __save_xml_attribute(reader, "nodisplay", &uiapplication->nodisplay, "false");
3093         __save_xml_attribute(reader, "multiple", &uiapplication->multiple, "false");
3094         __save_xml_attribute(reader, "type", &uiapplication->type, NULL);
3095         __save_xml_attribute(reader, "categories", &uiapplication->categories, NULL);
3096         __save_xml_attribute(reader, "extraid", &uiapplication->extraid, NULL);
3097         __save_xml_attribute(reader, "taskmanage", &uiapplication->taskmanage, "true");
3098         __save_xml_attribute(reader, "enabled", &uiapplication->enabled, "true");
3099         __save_xml_attribute(reader, "hw-acceleration", &uiapplication->hwacceleration, "default");
3100         __save_xml_attribute(reader, "screen-reader", &uiapplication->screenreader, "use-system-setting");
3101         __save_xml_attribute(reader, "mainapp", &uiapplication->mainapp, "false");
3102         __save_xml_attribute(reader, "recentimage", &uiapplication->recentimage, "false");
3103         __save_xml_attribute(reader, "launchcondition", &uiapplication->launchcondition, "false");
3104         __save_xml_attribute(reader, "indicatordisplay", &uiapplication->indicatordisplay, "true");
3105         __save_xml_attribute(reader, "portrait-effectimage", &uiapplication->portraitimg, NULL);
3106         __save_xml_attribute(reader, "landscape-effectimage", &uiapplication->landscapeimg, NULL);
3107         __save_xml_attribute(reader, "guestmode-visibility", &uiapplication->guestmode_visibility, "true");
3108         __save_xml_attribute(reader, "permission-type", &uiapplication->permission_type, "normal");
3109         __save_xml_attribute(reader, "component-type", &uiapplication->component_type, "uiapp");
3110         /*component_type has "svcapp" or "uiapp", if it is not, parsing manifest is fail*/
3111         retvm_if(((strcmp(uiapplication->component_type, "svcapp") != 0) && (strcmp(uiapplication->component_type, "uiapp") != 0) && (strcmp(uiapplication->component_type, "widgetapp") != 0)), PM_PARSER_R_ERROR, "invalid component_type[%s]\n", uiapplication->component_type);
3112         __save_xml_attribute(reader, "submode", &uiapplication->submode, "false");
3113         __save_xml_attribute(reader, "submode-mainid", &uiapplication->submode_mainid, NULL);
3114         __save_xml_attribute(reader, "launch_mode", &uiapplication->launch_mode, "caller");
3115
3116         uiapplication->package= strdup(package);
3117
3118         depth = xmlTextReaderDepth(reader);
3119         while ((ret = __next_child_element(reader, depth))) {
3120                 node = xmlTextReaderConstName(reader);
3121                 if (!node) {
3122                         _LOGD("xmlTextReaderConstName value is NULL\n");
3123                         return -1;
3124                 }
3125                 if (!strcmp(ASCII(node), "label")) {
3126                         label_x *label = malloc(sizeof(label_x));
3127                         if (label == NULL) {
3128                                 _LOGD("Malloc Failed\n");
3129                                 return -1;
3130                         }
3131                         memset(label, '\0', sizeof(label_x));
3132                         LISTADD(uiapplication->label, label);
3133                         ret = __ps_process_label(reader, label);
3134                 } else if (!strcmp(ASCII(node), "icon")) {
3135                         icon_x *icon = malloc(sizeof(icon_x));
3136                         if (icon == NULL) {
3137                                 _LOGD("Malloc Failed\n");
3138                                 return -1;
3139                         }
3140                         memset(icon, '\0', sizeof(icon_x));
3141                         LISTADD(uiapplication->icon, icon);
3142                         ret = __ps_process_icon(reader, icon, uid);
3143                 } else if (!strcmp(ASCII(node), "image")) {
3144                         image_x *image = malloc(sizeof(image_x));
3145                         if (image == NULL) {
3146                                 _LOGD("Malloc Failed\n");
3147                                 return -1;
3148                         }
3149                         memset(image, '\0', sizeof(image_x));
3150                         LISTADD(uiapplication->image, image);
3151                         ret = __ps_process_image(reader, image);
3152                 } else if (!strcmp(ASCII(node), "category")) {
3153                         category_x *category = malloc(sizeof(category_x));
3154                         if (category == NULL) {
3155                                 _LOGD("Malloc Failed\n");
3156                                 return -1;
3157                         }
3158                         memset(category, '\0', sizeof(category_x));
3159                         LISTADD(uiapplication->category, category);
3160                         ret = __ps_process_category(reader, category);
3161                 } else if (!strcmp(ASCII(node), "metadata")) {
3162                         metadata_x *metadata = malloc(sizeof(metadata_x));
3163                         if (metadata == NULL) {
3164                                 _LOGD("Malloc Failed\n");
3165                                 return -1;
3166                         }
3167                         memset(metadata, '\0', sizeof(metadata_x));
3168                         LISTADD(uiapplication->metadata, metadata);
3169                         ret = __ps_process_metadata(reader, metadata);
3170                 } else if (!strcmp(ASCII(node), "permission")) {
3171                         permission_x *permission = malloc(sizeof(permission_x));
3172                         if (permission == NULL) {
3173                                 _LOGD("Malloc Failed\n");
3174                                 return -1;
3175                         }
3176                         memset(permission, '\0', sizeof(permission_x));
3177                         LISTADD(uiapplication->permission, permission);
3178                         ret = __ps_process_permission(reader, permission);
3179                 } else if (!strcmp(ASCII(node), "app-control")) {
3180                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
3181                         if (appcontrol == NULL) {
3182                                 _LOGD("Malloc Failed\n");
3183                                 return -1;
3184                         }
3185                         memset(appcontrol, '\0', sizeof(appcontrol_x));
3186                         LISTADD(uiapplication->appcontrol, appcontrol);
3187                         ret = __ps_process_appcontrol(reader, appcontrol);
3188                 } else if (!strcmp(ASCII(node), "application-service")) {
3189                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
3190                         if (appsvc == NULL) {
3191                                 _LOGD("Malloc Failed\n");
3192                                 return -1;
3193                         }
3194                         memset(appsvc, '\0', sizeof(appsvc_x));
3195                         LISTADD(uiapplication->appsvc, appsvc);
3196                         ret = __ps_process_appsvc(reader, appsvc);
3197                 } else if (!strcmp(ASCII(node), "data-share")) {
3198                         datashare_x *datashare = malloc(sizeof(datashare_x));
3199                         if (datashare == NULL) {
3200                                 _LOGD("Malloc Failed\n");
3201                                 return -1;
3202                         }
3203                         memset(datashare, '\0', sizeof(datashare_x));
3204                         LISTADD(uiapplication->datashare, datashare);
3205                         ret = __ps_process_datashare(reader, datashare);
3206                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
3207                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
3208                         if (launchconditions == NULL) {
3209                                 _LOGD("Malloc Failed\n");
3210                                 return -1;
3211                         }
3212                         memset(launchconditions, '\0', sizeof(launchconditions_x));
3213                         LISTADD(uiapplication->launchconditions, launchconditions);
3214                         ret = __ps_process_launchconditions(reader, launchconditions);
3215                 } else if (!strcmp(ASCII(node), "notification")) {
3216                         notification_x *notification = malloc(sizeof(notification_x));
3217                         if (notification == NULL) {
3218                                 _LOGD("Malloc Failed\n");
3219                                 return -1;
3220                         }
3221                         memset(notification, '\0', sizeof(notification_x));
3222                         LISTADD(uiapplication->notification, notification);
3223                         ret = __ps_process_notification(reader, notification);
3224                 } else if (!strcmp(ASCII(node), "datacontrol")) {
3225                         datacontrol_x *datacontrol = malloc(sizeof(datacontrol_x));
3226                         if (datacontrol == NULL) {
3227                                 _LOGD("Malloc Failed\n");
3228                                 return -1;
3229                         }
3230                         memset(datacontrol, '\0', sizeof(datacontrol_x));
3231                         LISTADD(uiapplication->datacontrol, datacontrol);
3232                         ret = __ps_process_datacontrol(reader, datacontrol);
3233                 } else
3234                         return -1;
3235                 if (ret < 0) {
3236                         _LOGD("Processing uiapplication failed\n");
3237                         return ret;
3238                 }
3239         }
3240
3241         if (uiapplication->label) {
3242                 LISTHEAD(uiapplication->label, tmp1);
3243                 uiapplication->label = tmp1;
3244         }
3245         if (uiapplication->icon) {
3246                 LISTHEAD(uiapplication->icon, tmp2);
3247                 uiapplication->icon = tmp2;
3248         }
3249         if (uiapplication->appsvc) {
3250                 LISTHEAD(uiapplication->appsvc, tmp3);
3251                 uiapplication->appsvc = tmp3;
3252         }
3253         if (uiapplication->appcontrol) {
3254                 LISTHEAD(uiapplication->appcontrol, tmp4);
3255                 uiapplication->appcontrol = tmp4;
3256         }
3257         if (uiapplication->launchconditions) {
3258                 LISTHEAD(uiapplication->launchconditions, tmp5);
3259                 uiapplication->launchconditions = tmp5;
3260         }
3261         if (uiapplication->notification) {
3262                 LISTHEAD(uiapplication->notification, tmp6);
3263                 uiapplication->notification = tmp6;
3264         }
3265         if (uiapplication->datashare) {
3266                 LISTHEAD(uiapplication->datashare, tmp7);
3267                 uiapplication->datashare = tmp7;
3268         }
3269         if (uiapplication->category) {
3270                 LISTHEAD(uiapplication->category, tmp8);
3271                 uiapplication->category = tmp8;
3272         }
3273         if (uiapplication->metadata) {
3274                 LISTHEAD(uiapplication->metadata, tmp9);
3275                 uiapplication->metadata = tmp9;
3276         }
3277         if (uiapplication->image) {
3278                 LISTHEAD(uiapplication->image, tmp10);
3279                 uiapplication->image = tmp10;
3280         }
3281         if (uiapplication->permission) {
3282                 LISTHEAD(uiapplication->permission, tmp11);
3283                 uiapplication->permission = tmp11;
3284         }
3285         if (uiapplication->datacontrol) {
3286                 LISTHEAD(uiapplication->datacontrol, tmp12);
3287                 uiapplication->datacontrol = tmp12;
3288         }
3289
3290         return ret;
3291 }
3292
3293 static int __ps_process_serviceapplication(xmlTextReaderPtr reader, serviceapplication_x *serviceapplication, uid_t uid)
3294 {
3295         const xmlChar *node;
3296         int ret = -1;
3297         int depth = -1;
3298         label_x *tmp1 = NULL;
3299         icon_x *tmp2 = NULL;
3300         appsvc_x *tmp3 = NULL;
3301         appcontrol_x *tmp4 = NULL;
3302         datacontrol_x *tmp5 = NULL;
3303         launchconditions_x *tmp6 = NULL;
3304         notification_x *tmp7 = NULL;
3305         datashare_x *tmp8 = NULL;
3306         category_x *tmp9 = NULL;
3307         metadata_x *tmp10 = NULL;
3308         permission_x *tmp11 = NULL;
3309
3310         __save_xml_attribute(reader, "appid", &serviceapplication->appid, NULL);
3311         retvm_if(serviceapplication->appid == NULL, PM_PARSER_R_ERROR, "appid cant be NULL, appid field is mandatory\n");
3312         __save_xml_attribute(reader, "exec", &serviceapplication->exec, NULL);
3313         __save_xml_attribute(reader, "type", &serviceapplication->type, NULL);
3314         __save_xml_attribute(reader, "enabled", &serviceapplication->enabled, "true");
3315         __save_xml_attribute(reader, "permission-type", &serviceapplication->permission_type, "normal");
3316         __save_xml_attribute(reader, "auto-restart", &serviceapplication->autorestart, "false");
3317         __save_xml_attribute(reader, "on-boot", &serviceapplication->onboot, "false");
3318
3319         serviceapplication->package= strdup(package);
3320
3321         depth = xmlTextReaderDepth(reader);
3322         while ((ret = __next_child_element(reader, depth))) {
3323                 node = xmlTextReaderConstName(reader);
3324                 if (!node) {
3325                         _LOGD("xmlTextReaderConstName value is NULL\n");
3326                         return -1;
3327                 }
3328
3329                 if (!strcmp(ASCII(node), "label")) {
3330                         label_x *label = malloc(sizeof(label_x));
3331                         if (label == NULL) {
3332                                 _LOGD("Malloc Failed\n");
3333                                 return -1;
3334                         }
3335                         memset(label, '\0', sizeof(label_x));
3336                         LISTADD(serviceapplication->label, label);
3337                         ret = __ps_process_label(reader, label);
3338                 } else if (!strcmp(ASCII(node), "icon")) {
3339                         icon_x *icon = malloc(sizeof(icon_x));
3340                         if (icon == NULL) {
3341                                 _LOGD("Malloc Failed\n");
3342                                 return -1;
3343                         }
3344                         memset(icon, '\0', sizeof(icon_x));
3345                         LISTADD(serviceapplication->icon, icon);
3346                         ret = __ps_process_icon(reader, icon, uid);
3347                 } else if (!strcmp(ASCII(node), "category")) {
3348                         category_x *category = malloc(sizeof(category_x));
3349                         if (category == NULL) {
3350                                 _LOGD("Malloc Failed\n");
3351                                 return -1;
3352                         }
3353                         memset(category, '\0', sizeof(category_x));
3354                         LISTADD(serviceapplication->category, category);
3355                         ret = __ps_process_category(reader, category);
3356                 } else if (!strcmp(ASCII(node), "metadata")) {
3357                         metadata_x *metadata = malloc(sizeof(metadata_x));
3358                         if (metadata == NULL) {
3359                                 _LOGD("Malloc Failed\n");
3360                                 return -1;
3361                         }
3362                         memset(metadata, '\0', sizeof(metadata_x));
3363                         LISTADD(serviceapplication->metadata, metadata);
3364                         ret = __ps_process_metadata(reader, metadata);
3365                 } else if (!strcmp(ASCII(node), "permission")) {
3366                         permission_x *permission = malloc(sizeof(permission_x));
3367                         if (permission == NULL) {
3368                                 _LOGD("Malloc Failed\n");
3369                                 return -1;
3370                         }
3371                         memset(permission, '\0', sizeof(permission_x));
3372                         LISTADD(serviceapplication->permission, permission);
3373                         ret = __ps_process_permission(reader, permission);
3374                 } else if (!strcmp(ASCII(node), "app-control")) {
3375                         appcontrol_x *appcontrol = malloc(sizeof(appcontrol_x));
3376                         if (appcontrol == NULL) {
3377                                 _LOGD("Malloc Failed\n");
3378                                 return -1;
3379                         }
3380                         memset(appcontrol, '\0', sizeof(appcontrol_x));
3381                         LISTADD(serviceapplication->appcontrol, appcontrol);
3382                         ret = __ps_process_appcontrol(reader, appcontrol);
3383                 } else if (!strcmp(ASCII(node), "application-service")) {
3384                         appsvc_x *appsvc = malloc(sizeof(appsvc_x));
3385                         if (appsvc == NULL) {
3386                                 _LOGD("Malloc Failed\n");
3387                                 return -1;
3388                         }
3389                         memset(appsvc, '\0', sizeof(appsvc_x));
3390                         LISTADD(serviceapplication->appsvc, appsvc);
3391                         ret = __ps_process_appsvc(reader, appsvc);
3392                 } else if (!strcmp(ASCII(node), "data-share")) {
3393                         datashare_x *datashare = malloc(sizeof(datashare_x));
3394                         if (datashare == NULL) {
3395                                 _LOGD("Malloc Failed\n");
3396                                 return -1;
3397                         }
3398                         memset(datashare, '\0', sizeof(datashare_x));
3399                         LISTADD(serviceapplication->datashare, datashare);
3400                         ret = __ps_process_datashare(reader, datashare);
3401                 } else if (!strcmp(ASCII(node), "launch-conditions")) {
3402                         launchconditions_x *launchconditions = malloc(sizeof(launchconditions_x));
3403                         if (launchconditions == NULL) {
3404                                 _LOGD("Malloc Failed\n");
3405                                 return -1;
3406                         }
3407                         memset(launchconditions, '\0', sizeof(launchconditions_x));
3408                         LISTADD(serviceapplication->launchconditions, launchconditions);
3409                         ret = __ps_process_launchconditions(reader, launchconditions);
3410                 } else if (!strcmp(ASCII(node), "notification")) {
3411                         notification_x *notification = malloc(sizeof(notification_x));
3412                         if (notification == NULL) {
3413                                 _LOGD("Malloc Failed\n");
3414                                 return -1;
3415                         }
3416                         memset(notification, '\0', sizeof(notification_x));
3417                         LISTADD(serviceapplication->notification, notification);
3418                         ret = __ps_process_notification(reader, notification);
3419                 } else if (!strcmp(ASCII(node), "datacontrol")) {
3420                         datacontrol_x *datacontrol = malloc(sizeof(datacontrol_x));
3421                         if (datacontrol == NULL) {
3422                                 _LOGD("Malloc Failed\n");
3423                                 return -1;
3424                         }
3425                         memset(datacontrol, '\0', sizeof(datacontrol_x));
3426                         LISTADD(serviceapplication->datacontrol, datacontrol);
3427                         ret = __ps_process_datacontrol(reader, datacontrol);
3428                 } else
3429                         return -1;
3430                 if (ret < 0) {
3431                         _LOGD("Processing serviceapplication failed\n");
3432                         return ret;
3433                 }
3434         }
3435
3436         if (serviceapplication->label) {
3437                 LISTHEAD(serviceapplication->label, tmp1);
3438                 serviceapplication->label = tmp1;
3439         }
3440         if (serviceapplication->icon) {
3441                 LISTHEAD(serviceapplication->icon, tmp2);
3442                 serviceapplication->icon = tmp2;
3443         }
3444         if (serviceapplication->appsvc) {
3445                 LISTHEAD(serviceapplication->appsvc, tmp3);
3446                 serviceapplication->appsvc = tmp3;
3447         }
3448         if (serviceapplication->appcontrol) {
3449                 LISTHEAD(serviceapplication->appcontrol, tmp4);
3450                 serviceapplication->appcontrol = tmp4;
3451         }
3452         if (serviceapplication->datacontrol) {
3453                 LISTHEAD(serviceapplication->datacontrol, tmp5);
3454                 serviceapplication->datacontrol = tmp5;
3455         }
3456         if (serviceapplication->launchconditions) {
3457                 LISTHEAD(serviceapplication->launchconditions, tmp6);
3458                 serviceapplication->launchconditions = tmp6;
3459         }
3460         if (serviceapplication->notification) {
3461                 LISTHEAD(serviceapplication->notification, tmp7);
3462                 serviceapplication->notification = tmp7;
3463         }
3464         if (serviceapplication->datashare) {
3465                 LISTHEAD(serviceapplication->datashare, tmp8);
3466                 serviceapplication->datashare = tmp8;
3467         }
3468         if (serviceapplication->category) {
3469                 LISTHEAD(serviceapplication->category, tmp9);
3470                 serviceapplication->category = tmp9;
3471         }
3472         if (serviceapplication->metadata) {
3473                 LISTHEAD(serviceapplication->metadata, tmp10);
3474                 serviceapplication->metadata = tmp10;
3475         }
3476         if (serviceapplication->permission) {
3477                 LISTHEAD(serviceapplication->permission, tmp11);
3478                 serviceapplication->permission = tmp11;
3479         }
3480
3481         return ret;
3482 }
3483
3484 static int __ps_process_deviceprofile(xmlTextReaderPtr reader, deviceprofile_x *deviceprofile)
3485 {
3486         /*TODO: once policy is set*/
3487         return 0;
3488 }
3489
3490 static int __ps_process_font(xmlTextReaderPtr reader, font_x *font)
3491 {
3492         /*TODO: once policy is set*/
3493         return 0;
3494 }
3495
3496 static int __ps_process_theme(xmlTextReaderPtr reader, theme_x *theme)
3497 {
3498         /*TODO: once policy is set*/
3499         return 0;
3500 }
3501
3502 static int __ps_process_daemon(xmlTextReaderPtr reader, daemon_x *daemon)
3503 {
3504         /*TODO: once policy is set*/
3505         return 0;
3506 }
3507
3508 static int __ps_process_ime(xmlTextReaderPtr reader, ime_x *ime)
3509 {
3510         /*TODO: once policy is set*/
3511         return 0;
3512 }
3513
3514 static int __start_process(xmlTextReaderPtr reader, manifest_x * mfx, uid_t uid)
3515 {
3516         _LOGD("__start_process\n");
3517         const xmlChar *node;
3518         int ret = -1;
3519         int depth = -1;
3520         label_x *tmp1 = NULL;
3521         author_x *tmp2 = NULL;
3522         description_x *tmp3 = NULL;
3523         license_x *tmp4 = NULL;
3524         uiapplication_x *tmp5 = NULL;
3525         serviceapplication_x *tmp6 = NULL;
3526         daemon_x *tmp7 = NULL;
3527         theme_x *tmp8 = NULL;
3528         font_x *tmp9 = NULL;
3529         ime_x *tmp10 = NULL;
3530         icon_x *tmp11 = NULL;
3531         compatibility_x *tmp12 = NULL;
3532         deviceprofile_x *tmp13 = NULL;
3533         privileges_x *tmp14 = NULL;
3534
3535         depth = xmlTextReaderDepth(reader);
3536         while ((ret = __next_child_element(reader, depth))) {
3537                 node = xmlTextReaderConstName(reader);
3538                 if (!node) {
3539                         _LOGD("xmlTextReaderConstName value is NULL\n");
3540                         return -1;
3541                 }
3542
3543                 if (!strcmp(ASCII(node), "label")) {
3544                         label_x *label = malloc(sizeof(label_x));
3545                         if (label == NULL) {
3546                                 _LOGD("Malloc Failed\n");
3547                                 return -1;
3548                         }
3549                         memset(label, '\0', sizeof(label_x));
3550                         LISTADD(mfx->label, label);
3551                         ret = __ps_process_label(reader, label);
3552                 } else if (!strcmp(ASCII(node), "author")) {
3553                         author_x *author = malloc(sizeof(author_x));
3554                         if (author == NULL) {
3555                                 _LOGD("Malloc Failed\n");
3556                                 return -1;
3557                         }
3558                         memset(author, '\0', sizeof(author_x));
3559                         LISTADD(mfx->author, author);
3560                         ret = __ps_process_author(reader, author);
3561                 } else if (!strcmp(ASCII(node), "description")) {
3562                         description_x *description = malloc(sizeof(description_x));
3563                         if (description == NULL) {
3564                                 _LOGD("Malloc Failed\n");
3565                                 return -1;
3566                         }
3567                         memset(description, '\0', sizeof(description_x));
3568                         LISTADD(mfx->description, description);
3569                         ret = __ps_process_description(reader, description);
3570                 } else if (!strcmp(ASCII(node), "license")) {
3571                         license_x *license = malloc(sizeof(license_x));
3572                         if (license == NULL) {
3573                                 _LOGD("Malloc Failed\n");
3574                                 return -1;
3575                         }
3576                         memset(license, '\0', sizeof(license_x));
3577                         LISTADD(mfx->license, license);
3578                         ret = __ps_process_license(reader, license);
3579                 } else if (!strcmp(ASCII(node), "privileges")) {
3580                         privileges_x *privileges = malloc(sizeof(privileges_x));
3581                         if (privileges == NULL) {
3582                                 _LOGD("Malloc Failed\n");
3583                                 return -1;
3584                         }
3585                         memset(privileges, '\0', sizeof(privileges_x));
3586                         LISTADD(mfx->privileges, privileges);
3587                         ret = __ps_process_privileges(reader, privileges);
3588                 } else if (!strcmp(ASCII(node), "ui-application")) {
3589                         uiapplication_x *uiapplication = malloc(sizeof(uiapplication_x));
3590                         if (uiapplication == NULL) {
3591                                 _LOGD("Malloc Failed\n");
3592                                 return -1;
3593                         }
3594                         memset(uiapplication, '\0', sizeof(uiapplication_x));
3595                         LISTADD(mfx->uiapplication, uiapplication);
3596                         ret = __ps_process_uiapplication(reader, uiapplication, uid);
3597                 } else if (!strcmp(ASCII(node), "service-application")) {
3598                         serviceapplication_x *serviceapplication = malloc(sizeof(serviceapplication_x));
3599                         if (serviceapplication == NULL) {
3600                                 _LOGD("Malloc Failed\n");
3601                                 return -1;
3602                         }
3603                         memset(serviceapplication, '\0', sizeof(serviceapplication_x));
3604                         LISTADD(mfx->serviceapplication, serviceapplication);
3605                         ret = __ps_process_serviceapplication(reader, serviceapplication, uid);
3606                 } else if (!strcmp(ASCII(node), "daemon")) {
3607                         daemon_x *daemon = malloc(sizeof(daemon_x));
3608                         if (daemon == NULL) {
3609                                 _LOGD("Malloc Failed\n");
3610                                 return -1;
3611                         }
3612                         memset(daemon, '\0', sizeof(daemon_x));
3613                         LISTADD(mfx->daemon, daemon);
3614                         ret = __ps_process_daemon(reader, daemon);
3615                 } else if (!strcmp(ASCII(node), "theme")) {
3616                         theme_x *theme = malloc(sizeof(theme_x));
3617                         if (theme == NULL) {
3618                                 _LOGD("Malloc Failed\n");
3619                                 return -1;
3620                         }
3621                         memset(theme, '\0', sizeof(theme_x));
3622                         LISTADD(mfx->theme, theme);
3623                         ret = __ps_process_theme(reader, theme);
3624                 } else if (!strcmp(ASCII(node), "font")) {
3625                         font_x *font = malloc(sizeof(font_x));
3626                         if (font == NULL) {
3627                                 _LOGD("Malloc Failed\n");
3628                                 return -1;
3629                         }
3630                         memset(font, '\0', sizeof(font_x));
3631                         LISTADD(mfx->font, font);
3632                         ret = __ps_process_font(reader, font);
3633                 } else if (!strcmp(ASCII(node), "ime")) {
3634                         ime_x *ime = malloc(sizeof(ime_x));
3635                         if (ime == NULL) {
3636                                 _LOGD("Malloc Failed\n");
3637                                 return -1;
3638                         }
3639                         memset(ime, '\0', sizeof(ime_x));
3640                         LISTADD(mfx->ime, ime);
3641                         ret = __ps_process_ime(reader, ime);
3642                 } else if (!strcmp(ASCII(node), "icon")) {
3643                         icon_x *icon = malloc(sizeof(icon_x));
3644                         if (icon == NULL) {
3645                                 _LOGD("Malloc Failed\n");
3646                                 return -1;
3647                         }
3648                         memset(icon, '\0', sizeof(icon_x));
3649                         LISTADD(mfx->icon, icon);
3650                         ret = __ps_process_icon(reader, icon, uid);
3651                 } else if (!strcmp(ASCII(node), "profile")) {
3652                         deviceprofile_x *deviceprofile = malloc(sizeof(deviceprofile_x));
3653                         if (deviceprofile == NULL) {
3654                                 _LOGD("Malloc Failed\n");
3655                                 return -1;
3656                         }
3657                         memset(deviceprofile, '\0', sizeof(deviceprofile_x));
3658                         LISTADD(mfx->deviceprofile, deviceprofile);
3659                         ret = __ps_process_deviceprofile(reader, deviceprofile);
3660                 } else if (!strcmp(ASCII(node), "compatibility")) {
3661                         compatibility_x *compatibility = malloc(sizeof(compatibility_x));
3662                         if (compatibility == NULL) {
3663                                 _LOGD("Malloc Failed\n");
3664                                 return -1;
3665                         }
3666                         memset(compatibility, '\0', sizeof(compatibility_x));
3667                         LISTADD(mfx->compatibility, compatibility);
3668                         ret = __ps_process_compatibility(reader, compatibility);
3669                 } else if (!strcmp(ASCII(node), "shortcut-list")) {
3670                         continue;
3671                 } else if (!strcmp(ASCII(node), "livebox")) {
3672                         continue;
3673                 } else if (!strcmp(ASCII(node), "account")) {
3674                         continue;
3675                 } else if (!strcmp(ASCII(node), "notifications")) {
3676                         continue;
3677                 } else if (!strcmp(ASCII(node), "ime")) {
3678                         continue;
3679                 } else if (!strcmp(ASCII(node), "feature")) {
3680                         continue;
3681                 } else {
3682                         _LOGE("Unknown element: %s", ASCII(node));
3683                         return -1;
3684                 }
3685
3686                 if (ret < 0) {
3687                         _LOGD("Processing manifest failed\n");
3688                         return ret;
3689                 }
3690         }
3691         if (mfx->label) {
3692                 LISTHEAD(mfx->label, tmp1);
3693                 mfx->label = tmp1;
3694         }
3695         if (mfx->author) {
3696                 LISTHEAD(mfx->author, tmp2);
3697                 mfx->author = tmp2;
3698         }
3699         if (mfx->description) {
3700                 LISTHEAD(mfx->description, tmp3);
3701                 mfx->description= tmp3;
3702         }
3703         if (mfx->license) {
3704                 LISTHEAD(mfx->license, tmp4);
3705                 mfx->license= tmp4;
3706         }
3707         if (mfx->uiapplication) {
3708                 LISTHEAD(mfx->uiapplication, tmp5);
3709                 mfx->uiapplication = tmp5;
3710         }
3711         if (mfx->serviceapplication) {
3712                 LISTHEAD(mfx->serviceapplication, tmp6);
3713                 mfx->serviceapplication = tmp6;
3714         }
3715         if (mfx->daemon) {
3716                 LISTHEAD(mfx->daemon, tmp7);
3717                 mfx->daemon= tmp7;
3718         }
3719         if (mfx->theme) {
3720                 LISTHEAD(mfx->theme, tmp8);
3721                 mfx->theme= tmp8;
3722         }
3723         if (mfx->font) {
3724                 LISTHEAD(mfx->font, tmp9);
3725                 mfx->font= tmp9;
3726         }
3727         if (mfx->ime) {
3728                 LISTHEAD(mfx->ime, tmp10);
3729                 mfx->ime= tmp10;
3730         }
3731         if (mfx->icon) {
3732                 LISTHEAD(mfx->icon, tmp11);
3733                 mfx->icon= tmp11;
3734         }
3735         if (mfx->compatibility) {
3736                 LISTHEAD(mfx->compatibility, tmp12);
3737                 mfx->compatibility= tmp12;
3738         }
3739         if (mfx->deviceprofile) {
3740                 LISTHEAD(mfx->deviceprofile, tmp13);
3741                 mfx->deviceprofile= tmp13;
3742         }
3743         if (mfx->privileges) {
3744                 LISTHEAD(mfx->privileges, tmp14);
3745                 mfx->privileges = tmp14;
3746         }
3747         return ret;
3748 }
3749
3750 static int __process_manifest(xmlTextReaderPtr reader, manifest_x *mfx, uid_t uid)
3751 {
3752         const xmlChar *node;
3753         int ret = -1;
3754
3755         if ((ret = __next_child_element(reader, -1))) {
3756                 node = xmlTextReaderConstName(reader);
3757                 if (!node) {
3758                         _LOGD("xmlTextReaderConstName value is NULL\n");
3759                         return -1;
3760                 }
3761
3762                 if (!strcmp(ASCII(node), "manifest")) {
3763                         __save_xml_attribute(reader, "xmlns", &mfx->ns, NULL);
3764                         __save_xml_attribute(reader, "package", &mfx->package, NULL);
3765                         retvm_if(mfx->package == NULL, PM_PARSER_R_ERROR, "package cant be NULL, package field is mandatory\n");
3766                         __save_xml_attribute(reader, "version", &mfx->version, NULL);
3767                         __save_xml_attribute(reader, "size", &mfx->package_size, NULL);
3768                         __save_xml_attribute(reader, "install-location", &mfx->installlocation, "internal-only");
3769                         __save_xml_attribute(reader, "type", &mfx->type, "rpm");
3770                         __save_xml_attribute(reader, "root_path", &mfx->root_path, NULL);
3771                         __save_xml_attribute(reader, "csc_path", &mfx->csc_path, NULL);
3772                         __save_xml_attribute(reader, "appsetting", &mfx->appsetting, "false");
3773                         __save_xml_attribute(reader, "storeclient-id", &mfx->storeclient_id, NULL);
3774                         __save_xml_attribute(reader, "nodisplay-setting", &mfx->nodisplay_setting, "false");
3775                         __save_xml_attribute(reader, "url", &mfx->package_url, NULL);
3776                         __save_xml_attribute(reader, "api-version", &mfx->api_version, NULL);
3777
3778                         __save_xml_installed_time(mfx);
3779                         __save_xml_root_path(mfx, uid);
3780                         /*Assign default values. If required it will be overwritten in __add_preload_info()*/
3781                         __save_xml_default_value(mfx);
3782
3783                         ret = __start_process(reader, mfx, uid);
3784                 } else {
3785                         _LOGD("No Manifest element found\n");
3786                         return -1;
3787                 }
3788         }
3789         return ret;
3790 }
3791
3792 static char* __convert_to_system_locale(const char *mlocale)
3793 {
3794         if (mlocale == NULL)
3795                 return NULL;
3796         char *locale = NULL;
3797         locale = (char *)calloc(1, 6);
3798         if (!locale) {
3799                 _LOGE("Malloc Failed\n");
3800                 return NULL;
3801         }
3802
3803         strncpy(locale, mlocale, 2);
3804         strncat(locale, "_", 1);
3805         locale[3] = toupper(mlocale[3]);
3806         locale[4] = toupper(mlocale[4]);
3807         return locale;
3808 }
3809
3810 #define LIBAPPSVC_PATH LIB_PATH "/libappsvc.so.0"
3811
3812 static int __ps_remove_appsvc_db(manifest_x *mfx, uid_t uid)
3813 {
3814         void *lib_handle = NULL;
3815         int (*appsvc_operation) (const char *, uid_t);
3816         int ret = 0;
3817         uiapplication_x *uiapplication = mfx->uiapplication;
3818
3819         if ((lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY)) == NULL) {
3820                 _LOGE("dlopen is failed LIBAPPSVC_PATH[%s]\n", LIBAPPSVC_PATH);
3821                 goto END;
3822         }
3823
3824         if ((appsvc_operation =
3825                  dlsym(lib_handle, "appsvc_unset_defapp")) == NULL || dlerror() != NULL) {
3826                 _LOGE("can not find symbol \n");
3827                 goto END;
3828         }
3829
3830         for(; uiapplication; uiapplication=uiapplication->next) {
3831                 ret = appsvc_operation(uiapplication->appid, uid);
3832                 if (ret <0)
3833                         _LOGE("can not operation  symbol \n");
3834         }
3835
3836 END:
3837         if (lib_handle)
3838                 dlclose(lib_handle);
3839
3840         return ret;
3841 }
3842
3843 #define PRELOAD_PACKAGE_LIST SYSCONFDIR "/package-manager/preload/preload_list.txt"
3844 static int __add_preload_info(manifest_x * mfx, const char *manifest, uid_t uid)
3845 {
3846         FILE *fp = NULL;
3847         char buffer[1024] = { 0 };
3848         int state = 0;
3849
3850         if(strstr(manifest, getUserManifestPath(uid))) {
3851                 free((void *)mfx->readonly);
3852                 mfx->readonly = strdup("True");
3853
3854                 free((void *)mfx->preload);
3855                 mfx->preload = strdup("True");
3856
3857                 free((void *)mfx->removable);
3858                 mfx->removable = strdup("False");
3859
3860                 free((void *)mfx->system);
3861                 mfx->system = strdup("True");
3862
3863                 return 0;
3864         }
3865
3866         fp = fopen(PRELOAD_PACKAGE_LIST, "r");
3867         if (fp == NULL) {
3868                 _LOGE("no preload list\n");
3869                 return -1;
3870         }
3871
3872         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
3873                 if (buffer[0] == '#') {
3874                         if(strcasestr(buffer, "RW_NORM"))
3875                                 state = 2;
3876                         else if(strcasestr(buffer, "RW_RM"))
3877                                 state = 3;
3878                         else
3879                                 continue;
3880                 }
3881
3882                 __str_trim(buffer);
3883
3884                 if(!strcmp(mfx->package, buffer)) {
3885                         free((void *)mfx->preload);
3886                         mfx->preload = strdup("True");
3887                         if(state == 2){
3888                                 free((void *)mfx->readonly);
3889                                 mfx->readonly = strdup("False");
3890                                 free((void *)mfx->removable);
3891                                 mfx->removable = strdup("False");
3892                         } else if(state == 3){
3893                                 free((void *)mfx->readonly);
3894                                 mfx->readonly = strdup("False");
3895                                 free((void *)mfx->removable);
3896                                 mfx->removable = strdup("True");
3897                         }
3898                 }
3899
3900                 memset(buffer, 0x00, sizeof(buffer));
3901         }
3902
3903         if (fp != NULL)
3904                 fclose(fp);
3905
3906         return 0;
3907 }
3908
3909 static int __check_preload_updated(manifest_x * mfx, const char *manifest, uid_t uid)
3910 {
3911         if (!strstr(manifest, getUserManifestPath(uid))) {
3912                 /* if downloaded app is updated, then update tag set true*/
3913                 if (mfx->update)
3914                         free((void *)mfx->update);
3915                 mfx->update = strdup("true");
3916         }
3917
3918         return 0;
3919 }
3920
3921
3922 API int pkgmgr_parser_create_desktop_file(manifest_x *mfx)
3923 {
3924         /* desktop file is no longer used */
3925         return 0;
3926 }
3927
3928 API int pkgmgr_parser_create_usr_desktop_file(manifest_x *mfx, uid_t uid)
3929 {
3930         /* desktop file is no longer used */
3931         return 0;
3932 }
3933
3934
3935 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
3936 {
3937         if (mfx == NULL)
3938                 return;
3939         if (mfx->ns) {
3940                 free((void *)mfx->ns);
3941                 mfx->ns = NULL;
3942         }
3943         if (mfx->package) {
3944                 free((void *)mfx->package);
3945                 mfx->package = NULL;
3946         }
3947         if (mfx->version) {
3948                 free((void *)mfx->version);
3949                 mfx->version = NULL;
3950         }
3951         if (mfx->installlocation) {
3952                 free((void *)mfx->installlocation);
3953                 mfx->installlocation = NULL;
3954         }
3955         if (mfx->preload) {
3956                 free((void *)mfx->preload);
3957                 mfx->preload = NULL;
3958         }
3959         if (mfx->readonly) {
3960                 free((void *)mfx->readonly);
3961                 mfx->readonly = NULL;
3962         }
3963         if (mfx->removable) {
3964                 free((void *)mfx->removable);
3965                 mfx->removable = NULL;
3966         }
3967         if (mfx->update) {
3968                 free((void *)mfx->update);
3969                 mfx->update = NULL;
3970         }
3971         if (mfx->system) {
3972                 free((void *)mfx->system);
3973                 mfx->system = NULL;
3974         }
3975         if (mfx->type) {
3976                 free((void *)mfx->type);
3977                 mfx->type = NULL;
3978         }
3979         if (mfx->package_size) {
3980                 free((void *)mfx->package_size);
3981                 mfx->package_size = NULL;
3982         }
3983         if (mfx->installed_time) {
3984                 free((void *)mfx->installed_time);
3985                 mfx->installed_time = NULL;
3986         }
3987         if (mfx->installed_storage) {
3988                 free((void *)mfx->installed_storage);
3989                 mfx->installed_storage = NULL;
3990         }
3991         if (mfx->storeclient_id) {
3992                 free((void *)mfx->storeclient_id);
3993                 mfx->storeclient_id = NULL;
3994         }
3995         if (mfx->mainapp_id) {
3996                 free((void *)mfx->mainapp_id);
3997                 mfx->mainapp_id = NULL;
3998         }
3999         if (mfx->package_url) {
4000                 free((void *)mfx->package_url);
4001                 mfx->package_url = NULL;
4002         }
4003         if (mfx->root_path) {
4004                 free((void *)mfx->root_path);
4005                 mfx->root_path = NULL;
4006         }
4007         if (mfx->csc_path) {
4008                 free((void *)mfx->csc_path);
4009                 mfx->csc_path = NULL;
4010         }
4011         if (mfx->appsetting) {
4012                 free((void *)mfx->appsetting);
4013                 mfx->appsetting = NULL;
4014         }
4015         if (mfx->nodisplay_setting) {
4016                 free((void *)mfx->nodisplay_setting);
4017                 mfx->nodisplay_setting = NULL;
4018         }
4019         if (mfx->api_version) {
4020                 free((void *)mfx->api_version);
4021                 mfx->api_version = NULL;
4022         }
4023
4024         /*Free Icon*/
4025         if (mfx->icon) {
4026                 icon_x *icon = mfx->icon;
4027                 icon_x *tmp = NULL;
4028                 while(icon != NULL) {
4029                         tmp = icon->next;
4030                         __ps_free_icon(icon);
4031                         icon = tmp;
4032                 }
4033         }
4034         /*Free Label*/
4035         if (mfx->label) {
4036                 label_x *label = mfx->label;
4037                 label_x *tmp = NULL;
4038                 while(label != NULL) {
4039                         tmp = label->next;
4040                         __ps_free_label(label);
4041                         label = tmp;
4042                 }
4043         }
4044         /*Free Author*/
4045         if (mfx->author) {
4046                 author_x *author = mfx->author;
4047                 author_x *tmp = NULL;
4048                 while(author != NULL) {
4049                         tmp = author->next;
4050                         __ps_free_author(author);
4051                         author = tmp;
4052                 }
4053         }
4054         /*Free Description*/
4055         if (mfx->description) {
4056                 description_x *description = mfx->description;
4057                 description_x *tmp = NULL;
4058                 while(description != NULL) {
4059                         tmp = description->next;
4060                         __ps_free_description(description);
4061                         description = tmp;
4062                 }
4063         }
4064         /*Free License*/
4065         if (mfx->license) {
4066                 license_x *license = mfx->license;
4067                 license_x *tmp = NULL;
4068                 while(license != NULL) {
4069                         tmp = license->next;
4070                         __ps_free_license(license);
4071                         license = tmp;
4072                 }
4073         }
4074         /*Free Privileges*/
4075         if (mfx->privileges) {
4076                 privileges_x *privileges = mfx->privileges;
4077                 privileges_x *tmp = NULL;
4078                 while(privileges != NULL) {
4079                         tmp = privileges->next;
4080                         __ps_free_privileges(privileges);
4081                         privileges = tmp;
4082                 }
4083         }
4084         /*Free UiApplication*/
4085         if (mfx->uiapplication) {
4086                 uiapplication_x *uiapplication = mfx->uiapplication;
4087                 uiapplication_x *tmp = NULL;
4088                 while(uiapplication != NULL) {
4089                         tmp = uiapplication->next;
4090                         __ps_free_uiapplication(uiapplication);
4091                         uiapplication = tmp;
4092                 }
4093         }
4094         /*Free ServiceApplication*/
4095         if (mfx->serviceapplication) {
4096                 serviceapplication_x *serviceapplication = mfx->serviceapplication;
4097                 serviceapplication_x *tmp = NULL;
4098                 while(serviceapplication != NULL) {
4099                         tmp = serviceapplication->next;
4100                         __ps_free_serviceapplication(serviceapplication);
4101                         serviceapplication = tmp;
4102                 }
4103         }
4104         /*Free Daemon*/
4105         if (mfx->daemon) {
4106                 daemon_x *daemon = mfx->daemon;
4107                 daemon_x *tmp = NULL;
4108                 while(daemon != NULL) {
4109                         tmp = daemon->next;
4110                         __ps_free_daemon(daemon);
4111                         daemon = tmp;
4112                 }
4113         }
4114         /*Free Theme*/
4115         if (mfx->theme) {
4116                 theme_x *theme = mfx->theme;
4117                 theme_x *tmp = NULL;
4118                 while(theme != NULL) {
4119                         tmp = theme->next;
4120                         __ps_free_theme(theme);
4121                         theme = tmp;
4122                 }
4123         }
4124         /*Free Font*/
4125         if (mfx->font) {
4126                 font_x *font = mfx->font;
4127                 font_x *tmp = NULL;
4128                 while(font != NULL) {
4129                         tmp = font->next;
4130                         __ps_free_font(font);
4131                         font = tmp;
4132                 }
4133         }
4134         /*Free Ime*/
4135         if (mfx->ime) {
4136                 ime_x *ime = mfx->ime;
4137                 ime_x *tmp = NULL;
4138                 while(ime != NULL) {
4139                         tmp = ime->next;
4140                         __ps_free_ime(ime);
4141                         ime = tmp;
4142                 }
4143         }
4144         /*Free Compatibility*/
4145         if (mfx->compatibility) {
4146                 compatibility_x *compatibility = mfx->compatibility;
4147                 compatibility_x *tmp = NULL;
4148                 while(compatibility != NULL) {
4149                         tmp = compatibility->next;
4150                         __ps_free_compatibility(compatibility);
4151                         compatibility = tmp;
4152                 }
4153         }
4154         /*Free DeviceProfile*/
4155         if (mfx->deviceprofile) {
4156                 deviceprofile_x *deviceprofile = mfx->deviceprofile;
4157                 deviceprofile_x *tmp = NULL;
4158                 while(deviceprofile != NULL) {
4159                         tmp = deviceprofile->next;
4160                         __ps_free_deviceprofile(deviceprofile);
4161                         deviceprofile = tmp;
4162                 }
4163         }
4164         free((void*)mfx);
4165         mfx = NULL;
4166         return;
4167 }
4168
4169 API manifest_x *pkgmgr_parser_process_manifest_xml(const char *manifest)
4170 {
4171         _LOGD("parsing start pkgmgr_parser_process_manifest_xml\n");
4172         xmlTextReaderPtr reader;
4173         manifest_x *mfx = NULL;
4174
4175         reader = xmlReaderForFile(manifest, NULL, 0);
4176         if (reader) {
4177                 mfx = malloc(sizeof(manifest_x));
4178                 if (mfx) {
4179                         memset(mfx, '\0', sizeof(manifest_x));
4180                         if (__process_manifest(reader, mfx, GLOBAL_USER) < 0) {
4181                                 _LOGD("Parsing Failed\n");
4182                                 pkgmgr_parser_free_manifest_xml(mfx);
4183                                 mfx = NULL;
4184                         } else
4185                                 _LOGD("Parsing Success\n");
4186                 } else {
4187                         _LOGD("Memory allocation error\n");
4188                 }
4189                 xmlFreeTextReader(reader);
4190         } else {
4191                 _LOGD("Unable to create xml reader\n");
4192         }
4193         return mfx;
4194 }
4195
4196
4197 API manifest_x *pkgmgr_parser_usr_process_manifest_xml(const char *manifest, uid_t uid)
4198 {
4199         _LOGD("parsing start pkgmgr_parser_usr_process_manifest_xml\n");
4200         xmlTextReaderPtr reader;
4201         manifest_x *mfx = NULL;
4202
4203         reader = xmlReaderForFile(manifest, NULL, 0);
4204         if (reader) {
4205                 mfx = malloc(sizeof(manifest_x));
4206                 if (mfx) {
4207                         memset(mfx, '\0', sizeof(manifest_x));
4208                         if (__process_manifest(reader, mfx, uid) < 0) {
4209                                 _LOGD("Parsing Failed\n");
4210                                 pkgmgr_parser_free_manifest_xml(mfx);
4211                                 mfx = NULL;
4212                         } else
4213                                 _LOGD("Parsing Success\n");
4214                 } else {
4215                         _LOGD("Memory allocation error\n");
4216                 }
4217                 xmlFreeTextReader(reader);
4218         } else {
4219                 _LOGD("Unable to create xml reader\n");
4220         }
4221         return mfx;
4222 }
4223
4224 /* These APIs are intended to call parser directly */
4225
4226 API int pkgmgr_parser_parse_manifest_for_installation(const char *manifest, char *const tagv[])
4227 {
4228 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4229         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4230         _LOGD("parsing manifest for installation: %s\n", manifest);
4231
4232         manifest_x *mfx = NULL;
4233         int ret = -1;
4234
4235         xmlInitParser();
4236         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4237         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4238
4239         _LOGD("Parsing Finished\n");
4240
4241 //      __streamFile(manifest, ACTION_INSTALL, temp, mfx->package);
4242         __add_preload_info(mfx, manifest, GLOBAL_USER);
4243
4244         _LOGD("Added preload infomation\n");
4245
4246         __ps_process_tag(mfx, tagv);
4247
4248         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
4249         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
4250
4251         _LOGD("DB Insert Success\n");
4252
4253         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
4254         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
4255         if (ret == -1)
4256                 _LOGD("Creating metadata parser failed\n");
4257
4258         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
4259         if (ret == -1)
4260                 _LOGD("Creating category parser failed\n");
4261
4262         pkgmgr_parser_free_manifest_xml(mfx);
4263         _LOGD("Free Done\n");
4264         xmlCleanupParser();
4265
4266         return PMINFO_R_OK;
4267 }
4268 API int pkgmgr_parser_parse_usr_manifest_for_installation(const char *manifest, uid_t uid, char *const tagv[])
4269 {
4270 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4271         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4272         _LOGD("parsing manifest for installation: %s\n", manifest);
4273         manifest_x *mfx = NULL;
4274         int ret = -1;
4275
4276         xmlInitParser();
4277         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
4278         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4279
4280         _LOGD("Parsing Finished\n");
4281 //      __streamFile(manifest, ACTION_INSTALL, temp, mfx->package);
4282
4283         __ps_process_tag(mfx, tagv);
4284
4285         ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
4286         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
4287
4288         _LOGD("DB Insert Success\n");
4289
4290         __ps_process_tag_parser(mfx, manifest, ACTION_INSTALL);
4291         ret = __ps_process_metadata_parser(mfx, ACTION_INSTALL);
4292         if (ret == -1)
4293                 _LOGD("Creating metadata parser failed\n");
4294         ret = __ps_process_category_parser(mfx, ACTION_INSTALL);
4295         if (ret == -1)
4296                 _LOGD("Creating category parser failed\n");
4297
4298         pkgmgr_parser_free_manifest_xml(mfx);
4299         _LOGD("Free Done\n");
4300         xmlCleanupParser();
4301
4302         return PMINFO_R_OK;
4303 }
4304
4305 API int pkgmgr_parser_parse_manifest_for_upgrade(const char *manifest, char *const tagv[])
4306 {
4307 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4308         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4309         _LOGD("pkgmgr_parser_parse_manifest_for_upgrade  parsing manifest for upgradation: %s\n", manifest);
4310         manifest_x *mfx = NULL;
4311         int ret = -1;
4312         bool preload = false;
4313         bool system = false;
4314         char *csc_path = NULL;
4315         pkgmgrinfo_pkginfo_h handle = NULL;
4316
4317         xmlInitParser();
4318         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4319         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4320
4321         _LOGD("Parsing Finished\n");
4322 //      __streamFile(manifest, ACTION_UPGRADE, temp, mfx->package);
4323         __add_preload_info(mfx, manifest, GLOBAL_USER);
4324         _LOGD("Added preload infomation\n");
4325         __check_preload_updated(mfx, manifest, GLOBAL_USER);
4326
4327         ret = pkgmgrinfo_pkginfo_get_pkginfo(mfx->package, &handle);
4328         if (ret != PMINFO_R_OK)
4329                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
4330         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
4331         if (ret != PMINFO_R_OK)
4332                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
4333
4334         if (preload) {
4335                 free((void *)mfx->preload);
4336                 mfx->preload = strdup("true");
4337         }
4338
4339         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
4340         if (ret != PMINFO_R_OK)
4341                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
4342         if (system) {
4343                 free((void *)mfx->system);
4344                 mfx->system = strdup("true");
4345         }
4346
4347         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
4348         if (ret != PMINFO_R_OK)
4349                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
4350         
4351         if (csc_path != NULL) {
4352                 if (mfx->csc_path)
4353                         free((void *)mfx->csc_path);
4354                 mfx->csc_path = strdup(csc_path);
4355         }
4356
4357         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
4358         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
4359
4360         _LOGD("DB Update Success\n");
4361
4362         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
4363         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
4364         if (ret == -1){
4365                 _LOGD("Upgrade metadata parser failed\n");
4366         }
4367         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
4368         if (ret == -1)
4369                 _LOGD("Creating category parser failed\n");
4370         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4371         pkgmgr_parser_free_manifest_xml(mfx);
4372         _LOGD("Free Done\n");
4373         xmlCleanupParser();
4374
4375         return PMINFO_R_OK;
4376 }
4377
4378 API int pkgmgr_parser_parse_usr_manifest_for_upgrade(const char *manifest, uid_t uid, char *const tagv[])
4379 {
4380 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4381         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4382         _LOGD(" pkgmgr_parser_parse_usr_manifest_for_upgrade parsing manifest for upgradation: %s\n", manifest);
4383         manifest_x *mfx = NULL;
4384         int ret = -1;
4385         bool preload = false;
4386         bool system = false;
4387         char *csc_path = NULL;
4388         pkgmgrinfo_pkginfo_h handle = NULL;
4389
4390         xmlInitParser();
4391         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
4392         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4393
4394         _LOGD("Parsing Finished\n");
4395         //__streamFile(manifest, ACTION_UPGRADE, temp, mfx->package);
4396         __check_preload_updated(mfx, manifest, uid);
4397
4398         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(mfx->package, uid, &handle);
4399         if (ret != PMINFO_R_OK)
4400                 _LOGD("pkgmgrinfo_pkginfo_get_pkginfo failed\n");
4401         ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
4402         if (ret != PMINFO_R_OK)
4403                 _LOGD("pkgmgrinfo_pkginfo_is_preload failed\n");
4404
4405         if (preload) {
4406                 free((void *)mfx->preload);
4407                 mfx->preload = strdup("true");
4408         }
4409
4410         ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
4411         if (ret != PMINFO_R_OK)
4412                 _LOGD("pkgmgrinfo_pkginfo_is_system failed\n");
4413
4414         if (system) {
4415                 free((void *)mfx->system);
4416                 mfx->system = strdup("true");
4417         }
4418
4419         ret = pkgmgrinfo_pkginfo_get_csc_path(handle, &csc_path);
4420         if (ret != PMINFO_R_OK)
4421                 _LOGD("pkgmgrinfo_pkginfo_get_csc_path failed\n");
4422         if (csc_path != NULL) {
4423                 if (mfx->csc_path)
4424                         free((void *)mfx->csc_path);
4425                 mfx->csc_path = strdup(csc_path);
4426         }
4427
4428         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
4429         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
4430         _LOGD("DB Update Success\n");
4431
4432         __ps_process_tag_parser(mfx, manifest, ACTION_UPGRADE);
4433         ret = __ps_process_metadata_parser(mfx, ACTION_UPGRADE);
4434         if (ret == -1)
4435                 _LOGD("Upgrade metadata parser failed\n");
4436         ret = __ps_process_category_parser(mfx, ACTION_UPGRADE);
4437         if (ret == -1)
4438                 _LOGD("Creating category parser failed\n");
4439         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4440         pkgmgr_parser_free_manifest_xml(mfx);
4441         _LOGD("Free Done\n");
4442         xmlCleanupParser();
4443
4444         return PMINFO_R_OK;
4445 }
4446
4447 API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, char *const tagv[])
4448 {
4449 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4450         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4451         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
4452
4453         manifest_x *mfx = NULL;
4454         int ret = -1;
4455         xmlInitParser();
4456         mfx = pkgmgr_parser_process_manifest_xml(manifest);
4457         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4458
4459         _LOGD("Parsing Finished\n");
4460
4461 //      __streamFile(manifest, ACTION_UNINSTALL, temp, mfx->package);
4462         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
4463
4464         __add_preload_info(mfx, manifest, GLOBAL_USER);
4465         _LOGD("Added preload infomation\n");
4466
4467         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
4468         if (ret == -1)
4469                 _LOGD("Removing metadata parser failed\n");
4470
4471         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
4472         if (ret == -1)
4473                 _LOGD("Creating category parser failed\n");
4474
4475         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
4476         if (ret == -1)
4477                 _LOGD("DB Delete failed\n");
4478         else
4479                 _LOGD("DB Delete Success\n");
4480
4481         pkgmgr_parser_free_manifest_xml(mfx);
4482         _LOGD("Free Done\n");
4483         xmlCleanupParser();
4484
4485         return PMINFO_R_OK;
4486 }
4487
4488
4489 API int pkgmgr_parser_parse_usr_manifest_for_uninstallation(const char *manifest, uid_t uid, char *const tagv[])
4490 {
4491 //      char *temp[] = {"shortcut-list", "livebox", "account", "notifications", "privileges", "ime", "font", NULL};
4492         retvm_if(manifest == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4493         _LOGD("parsing manifest for uninstallation: %s\n", manifest);
4494
4495         manifest_x *mfx = NULL;
4496         int ret = -1;
4497         xmlInitParser();
4498         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
4499         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
4500
4501         _LOGD("Parsing Finished\n");
4502
4503 //      __streamFile(manifest, ACTION_UNINSTALL, temp, mfx->package);
4504         __ps_process_tag_parser(mfx, manifest, ACTION_UNINSTALL);
4505
4506         ret = __ps_process_metadata_parser(mfx, ACTION_UNINSTALL);
4507         if (ret == -1)
4508                 _LOGD("Removing metadata parser failed\n");
4509
4510         ret = __ps_process_category_parser(mfx, ACTION_UNINSTALL);
4511         if (ret == -1)
4512                 _LOGD("Creating category parser failed\n");
4513
4514         ret = pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, uid);
4515         if (ret == -1)
4516                 _LOGD("DB Delete failed\n");
4517         else
4518                 _LOGD("DB Delete Success\n");
4519
4520         ret = __ps_remove_appsvc_db(mfx, uid);
4521         if (ret == -1)
4522                 _LOGD("Removing appsvc_db failed\n");
4523         else
4524                 _LOGD("Removing appsvc_db Success\n");
4525
4526         pkgmgr_parser_free_manifest_xml(mfx);
4527         _LOGD("Free Done\n");
4528         xmlCleanupParser();
4529
4530         return PMINFO_R_OK;
4531 }
4532
4533 API int pkgmgr_parser_parse_manifest_for_preload()
4534 {
4535         return pkgmgr_parser_update_preload_info_in_db();
4536 }
4537
4538 API int pkgmgr_parser_parse_usr_manifest_for_preload(uid_t uid)
4539 {
4540         return pkgmgr_parser_update_preload_info_in_usr_db(uid);
4541 }
4542
4543
4544 API char *pkgmgr_parser_get_usr_manifest_file(const char *pkgid, uid_t uid)
4545 {
4546         return __pkgid_to_manifest(pkgid, uid);
4547 }
4548
4549 API char *pkgmgr_parser_get_manifest_file(const char *pkgid)
4550 {
4551         return __pkgid_to_manifest(pkgid, GLOBAL_USER);
4552 }
4553
4554 API int pkgmgr_parser_run_parser_for_installation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4555 {
4556         return __ps_run_parser(docPtr, tag, ACTION_INSTALL, pkgid);
4557 }
4558
4559 API int pkgmgr_parser_run_parser_for_upgrade(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4560 {
4561         return __ps_run_parser(docPtr, tag, ACTION_UPGRADE, pkgid);
4562 }
4563
4564 API int pkgmgr_parser_run_parser_for_uninstallation(xmlDocPtr docPtr, const char *tag, const char *pkgid)
4565 {
4566         return __ps_run_parser(docPtr, tag, ACTION_UNINSTALL, pkgid);
4567 }
4568
4569 #define SCHEMA_FILE SYSCONFDIR "/package-manager/preload/manifest.xsd"
4570 #if 1
4571 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
4572 {
4573         if (manifest == NULL) {
4574                 _LOGE("manifest file is NULL\n");
4575                 return PMINFO_R_EINVAL;
4576         }
4577         int ret = -1;
4578         xmlSchemaParserCtxtPtr ctx;
4579         xmlSchemaValidCtxtPtr vctx;
4580         xmlSchemaPtr xschema;
4581         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
4582         if (ctx == NULL) {
4583                 _LOGE("xmlSchemaNewParserCtxt() Failed\n");
4584                 return PMINFO_R_ERROR;
4585         }
4586         xschema = xmlSchemaParse(ctx);
4587         if (xschema == NULL) {
4588                 _LOGE("xmlSchemaParse() Failed\n");
4589                 return PMINFO_R_ERROR;
4590         }
4591         vctx = xmlSchemaNewValidCtxt(xschema);
4592         if (vctx == NULL) {
4593                 _LOGE("xmlSchemaNewValidCtxt() Failed\n");
4594                 return PMINFO_R_ERROR;
4595         }
4596         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
4597         ret = xmlSchemaValidateFile(vctx, manifest, 0);
4598         if (ret == -1) {
4599                 _LOGE("xmlSchemaValidateFile() failed\n");
4600                 return PMINFO_R_ERROR;
4601         } else if (ret == 0) {
4602                 _LOGE("Manifest is Valid\n");
4603                 return PMINFO_R_OK;
4604         } else {
4605                 _LOGE("Manifest Validation Failed with error code %d\n", ret);
4606                 return PMINFO_R_ERROR;
4607         }
4608         return PMINFO_R_OK;
4609 }
4610
4611 #else
4612 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
4613 {
4614         int err = 0;
4615         int status = 0;
4616         pid_t pid;
4617
4618         pid = fork();
4619
4620         switch (pid) {
4621         case -1:
4622                 _LOGE("fork failed\n");
4623                 return -1;
4624         case 0:
4625                 /* child */
4626                 {
4627                         int dev_null_fd = open ("/dev/null", O_RDWR);
4628                         if (dev_null_fd >= 0)
4629                         {
4630                                 dup2 (dev_null_fd, 0);/*stdin*/
4631                                 dup2 (dev_null_fd, 1);/*stdout*/
4632                                 dup2 (dev_null_fd, 2);/*stderr*/
4633                         }
4634
4635                         if (execl("/usr/bin/xmllint", "xmllint", manifest, "--schema",
4636                                 SCHEMA_FILE, NULL) < 0) {
4637                                 _LOGE("execl error\n");
4638                         }
4639
4640                         _exit(100);
4641                 }
4642         default:
4643                 /* parent */
4644                 break;
4645         }
4646
4647         while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
4648                 if (err < 0) {
4649                         if (errno == EINTR)
4650                                 continue;
4651                         _LOGE("waitpid failed\n");
4652                         return -1;
4653                 }
4654         }
4655
4656
4657         if(WIFEXITED(status) && !WEXITSTATUS(status))
4658                 return 0;
4659         else
4660                 return -1;
4661 }
4662 #endif