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