be7df99cb084dec2875d6d36ad17067d0e4f1ae6
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_pkginfo.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  
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdbool.h>
28 #include <unistd.h>
29 #include <ctype.h>
30 #include <sys/smack.h>
31 #include <linux/limits.h>
32 #include <libgen.h>
33 #include <sys/stat.h>
34
35 #include <libxml/parser.h>
36 #include <libxml/xmlreader.h>
37 #include <libxml/xmlschemas.h>
38 #include <sqlite3.h>
39 #include <glib.h>
40
41 #include "pkgmgr_parser.h"
42 #include "pkgmgrinfo_basic.h"
43 #include "pkgmgrinfo_private.h"
44 #include "pkgmgrinfo_debug.h"
45 #include "pkgmgr-info.h"
46 #include "pkgmgr_parser_db.h"
47 #include "pkgmgr_parser_internal.h"
48
49 static int _pkginfo_get_pkginfo(const char *pkgid, uid_t uid,
50                 pkgmgr_pkginfo_x **pkginfo);
51 static char *_get_filtered_query(const char *query_raw,
52                 pkgmgrinfo_filter_x *filter);
53
54 static gint __compare_func(gconstpointer data1, gconstpointer data2)
55 {
56         pkgmgrinfo_node_x *node1 = (pkgmgrinfo_node_x*)data1;
57         pkgmgrinfo_node_x *node2 = (pkgmgrinfo_node_x*)data2;
58         if (node1->prop == node2->prop)
59                 return 0;
60         else if (node1->prop > node2->prop)
61                 return 1;
62         else
63                 return -1;
64 }
65
66 static void __destroy_each_node(gpointer data, gpointer user_data)
67 {
68         ret_if(data == NULL);
69         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x*)data;
70         if (node->value) {
71                 free(node->value);
72                 node->value = NULL;
73         }
74         if (node->key) {
75                 free(node->key);
76                 node->key = NULL;
77         }
78         free(node);
79         node = NULL;
80 }
81
82 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data)
83 {
84         ret_if(data == NULL);
85         if (data->locale){
86                 free((void *)data->locale);
87                 data->locale = NULL;
88         }
89
90         pkgmgrinfo_basic_free_package(data->pkg_info);
91         free((void *)data);
92         data = NULL;
93         return;
94 }
95
96 static int __child_element(xmlTextReaderPtr reader, int depth)
97 {
98         int ret = xmlTextReaderRead(reader);
99         int cur = xmlTextReaderDepth(reader);
100         while (ret == 1) {
101
102                 switch (xmlTextReaderNodeType(reader)) {
103                 case XML_READER_TYPE_ELEMENT:
104                         if (cur == depth + 1)
105                                 return 1;
106                         break;
107                 case XML_READER_TYPE_TEXT:
108                         /*text is handled by each function separately*/
109                         if (cur == depth + 1)
110                                 return 0;
111                         break;
112                 case XML_READER_TYPE_END_ELEMENT:
113                         if (cur == depth)
114                                 return 0;
115                         break;
116                 default:
117                         if (cur <= depth)
118                                 return 0;
119                         break;
120                 }
121                 ret = xmlTextReaderRead(reader);
122                 cur = xmlTextReaderDepth(reader);
123         }
124         return ret;
125 }
126
127 long long _pkgmgr_calculate_dir_size(char *dirname)
128 {
129         long long total = 0;
130         long long ret = 0;
131         int q = 0; /*quotient*/
132         int r = 0; /*remainder*/
133         DIR *dp = NULL;
134         struct dirent *ep = NULL;
135         struct stat fileinfo;
136         char abs_filename[FILENAME_MAX] = { 0, };
137         retvm_if(dirname == NULL, PMINFO_R_ERROR, "dirname is NULL");
138
139         dp = opendir(dirname);
140         if (dp != NULL) {
141                 while ((ep = readdir(dp)) != NULL) {
142                         if (!strcmp(ep->d_name, ".") ||
143                                 !strcmp(ep->d_name, "..")) {
144                                 continue;
145                         }
146                         snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
147                                  ep->d_name);
148                         if (lstat(abs_filename, &fileinfo) < 0)
149                                 perror(abs_filename);
150                         else {
151                                 if (S_ISDIR(fileinfo.st_mode)) {
152                                         total += fileinfo.st_size;
153                                         if (strcmp(ep->d_name, ".")
154                                             && strcmp(ep->d_name, "..")) {
155                                                 ret = _pkgmgr_calculate_dir_size
156                                                     (abs_filename);
157                                                 total = total + ret;
158                                         }
159                                 } else if (S_ISLNK(fileinfo.st_mode)) {
160                                         continue;
161                                 } else {
162                                         /*It is a file. Calculate the actual
163                                         size occupied (in terms of 4096 blocks)*/
164                                 q = (fileinfo.st_size / BLOCK_SIZE);
165                                 r = (fileinfo.st_size % BLOCK_SIZE);
166                                 if (r) {
167                                         q = q + 1;
168                                 }
169                                 total += q * BLOCK_SIZE;
170                                 }
171                         }
172                 }
173                 (void)closedir(dp);
174         } else {
175                 _LOGE("Couldn't open the directory\n");
176                 return -1;
177         }
178         return total;
179
180 }
181
182 static gint __list_strcmp(gconstpointer a, gconstpointer b)
183 {
184         return strcmp((char *)a, (char *)b);
185 }
186
187 static int _pkginfo_get_list(sqlite3 *db, const char *locale,
188                 pkgmgrinfo_filter_x *filter, GList **list)
189 {
190         static const char query_raw[] =
191                 "SELECT DISTINCT package_info.package FROM package_info"
192                 " LEFT OUTER JOIN package_localized_info"
193                 "  ON package_info.package=package_localized_info.package"
194                 "  AND package_localized_info.package_locale=%Q "
195                 " LEFT OUTER JOIN package_privilege_info"
196                 "  ON package_info.package=package_privilege_info.package";
197         int ret;
198         char *query;
199         char *query_localized;
200         sqlite3_stmt *stmt;
201         char *pkgid = NULL;
202
203         query = _get_filtered_query(query_raw, filter);
204         if (query == NULL)
205                 return -1;
206         query_localized = sqlite3_mprintf(query, locale);
207         free(query);
208         if (query_localized == NULL)
209                 return -1;
210
211         ret = sqlite3_prepare_v2(db, query_localized,
212                         strlen(query_localized), &stmt, NULL);
213         sqlite3_free(query_localized);
214         if (ret != SQLITE_OK) {
215                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
216                 return -1;
217         }
218
219         while (sqlite3_step(stmt) == SQLITE_ROW) {
220                 _save_column_str(stmt, 0, (const char **)&pkgid);
221                 if (pkgid != NULL)
222                         *list = g_list_insert_sorted(*list, pkgid,
223                                         __list_strcmp);
224         }
225
226         sqlite3_finalize(stmt);
227
228         return 0;
229 }
230
231 static int _pkginfo_get_filtered_list(pkgmgrinfo_filter_x *filter, uid_t uid,
232                 GList **list)
233 {
234         int ret;
235         sqlite3 *db;
236         const char *dbpath;
237         char *locale;
238         GList *tmp;
239
240         locale = _get_system_locale();
241         if (locale == NULL)
242                 return PMINFO_R_ERROR;
243
244         dbpath = getUserPkgParserDBPathUID(uid);
245         if (dbpath == NULL) {
246                 free(locale);
247                 return PMINFO_R_ERROR;
248         }
249
250         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
251         if (ret != SQLITE_OK) {
252                 _LOGE("failed to open db: %d", ret);
253                 free(locale);
254                 return PMINFO_R_ERROR;
255         }
256
257         if (_pkginfo_get_list(db, locale, filter, list)) {
258                 free(locale);
259                 sqlite3_close_v2(db);
260                 return PMINFO_R_ERROR;
261         }
262         sqlite3_close_v2(db);
263
264         if (uid == GLOBAL_USER) {
265                 free(locale);
266                 return PMINFO_R_OK;
267         }
268
269         /* search again from global */
270         dbpath = getUserPkgParserDBPathUID(GLOBAL_USER);
271         if (dbpath == NULL) {
272                 free(locale);
273                 return PMINFO_R_ERROR;
274         }
275
276         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
277         if (ret != SQLITE_OK) {
278                 _LOGE("failed to open db: %d", ret);
279                 free(locale);
280                 return PMINFO_R_ERROR;
281         }
282
283         if (_pkginfo_get_list(db, locale, filter, list)) {
284                 free(locale);
285                 sqlite3_close_v2(db);
286                 return PMINFO_R_ERROR;
287         }
288         sqlite3_close_v2(db);
289
290         /* remove duplicate element:
291          * since the list is sorted, we can remove duplicates in linear time
292          */
293         for (tmp = *list; tmp; tmp = tmp->next) {
294                 if (tmp->prev == NULL || tmp->data == NULL)
295                         continue;
296                 if (strcmp((const char *)tmp->prev->data,
297                                         (const char *)tmp->data) == 0)
298                         *list = g_list_delete_link(*list, tmp);
299         }
300
301         return PMINFO_R_OK;
302 }
303
304
305 static int _pkginfo_get_filtered_foreach_pkginfo(pkgmgrinfo_filter_x *filter,
306                 pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data, uid_t uid)
307 {
308         int ret;
309         pkgmgr_pkginfo_x *info;
310         GList *list = NULL;
311         GList *tmp;
312         char *pkgid;
313         int stop = 0;
314
315         ret = _pkginfo_get_filtered_list(filter, uid, &list);
316         if (ret != PMINFO_R_OK)
317                 return PMINFO_R_ERROR;
318
319         for (tmp = list; tmp; tmp = tmp->next) {
320                 pkgid = (char *)tmp->data;
321                 if (stop == 0) {
322                         ret = _pkginfo_get_pkginfo(pkgid, uid, &info);
323                         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
324                                 ret = _pkginfo_get_pkginfo(pkgid, GLOBAL_USER,
325                                                 &info);
326                         if (ret != PMINFO_R_OK) {
327                                 free(pkgid);
328                                 continue;
329                         }
330                         if (pkg_list_cb(info, user_data) < 0)
331                                 stop = 1;
332                         pkgmgrinfo_pkginfo_destroy_pkginfo(info);
333                 }
334                 free(pkgid);
335         }
336
337         g_list_free(list);
338
339         return PMINFO_R_OK;
340 }
341
342 API int pkgmgrinfo_pkginfo_get_usr_list(pkgmgrinfo_pkg_list_cb pkg_list_cb,
343                 void *user_data, uid_t uid)
344 {
345         if (pkg_list_cb == NULL) {
346                 LOGE("invalid parameter");
347                 return PMINFO_R_EINVAL;
348         }
349
350         return _pkginfo_get_filtered_foreach_pkginfo(NULL, pkg_list_cb,
351                         user_data, uid);
352 }
353
354 API int pkgmgrinfo_pkginfo_get_list(pkgmgrinfo_pkg_list_cb pkg_list_cb, void *user_data)
355 {
356         return pkgmgrinfo_pkginfo_get_usr_list(pkg_list_cb, user_data, GLOBAL_USER);
357 }
358
359 static int _pkginfo_get_author(sqlite3 *db, const char *pkgid,
360                 author_x **author)
361 {
362         static const char query_raw[] =
363                 "SELECT author_name, author_email, author_href "
364                 "FROM package_info WHERE package=%Q";
365         int ret;
366         char *query;
367         sqlite3_stmt *stmt;
368         int idx = 0;
369         author_x *info;
370
371         query = sqlite3_mprintf(query_raw, pkgid);
372         if (query == NULL) {
373                 LOGE("out of memory");
374                 return PMINFO_R_ERROR;
375         }
376
377         ret = sqlite3_prepare_v2(db, query, strlen(query),
378                         &stmt, NULL);
379         sqlite3_free(query);
380         if (ret != SQLITE_OK) {
381                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
382                 return PMINFO_R_ERROR;
383         }
384
385         if (sqlite3_step(stmt) == SQLITE_ERROR) {
386                 LOGE("step error: %s", sqlite3_errmsg(db));
387                 sqlite3_finalize(stmt);
388                 return PMINFO_R_ERROR;
389         }
390
391         /* one author per one package */
392         info = calloc(1, sizeof(author_x));
393         if (info == NULL) {
394                 LOGE("out of memory");
395                 sqlite3_finalize(stmt);
396                 return PMINFO_R_ERROR;
397         }
398
399         _save_column_str(stmt, idx++, &info->text);
400         _save_column_str(stmt, idx++, &info->email);
401         _save_column_str(stmt, idx++, &info->href);
402
403         *author = info;
404
405         sqlite3_finalize(stmt);
406
407         return PMINFO_R_OK;
408 }
409
410 static int _pkginfo_get_label(sqlite3 *db, const char *pkgid,
411                 const char *locale, label_x **label)
412 {
413         static const char query_raw[] =
414                 "SELECT package_label, package_locale "
415                 "FROM package_localized_info "
416                 "WHERE package=%Q AND package_locale IN (%Q, %Q)";
417         int ret;
418         char *query;
419         sqlite3_stmt *stmt;
420         int idx;
421         label_x *info;
422
423         query = sqlite3_mprintf(query_raw, pkgid, locale, DEFAULT_LOCALE);
424         if (query == NULL) {
425                 LOGE("out of memory");
426                 return PMINFO_R_ERROR;
427         }
428
429         ret = sqlite3_prepare_v2(db, query, strlen(query),
430                         &stmt, NULL);
431         sqlite3_free(query);
432         if (ret != SQLITE_OK) {
433                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
434                 return PMINFO_R_ERROR;
435         }
436
437         while (sqlite3_step(stmt) == SQLITE_ROW) {
438                 info = calloc(1, sizeof(label_x));
439                 if (info == NULL) {
440                         LOGE("out of memory");
441                         sqlite3_finalize(stmt);
442                         if (*label) {
443                                 LISTHEAD(*label, info);
444                                 *label = info;
445                         }
446                         return PMINFO_R_ERROR;
447                 }
448                 idx = 0;
449                 _save_column_str(stmt, idx++, &info->text);
450                 _save_column_str(stmt, idx++, &info->lang);
451                 LISTADD(*label, info);
452         }
453
454         if (*label) {
455                 LISTHEAD(*label, info);
456                 *label = info;
457         }
458
459         sqlite3_finalize(stmt);
460
461         return PMINFO_R_OK;
462 }
463
464 static int _pkginfo_get_icon(sqlite3 *db, const char *pkgid, const char *locale,
465                 icon_x **icon)
466 {
467         static const char query_raw[] =
468                 "SELECT package_icon, package_locale "
469                 "FROM package_localized_info "
470                 "WHERE package=%Q AND package_locale IN (%Q, %Q)";
471         int ret;
472         char *query;
473         sqlite3_stmt *stmt;
474         int idx;
475         icon_x *info;
476
477         query = sqlite3_mprintf(query_raw, pkgid, locale, DEFAULT_LOCALE);
478         if (query == NULL) {
479                 LOGE("out of memory");
480                 return PMINFO_R_ERROR;
481         }
482
483         ret = sqlite3_prepare_v2(db, query, strlen(query),
484                         &stmt, NULL);
485         sqlite3_free(query);
486         if (ret != SQLITE_OK) {
487                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
488                 return PMINFO_R_ERROR;
489         }
490
491         while (sqlite3_step(stmt) == SQLITE_ROW) {
492                 info = calloc(1, sizeof(icon_x));
493                 if (info == NULL) {
494                         LOGE("out of memory");
495                         sqlite3_finalize(stmt);
496                         if (*icon) {
497                                 LISTHEAD(*icon, info);
498                                 *icon = info;
499                         }
500                         return PMINFO_R_ERROR;
501                 }
502                 idx = 0;
503                 _save_column_str(stmt, idx++, &info->text);
504                 _save_column_str(stmt, idx++, &info->lang);
505                 LISTADD(*icon, info);
506         }
507
508         if (*icon) {
509                 LISTHEAD(*icon, info);
510                 *icon = info;
511         }
512
513         sqlite3_finalize(stmt);
514
515         return PMINFO_R_OK;
516 }
517
518 static int _pkginfo_get_description(sqlite3 *db, const char *pkgid,
519                 const char *locale, description_x **description)
520 {
521         static const char query_raw[] =
522                 "SELECT package_description, package_locale "
523                 "FROM package_localized_info "
524                 "WHERE package=%Q AND package_locale IN (%Q, %Q)";
525         int ret;
526         char *query;
527         sqlite3_stmt *stmt;
528         int idx;
529         description_x *info;
530
531         query = sqlite3_mprintf(query_raw, pkgid, locale, DEFAULT_LOCALE);
532         if (query == NULL) {
533                 LOGE("out of memory");
534                 return PMINFO_R_ERROR;
535         }
536
537         ret = sqlite3_prepare_v2(db, query, strlen(query),
538                         &stmt, NULL);
539         sqlite3_free(query);
540         if (ret != SQLITE_OK) {
541                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
542                 return PMINFO_R_ERROR;
543         }
544
545         while (sqlite3_step(stmt) == SQLITE_ROW) {
546                 info = calloc(1, sizeof(description_x));
547                 if (info == NULL) {
548                         LOGE("out of memory");
549                         sqlite3_finalize(stmt);
550                         if (*description) {
551                                 LISTHEAD(*description, info);
552                                 *description = info;
553                         }
554                         return PMINFO_R_ERROR;
555                 }
556                 idx = 0;
557                 _save_column_str(stmt, idx++, &info->text);
558                 _save_column_str(stmt, idx++, &info->lang);
559                 LISTADD(*description, info);
560         }
561
562         if (*description) {
563                 LISTHEAD(*description, info);
564                 *description = info;
565         }
566
567         sqlite3_finalize(stmt);
568
569         return PMINFO_R_OK;
570 }
571
572 static int _pkginfo_get_privilege(sqlite3 *db, const char *pkgid,
573                 privileges_x **privileges)
574 {
575         static const char query_raw[] =
576                 "SELECT privilege FROM package_privilege_info WHERE package=%Q";
577         int ret;
578         char *query;
579         sqlite3_stmt *stmt;
580         privileges_x *p;
581         privilege_x *info;
582
583         /* privilege list should stored in privileges_x... */
584         p = calloc(1, sizeof(privileges_x));
585         if (p == NULL) {
586                 LOGE("out of memory");
587                 return PMINFO_R_ERROR;
588         }
589         *privileges = p;
590
591         query = sqlite3_mprintf(query_raw, pkgid);
592         if (query == NULL) {
593                 LOGE("out of memory");
594                 free(p);
595                 return PMINFO_R_ERROR;
596         }
597
598         ret = sqlite3_prepare_v2(db, query, strlen(query),
599                         &stmt, NULL);
600         sqlite3_free(query);
601         if (ret != SQLITE_OK) {
602                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
603                 free(p);
604                 return PMINFO_R_ERROR;
605         }
606
607         while (sqlite3_step(stmt) == SQLITE_ROW) {
608                 info = calloc(1, sizeof(privilege_x));
609                 if (info == NULL) {
610                         LOGE("out of memory");
611                         sqlite3_finalize(stmt);
612                         if (p->privilege) {
613                                 LISTHEAD(p->privilege, info);
614                                 p->privilege = info;
615                         }
616                         return PMINFO_R_ERROR;
617                 }
618                 _save_column_str(stmt, 0, &info->text);
619                 LISTADD(p->privilege, info);
620         }
621
622         if (p->privilege) {
623                 LISTHEAD(p->privilege, info);
624                 p->privilege = info;
625         }
626
627         sqlite3_finalize(stmt);
628
629         return PMINFO_R_OK;
630 }
631
632 static char *_get_filtered_query(const char *query_raw,
633                 pkgmgrinfo_filter_x *filter)
634 {
635         char buf[MAX_QUERY_LEN] = { 0, };
636         char *condition;
637         size_t len;
638         GSList *list;
639         GSList *head = NULL;
640
641         if (filter)
642                 head = filter->list;
643
644         strncat(buf, query_raw, MAX_QUERY_LEN - 1);
645         len = strlen(buf);
646         for (list = head; list; list = list->next) {
647                 /* TODO: revise condition getter function */
648                 __get_filter_condition(list->data, &condition);
649                 if (condition == NULL)
650                         continue;
651                 if (buf[strlen(query_raw)] == '\0') {
652                         len += strlen(" WHERE ");
653                         strncat(buf, " WHERE ", MAX_QUERY_LEN - len - 1);
654                 } else {
655                         len += strlen(" AND ");
656                         strncat(buf, " AND ", MAX_QUERY_LEN -len - 1);
657                 }
658                 len += strlen(condition);
659                 strncat(buf, condition, sizeof(buf) - len - 1);
660                 free(condition);
661                 condition = NULL;
662         }
663
664         return strdup(buf);
665 }
666
667 static int _pkginfo_get_package(sqlite3 *db, const char *pkgid,
668                 const char *locale, package_x **package)
669 {
670         static const char query_raw[] =
671                 "SELECT package, package_version, "
672                 "install_location, package_removable, package_preload, "
673                 "package_readonly, package_update, package_appsetting, "
674                 "package_system, package_type, package_size, installed_time, "
675                 "installed_storage, storeclient_id, mainapp_id, package_url, "
676                 "root_path, csc_path, package_nodisplay, package_api_version, "
677                 "package_support_disable "
678                 "FROM package_info WHERE package=%Q";
679         int ret;
680         char *query;
681         sqlite3_stmt *stmt;
682         int idx;
683         package_x *info;
684
685         query = sqlite3_mprintf(query_raw, pkgid);
686         if (query == NULL) {
687                 LOGE("out of memory");
688                 return PMINFO_R_ERROR;
689         }
690
691         ret = sqlite3_prepare_v2(db, query, strlen(query),
692                         &stmt, NULL);
693         sqlite3_free(query);
694         if (ret != SQLITE_OK) {
695                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
696                 return PMINFO_R_ERROR;
697         }
698
699         ret = sqlite3_step(stmt);
700         if (ret == SQLITE_DONE) {
701                 sqlite3_finalize(stmt);
702                 return PMINFO_R_ENOENT;
703         } else if (ret != SQLITE_ROW) {
704                 LOGE("step failed: %s", sqlite3_errmsg(db));
705                 sqlite3_finalize(stmt);
706                 return PMINFO_R_ERROR;
707         }
708
709         info = calloc(1, sizeof(package_x));
710         if (info == NULL) {
711                 LOGE("out of memory");
712                 sqlite3_finalize(stmt);
713                 return PMINFO_R_ERROR;
714         }
715         idx = 0;
716         _save_column_str(stmt, idx++, &info->package);
717         _save_column_str(stmt, idx++, &info->version);
718         _save_column_str(stmt, idx++, &info->installlocation);
719         _save_column_str(stmt, idx++, &info->removable);
720         _save_column_str(stmt, idx++, &info->preload);
721         _save_column_str(stmt, idx++, &info->readonly);
722         _save_column_str(stmt, idx++, &info->update);
723         _save_column_str(stmt, idx++, &info->appsetting);
724         _save_column_str(stmt, idx++, &info->system);
725         _save_column_str(stmt, idx++, &info->type);
726         _save_column_str(stmt, idx++, &info->package_size);
727         _save_column_str(stmt, idx++, &info->installed_time);
728         _save_column_str(stmt, idx++, &info->installed_storage);
729         _save_column_str(stmt, idx++, &info->storeclient_id);
730         _save_column_str(stmt, idx++, &info->mainapp_id);
731         _save_column_str(stmt, idx++, &info->package_url);
732         _save_column_str(stmt, idx++, &info->root_path);
733         _save_column_str(stmt, idx++, &info->csc_path);
734         _save_column_str(stmt, idx++, &info->nodisplay_setting);
735         _save_column_str(stmt, idx++, &info->api_version);
736         _save_column_str(stmt, idx++, &info->support_disable);
737
738         if (_pkginfo_get_author(db, info->package, &info->author)) {
739                 pkgmgrinfo_basic_free_package(info);
740                 sqlite3_finalize(stmt);
741                 return PMINFO_R_ERROR;
742         }
743
744         if (_pkginfo_get_label(db, info->package, locale, &info->label)) {
745                 pkgmgrinfo_basic_free_package(info);
746                 sqlite3_finalize(stmt);
747                 return PMINFO_R_ERROR;
748         }
749
750         if (_pkginfo_get_icon(db, info->package, locale, &info->icon)) {
751                 pkgmgrinfo_basic_free_package(info);
752                 sqlite3_finalize(stmt);
753                 return PMINFO_R_ERROR;
754         }
755
756         if (_pkginfo_get_description(db, info->package, locale,
757                                 &info->description)) {
758                 pkgmgrinfo_basic_free_package(info);
759                 sqlite3_finalize(stmt);
760                 return PMINFO_R_ERROR;
761         }
762
763         if (_pkginfo_get_privilege(db, info->package, &info->privileges)) {
764                 pkgmgrinfo_basic_free_package(info);
765                 sqlite3_finalize(stmt);
766                 return PMINFO_R_ERROR;
767         }
768
769         *package = info;
770         sqlite3_finalize(stmt);
771
772         return PMINFO_R_OK;
773 }
774
775 static int _pkginfo_get_pkginfo(const char *pkgid, uid_t uid,
776                 pkgmgr_pkginfo_x **pkginfo)
777 {
778         int ret;
779         sqlite3 *db;
780         const char *dbpath;
781         char *locale;
782         pkgmgr_pkginfo_x *info;
783
784         dbpath = getUserPkgParserDBPathUID(uid);
785         if (dbpath == NULL)
786                 return PMINFO_R_ERROR;
787
788         locale = _get_system_locale();
789         if (locale == NULL)
790                 return PMINFO_R_ERROR;
791
792         ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
793         if (ret != SQLITE_OK) {
794                 _LOGE("failed to open db: %d", ret);
795                 free(locale);
796                 return PMINFO_R_ERROR;
797         }
798
799         info = calloc(1, sizeof(pkgmgr_pkginfo_x));
800         if (info == NULL) {
801                 _LOGE("out of memory");
802                 free(locale);
803                 sqlite3_close_v2(db);
804                 return PMINFO_R_ERROR;
805         }
806
807         ret = _pkginfo_get_package(db, pkgid, locale, &info->pkg_info);
808         if (ret == PMINFO_R_OK) {
809                 info->locale = strdup(locale);
810                 info->uid = uid;
811                 info->pkg_info->for_all_users = strdup(
812                                 uid != GLOBAL_USER ? "false" : "true");
813         }
814
815         *pkginfo = info;
816
817         free(locale);
818         sqlite3_close_v2(db);
819
820         return ret;
821 }
822
823 API int pkgmgrinfo_pkginfo_get_usr_pkginfo(const char *pkgid, uid_t uid,
824                 pkgmgrinfo_pkginfo_h *handle)
825 {
826         int ret;
827
828         if (pkgid == NULL || handle == NULL) {
829                 LOGE("invalid parameter");
830                 return PMINFO_R_EINVAL;
831         }
832
833         ret = _pkginfo_get_pkginfo(pkgid, uid, (pkgmgr_pkginfo_x **)handle);
834         if (ret == PMINFO_R_ENOENT && uid != GLOBAL_USER)
835                 ret = _pkginfo_get_pkginfo(pkgid, GLOBAL_USER,
836                                 (pkgmgr_pkginfo_x **)handle);
837
838         if (ret != PMINFO_R_OK)
839                 _LOGE("failed to get pkginfo of %s for user %d", pkgid, uid);
840
841         return ret;
842 }
843
844 API int pkgmgrinfo_pkginfo_get_pkginfo(const char *pkgid, pkgmgrinfo_pkginfo_h *handle)
845 {
846         return pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, GLOBAL_USER, handle);
847 }
848
849 API int pkgmgrinfo_pkginfo_get_pkgname(pkgmgrinfo_pkginfo_h handle, char **pkg_name)
850 {
851         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
852
853         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
854         retvm_if(pkg_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
855
856         if (info->pkg_info == NULL || info->pkg_info->package == NULL)
857                 return PMINFO_R_ERROR;
858
859         *pkg_name = (char *)info->pkg_info->package;
860
861         return PMINFO_R_OK;
862 }
863
864 API int pkgmgrinfo_pkginfo_get_pkgid(pkgmgrinfo_pkginfo_h handle, char **pkgid)
865 {
866         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
867
868         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
869         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
870
871         if (info->pkg_info == NULL || info->pkg_info->package == NULL)
872                 return PMINFO_R_ERROR;
873
874         *pkgid = (char *)info->pkg_info->package;
875
876         return PMINFO_R_OK;
877 }
878
879 API int pkgmgrinfo_pkginfo_get_type(pkgmgrinfo_pkginfo_h handle, char **type)
880 {
881         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
882
883         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
884         retvm_if(type == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
885
886         if (info->pkg_info == NULL || info->pkg_info->type == NULL)
887                 return PMINFO_R_ERROR;
888
889         *type = (char *)info->pkg_info->type;
890
891         return PMINFO_R_OK;
892 }
893
894 API int pkgmgrinfo_pkginfo_get_version(pkgmgrinfo_pkginfo_h handle, char **version)
895 {
896         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
897
898         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
899         retvm_if(version == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
900
901         if (info->pkg_info == NULL || info->pkg_info->version == NULL)
902                 return PMINFO_R_ERROR;
903
904         *version = (char *)info->pkg_info->version;
905
906         return PMINFO_R_OK;
907 }
908
909 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
910 {
911         char *val;
912         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
913
914         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
915         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
916
917         if (info->pkg_info == NULL || info->pkg_info->installlocation == NULL)
918                 return PMINFO_R_ERROR;
919
920         val = (char *)info->pkg_info->installlocation;
921         if (strcmp(val, "internal-only") == 0)
922                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
923         else if (strcmp(val, "prefer-external") == 0)
924                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
925         else
926                 *location = PMINFO_INSTALL_LOCATION_AUTO;
927
928         return PMINFO_R_OK;
929 }
930
931 API int pkgmgrinfo_pkginfo_get_package_size(pkgmgrinfo_pkginfo_h handle, int *size)
932 {
933         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
934
935         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
936         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
937
938         if (info->pkg_info == NULL || info->pkg_info->package_size == NULL)
939                 return PMINFO_R_ERROR;
940
941         *size = atoi((char *)info->pkg_info->package_size);
942
943         return PMINFO_R_OK;
944 }
945
946 API int pkgmgrinfo_pkginfo_get_total_size(pkgmgrinfo_pkginfo_h handle, int *size)
947 {
948         char *pkgid;
949         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
950         long long rw_size = 0;
951         long long ro_size = 0;
952         long long tmp_size = 0;
953         long long total_size = 0;
954         struct stat fileinfo;
955         int ret;
956
957         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
958         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
959
960         ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
961         if (ret < 0)
962                 return PMINFO_R_ERROR;
963
964         /* RW area */
965         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RW_PATH, pkgid);
966         if (lstat(device_path, &fileinfo) == 0) {
967                 if (!S_ISLNK(fileinfo.st_mode)) {
968                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
969                         if (tmp_size > 0)
970                                 rw_size += tmp_size;
971                 }
972         }
973
974         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RW_PATH, pkgid);
975         if (lstat(device_path, &fileinfo) == 0) {
976                 if (!S_ISLNK(fileinfo.st_mode)) {
977                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
978                         if (tmp_size > 0)
979                         rw_size += tmp_size;
980                 }
981         }
982
983         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RW_PATH, pkgid);
984         if (lstat(device_path, &fileinfo) == 0) {
985                 if (!S_ISLNK(fileinfo.st_mode)) {
986                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
987                         if (tmp_size > 0)
988                         rw_size += tmp_size;
989                 }
990         }
991
992         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
993         if (lstat(device_path, &fileinfo) == 0) {
994                 if (!S_ISLNK(fileinfo.st_mode)) {
995                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
996                         if (tmp_size > 0)
997                                 rw_size += tmp_size;
998                 }
999         }
1000
1001         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RW_PATH, pkgid);
1002         if (lstat(device_path, &fileinfo) == 0) {
1003                 if (!S_ISLNK(fileinfo.st_mode)) {
1004                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1005                         if (tmp_size > 0)
1006                                 rw_size += tmp_size;
1007         }
1008         }
1009
1010         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RW_PATH, pkgid);
1011         if (lstat(device_path, &fileinfo) == 0) {
1012                 if (!S_ISLNK(fileinfo.st_mode)) {
1013                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1014                         if (tmp_size > 0)
1015                                 rw_size += tmp_size;
1016                 }
1017         }
1018
1019         /* RO area */
1020         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/bin", PKG_RO_PATH, pkgid);
1021         if (lstat(device_path, &fileinfo) == 0) {
1022                 if (!S_ISLNK(fileinfo.st_mode)) {
1023                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1024                         if (tmp_size > 0)
1025                                 ro_size += tmp_size;
1026                 }
1027         }
1028
1029         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/info", PKG_RO_PATH, pkgid);
1030         if (lstat(device_path, &fileinfo) == 0) {
1031                 if (!S_ISLNK(fileinfo.st_mode)) {
1032                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1033                         if (tmp_size > 0)
1034                                 ro_size += tmp_size;
1035                 }
1036         }
1037
1038         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/res", PKG_RO_PATH, pkgid);
1039         if (lstat(device_path, &fileinfo) == 0) {
1040                 if (!S_ISLNK(fileinfo.st_mode)) {
1041                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1042                         if (tmp_size > 0)
1043                                 ro_size += tmp_size;
1044                 }
1045         }
1046
1047         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RO_PATH, pkgid);
1048         if (lstat(device_path, &fileinfo) == 0) {
1049                 if (!S_ISLNK(fileinfo.st_mode)) {
1050                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1051                         if (tmp_size > 0)
1052                                 ro_size += tmp_size;
1053                 }
1054         }
1055
1056         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/shared", PKG_RO_PATH, pkgid);
1057         if (lstat(device_path, &fileinfo) == 0) {
1058                 if (!S_ISLNK(fileinfo.st_mode)) {
1059                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1060                         if (tmp_size > 0)
1061                                 ro_size += tmp_size;
1062                 }
1063         }
1064
1065         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/setting", PKG_RO_PATH, pkgid);
1066         if (lstat(device_path, &fileinfo) == 0) {
1067                 if (!S_ISLNK(fileinfo.st_mode)) {
1068                         tmp_size = _pkgmgr_calculate_dir_size(device_path);
1069                         if (tmp_size > 0)
1070                                 ro_size += tmp_size;
1071                 }
1072         }
1073
1074         /* Total size */
1075         total_size = rw_size + ro_size;
1076         *size = (int)total_size;
1077
1078         return PMINFO_R_OK;
1079 }
1080
1081 API int pkgmgrinfo_pkginfo_get_data_size(pkgmgrinfo_pkginfo_h handle, int *size)
1082 {
1083         char *pkgid;
1084         char device_path[PKG_STRING_LEN_MAX] = { '\0', };
1085         long long total_size = 0;
1086
1087         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1088         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1089
1090         if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid) < 0)
1091                 return PMINFO_R_ERROR;
1092
1093         snprintf(device_path, PKG_STRING_LEN_MAX, "%s%s/data", PKG_RW_PATH, pkgid);
1094         if (access(device_path, R_OK) == 0)
1095                 total_size = _pkgmgr_calculate_dir_size(device_path);
1096         if (total_size < 0)
1097                 return PMINFO_R_ERROR;
1098
1099         *size = (int)total_size;
1100
1101         return PMINFO_R_OK;
1102 }
1103
1104 API int pkgmgrinfo_pkginfo_get_icon(pkgmgrinfo_pkginfo_h handle, char **icon)
1105 {
1106         char *locale;
1107         icon_x *ptr;
1108         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1109
1110         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
1111         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1112
1113         locale = info->locale;
1114         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1115
1116         for (ptr = info->pkg_info->icon; ptr != NULL; ptr = ptr->next) {
1117                 if (ptr->lang == NULL)
1118                         continue;
1119
1120                 if (strcmp(ptr->lang, locale) == 0) {
1121                         *icon = (char *)ptr->text;
1122                         if (strcasecmp(*icon, "(null)") == 0) {
1123                                 locale = DEFAULT_LOCALE;
1124                                 continue;
1125                         } else {
1126                                 return PMINFO_R_OK;
1127                         }
1128                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1129                         *icon = (char *)ptr->text;
1130                         return PMINFO_R_OK;
1131                 }
1132         }
1133
1134         return PMINFO_R_ERROR;
1135 }
1136
1137 API int pkgmgrinfo_pkginfo_get_label(pkgmgrinfo_pkginfo_h handle, char **label)
1138 {
1139         char *locale;
1140         label_x *ptr;
1141         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1142
1143         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
1144         retvm_if(label == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
1145
1146         locale = info->locale;
1147         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1148
1149         for (ptr = info->pkg_info->label; ptr != NULL; ptr = ptr->next) {
1150                 if (ptr->lang == NULL)
1151                         continue;
1152
1153                 if (strcmp(ptr->lang, locale) == 0) {
1154                         *label = (char *)ptr->text;
1155                         if (strcasecmp(*label, "(null)") == 0) {
1156                                 locale = DEFAULT_LOCALE;
1157                                 continue;
1158                         } else {
1159                                 return PMINFO_R_OK;
1160                         }
1161                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1162                         *label = (char *)ptr->text;
1163                         return PMINFO_R_OK;
1164                 }
1165         }
1166
1167         return PMINFO_R_ERROR;
1168 }
1169
1170 API int pkgmgrinfo_pkginfo_get_description(pkgmgrinfo_pkginfo_h handle, char **description)
1171 {
1172         char *locale;
1173         description_x *ptr;
1174         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1175
1176         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1177         retvm_if(description == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1178
1179         locale = info->locale;
1180         retvm_if(locale == NULL, PMINFO_R_ERROR, "manifest locale is NULL");
1181
1182         for (ptr = info->pkg_info->description; ptr != NULL; ptr = ptr->next) {
1183                 if (ptr->lang == NULL)
1184                         continue;
1185
1186                 if (strcmp(ptr->lang, locale) == 0) {
1187                         *description = (char *)ptr->text;
1188                         if (strcasecmp(*description, PKGMGR_PARSER_EMPTY_STR) == 0) {
1189                                 locale = DEFAULT_LOCALE;
1190                                 continue;
1191                         } else {
1192                                 return PMINFO_R_OK;
1193                         }
1194                 } else if (strcmp(ptr->lang, DEFAULT_LOCALE) == 0) {
1195                         *description = (char *)ptr->text;
1196                         return PMINFO_R_OK;
1197                 }
1198         }
1199
1200         return PMINFO_R_ERROR;
1201 }
1202
1203 API int pkgmgrinfo_pkginfo_get_author_name(pkgmgrinfo_pkginfo_h handle, char **author_name)
1204 {
1205         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1206
1207         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1208         retvm_if(author_name == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1209
1210         if (info->pkg_info == NULL || info->pkg_info->author == NULL ||
1211                         info->pkg_info->author->text == NULL)
1212                 return PMINFO_R_ERROR;
1213
1214         *author_name = (char *)info->pkg_info->author->text;
1215
1216         return PMINFO_R_OK;
1217 }
1218
1219 API int pkgmgrinfo_pkginfo_get_author_email(pkgmgrinfo_pkginfo_h handle, char **author_email)
1220 {
1221         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1222
1223         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1224         retvm_if(author_email == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1225
1226         if (info->pkg_info == NULL || info->pkg_info->author == NULL ||
1227                         info->pkg_info->author->email == NULL)
1228                 return PMINFO_R_ERROR;
1229
1230         *author_email = (char *)info->pkg_info->author->email;
1231
1232         return PMINFO_R_OK;
1233 }
1234
1235 API int pkgmgrinfo_pkginfo_get_author_href(pkgmgrinfo_pkginfo_h handle, char **author_href)
1236 {
1237         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1238
1239         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1240         retvm_if(author_href == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1241
1242         if (info->pkg_info == NULL || info->pkg_info->author == NULL ||
1243                         info->pkg_info->author->href == NULL)
1244                 return PMINFO_R_ERROR;
1245
1246         *author_href = (char *)info->pkg_info->author->href;
1247
1248         return PMINFO_R_OK;
1249 }
1250
1251 API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_installed_storage *storage)
1252 {
1253         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1254
1255         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1256         retvm_if(storage == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1257
1258         if (info->pkg_info == NULL || info->pkg_info->installed_storage == NULL)
1259                 return PMINFO_R_ERROR;
1260
1261         if (strcmp(info->pkg_info->installed_storage,"installed_internal") == 0)
1262                 *storage = PMINFO_INTERNAL_STORAGE;
1263         else if (strcmp(info->pkg_info->installed_storage,"installed_external") == 0)
1264                 *storage = PMINFO_EXTERNAL_STORAGE;
1265         else
1266                 return PMINFO_R_ERROR;
1267
1268         return PMINFO_R_OK;
1269 }
1270
1271 API int pkgmgrinfo_pkginfo_get_installed_time(pkgmgrinfo_pkginfo_h handle, int *installed_time)
1272 {
1273         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1274
1275         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1276         retvm_if(installed_time == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1277
1278         if (info->pkg_info == NULL || info->pkg_info->installed_time == NULL)
1279                 return PMINFO_R_ERROR;
1280
1281         *installed_time = atoi(info->pkg_info->installed_time);
1282
1283         return PMINFO_R_OK;
1284 }
1285
1286 API int pkgmgrinfo_pkginfo_get_storeclientid(pkgmgrinfo_pkginfo_h handle, char **storeclientid)
1287 {
1288         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1289
1290         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1291         retvm_if(storeclientid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1292
1293         if (info->pkg_info == NULL || info->pkg_info->storeclient_id == NULL)
1294                 return PMINFO_R_ERROR;
1295
1296         *storeclientid = (char *)info->pkg_info->storeclient_id;
1297
1298         return PMINFO_R_OK;
1299 }
1300
1301 API int pkgmgrinfo_pkginfo_get_mainappid(pkgmgrinfo_pkginfo_h handle, char **mainappid)
1302 {
1303         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1304
1305         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1306         retvm_if(mainappid == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1307
1308         if (info->pkg_info == NULL || info->pkg_info->mainapp_id == NULL)
1309                 return PMINFO_R_ERROR;
1310
1311         *mainappid = (char *)info->pkg_info->mainapp_id;
1312
1313         return PMINFO_R_OK;
1314 }
1315
1316 API int pkgmgrinfo_pkginfo_get_url(pkgmgrinfo_pkginfo_h handle, char **url)
1317 {
1318         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1319
1320         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1321         retvm_if(url == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1322
1323         if (info->pkg_info == NULL || info->pkg_info->package_url == NULL)
1324                 return PMINFO_R_ERROR;
1325
1326         *url = (char *)info->pkg_info->package_url;
1327
1328         return PMINFO_R_OK;
1329 }
1330
1331 API int pkgmgrinfo_pkginfo_get_size_from_xml(const char *manifest, int *size)
1332 {
1333         const char *val = NULL;
1334         const xmlChar *node;
1335         xmlTextReaderPtr reader;
1336         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
1337         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1338
1339         xmlInitParser();
1340         reader = xmlReaderForFile(manifest, NULL, 0);
1341
1342         if (reader){
1343                 if (__child_element(reader, -1)) {
1344                         node = xmlTextReaderConstName(reader);
1345                         if (!node) {
1346                                 _LOGE("xmlTextReaderConstName value is NULL\n");
1347                                 xmlFreeTextReader(reader);
1348                                 xmlCleanupParser();
1349                                 return PMINFO_R_ERROR;
1350                         }
1351
1352                         if (!strcmp(ASC_CHAR(node), "manifest")) {
1353                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("size")))
1354                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("size")));
1355
1356                                 if (val) {
1357                                         *size = atoi(val);
1358                                 } else {
1359                                         *size = 0;
1360                                         _LOGE("package size is not specified\n");
1361                                         xmlFreeTextReader(reader);
1362                                         xmlCleanupParser();
1363                                         return PMINFO_R_ERROR;
1364                                 }
1365                         } else {
1366                                 _LOGE("Unable to create xml reader\n");
1367                                 xmlFreeTextReader(reader);
1368                                 xmlCleanupParser();
1369                                 return PMINFO_R_ERROR;
1370                         }
1371                 }
1372         } else {
1373                 _LOGE("xmlReaderForFile value is NULL\n");
1374                 xmlCleanupParser();
1375                 return PMINFO_R_ERROR;
1376         }
1377
1378         xmlFreeTextReader(reader);
1379         xmlCleanupParser();
1380
1381         return PMINFO_R_OK;
1382 }
1383
1384 API int pkgmgrinfo_pkginfo_get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
1385 {
1386         const char *val = NULL;
1387         const xmlChar *node;
1388         xmlTextReaderPtr reader;
1389         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "Input argument is NULL\n");
1390         retvm_if(location == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1391
1392         xmlInitParser();
1393         reader = xmlReaderForFile(manifest, NULL, 0);
1394
1395         if (reader) {
1396                 if ( __child_element(reader, -1)) {
1397                         node = xmlTextReaderConstName(reader);
1398                         if (!node) {
1399                                 _LOGE("xmlTextReaderConstName value is NULL\n");
1400                                 xmlFreeTextReader(reader);
1401                                 xmlCleanupParser();
1402                                 return PMINFO_R_ERROR;
1403                         }
1404
1405                         if (!strcmp(ASC_CHAR(node), "manifest")) {
1406                                 if (xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")))
1407                                         val = ASC_CHAR(xmlTextReaderGetAttribute(reader, XML_CHAR("install-location")));
1408
1409                                 if (val) {
1410                                         if (strcmp(val, "internal-only") == 0)
1411                                                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
1412                                         else if (strcmp(val, "prefer-external") == 0)
1413                                                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
1414                                         else
1415                                                 *location = PMINFO_INSTALL_LOCATION_AUTO;
1416                                 }
1417                         } else {
1418                                 _LOGE("Unable to create xml reader\n");
1419                                 xmlFreeTextReader(reader);
1420                                 xmlCleanupParser();
1421                                 return PMINFO_R_ERROR;
1422                         }
1423                 }
1424         } else {
1425                 _LOGE("xmlReaderForFile value is NULL\n");
1426                 xmlCleanupParser();
1427                 return PMINFO_R_ERROR;
1428         }
1429
1430         xmlFreeTextReader(reader);
1431         xmlCleanupParser();
1432
1433         return PMINFO_R_OK;
1434 }
1435
1436
1437 API int pkgmgrinfo_pkginfo_get_root_path(pkgmgrinfo_pkginfo_h handle, char **path)
1438 {
1439         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1440
1441         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1442         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1443
1444         if (info->pkg_info == NULL || info->pkg_info->root_path == NULL)
1445                 return PMINFO_R_ERROR;
1446
1447         *path = (char *)info->pkg_info->root_path;
1448
1449         return PMINFO_R_OK;
1450 }
1451
1452 API int pkgmgrinfo_pkginfo_get_csc_path(pkgmgrinfo_pkginfo_h handle, char **path)
1453 {
1454         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1455
1456         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1457         retvm_if(path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1458
1459         if (info->pkg_info == NULL || info->pkg_info->csc_path == NULL)
1460                 return PMINFO_R_ERROR;
1461
1462         *path = (char *)info->pkg_info->csc_path;
1463
1464         return PMINFO_R_OK;
1465 }
1466
1467
1468 API int pkgmgrinfo_pkginfo_is_accessible(pkgmgrinfo_pkginfo_h handle, bool *accessible)
1469 {
1470         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1471         retvm_if(accessible == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1472
1473 #if 0 //smack issue occured, check later
1474         char *pkgid = NULL;
1475         pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
1476         if (pkgid == NULL){
1477                  _LOGD("invalid func parameters\n");
1478                  return PMINFO_R_ERROR;
1479         }
1480          _LOGD("pkgmgr_get_pkg_external_validation() called\n");
1481
1482         FILE *fp = NULL;
1483         char app_mmc_path[FILENAME_MAX] = { 0, };
1484         char app_dir_path[FILENAME_MAX] = { 0, };
1485         char app_mmc_internal_path[FILENAME_MAX] = { 0, };
1486         snprintf(app_dir_path, FILENAME_MAX,"%s%s", PKG_INSTALLATION_PATH, pkgid);
1487         snprintf(app_mmc_path, FILENAME_MAX,"%s%s", PKG_SD_PATH, pkgid);
1488         snprintf(app_mmc_internal_path, FILENAME_MAX,"%s%s/.mmc", PKG_INSTALLATION_PATH, pkgid);
1489
1490         /*check whether application is in external memory or not */
1491         fp = fopen(app_mmc_path, "r");
1492         if (fp == NULL){
1493                 _LOGD(" app path in external memory not accesible\n");
1494         } else {
1495                 fclose(fp);
1496                 fp = NULL;
1497                 *accessible = 1;
1498                 _LOGD("pkgmgr_get_pkg_external_validation() : SD_CARD \n");
1499                 return PMINFO_R_OK;
1500         }
1501
1502         /*check whether application is in internal or not */
1503         fp = fopen(app_dir_path, "r");
1504         if (fp == NULL) {
1505                 _LOGD(" app path in internal memory not accesible\n");
1506                 *accessible = 0;
1507                 return PMINFO_R_ERROR;
1508         } else {
1509                 fclose(fp);
1510                 /*check whether the application is installed in SD card
1511                 but SD card is not present*/
1512                 fp = fopen(app_mmc_internal_path, "r");
1513                 if (fp == NULL){
1514                         *accessible = 1;
1515                         _LOGD("pkgmgr_get_pkg_external_validation() : INTERNAL_MEM \n");
1516                         return PMINFO_R_OK;
1517                 }
1518                 else{
1519                         *accessible = 0;
1520                         _LOGD("pkgmgr_get_pkg_external_validation() : ERROR_MMC_STATUS \n");
1521                 }
1522                 fclose(fp);
1523         }
1524
1525         _LOGD("pkgmgr_get_pkg_external_validation() end\n");
1526 #endif
1527
1528         *accessible = 1;
1529         return PMINFO_R_OK;
1530 }
1531
1532 API int pkgmgrinfo_pkginfo_is_removable(pkgmgrinfo_pkginfo_h handle, bool *removable)
1533 {
1534         char *val;
1535         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1536
1537         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1538         retvm_if(removable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1539
1540         if (info->pkg_info == NULL || info->pkg_info->removable == NULL)
1541                 return PMINFO_R_ERROR;
1542
1543         val = (char *)info->pkg_info->removable;
1544         if (strcasecmp(val, "true") == 0)
1545                 *removable = 1;
1546         else if (strcasecmp(val, "false") == 0)
1547                 *removable = 0;
1548         else
1549                 *removable = 1;
1550
1551         return PMINFO_R_OK;
1552 }
1553
1554 API int pkgmgrinfo_pkginfo_is_movable(pkgmgrinfo_pkginfo_h handle, bool *movable)
1555 {
1556         char *val;
1557         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1558
1559         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1560         retvm_if(movable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1561
1562         if (info->pkg_info == NULL || info->pkg_info->installlocation == NULL)
1563                 return PMINFO_R_ERROR;
1564
1565         val = (char *)info->pkg_info->installlocation;
1566         if (strcmp(val, "internal-only") == 0)
1567                 *movable = 0;
1568         else if (strcmp(val, "prefer-external") == 0)
1569                 *movable = 1;
1570         else
1571                 *movable = 1;
1572
1573         return PMINFO_R_OK;
1574 }
1575
1576 API int pkgmgrinfo_pkginfo_is_preload(pkgmgrinfo_pkginfo_h handle, bool *preload)
1577 {
1578         char *val;
1579         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1580
1581         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1582         retvm_if(preload == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1583
1584         if (info->pkg_info == NULL || info->pkg_info->preload == NULL)
1585                 return PMINFO_R_ERROR;
1586
1587         val = (char *)info->pkg_info->preload;
1588         if (strcasecmp(val, "true") == 0)
1589                 *preload = 1;
1590         else if (strcasecmp(val, "false") == 0)
1591                 *preload = 0;
1592         else
1593                 *preload = 0;
1594
1595         return PMINFO_R_OK;
1596 }
1597
1598 API int pkgmgrinfo_pkginfo_is_system(pkgmgrinfo_pkginfo_h handle, bool *system)
1599 {
1600         char *val;
1601         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1602
1603         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1604         retvm_if(system == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1605
1606         if (info->pkg_info == NULL || info->pkg_info->system == NULL)
1607                 return PMINFO_R_ERROR;
1608
1609         val = (char *)info->pkg_info->system;
1610         if (strcasecmp(val, "true") == 0)
1611                 *system = 1;
1612         else if (strcasecmp(val, "false") == 0)
1613                 *system = 0;
1614         else
1615                 *system = 0;
1616
1617         return PMINFO_R_OK;
1618 }
1619
1620 API int pkgmgrinfo_pkginfo_is_readonly(pkgmgrinfo_pkginfo_h handle, bool *readonly)
1621 {
1622         char *val;
1623         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1624
1625         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1626         retvm_if(readonly == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1627
1628         if (info->pkg_info == NULL || info->pkg_info->readonly == NULL)
1629                 return PMINFO_R_ERROR;
1630
1631         val = (char *)info->pkg_info->readonly;
1632         if (strcasecmp(val, "true") == 0)
1633                 *readonly = 1;
1634         else if (strcasecmp(val, "false") == 0)
1635                 *readonly = 0;
1636         else
1637                 *readonly = 0;
1638
1639         return PMINFO_R_OK;
1640 }
1641
1642 API int pkgmgrinfo_pkginfo_is_update(pkgmgrinfo_pkginfo_h handle, bool *update)
1643 {
1644         char *val;
1645         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1646
1647         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1648         retvm_if(update == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1649
1650         if (info->pkg_info == NULL || info->pkg_info->update == NULL)
1651                 return PMINFO_R_ERROR;
1652
1653         val = (char *)info->pkg_info->update;
1654         if (strcasecmp(val, "true") == 0)
1655                 *update = 1;
1656         else if (strcasecmp(val, "false") == 0)
1657                 *update = 0;
1658         else
1659                 *update = 1;
1660
1661         return PMINFO_R_OK;
1662 }
1663
1664 API int pkgmgrinfo_pkginfo_is_support_disable(pkgmgrinfo_pkginfo_h handle, bool *support_disable)
1665 {
1666         char *val;
1667         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1668
1669         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1670         retvm_if(support_disable == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1671
1672         if (info->pkg_info == NULL || info->pkg_info->support_disable == NULL)
1673                 return PMINFO_R_ERROR;
1674
1675         val = (char *)info->pkg_info->support_disable;
1676         if (strcasecmp(val, "true") == 0)
1677                 *support_disable = 1;
1678         else if (strcasecmp(val, "false") == 0)
1679                 *support_disable = 0;
1680         else
1681                 *support_disable = 1;
1682
1683         return PMINFO_R_OK;
1684 }
1685
1686 API int pkgmgrinfo_pkginfo_is_for_all_users(pkgmgrinfo_pkginfo_h handle, bool *for_all_users)
1687 {
1688         char *val;
1689         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1690
1691         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1692         retvm_if(for_all_users == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
1693
1694         if (info->pkg_info == NULL || info->pkg_info->for_all_users == NULL)
1695                 return PMINFO_R_ERROR;
1696
1697         val = (char *)info->pkg_info->for_all_users;
1698         if (strcasecmp(val, "true") == 0)
1699                 *for_all_users = 1;
1700         else if (strcasecmp(val, "false") == 0)
1701                 *for_all_users = 0;
1702         else
1703                 *for_all_users = 1;
1704
1705         return PMINFO_R_OK;
1706 }
1707
1708 API int pkgmgrinfo_pkginfo_destroy_pkginfo(pkgmgrinfo_pkginfo_h handle)
1709 {
1710         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1711
1712         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
1713
1714         __cleanup_pkginfo(info);
1715
1716         return PMINFO_R_OK;
1717 }
1718
1719 API int pkgmgrinfo_pkginfo_filter_create(pkgmgrinfo_pkginfo_filter_h *handle)
1720 {
1721         pkgmgrinfo_filter_x *filter;
1722
1723         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle output parameter is NULL\n");
1724
1725         filter = (pkgmgrinfo_filter_x*)calloc(1, sizeof(pkgmgrinfo_filter_x));
1726         if (filter == NULL) {
1727                 _LOGE("Out of Memory!!!");
1728                 return PMINFO_R_ERROR;
1729         }
1730
1731         *handle = filter;
1732
1733         return PMINFO_R_OK;
1734 }
1735
1736 API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
1737 {
1738         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1739
1740         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1741
1742         if (filter->list) {
1743                 g_slist_foreach(filter->list, __destroy_each_node, NULL);
1744                 g_slist_free(filter->list);
1745         }
1746
1747         free(filter);
1748
1749         return PMINFO_R_OK;
1750 }
1751
1752 API int pkgmgrinfo_pkginfo_filter_add_int(pkgmgrinfo_pkginfo_filter_h handle,
1753                                 const char *property, const int value)
1754 {
1755         char buf[PKG_VALUE_STRING_LEN_MAX] = {'\0'};
1756         char *val;
1757         GSList *link;
1758         int prop;
1759         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1760         pkgmgrinfo_node_x *node;
1761
1762         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1763         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1764
1765         prop = _pminfo_pkginfo_convert_to_prop_int(property);
1766         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT ||
1767                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT) {
1768                 _LOGE("Invalid Integer Property\n");
1769                 return PMINFO_R_EINVAL;
1770         }
1771         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
1772         if (node == NULL) {
1773                 _LOGE("Out of Memory!!!\n");
1774                 return PMINFO_R_ERROR;
1775         }
1776         snprintf(buf, PKG_VALUE_STRING_LEN_MAX - 1, "%d", value);
1777         val = strndup(buf, PKG_VALUE_STRING_LEN_MAX - 1);
1778         if (val == NULL) {
1779                 _LOGE("Out of Memory\n");
1780                 free(node);
1781                 return PMINFO_R_ERROR;
1782         }
1783         node->prop = prop;
1784         node->value = val;
1785         /*If API is called multiple times for same property, we should override the previous values.
1786         Last value set will be used for filtering.*/
1787         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1788         if (link)
1789                 filter->list = g_slist_delete_link(filter->list, link);
1790         filter->list = g_slist_append(filter->list, (gpointer)node);
1791         return PMINFO_R_OK;
1792
1793 }
1794
1795 API int pkgmgrinfo_pkginfo_filter_add_bool(pkgmgrinfo_pkginfo_filter_h handle,
1796                                 const char *property, const bool value)
1797 {
1798         char *val;
1799         GSList *link;
1800         int prop;
1801         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1802         pkgmgrinfo_node_x *node;
1803
1804         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1805         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1806
1807         prop = _pminfo_pkginfo_convert_to_prop_bool(property);
1808         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL ||
1809                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL) {
1810                 _LOGE("Invalid Boolean Property\n");
1811                 return PMINFO_R_EINVAL;
1812         }
1813         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
1814         if (node == NULL) {
1815                 _LOGE("Out of Memory!!!\n");
1816                 return PMINFO_R_ERROR;
1817         }
1818         if (value)
1819                 val = strndup("('true','True')", 15);
1820         else
1821                 val = strndup("('false','False')", 17);
1822         if (val == NULL) {
1823                 _LOGE("Out of Memory\n");
1824                 free(node);
1825                 return PMINFO_R_ERROR;
1826         }
1827         node->prop = prop;
1828         node->value = val;
1829         /*If API is called multiple times for same property, we should override the previous values.
1830         Last value set will be used for filtering.*/
1831         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1832         if (link)
1833                 filter->list = g_slist_delete_link(filter->list, link);
1834         filter->list = g_slist_append(filter->list, (gpointer)node);
1835         return PMINFO_R_OK;
1836
1837 }
1838
1839 API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
1840                                 const char *property, const char *value)
1841 {
1842         char *val;
1843         GSList *link;
1844         int prop;
1845         pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x *)handle;
1846         pkgmgrinfo_node_x *node;
1847
1848         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1849         retvm_if(property == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1850         retvm_if(value == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
1851
1852         prop = _pminfo_pkginfo_convert_to_prop_str(property);
1853         if (prop < E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR ||
1854                 prop > E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR) {
1855                 _LOGE("Invalid String Property\n");
1856                 return PMINFO_R_EINVAL;
1857         }
1858         node = (pkgmgrinfo_node_x *)calloc(1, sizeof(pkgmgrinfo_node_x));
1859         if (node == NULL) {
1860                 _LOGE("Out of Memory!!!\n");
1861                 return PMINFO_R_ERROR;
1862         }
1863         if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_AUTO) == 0)
1864                 val = strndup("auto", PKG_STRING_LEN_MAX - 1);
1865         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_INTERNAL) == 0)
1866                 val = strndup("internal-only", PKG_STRING_LEN_MAX - 1);
1867         else if (strcmp(value, PMINFO_PKGINFO_INSTALL_LOCATION_EXTERNAL) == 0)
1868                 val = strndup("prefer-external", PKG_STRING_LEN_MAX - 1);
1869         else if (strcmp(value, "installed_internal") == 0)
1870                 val = strndup("installed_internal", PKG_STRING_LEN_MAX - 1);
1871         else if (strcmp(value, "installed_external") == 0)
1872                 val = strndup("installed_external", PKG_STRING_LEN_MAX - 1);
1873         else
1874                 val = strndup(value, PKG_STRING_LEN_MAX - 1);
1875         if (val == NULL) {
1876                 _LOGE("Out of Memory\n");
1877                 free(node);
1878                 return PMINFO_R_ERROR;
1879         }
1880         node->prop = prop;
1881         node->value = val;
1882         /*If API is called multiple times for same property, we should override the previous values.
1883         Last value set will be used for filtering.*/
1884         link = g_slist_find_custom(filter->list, (gconstpointer)node, __compare_func);
1885         if (link)
1886                 filter->list = g_slist_delete_link(filter->list, link);
1887         filter->list = g_slist_append(filter->list, (gpointer)node);
1888         return PMINFO_R_OK;
1889
1890 }
1891
1892 API int pkgmgrinfo_pkginfo_usr_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count, uid_t uid)
1893 {
1894         int ret;
1895         GList *list = NULL;
1896
1897         if (handle == NULL || count == NULL) {
1898                 _LOGE("invalid parameter");
1899                 return PMINFO_R_EINVAL;
1900         }
1901
1902         ret = _pkginfo_get_filtered_list((pkgmgrinfo_filter_x *)handle, uid, &list);
1903         if (ret != PMINFO_R_OK)
1904                 return PMINFO_R_ERROR;
1905
1906         *count = g_list_length(list);
1907
1908         g_list_free_full(list, free);
1909
1910         return PMINFO_R_OK;
1911 }
1912
1913 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)
1914 {
1915         return pkgmgrinfo_pkginfo_usr_filter_count(handle, count, GLOBAL_USER);
1916 }
1917
1918 API int pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(
1919                 pkgmgrinfo_pkginfo_filter_h handle,
1920                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data, uid_t uid)
1921 {
1922         if (handle == NULL || pkg_cb == NULL) {
1923                 LOGE("invalid parameter");
1924                 return PMINFO_R_EINVAL;
1925         }
1926
1927         return _pkginfo_get_filtered_foreach_pkginfo(handle, pkg_cb, user_data,
1928                         uid);
1929 }
1930
1931 API int pkgmgrinfo_pkginfo_filter_foreach_pkginfo(pkgmgrinfo_pkginfo_filter_h handle,
1932                                 pkgmgrinfo_pkg_list_cb pkg_cb, void *user_data)
1933 {
1934         return pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(handle, pkg_cb, user_data, GLOBAL_USER);
1935 }
1936
1937 API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle,
1938                         pkgmgrinfo_pkg_privilege_list_cb privilege_func, void *user_data)
1939 {
1940         retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
1941         retvm_if(privilege_func == NULL, PMINFO_R_EINVAL, "Callback function is NULL");
1942         int ret = -1;
1943         privilege_x *ptr = NULL;
1944         pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
1945         ptr = info->pkg_info->privileges->privilege;
1946         for (; ptr; ptr = ptr->next) {
1947                 if (ptr->text){
1948                         ret = privilege_func(ptr->text, user_data);
1949                         if (ret < 0)
1950                                 break;
1951                 }
1952         }
1953         return PMINFO_R_OK;
1954 }
1955
1956 API int pkgmgrinfo_create_pkgusrdbinfo(const char *pkgid, uid_t uid, pkgmgrinfo_pkgdbinfo_h *handle)
1957 {
1958         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
1959         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
1960
1961         char *manifest = NULL;
1962         manifest_x *mfx = NULL;
1963         *handle = NULL;
1964         manifest = pkgmgr_parser_get_usr_manifest_file(pkgid, uid);
1965         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
1966
1967         mfx = pkgmgr_parser_usr_process_manifest_xml(manifest, uid);
1968         if (manifest) {
1969                 free(manifest);
1970                 manifest = NULL;
1971         }
1972         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
1973
1974         *handle = (void *)mfx;
1975
1976         return PMINFO_R_OK;
1977 }
1978
1979 API int pkgmgrinfo_create_pkgdbinfo(const char *pkgid, pkgmgrinfo_pkgdbinfo_h *handle)
1980 {
1981         retvm_if(!pkgid, PMINFO_R_EINVAL, "pkgid is NULL");
1982         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
1983
1984         char *manifest = NULL;
1985         manifest_x *mfx = NULL;
1986         *handle = NULL;
1987         manifest = pkgmgr_parser_get_manifest_file(pkgid);
1988         retvm_if(manifest == NULL, PMINFO_R_EINVAL, "pkg[%s] dont have manifest file", pkgid);
1989
1990         mfx = pkgmgr_parser_process_manifest_xml(manifest);
1991         if (manifest) {
1992                 free(manifest);
1993                 manifest = NULL;
1994         }
1995         retvm_if(mfx == NULL, PMINFO_R_EINVAL, "pkg[%s] parsing fail", pkgid);
1996
1997         *handle = (void *)mfx;
1998
1999         return PMINFO_R_OK;
2000 }
2001
2002 API int pkgmgrinfo_set_type_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *type)
2003 {
2004         int len;
2005         manifest_x *mfx = (manifest_x *)handle;
2006
2007         retvm_if(!type, PMINFO_R_EINVAL, "Argument supplied is NULL");
2008         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2009
2010         len = strlen(type);
2011         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2012
2013         if (mfx->type)
2014                 free((void *)mfx->type);
2015
2016         mfx->type = strndup(type, PKG_TYPE_STRING_LEN_MAX);
2017
2018         return PMINFO_R_OK;
2019 }
2020
2021 API int pkgmgrinfo_set_version_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *version)
2022 {
2023         int len;
2024         manifest_x *mfx = (manifest_x *)handle;
2025
2026         retvm_if(!version, PMINFO_R_EINVAL, "Argument supplied is NULL");
2027         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2028
2029         len = strlen(version);
2030         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2031
2032         if (mfx->version)
2033                 free((void *)mfx->version);
2034
2035         mfx->version = strndup(version, PKG_VERSION_STRING_LEN_MAX);
2036         return PMINFO_R_OK;
2037 }
2038
2039 API int pkgmgrinfo_set_install_location_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
2040 {
2041         manifest_x *mfx = (manifest_x *)handle;
2042
2043         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2044         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2045
2046         if (mfx->installlocation)
2047                 free((void *)mfx->installlocation);
2048
2049         if (location == INSTALL_INTERNAL)
2050                 mfx->installlocation = strdup("internal-only");
2051         else if (location == INSTALL_EXTERNAL)
2052                 mfx->installlocation = strdup("prefer-external");
2053
2054         return PMINFO_R_OK;
2055 }
2056
2057 API int pkgmgrinfo_set_size_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *size)
2058 {
2059         manifest_x *mfx = (manifest_x *)handle;
2060
2061         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2062         retvm_if(size == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
2063
2064         if (mfx->package_size)
2065                 free((void *)mfx->package_size);
2066
2067         mfx->package_size = strdup(size);
2068
2069         return PMINFO_R_OK;
2070 }
2071
2072 API int pkgmgrinfo_set_label_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *label_txt, const char *locale)
2073 {
2074         int len;
2075         manifest_x *mfx = (manifest_x *)handle;
2076         label_x *label;
2077
2078         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2079         retvm_if(!label_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
2080
2081         len = strlen(label_txt);
2082         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2083
2084         label = calloc(1, sizeof(label_x));
2085         retvm_if(label == NULL, PMINFO_R_EINVAL, "Malloc Failed");
2086
2087         LISTADD(mfx->label, label);
2088         if (locale)
2089                 mfx->label->lang = strdup(locale);
2090         else
2091                 mfx->label->lang = strdup(DEFAULT_LOCALE);
2092         mfx->label->text = strdup(label_txt);
2093
2094         return PMINFO_R_OK;
2095 }
2096
2097 API int pkgmgrinfo_set_icon_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *icon_txt, const char *locale)
2098 {
2099         int len;
2100         manifest_x *mfx = (manifest_x *)handle;
2101         icon_x *icon;
2102
2103         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2104         retvm_if(!icon_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
2105
2106         len = strlen(icon_txt);
2107         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2108
2109         icon = calloc(1, sizeof(icon_x));
2110         retvm_if(icon == NULL, PMINFO_R_EINVAL, "Malloc Failed");
2111
2112         LISTADD(mfx->icon, icon);
2113         if (locale)
2114                 mfx->icon->lang = strdup(locale);
2115         else
2116                 mfx->icon->lang = strdup(DEFAULT_LOCALE);
2117         mfx->icon->text = strdup(icon_txt);
2118
2119         return PMINFO_R_OK;
2120 }
2121
2122 API int pkgmgrinfo_set_description_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *desc_txt, const char *locale)
2123 {
2124         int len = strlen(desc_txt);
2125         manifest_x *mfx = (manifest_x *)handle;
2126         description_x *description;
2127
2128         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2129         retvm_if(!desc_txt, PMINFO_R_EINVAL, "Argument supplied is NULL");
2130
2131         len = strlen(desc_txt);
2132         retvm_if(len > PKG_TYPE_STRING_LEN_MAX, PMINFO_R_EINVAL, "pkg type length exceeds the max limit");
2133
2134         description = calloc(1, sizeof(description_x));
2135         retvm_if(description == NULL, PMINFO_R_EINVAL, "Malloc Failed");
2136
2137         LISTADD(mfx->description, description);
2138         if (locale)
2139                 mfx->description->lang = strdup(locale);
2140         else
2141                 mfx->description->lang = strdup(DEFAULT_LOCALE);
2142         mfx->description->text = strdup(desc_txt);
2143
2144         return PMINFO_R_OK;
2145 }
2146
2147 API int pkgmgrinfo_set_author_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, const char *author_name,
2148                 const char *author_email, const char *author_href, const char *locale)
2149 {
2150         manifest_x *mfx = (manifest_x *)handle;
2151         author_x *author;
2152
2153         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2154
2155         author = calloc(1, sizeof(author_x));
2156         retvm_if(author == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL");
2157
2158         LISTADD(mfx->author, author);
2159         if (author_name)
2160                 mfx->author->text = strdup(author_name);
2161         if (author_email)
2162                 mfx->author->email = strdup(author_email);
2163         if (author_href)
2164                 mfx->author->href = strdup(author_href);
2165         if (locale)
2166                 mfx->author->lang = strdup(locale);
2167         else
2168                 mfx->author->lang = strdup(DEFAULT_LOCALE);
2169         return PMINFO_R_OK;
2170 }
2171
2172 API int pkgmgrinfo_set_removable_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int removable)
2173 {
2174         manifest_x *mfx = (manifest_x *)handle;
2175
2176         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2177         retvm_if((removable < 0) || (removable > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2178
2179         if (mfx->removable)
2180                 free((void *)mfx->removable);
2181
2182         if (removable == 0)
2183                 mfx->removable = strdup("false");
2184         else if (removable == 1)
2185                 mfx->removable = strdup("true");
2186
2187         return PMINFO_R_OK;
2188 }
2189
2190 API int pkgmgrinfo_set_preload_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, int preload)
2191 {
2192         manifest_x *mfx = (manifest_x *)handle;
2193
2194         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2195         retvm_if((preload < 0) || (preload > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2196
2197         if (mfx->preload)
2198                 free((void *)mfx->preload);
2199
2200         if (preload == 0)
2201                 mfx->preload = strdup("false");
2202         else if (preload == 1)
2203                 mfx->preload = strdup("true");
2204
2205         return PMINFO_R_OK;
2206 }
2207
2208 API int pkgmgrinfo_set_installed_storage_to_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle, INSTALL_LOCATION location)
2209 {
2210         manifest_x *mfx = (manifest_x *)handle;
2211
2212         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2213         retvm_if((location < 0) || (location > 1), PMINFO_R_EINVAL, "Argument supplied is NULL");
2214
2215         if (mfx->installed_storage)
2216                 free((void *)mfx->installed_storage);
2217
2218         if (location == INSTALL_INTERNAL)
2219                 mfx->installed_storage = strdup("installed_internal");
2220         else if (location == INSTALL_EXTERNAL)
2221                 mfx->installed_storage = strdup("installed_external");
2222
2223         return PMINFO_R_OK;
2224 }
2225
2226 API int pkgmgrinfo_save_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
2227 {
2228         int ret;
2229         manifest_x *mfx = (manifest_x *)handle;
2230         mfx = (manifest_x *)handle;
2231
2232         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2233
2234         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
2235         if (ret == 0) {
2236                 _LOGE("Successfully stored info in DB\n");
2237                 return PMINFO_R_OK;
2238         } else {
2239                 _LOGE("Failed to store info in DB\n");
2240                 return PMINFO_R_ERROR;
2241         }
2242 }
2243
2244 API int pkgmgrinfo_save_pkgusrdbinfo(pkgmgrinfo_pkgdbinfo_h handle, uid_t uid)
2245 {
2246         int ret;
2247         manifest_x *mfx = (manifest_x *)handle;
2248
2249         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2250
2251         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
2252         if (ret == 0) {
2253                 _LOGE("Successfully stored info in DB\n");
2254                 return PMINFO_R_OK;
2255         } else {
2256                 _LOGE("Failed to store info in DB\n");
2257                 return PMINFO_R_ERROR;
2258         }
2259 }
2260
2261 API int pkgmgrinfo_destroy_pkgdbinfo(pkgmgrinfo_pkgdbinfo_h handle)
2262 {
2263         manifest_x *mfx = (manifest_x *)handle;
2264
2265         retvm_if(!handle, PMINFO_R_EINVAL, "Argument supplied is NULL");
2266
2267         pkgmgrinfo_basic_free_package(mfx);
2268
2269         return PMINFO_R_OK;
2270 }