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