seperate tool from slp-pkgmgr project 84/50484/1 accepted/tizen/mobile/20151031.105507 accepted/tizen/tv/20151031.110237 accepted/tizen/wearable/20151031.110515 submit/tizen/20151030.025053 submit/tizen/20151030.025235
authorjongmyeongko <jongmyeong.ko@samsung.com>
Thu, 29 Oct 2015 07:27:54 +0000 (16:27 +0900)
committerjongmyeongko <jongmyeong.ko@samsung.com>
Thu, 29 Oct 2015 07:35:30 +0000 (16:35 +0900)
this change is for preventing cyclic dependency.

Change-Id: I59cfc691218a592a8ba88497802de8993604538c
Signed-off-by: jongmyeongko <jongmyeong.ko@samsung.com>
16 files changed:
AUTHORS [new file with mode: 0644]
CMakeLists.txt [new file with mode: 0644]
LICENSE [new file with mode: 0644]
data/10_package-manager-add.post [new file with mode: 0644]
data/mime.tpk.xml [new file with mode: 0644]
data/mime.wac.xml [new file with mode: 0644]
data/pkgmgr.patch.sh.in [new file with mode: 0644]
packaging/pkgmgr-tool.manifest [new file with mode: 0644]
packaging/pkgmgr-tool.spec [new file with mode: 0644]
src/pkg_clearcache.c [new file with mode: 0644]
src/pkg_cmd.c [new file with mode: 0644]
src/pkg_getsize.c [new file with mode: 0644]
src/pkg_info.c [new file with mode: 0644]
src/pkg_initdb.c [new file with mode: 0644]
src/pkg_install_ug.c [new file with mode: 0644]
src/pkg_privilege.c [new file with mode: 0644]

diff --git a/AUTHORS b/AUTHORS
new file mode 100644 (file)
index 0000000..d908bc1
--- /dev/null
+++ b/AUTHORS
@@ -0,0 +1,4 @@
+Jayoun Lee <airjany@samsung.com>
+Sewook Park <sewook7.park@samsung.com>
+Jaeho Lee <jaeho81.lee@samsung.com>
+Shobhit Srivastava <shobhit.s@samsung.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..18b6ccf
--- /dev/null
@@ -0,0 +1,79 @@
+#
+# Copyright (c) 2015 Samsung Electronics Co., Ltd.
+# All rights reserved
+#
+
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
+SET(CMAKE_SKIP_BUILD_RPATH true)
+
+PROJECT(pkgmgr-tool C)
+
+SET(VERSION 0.1.0)
+SET(VERSION_MAJOR 0)
+
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+SET(EXEC_PREFIX "\${prefix}")
+SET(LIBDIR ${LIB_INSTALL_DIR})
+SET(INCLUDEDIR "\${prefix}/include")
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TEST_CFLAGS}")
+
+#Verbose
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/client/include)
+
+INCLUDE(FindPkgConfig)
+
+pkg_check_modules(pkgs_initdb REQUIRED libsmack libxml-2.0 bundle pkgmgr-parser pkgmgr-info libtzplatform-config pkgmgr)
+FOREACH(flag ${pkgs_initdb_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+pkg_check_modules(pkgs_test REQUIRED dlog glib-2.0 libxml-2.0 bundle pkgmgr-parser pkgmgr-info libtzplatform-config security-manager pkgmgr)
+FOREACH(flag ${pkgs_test_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs -pie" )
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -Wall")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIE")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -fPIE")
+SET(CMAKE_C_FLAGS_RELEASE "-O2 -fPIE")
+
+ADD_EXECUTABLE(pkgcmd src/pkg_cmd.c)
+TARGET_LINK_LIBRARIES(pkgcmd pkgmgr-client ${pkgs_test_LDFLAGS})
+INSTALL(TARGETS pkgcmd DESTINATION bin)
+
+ADD_EXECUTABLE(pkginfo src/pkg_info.c)
+TARGET_LINK_LIBRARIES(pkginfo pkgmgr-client pkgmgr_installer ${pkgs_test_LDFLAGS})
+INSTALL(TARGETS pkginfo DESTINATION bin)
+
+ADD_EXECUTABLE(pkg_privilege src/pkg_privilege.c)
+TARGET_LINK_LIBRARIES(pkg_privilege ${pkgs_test_LDFLAGS})
+INSTALL(TARGETS pkg_privilege DESTINATION bin)
+
+ADD_EXECUTABLE(pkg_install_ug src/pkg_install_ug.c)
+TARGET_LINK_LIBRARIES(pkg_install_ug ${pkgs_test_LDFLAGS})
+INSTALL(TARGETS pkg_install_ug DESTINATION bin)
+
+ADD_EXECUTABLE(pkg_getsize src/pkg_getsize.c)
+TARGET_LINK_LIBRARIES(pkg_getsize pkgmgr-client pkgmgr_installer ${pkgs_test_LDFLAGS})
+INSTALL(TARGETS pkg_getsize DESTINATION bin)
+
+ADD_EXECUTABLE(pkg_clearcache src/pkg_clearcache.c)
+TARGET_LINK_LIBRARIES(pkg_clearcache ${pkgs_test_LDFLAGS})
+INSTALL(TARGETS pkg_clearcache DESTINATION bin)
+
+ADD_EXECUTABLE(pkg_initdb src/pkg_initdb.c)
+TARGET_LINK_LIBRARIES(pkg_initdb ${pkgs_initdb_LDFLAGS})
+INSTALL(TARGETS pkg_initdb DESTINATION bin)
+
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/10_package-manager-add.post  DESTINATION ${SYSCONF_INSTALL_DIR}/gumd/useradd.d/)
+
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/mime.wac.xml DESTINATION /usr/share/mime/packages/)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/mime.tpk.xml DESTINATION /usr/share/mime/packages/)
+
+CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/data/pkgmgr.patch.sh.in pkgmgr.patch.sh @ONLY)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/pkgmgr.patch.sh DESTINATION ${SYSCONF_INSTALL_DIR}/opt/upgrade/)
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..a06208b
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,204 @@
+Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
diff --git a/data/10_package-manager-add.post b/data/10_package-manager-add.post
new file mode 100644 (file)
index 0000000..337a4c9
--- /dev/null
@@ -0,0 +1 @@
+pkg_initdb $2
diff --git a/data/mime.tpk.xml b/data/mime.tpk.xml
new file mode 100644 (file)
index 0000000..3aedf49
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
+    <mime-type  type="application/vnd.tizen.package">
+        <!--Created automatically by update-mime-database. DO NOT EDIT!-->
+        <comment>Tizen Package</comment>
+        <acronym>TPK</acronym>
+        <expanded-acronym>Tizen PacKage</expanded-acronym>
+        <glob pattern="*.tpk"/>
+    </mime-type>
+</mime-info>
diff --git a/data/mime.wac.xml b/data/mime.wac.xml
new file mode 100644 (file)
index 0000000..099c661
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
+    <mime-type  type="application/widget">
+        <!--Created automatically by update-mime-database. DO NOT EDIT!-->
+        <comment>WAC Widget</comment>
+        <comment xml:lang="ko">WAC 위젯</comment>
+        <acronym>WGT</acronym>
+        <expanded-acronym>wac WidGeT</expanded-acronym>
+        <glob pattern="*.wgt"/>
+        <alias type="application/vnd.wac.widget-sharing"/>
+    </mime-type>
+</mime-info>
diff --git a/data/pkgmgr.patch.sh.in b/data/pkgmgr.patch.sh.in
new file mode 100644 (file)
index 0000000..d0c2da0
--- /dev/null
@@ -0,0 +1,8 @@
+echo "--------------------------------------"
+echo "Update package database..............."
+echo "--------------------------------------"
+
+source /etc/tizen-platform.conf
+
+/bin/rm $TZ_SYS_DB/.pkgmgr_parser.db
+$TZ_SYS_BIN/pkg_initdb
diff --git a/packaging/pkgmgr-tool.manifest b/packaging/pkgmgr-tool.manifest
new file mode 100644 (file)
index 0000000..02dc527
--- /dev/null
@@ -0,0 +1,8 @@
+<manifest>
+       <request>
+               <domain name="_" />
+       </request>
+       <assign>
+               <filesystem path="/usr/bin/pkgcmd" exec_label="User" />
+       </assign>
+</manifest>
diff --git a/packaging/pkgmgr-tool.spec b/packaging/pkgmgr-tool.spec
new file mode 100644 (file)
index 0000000..43d2394
--- /dev/null
@@ -0,0 +1,73 @@
+Name:       pkgmgr-tool
+Summary:    Packager Manager Tool package
+Version:    0.1.0
+Release:    1
+Group:      Application Framework/Package Management
+License:    Apache-2.0
+Source0:    %{name}-%{version}.tar.gz
+Source1001: %{name}.manifest
+
+BuildRequires:  cmake
+BuildRequires:  unzip
+BuildRequires:  gettext-tools
+BuildRequires:  pkgconfig(dbus-glib-1)
+BuildRequires:  pkgconfig(glib-2.0)
+BuildRequires:  pkgconfig(gio-2.0)
+BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(bundle)
+BuildRequires:  pkgconfig(pkgmgr-info)
+BuildRequires:  pkgconfig(iniparser)
+BuildRequires:  pkgconfig(libtzplatform-config)
+BuildRequires:  pkgconfig(security-manager)
+BuildRequires:  pkgconfig(xdgmime)
+BuildRequires:  pkgconfig(db-util)
+BuildRequires:  pkgconfig(libsmack)
+BuildRequires:  pkgconfig(pkgmgr)
+BuildRequires:  pkgmgr-info-parser-devel
+BuildRequires:  pkgmgr-info-parser
+BuildRequires:  fdupes
+
+%description
+Packager Manager Tool for packaging
+
+%prep
+%setup -q
+cp %{SOURCE1001} .
+
+%build
+%cmake .
+
+%__make %{?_smp_mflags}
+
+%install
+%make_install
+mkdir -p %{buildroot}%{_sysconfdir}/opt/upgrade
+
+mkdir -p %{buildroot}/usr/share/license
+cp LICENSE %{buildroot}/usr/share/license/%{name}
+
+%fdupes %{buildroot}
+
+%post
+/sbin/ldconfig
+
+# Update mime database to support package mime types
+update-mime-database %{_datadir}/mime
+chsmack -a '*' %{TZ_SYS_RW_PACKAGES}
+
+%files
+%manifest %{name}.manifest
+%defattr(-,root,root,-)
+%dir %{_sysconfdir}/opt/upgrade
+%{_sysconfdir}/opt/upgrade/pkgmgr.patch.sh
+%{_bindir}/pkgcmd
+%attr(06755,root,root) %{_bindir}/pkg_initdb
+%attr(755,root,root) %{_sysconfdir}/gumd/useradd.d/10_package-manager-add.post
+%{_bindir}/pkg_getsize
+%{_bindir}/pkg_clearcache
+%{_bindir}/pkg_privilege
+%{_bindir}/pkg_install_ug
+%{_bindir}/pkginfo
+%{_datadir}/mime/packages/mime.wac.xml
+%{_datadir}/mime/packages/mime.tpk.xml
+/usr/share/license/%{name}
diff --git a/src/pkg_clearcache.c b/src/pkg_clearcache.c
new file mode 100644 (file)
index 0000000..9a20dd9
--- /dev/null
@@ -0,0 +1,212 @@
+/*
+ * slp-pkgmgr
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
+ * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <dlog.h>
+#include <tzplatform_config.h>
+#include <pkgmgr-info.h>
+#include <package-manager.h>
+
+#define MAX_PKG_NAME_LEN       256
+#define INTERNAL_CACHE_PATH_PREFIX tzplatform_getenv(TZ_USER_APP)
+#define CACHE_PATH_POSTFIX "/cache"
+#define SHARED_PATH_POSTFIX "/shared/cache"
+
+
+#undef LOG_TAG
+#ifndef LOG_TAG
+#define LOG_TAG "PKGMGR_CLEARCACHE"
+#endif                         /* LOG_TAG */
+
+static int __clear_dir(const char *dirname)
+{
+       int ret = 0;
+       DIR *dp = NULL;
+       struct dirent *ep = NULL;
+       char *abs_filename = NULL;
+       struct stat stFileInfo;
+
+       LOGD("Cache directory name to clear [%s]\n", dirname);
+
+       abs_filename = (char *)malloc(sizeof(char) * PATH_MAX);
+       if (abs_filename == NULL) {
+               LOGE("Memory allocation failed\n");
+               goto err;
+       }
+
+       dp = opendir(dirname);
+       if (dp != NULL) {
+               while ((ep = readdir(dp))) {
+                       snprintf(abs_filename, PATH_MAX - 1, "%s/%s", dirname, ep->d_name);
+                       if (lstat(abs_filename, &stFileInfo) < 0) {
+                               perror(abs_filename);
+                       }
+                       if (S_ISDIR(stFileInfo.st_mode)) {
+                               if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) {
+                                       ret = __clear_dir(abs_filename);
+                                       if (ret != 0) {
+                                               LOGE("Couldn't remove the directory. errno : %d (%s)\n", errno, strerror(errno));
+                                       }
+
+                                       ret = remove(abs_filename);
+                                       if (ret != 0) {
+                                               LOGE("Couldn't remove the directory. errno : %d (%s)\n", errno, strerror(errno));
+                                               goto err;
+                                       }
+                               }
+                       } else {
+                               ret = remove(abs_filename);
+                               if (ret != 0) {
+                                       LOGE("Couldn't remove the directory. errno : %d (%s)\n", errno, strerror(errno));
+                                       goto err;
+                               }
+                       }
+               }
+               (void)closedir(dp);
+       } else {
+               LOGE("Couldn't open the directory. errno : %d (%s)\n", errno, strerror(errno));
+               goto err;
+       }
+
+       free(abs_filename);
+       return 0;
+
+err:
+       if (abs_filename) {
+               free(abs_filename);
+       }
+       if(dp){
+               (void)closedir(dp);
+               dp = NULL;
+       }
+       return -1;
+}
+
+static int __clear_cache_dir(const char *pkgid)
+{
+       int ret = 0;
+       char dirname[PATH_MAX] = {0,};
+
+       if(pkgid == NULL) {
+               LOGE("pkgid is NULL\n");
+               return -1;
+       }
+
+       // cache internal
+       snprintf(dirname, sizeof(dirname), "%s/%s%s",
+                       INTERNAL_CACHE_PATH_PREFIX, pkgid, CACHE_PATH_POSTFIX);
+
+       ret = __clear_dir(dirname);
+       if (ret < 0) {
+               LOGE("Failed to clear internal cache dir.");
+       }
+
+       // shared/cache internal
+       snprintf(dirname, sizeof(dirname), "%s/%s%s",
+                       INTERNAL_CACHE_PATH_PREFIX, pkgid, SHARED_PATH_POSTFIX);
+
+       ret = __clear_dir(dirname);
+       if (ret < 0) {
+               LOGE("Failed to clear external shared cache dir.");
+       }
+
+       return 0;
+}
+
+static int __clear_all_cache_dir_cb(const pkgmgrinfo_pkginfo_h handle, void *user_data)
+{
+       int res = 0;
+       char *pkgid;
+       int *err_cnt = (int *)user_data;
+
+       res = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
+       if(res != PMINFO_R_OK) {
+               LOGE("pkgmgr_pkginfo_get_pkgid() failed");
+               --(*err_cnt);
+               return 0;
+       }
+
+       res = __clear_cache_dir(pkgid);
+       if (res != 0)
+       {       // error flag
+               LOGE("Failed to clear cache dir of %s", pkgid);
+               --(*err_cnt);
+               return 0;
+       }
+
+       return 0;
+}
+
+static int __clear_all_cache_dir(void)
+{
+       int err_cnt = 0;
+
+       int res = pkgmgrinfo_pkginfo_get_usr_list(__clear_all_cache_dir_cb, &err_cnt, getuid());
+       if (res != PMINFO_R_OK)
+       {
+               LOGE("Failed to get pkg list. (%d)", res);
+               return -1;
+       }
+       else if (err_cnt != 0)
+       {
+               LOGE("Error occured in %d packages.", err_cnt);
+               return -1;
+       }
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int ret = 0;
+
+       if(argv[0] == NULL) {
+               LOGE("pkgid is NULL\n");
+               return -1;
+       }
+
+       char pkgid[MAX_PKG_NAME_LEN]={0};
+
+       snprintf(pkgid,MAX_PKG_NAME_LEN,"%s",argv[0]);
+
+       if (strcmp(pkgid, PKG_CLEAR_ALL_CACHE) == 0)
+       {       // clear all
+               ret = __clear_all_cache_dir();
+       }
+       else
+       {
+               ret = __clear_cache_dir(pkgid);
+       }
+
+
+       return ret;
+}
diff --git a/src/pkg_cmd.c b/src/pkg_cmd.c
new file mode 100644 (file)
index 0000000..f143ec2
--- /dev/null
@@ -0,0 +1,985 @@
+
+/*
+ * slp-pkgmgr
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
+ * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <getopt.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <dlfcn.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <glib.h>
+#include <glib-object.h>
+#include <sqlite3.h>
+
+#include <pkgmgr-info.h>
+/* For multi-user support */
+#include <tzplatform_config.h>
+
+#include <package-manager.h>
+#include <package-manager-types.h>
+
+#define PKG_TOOL_VERSION       "0.1"
+#define APP_INSTALLATION_PATH_RW       tzplatform_getenv(TZ_USER_APP)
+#define MAX_QUERY_LEN  4096
+
+#define OWNER_ROOT 0
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
+
+static int __process_request(uid_t uid);
+static void __print_usage();
+static int __is_app_installed(char *pkgid, uid_t uid);
+static int __return_cb(uid_t target_uid, int req_id, const char *pkg_type,
+                      const char *pkgid, const char *key, const char *val,
+                      const void *pmsg, void *data);
+static int __convert_to_absolute_path(char *path);
+
+/* Supported options */
+const char *short_options = "iurmcgCkaADL:lsd:p:t:n:T:S:Gqh";
+const struct option long_options[] = {
+       {"install", 0, NULL, 'i'},
+       {"uninstall", 0, NULL, 'u'},
+       {"reinstall", 0, NULL, 'r'},
+       {"move", 0, NULL, 'm'},
+       {"clear", 0, NULL, 'c'},
+       {"getsize", 0, NULL, 'g'},
+       {"activate", 0, NULL, 'A'},
+       {"deactivate", 0, NULL, 'D'},
+       {"activate with Label", 1, NULL, 'L'},
+       {"check", 0, NULL, 'C'},
+       {"kill", 0, NULL, 'k'},
+       {"app-path", 0, NULL, 'a'},
+       {"list", 0, NULL, 'l'},
+       {"show", 0, NULL, 's'},
+       {"descriptor", 1, NULL, 'd'},
+       {"package-path", 1, NULL, 'p'},
+       {"package-type", 1, NULL, 't'},
+       {"package-name", 1, NULL, 'n'},
+       {"move-type", 1, NULL, 'T'},
+       {"getsize-type", 1, NULL, 'T'},
+       {"csc", 1, NULL, 'S'},
+       {"global", 0, NULL, 'G'},
+       {"quiet", 0, NULL, 'q'},
+       {"help", 0, NULL, 'h'},
+       {0, 0, 0, 0}            /* sentinel */
+};
+
+enum pm_tool_request_e {
+       INSTALL_REQ = 1,
+       UNINSTALL_REQ,
+       REINSTALL_REQ,
+       CSC_REQ,
+       GETSIZE_REQ,
+       CLEAR_REQ,
+       MOVE_REQ,
+       ACTIVATE_REQ,
+       DEACTIVATE_REQ,
+       APPPATH_REQ,
+       CHECKAPP_REQ,
+       KILLAPP_REQ,
+       LIST_REQ,
+       SHOW_REQ,
+       HELP_REQ
+};
+typedef enum pm_tool_request_e req_type;
+
+struct pm_tool_args_t {
+       req_type request;
+       char pkg_path[PKG_NAME_STRING_LEN_MAX];
+       char pkg_type[PKG_TYPE_STRING_LEN_MAX];
+       char pkgid[PKG_NAME_STRING_LEN_MAX];
+       char des_path[PKG_NAME_STRING_LEN_MAX];
+       char label[PKG_NAME_STRING_LEN_MAX];
+       int global;
+       int type;
+       int result;
+};
+typedef struct pm_tool_args_t pm_tool_args;
+pm_tool_args data;
+
+static GMainLoop *main_loop = NULL;
+
+static void __error_no_to_string(int errnumber, char **errstr)
+{
+       if (errstr == NULL)
+               return;
+       switch (errnumber) {
+       case PKGCMD_ERR_PACKAGE_NOT_FOUND:
+               *errstr = PKGCMD_ERR_PACKAGE_NOT_FOUND_STR;
+               break;
+       case PKGCMD_ERR_PACKAGE_INVALID:
+               *errstr = PKGCMD_ERR_PACKAGE_INVALID_STR;
+               break;
+       case PKGCMD_ERR_PACKAGE_LOWER_VERSION:
+               *errstr = PKGCMD_ERR_PACKAGE_LOWER_VERSION_STR;
+               break;
+       case PKGCMD_ERR_PACKAGE_EXECUTABLE_NOT_FOUND:
+               *errstr = PKGCMD_ERR_PACKAGE_EXECUTABLE_NOT_FOUND_STR;
+               break;
+       case PKGCMD_ERR_MANIFEST_INVALID:
+               *errstr = PKGCMD_ERR_MANIFEST_INVALID_STR;
+               break;
+       case PKGCMD_ERR_CONFIG_NOT_FOUND:
+               *errstr = PKGCMD_ERR_CONFIG_NOT_FOUND_STR;
+               break;
+       case PKGCMD_ERR_CONFIG_INVALID:
+               *errstr = PKGCMD_ERR_CONFIG_INVALID_STR;
+               break;
+       case PKGCMD_ERR_SIGNATURE_NOT_FOUND:
+               *errstr = PKGCMD_ERR_SIGNATURE_NOT_FOUND_STR;
+               break;
+       case PKGCMD_ERR_SIGNATURE_INVALID:
+               *errstr = PKGCMD_ERR_SIGNATURE_INVALID_STR;
+               break;
+       case PKGCMD_ERR_SIGNATURE_VERIFICATION_FAILED:
+               *errstr = PKGCMD_ERR_SIGNATURE_VERIFICATION_FAILED_STR;
+               break;
+       case PKGCMD_ERR_ROOT_CERTIFICATE_NOT_FOUND:
+               *errstr = PKGCMD_ERR_ROOT_CERTIFICATE_NOT_FOUND_STR;
+               break;
+       case PKGCMD_ERR_CERTIFICATE_INVALID:
+               *errstr = PKGCMD_ERR_CERTIFICATE_INVALID_STR;
+               break;
+       case PKGCMD_ERR_CERTIFICATE_CHAIN_VERIFICATION_FAILED:
+               *errstr = PKGCMD_ERR_CERTIFICATE_CHAIN_VERIFICATION_FAILED_STR;
+               break;
+       case PKGCMD_ERR_CERTIFICATE_EXPIRED:
+               *errstr = PKGCMD_ERR_CERTIFICATE_EXPIRED_STR;
+               break;
+       case PKGCMD_ERR_INVALID_PRIVILEGE:
+               *errstr = PKGCMD_ERR_INVALID_PRIVILEGE_STR;
+               break;
+       case PKGCMD_ERR_MENU_ICON_NOT_FOUND:
+               *errstr = PKGCMD_ERR_MENU_ICON_NOT_FOUND_STR;
+               break;
+       case PKGCMD_ERR_FATAL_ERROR:
+               *errstr = PKGCMD_ERR_FATAL_ERROR_STR;
+               break;
+       case PKGCMD_ERR_OUT_OF_STORAGE:
+               *errstr = PKGCMD_ERR_OUT_OF_STORAGE_STR;
+               break;
+       case PKGCMD_ERR_OUT_OF_MEMORY:
+               *errstr = PKGCMD_ERR_OUT_OF_MEMORY_STR;
+               break;
+       case PKGCMD_ERR_ARGUMENT_INVALID:
+               *errstr = PKGCMD_ERR_ARGUMENT_INVALID_STR;
+               break;
+       default:
+               *errstr = PKGCMD_ERR_UNKNOWN_STR;
+               break;
+       }
+}
+
+static int __return_cb(uid_t target_uid, int req_id, const char *pkg_type,
+                      const char *pkgid, const char *key, const char *val,
+                      const void *pmsg, void *priv_data)
+{
+       int ret_val;
+       char delims[] = ":";
+       char *extra_str = NULL;
+       char *ret_result = NULL;
+
+       if (strncmp(key, "error", strlen("error")) == 0) {
+               ret_val = atoi(val);
+               data.result = ret_val;
+
+               ret_result = strstr((char *)val, delims);
+               if (ret_result){
+                       extra_str = strdup(ret_result);
+                       printf("__return_cb req_id[%d] pkg_type[%s] pkgid[%s] key[%s] val[%d] error message: %s\n",
+                                          req_id, pkg_type, pkgid, key, ret_val, extra_str);
+                       free(extra_str);
+               }
+               else
+                       printf("__return_cb req_id[%d] pkg_type[%s] pkgid[%s] key[%s] val[%d]\n",
+                                          req_id, pkg_type, pkgid, key, ret_val);
+       } else
+               printf("__return_cb req_id[%d] pkg_type[%s] "
+                          "pkgid[%s] key[%s] val[%s]\n",
+                          req_id, pkg_type, pkgid, key, val);
+
+       if (strncmp(key, "end", strlen("end")) == 0) {
+               if ((strncmp(val, "fail", strlen("fail")) == 0) && data.result == 0){
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+               }
+               g_main_loop_quit(main_loop);
+       }
+
+       return 0;
+}
+
+static int __convert_to_absolute_path(char *path)
+{
+       char abs[PKG_NAME_STRING_LEN_MAX] = {'\0'};
+       char temp[PKG_NAME_STRING_LEN_MAX] = {'\0'};
+       char *ptr = NULL;
+       if (path == NULL) {
+               printf("path is NULL\n");
+               return -1;
+       }
+       strncpy(temp, path, PKG_NAME_STRING_LEN_MAX - 1);
+       if (strchr(path, '/') == NULL) {
+               if (getcwd(abs, PKG_NAME_STRING_LEN_MAX - 1) == NULL) {
+                       printf("getcwd() failed\n");
+                       return -1;
+               }
+               memset(data.pkg_path, '\0', PKG_NAME_STRING_LEN_MAX);
+               snprintf(data.pkg_path, PKG_NAME_STRING_LEN_MAX - 1, "%s/%s", abs, temp);
+               return 0;
+       }
+       if (strncmp(path, "./", 2) == 0) {
+               ptr = temp;
+               if (getcwd(abs, PKG_NAME_STRING_LEN_MAX - 1) == NULL) {
+                       printf("getcwd() failed\n");
+                       return -1;
+               }
+               ptr = ptr + 2;
+               memset(data.pkg_path, '\0', PKG_NAME_STRING_LEN_MAX);
+               snprintf(data.pkg_path, PKG_NAME_STRING_LEN_MAX - 1, "%s/%s", abs, ptr);
+               return 0;
+       }
+       return 0;
+}
+
+static int __is_app_installed(char *pkgid, uid_t uid)
+{
+       pkgmgrinfo_pkginfo_h handle;
+       int ret;
+       if (uid != GLOBAL_USER)
+               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
+       else
+               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+
+       if(ret < 0) {
+               printf("package is not in pkgmgr_info DB\n");
+               return -1;
+       } else
+               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+
+       return 0;
+}
+
+static void __print_usage()
+{
+       printf("\nPackage Manager Tool Version: %s\n\n", PKG_TOOL_VERSION);
+       printf("-i, --install           install the package\n");
+       printf("-u, --uninstall         uninstall the package\n");
+       printf("-r, --reinstall         reinstall the package\n");
+       printf("-c, --clear             clear user data\n");
+       printf("-m, --move              move package\n");
+       printf("-g, --getsize           get size of given package\n");
+       printf("-T, --getsize-type      get type [0 : total size /1: data size]\n");
+       printf("-l, --list              display list of installed packages available for the current user [i.e. User's specific Apps and Global Apps] \n");
+       printf("-s, --show              show detail package info\n");
+       printf("-a, --app-path          show app installation path\n");
+       printf("-C, --check             check if applications belonging to a package are running or not\n");
+       printf("-k, --kill              terminate applications belonging to a package\n");
+       printf("-d, --descriptor        provide descriptor path\n");
+       printf("-p, --package-path      provide package path\n");
+       printf("-n, --package-name      provide package name\n");
+       printf("-t, --package-type      provide package type\n");
+       printf("-T, --move-type provide move type [0 : move to internal /1: move to external]\n");
+       printf("-G, --global    Global Mode [Warning user should be privilegied to use this mode] \n");
+       printf("-h, --help      .       print this help\n\n");
+
+       printf("Usage: pkgcmd [options]\n");
+       printf("pkgcmd -i -t <pkg type> (-d <descriptor path>) -p <pkg path> (-G)\n");
+       printf("pkgcmd -u -n <pkgid> (-G)\n");
+       printf("pkgcmd -r -t <pkg type> -n <pkgid> (-G) \n");
+       printf("pkgcmd -l (-t <pkg type>) \n");
+       printf("pkgcmd -s -t <pkg type> -p <pkg path>\n");
+       printf("pkgcmd -s -t <pkg type> -n <pkg name>\n");
+       printf("pkgcmd -m -t <pkg type> -T <move type> -n <pkg name>\n\n");
+       printf("pkgcmd -g -T <getsize type> -n <pkgid> \n");
+       printf("pkgcmd -C -n <pkgid> \n");
+       printf("pkgcmd -k -n <pkgid> \n");
+
+       printf("Example:\n");
+       printf("pkgcmd -u -n com.samsung.calculator\n");
+       printf("pkgcmd -i -t rpm -p /mnt/nfs/com.samsung.calculator_0.1.2-95_armel.rpm\n");
+       printf("pkgcmd -r -t rpm -n com.samsung.calculator\n");
+       printf("pkgcmd -c -t rpm -n com.samsung.hello\n");
+       printf("pkgcmd -m -t rpm -T 1 -n com.samsung.hello\n");
+       printf("pkgcmd -C -n com.samsung.hello\n");
+       printf("pkgcmd -k -n com.samsung.hello\n");
+       printf("pkgcmd -a\n");
+       printf("pkgcmd -a -t rpm -n com.samsung.hello\n");
+       printf("pkgcmd -l\n");
+       printf("pkgcmd -l -t tpk\n");
+       printf("pkgcmd -g -T 0 -n com.samsung.calculator\n");
+
+       exit(0);
+
+}
+
+static int __pkgmgr_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
+{
+       int ret = -1;
+       char *pkgid = NULL;
+       char *pkg_type = NULL;
+       char *pkg_version = NULL;
+       char *pkg_label = NULL;
+       bool for_all_users = 0;
+
+       ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
+       if (ret == -1) {
+               printf("Failed to get pkgmgrinfo_pkginfo_get_pkgid\n");
+               return ret;
+       }
+       ret = pkgmgrinfo_pkginfo_get_type(handle, &pkg_type);
+       if (ret == -1) {
+               printf("Failed to get pkgmgrinfo_pkginfo_get_type\n");
+               return ret;
+       }
+       ret = pkgmgrinfo_pkginfo_get_version(handle, &pkg_version);
+       if (ret == -1) {
+               printf("Failed to get pkgmgrinfo_pkginfo_get_version\n");
+               return ret;
+       }
+       ret = pkgmgrinfo_pkginfo_get_label(handle, &pkg_label);
+       if (ret == -1)
+               pkg_label = "(null)";
+
+       ret = pkgmgrinfo_pkginfo_is_for_all_users(handle, &for_all_users);
+       if (ret == -1) {
+               printf("Failed to get pkgmgrinfo_pkginfo_is_for_all_users\n");
+               return ret;
+       }
+
+       printf("%s\tpkg_type [%s]\tpkgid [%s]\tname [%s]\tversion [%s]\n", for_all_users ? "system apps" : "user apps ", pkg_type, pkgid, pkg_label, pkg_version);
+       return ret;
+}
+
+static int __pkg_list_cb(const pkgmgrinfo_pkginfo_h handle, void *user_data)
+{
+       int ret = -1;
+       char *pkgid;
+       pkgmgrinfo_uidinfo_t *uid_info = (pkgmgrinfo_uidinfo_t *) handle;
+
+       ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
+       if(ret < 0) {
+               printf("pkgmgrinfo_pkginfo_get_pkgid() failed\n");
+       }
+  if (uid_info->uid != GLOBAL_USER)
+         ret = pkgmgr_client_usr_request_service(PM_REQUEST_GET_SIZE, PM_GET_TOTAL_SIZE, (pkgmgr_client *)user_data, NULL, pkgid, uid_info->uid, NULL, NULL, NULL);
+  else
+         ret = pkgmgr_client_request_service(PM_REQUEST_GET_SIZE, PM_GET_TOTAL_SIZE, (pkgmgr_client *)user_data, NULL, pkgid, NULL, NULL, NULL);
+       if (ret < 0){
+               printf("pkgmgr_client_request_service Failed\n");
+               return -1;
+       }
+
+       printf("pkg[%s] size = %d\n", pkgid, ret);
+
+       return 0;
+}
+
+static int __process_request(uid_t uid)
+{
+       int ret = -1;
+       pkgmgr_client *pc = NULL;
+       char buf[1024] = {'\0'};
+       int pid = -1;
+#if !GLIB_CHECK_VERSION(2,35,0)
+       g_type_init();
+#endif
+       switch (data.request) {
+       case INSTALL_REQ:
+               if (data.pkg_type[0] == '\0' || data.pkg_path[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       data.result = PKGCMD_ERR_ARGUMENT_INVALID;
+                       break;
+               }
+               main_loop = g_main_loop_new(NULL, FALSE);
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       break;
+               }
+               if (data.des_path[0] == '\0')
+               {
+                       ret =
+                               pkgmgr_client_usr_install(pc, data.pkg_type, NULL,
+                                               data.pkg_path, NULL, PM_QUIET,
+                                               __return_cb, pc, uid);
+               }else{
+                       ret =
+                               pkgmgr_client_usr_install(pc, data.pkg_type,
+                                               data.des_path, data.pkg_path,
+                                               NULL, PM_QUIET, __return_cb, pc, uid);
+
+               }
+               if (ret < 0){
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       if (access(data.pkg_path, F_OK) != 0)
+                               data.result = PKGCMD_ERR_PACKAGE_NOT_FOUND;
+                       break;
+               }
+               g_main_loop_run(main_loop);
+               ret = data.result;
+               break;
+
+       case UNINSTALL_REQ:
+               if (data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       data.result = PKGCMD_ERR_ARGUMENT_INVALID;
+                       break;
+               }
+               main_loop = g_main_loop_new(NULL, FALSE);
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       break;
+               }
+//if global
+               ret = __is_app_installed(data.pkgid, uid);
+               if (ret == -1) {
+                       printf("package is not installed\n");
+                       break;
+               }
+
+               ret =
+                   pkgmgr_client_usr_uninstall(pc, data.pkg_type, data.pkgid,
+                                           PM_QUIET, __return_cb, NULL,uid);
+               if (ret < 0){
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       if (access(data.pkg_path, F_OK) != 0)
+                               data.result = PKGCMD_ERR_PACKAGE_NOT_FOUND;
+                       break;
+               }
+               g_main_loop_run(main_loop);
+               ret = data.result;
+               break;
+
+       case REINSTALL_REQ:
+               if (data.pkg_type[0] == '\0' || data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       data.result = PKGCMD_ERR_ARGUMENT_INVALID;
+                       break;
+               }
+               main_loop = g_main_loop_new(NULL, FALSE);
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       break;
+               }
+
+               ret = pkgmgr_client_usr_reinstall(pc, data.pkg_type, data.pkgid, NULL, PM_QUIET, __return_cb, pc, uid);
+               if (ret < 0){
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       if (access(data.pkg_path, F_OK) != 0)
+                               data.result = PKGCMD_ERR_PACKAGE_NOT_FOUND;
+                       break;
+               }
+               g_main_loop_run(main_loop);
+               ret = data.result;
+               break;
+
+       case CLEAR_REQ:
+               if (data.pkg_type[0] == '\0' || data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       ret = -1;
+                       break;
+               }
+
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       ret = -1;
+                       break;
+               }
+               ret = __is_app_installed(data.pkgid, uid);
+               if (ret == -1) {
+                       printf("package is not installed\n");
+                       break;
+               }
+               ret = pkgmgr_client_usr_clear_user_data(pc, data.pkg_type,
+                                                   data.pkgid, PM_QUIET, uid);
+               if (ret < 0)
+                       break;
+               ret = data.result;
+               break;
+
+       case ACTIVATE_REQ:
+               if (data.pkg_type[0] == '\0' || data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       ret = -1;
+                       break;
+               }
+
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       ret = -1;
+                       break;
+               }
+
+               if ( strcmp(data.pkg_type, "app") == 0 ) {
+                       if (strlen(data.label) == 0) {
+                               ret = pkgmgr_client_usr_activate_app(pc, data.pkgid, uid);
+                               if (ret < 0)
+                                       break;
+                       } else {
+                               printf("label [%s]\n", data.label);
+                               char *largv[3] = {NULL, };
+                               largv[0] = "-l";
+                               largv[1] = data.label;
+                               ret = pkgmgr_client_usr_activate_appv(pc, data.pkgid, largv, uid);
+                               if (ret < 0)
+                                       break;
+                       }
+               } else {
+                               ret = pkgmgr_client_usr_activate(pc, data.pkg_type, data.pkgid, uid);
+                               if (ret < 0)
+                                       break;
+               }
+               ret = data.result;
+
+               break;
+
+
+       case DEACTIVATE_REQ:
+               if (data.pkg_type[0] == '\0' || data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       ret = -1;
+                       break;
+               }
+
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       ret = -1;
+                       break;
+               }
+
+               if ( strcmp(data.pkg_type, "app") == 0 ) {
+                       ret = pkgmgr_client_usr_deactivate_app(pc, data.pkgid, uid);
+                       if (ret < 0)
+                               break;
+               }else {
+                       ret = pkgmgr_client_usr_deactivate(pc, data.pkg_type, data.pkgid, uid);
+                       if (ret < 0)
+                               break;
+               }
+               ret = data.result;
+
+               break;
+
+       case MOVE_REQ:
+               if (data.pkg_type[0] == '\0' || data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       ret = -1;
+                       break;
+               }
+               if (data.type < 0 || data.type > 1) {
+                       printf("Invalid move type...See usage\n");
+                       ret = -1;
+                       break;
+               }
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       ret = -1;
+                       break;
+               }
+               ret = __is_app_installed(data.pkgid, uid);
+               if (ret == -1) {
+                       printf("package is not installed\n");
+                       break;
+               }
+               ret = pkgmgr_client_usr_request_service(PM_REQUEST_MOVE, data.type, pc, NULL, data.pkgid, uid, NULL, NULL, NULL);
+
+               printf("pkg[%s] move result = %d\n", data.pkgid, ret);
+
+               if (ret < 0)
+                       break;
+               ret = data.result;
+               break;
+
+       case APPPATH_REQ:
+               if (data.pkg_type[0] == '\0' && data.pkgid[0] == '\0') {
+                       printf("Tizen Application Installation Path: %s\n", APP_INSTALLATION_PATH_RW);
+                       ret = 0;
+                       break;
+               }
+               if ((data.pkg_type[0] == '\0') || (data.pkgid[0] == '\0')) {
+                       printf("Use -h option to see usage\n");
+                       ret = -1;
+                       break;
+               }
+               if (strncmp(data.pkg_type, "rpm", PKG_TYPE_STRING_LEN_MAX - 1) == 0) {
+                       snprintf(buf, 1023, "%s/%s", APP_INSTALLATION_PATH_RW, data.pkgid);
+                       printf("Tizen Application Installation Path: %s\n", buf);
+                       ret = 0;
+                       break;
+               } else if (strncmp(data.pkg_type, "wgt", PKG_TYPE_STRING_LEN_MAX - 1) == 0) {
+                       snprintf(buf, 1023, "%s/%s/res/wgt", APP_INSTALLATION_PATH_RW, data.pkgid);
+                       printf("Tizen Application Installation Path: %s\n", buf);
+                       ret = 0;
+                       break;
+               } else if (strncmp(data.pkg_type, "tpk", PKG_TYPE_STRING_LEN_MAX - 1) == 0) {
+                       snprintf(buf, 1023, "%s/%s", APP_INSTALLATION_PATH_RW, data.pkgid);
+                       printf("Tizen Application Installation Path: %s\n", buf);
+                       ret = 0;
+                       break;
+               } else {
+                       printf("Invalid package type.\n");
+                       printf("use -h option to see usage\n");
+                       ret = -1;
+                       break;
+               }
+               break;
+
+       case KILLAPP_REQ:
+       case CHECKAPP_REQ:
+               if (data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       data.result = PKGCMD_ERR_ARGUMENT_INVALID;
+                       break;
+               }
+
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       break;
+               }
+
+               if (data.request == KILLAPP_REQ) {
+                       ret = pkgmgr_client_usr_request_service(PM_REQUEST_KILL_APP, 0, pc, NULL, data.pkgid, uid, NULL, NULL, &pid);
+                       if (ret < 0){
+                               data.result = PKGCMD_ERR_FATAL_ERROR;
+                               break;
+                       }
+                       if (pid)
+                               printf("Pkgid: %s is Terminated\n", data.pkgid);
+                       else
+                               printf("Pkgid: %s is already Terminated\n", data.pkgid);
+
+               } else if (data.request == CHECKAPP_REQ) {
+                       ret = pkgmgr_client_usr_request_service(PM_REQUEST_CHECK_APP, 0, pc, NULL, data.pkgid, uid, NULL, NULL, &pid);
+                       if (ret < 0){
+                               data.result = PKGCMD_ERR_FATAL_ERROR;
+                               break;
+                       }
+
+                       if (pid)
+                               printf("Pkgid: %s is Running\n", data.pkgid);
+                       else
+                               printf("Pkgid: %s is Not Running\n", data.pkgid);
+               }
+               ret = data.result;
+               break;
+
+       case LIST_REQ:
+               if (data.pkg_type[0] == '\0') {
+                       ret = 0;
+                       if (uid != GLOBAL_USER) {
+                               ret = pkgmgrinfo_pkginfo_get_usr_list(__pkgmgr_list_cb, NULL, uid);
+                       } else {
+                               ret = pkgmgrinfo_pkginfo_get_list(__pkgmgr_list_cb, NULL);
+                               }
+                               if (ret == -1)
+                                       printf("no packages found\n");
+                               break;
+               } else {
+                       pkgmgrinfo_pkginfo_filter_h handle;
+                       ret = pkgmgrinfo_pkginfo_filter_create(&handle);
+                       if (ret == -1) {
+                               printf("Failed to get package filter handle\n");
+                               break;
+                       }
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_TYPE, data.pkg_type);
+                       if (ret == -1) {
+                               printf("Failed to add package type filter\n");
+                               pkgmgrinfo_pkginfo_filter_destroy(handle);
+                               break;
+                       }
+                       if (uid != GLOBAL_USER) {
+                               ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(handle, __pkgmgr_list_cb, NULL, uid);
+                       } else {
+                               ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __pkgmgr_list_cb, NULL);
+                       }
+                               if (ret != PMINFO_R_OK)
+                                       printf("no package filter list\n");
+                                       pkgmgrinfo_pkginfo_filter_destroy(handle);
+                       break;
+               }
+
+       case SHOW_REQ:
+               /* unsupported */
+               ret = -1;
+               break;
+
+       case CSC_REQ:
+               ret = pkgmgr_client_usr_request_service(PM_REQUEST_CSC, 0, NULL, NULL, NULL, uid, data.des_path, NULL, (void *)data.pkg_path);
+               if (ret < 0)
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+               break;
+
+       case GETSIZE_REQ:
+               if (data.pkgid[0] == '\0') {
+                       printf("Please provide the arguments.\n");
+                       printf("use -h option to see usage\n");
+                       ret = -1;
+                       break;
+               }
+
+               pc = pkgmgr_client_new(PC_REQUEST);
+               if (pc == NULL) {
+                       printf("PkgMgr Client Creation Failed\n");
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       break;
+               }
+
+               if (data.type == 9) {
+                       ret = pkgmgrinfo_pkginfo_get_usr_list(__pkg_list_cb, (void *)pc, uid);
+                       break;
+               }
+               ret = pkgmgr_client_usr_request_service(PM_REQUEST_GET_SIZE, data.type, pc, NULL, data.pkgid, uid, NULL, NULL, NULL);
+               if (ret < 0){
+                       data.result = PKGCMD_ERR_FATAL_ERROR;
+                       break;
+               }
+
+               printf("pkg[%s] size = %d\n", data.pkgid, ret);
+               ret = data.result;
+               break;
+
+       case HELP_REQ:
+               __print_usage();
+               ret = 0;
+               break;
+
+       default:
+               printf("Wrong Request\n");
+               ret = -1;
+               break;
+       }
+
+       if (pc) {
+               pkgmgr_client_free(pc);
+               pc = NULL;
+       }
+       return ret;
+}
+
+int main(int argc, char *argv[])
+{
+       optind = 1;
+       int opt_idx = 0;
+       int c = -1;
+       int ret = -1;
+       char *errstr = NULL;
+       long starttime;
+       long endtime;
+       struct timeval tv;
+
+
+       if (argc == 1)
+               __print_usage();
+
+       gettimeofday(&tv, NULL);
+       starttime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
+
+       data.request = -1;
+       memset(data.des_path, '\0', PKG_NAME_STRING_LEN_MAX);
+       memset(data.pkg_path, '\0', PKG_NAME_STRING_LEN_MAX);
+       memset(data.pkgid, '\0', PKG_NAME_STRING_LEN_MAX);
+       memset(data.pkg_type, '\0', PKG_TYPE_STRING_LEN_MAX);
+       memset(data.label, '\0', PKG_TYPE_STRING_LEN_MAX);
+       data.global = 0; //By default pkg_cmd will manage for the current user 
+       data.result = 0;
+       data.type = -1;
+       while (1) {
+               c = getopt_long(argc, argv, short_options, long_options,
+                               &opt_idx);
+               if (c == -1)
+                       break;  /* Parse end */
+               switch (c) {
+               case 'G':       /* install */
+                       data.global = 1;
+                       break;
+
+               case 'i':       /* install */
+                       data.request = INSTALL_REQ;
+                       break;
+
+               case 'u':       /* uninstall */
+                       data.request = UNINSTALL_REQ;
+                       break;
+
+               case 'r':       /* reinstall */
+                       data.request = REINSTALL_REQ;
+                       break;
+
+               case 'c':       /* clear */
+                       data.request = CLEAR_REQ;
+                       break;
+
+               case 'g':       /* get pkg size */
+                       data.request = GETSIZE_REQ;
+                       break;
+
+               case 'm':       /* move */
+                       data.request = MOVE_REQ;
+                       break;
+
+               case 'S': /* csc packages */
+                       data.request = CSC_REQ;
+                       if (optarg)
+                               snprintf(data.des_path, sizeof(data.des_path),
+                                               "%s", optarg);
+                       printf("csc file is %s\n", data.des_path);
+                       break;
+
+               case 'A':       /* activate */
+                       data.request = ACTIVATE_REQ;
+                       break;
+
+               case 'D':       /* deactivate */
+                       data.request = DEACTIVATE_REQ;
+                       break;
+
+               case 'L':       /* activate with Label */
+                       data.request = ACTIVATE_REQ;
+                       if (optarg)
+                               snprintf(data.pkg_path, sizeof(data.pkg_path),
+                                               "%s", optarg);
+                       break;
+
+               case 'a':       /* app installation path */
+                       data.request = APPPATH_REQ;
+                       break;
+
+               case 'k':       /* Terminate applications of a package */
+                       data.request = KILLAPP_REQ;
+                       break;
+
+               case 'C':       /* Check running status of applications of a package */
+                       data.request = CHECKAPP_REQ;
+                       break;
+
+               case 'l':       /* list */
+                       data.request = LIST_REQ;
+                       break;
+
+               case 's':       /* show */
+                       data.request = SHOW_REQ;
+                       break;
+
+               case 'p':       /* package path */
+                       if (optarg)
+                               snprintf(data.pkg_path, sizeof(data.pkg_path),
+                                               "%s", optarg);
+                       ret = __convert_to_absolute_path(data.pkg_path);
+                       if (ret == -1) {
+                               printf("conversion of relative path to absolute path failed\n");
+                               return -1;
+                       }
+                       printf("path is %s\n", data.pkg_path);
+                       break;
+
+               case 'd':       /* descriptor path */
+                       if (optarg)
+                               snprintf(data.des_path, sizeof(data.des_path),
+                                               "%s", optarg);
+                       break;
+
+               case 'n':       /* package name */
+                       if (optarg)
+                               snprintf(data.pkgid, sizeof(data.pkgid),
+                                               "%s", optarg);
+                       break;
+
+               case 't':       /* package type */
+                       if (optarg)
+                               snprintf(data.pkg_type, sizeof(data.pkg_type),
+                                               "%s", optarg);
+                       break;
+
+               case 'T':       /* move type */
+                       data.type = atoi(optarg);
+                       break;
+
+               case 'h':       /* help */
+                       data.request = HELP_REQ;
+                       break;
+
+               case 'q':       /* quiet mode is removed */
+                       break;
+
+                       /* Otherwise */
+               case '?':       /* Not an option */
+                       __print_usage();
+                       break;
+
+               case ':':       /* */
+                       break;
+
+               default:
+                       break;
+
+               }
+       }
+       uid_t uid = getuid();
+       if(uid == OWNER_ROOT) {
+               printf("Current User is Root! : Only regular users are allowed\n");
+               return -1;
+       }
+       if(data.global == 1) {
+               uid = GLOBAL_USER;
+       }
+       ret = __process_request(uid);
+       if ((ret == -1) && (data.result != 0))
+               data.result = PKGCMD_ERR_ARGUMENT_INVALID;
+
+       if (ret != 0) {
+               __error_no_to_string(data.result, &errstr);
+               printf("processing result : %s [%d] failed\n", errstr, data.result);
+       }
+
+       gettimeofday(&tv, NULL);
+       endtime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
+       printf("spend time for pkgcmd is [%d]ms\n", (int)(endtime - starttime));
+
+       return data.result;
+}
diff --git a/src/pkg_getsize.c b/src/pkg_getsize.c
new file mode 100644 (file)
index 0000000..3f096af
--- /dev/null
@@ -0,0 +1,649 @@
+/*
+ * slp-pkgmgr
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
+ * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <pkgmgr-info.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <fcntl.h>
+
+/* For multi-user support */
+#include <tzplatform_config.h>
+
+#include <package-manager.h>
+#include <package-manager-debug.h>
+#include <pkgmgr_installer.h>
+#include <pkgmgr/comm_config.h>
+
+#undef LOG_TAG
+#ifndef LOG_TAG
+#define LOG_TAG "PKGMGR_GETSIZE"
+#endif                         /* LOG_TAG */
+
+#define MAX_PKG_BUF_LEN                        1024
+#define BLOCK_SIZE                     4096 /*in bytes*/
+#define MAX_PATH_LENGTH                512
+#define MAX_LONGLONG_LENGTH    32
+#define MAX_SIZE_INFO_SIZE             128
+
+#define OWNER_ROOT 0
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
+
+#if 0 /* installed at external storage is not supported yet */
+#define APP_BASE_EXTERNAL_PATH ""
+#endif
+
+typedef enum
+{
+       STORAGE_TYPE_INTERNAL,
+       STORAGE_TYPE_EXTERNAL,
+       STORAGE_TYPE_MAX = 255,
+} STORAGE_TYPE;
+
+long long __stat_size(struct stat *s)
+{
+       long long blksize = s->st_blksize;
+       long long size = (long long)s->st_blocks * 512;
+
+       if (blksize) {
+               size = (size + blksize - 1) & (~(blksize - 1));
+       }
+
+       return size;
+}
+
+static long long __calculate_directory_size(int dfd, bool include_itself)
+{
+       long long size = 0;
+       struct stat st;
+       int subfd;
+       int ret;
+       DIR *dir;
+       struct dirent *dent;
+       const char *entry;
+
+       if (include_itself) {
+               ret = fstat(dfd, &st);
+               if (ret < 0) {
+                       ERR("fstat() failed, entry: ., errno: %d (%s)", errno,
+                                       strerror(errno));
+                       return -1;
+               }
+               size += __stat_size(&st);
+       }
+
+       dir = fdopendir(dfd);
+       if (dir == NULL) {
+               ERR("fdopendir() failed, errno: %d (%s)", errno,
+                               strerror(errno));
+               return -1;
+       }
+
+       while ((dent = readdir(dir))) {
+               entry = dent->d_name;
+               if (entry[0] == '.') {
+                       if (entry[1] == '\0')
+                               continue;
+                       if ((entry[1] == '.') && (entry[2] == '\0'))
+                               continue;
+               }
+
+               if (dent->d_type == DT_DIR) {
+                       subfd = openat(dfd, entry, O_RDONLY | O_DIRECTORY);
+                       if (subfd < 0) {
+                               ERR("openat() failed, entry:%s, errno: %d(%s)",
+                                               entry, errno, strerror(errno));
+                               goto error;
+                       }
+
+                       DBG("traverse entry: %s", entry);
+                       size += __calculate_directory_size(subfd, true);
+                       close(subfd);
+               } else {
+                       ret = fstatat(dfd, entry, &st, AT_SYMLINK_NOFOLLOW);
+                       if (ret < 0) {
+                               ERR("fstatat() failed, entry:%s, errno: %d(%s)",
+                                               entry, errno, strerror(errno));
+                               goto error;
+                       }
+                       size += __stat_size(&st);
+               }
+       }
+
+       closedir(dir);
+       return size;
+
+error:
+       closedir(dir);
+       return -1;
+}
+
+static long long __calculate_shared_dir_size(int dfd, const char *app_root_dir,
+               long long *data_size, long long *app_size)
+{
+       int fd = -1;
+       int subfd = -1;
+       long long size = 0;
+       struct stat st;
+       int ret;
+
+       DBG("traverse path: %s/shared", app_root_dir);
+
+       fd = openat(dfd, "shared", O_RDONLY | O_DIRECTORY);
+       if (fd < 0) {
+               ERR("openat() failed, path: %s/shared, errno: %d (%s)",
+                               app_root_dir, errno, strerror(errno));
+               return -1;
+       }
+
+       ret = fstat(fd, &st);
+       if (ret < 0) {
+               ERR("fstat() failed, path: %s/shared, errno: %d (%s)",
+                               app_root_dir, errno, strerror(errno));
+               goto error;
+       }
+       *app_size += __stat_size(&st); // shared directory
+       DBG("app_size: %lld", *app_size);
+
+       DBG("traverse path: %s/shared/data", app_root_dir);
+
+       subfd = openat(fd, "data", O_RDONLY | O_DIRECTORY);
+       if (subfd >= 0) {
+               size = __calculate_directory_size(subfd, true);
+               if (size < 0)
+               {
+                       ERR("Calculating shared/data directory failed.");
+                       goto error;
+               }
+               *data_size += size;
+               DBG("data_size: %lld", *data_size);
+               close(subfd);
+       } else if (subfd < 0 && errno != ENOENT) {
+               ERR("openat() failed, entry: data, errno: %d (%s)",
+                               errno, strerror(errno));
+               goto error;
+       }
+
+       DBG("traverse path: %s/shared/trusted", app_root_dir);
+
+       subfd = openat(fd, "trusted", O_RDONLY | O_DIRECTORY);
+       if (subfd >= 0) {
+               size = __calculate_directory_size(subfd, true);
+               if (size < 0) {
+                       ERR("Calculating shared/trusted directory failed.");
+                       goto error;
+               }
+               *data_size += size;
+               DBG("data_size: %lld", *data_size);
+               close(subfd);
+       } else if (subfd < 0 && errno != ENOENT) {
+               DBG("openat() failed, entry: trusted, errno: %d (%s)",
+                               errno, strerror(errno));
+               goto error;
+       }
+
+       DBG("traverse path: %s/shared/res", app_root_dir);
+
+       subfd = openat(fd, "res", O_RDONLY | O_DIRECTORY);
+       if (subfd >= 0) {
+               size = __calculate_directory_size(subfd, true);
+               if (size < 0) {
+                       ERR("Calculating shared/res directory failed.");
+                       goto error;
+               }
+               *app_size += size;
+               DBG("app_size: %lld", *app_size);
+               close(subfd);
+       } else if (subfd < 0 && errno != ENOENT) {
+               ERR("openat() failed, entry: res, errno: %d (%s)",
+                               errno, strerror(errno));
+               goto error;
+       }
+
+       DBG("traverse path: %s/shared/cache", app_root_dir);
+
+       subfd = openat(fd, "cache", O_RDONLY | O_DIRECTORY);
+       if (subfd >= 0) {
+               size = __calculate_directory_size(subfd, true);
+               if (size < 0) {
+                       ERR("Calculating shared/cache directory failed.");
+                       goto error;
+               }
+               *data_size += size;
+               DBG("data_size: %lld", *data_size);
+               close(subfd);
+       } else if (subfd < 0 && errno != ENOENT) {
+               ERR("openat() failed, entry: data, errno: %d (%s)",
+                               errno, strerror(errno));
+               goto error;
+       }
+
+       close(fd);
+       return 0;
+
+error:
+       if (fd != -1)
+               close(fd);
+       if (subfd != -1)
+               close(subfd);
+
+       return -1;
+}
+
+static int __is_global(uid_t uid)
+{
+       return (uid == OWNER_ROOT || uid == GLOBAL_USER) ? 1 : 0;
+}
+
+static int __calculate_pkg_size_info(STORAGE_TYPE type, const char *pkgid,
+               long long *data_size, long long *cache_size,
+               long long *app_size)
+{
+       uid_t uid = getuid();
+       char app_root_dir[MAX_PATH_LENGTH] = {0, };
+       DIR *dir;
+       int dfd;
+       int subfd = -1;
+       struct stat st;
+       int ret;
+       struct dirent *ent;
+       long long size = 0;
+
+       if (type == STORAGE_TYPE_INTERNAL) {
+               if (!__is_global(uid))
+                       tzplatform_set_user(uid);
+               snprintf(app_root_dir, sizeof(app_root_dir), "%s",
+                               tzplatform_mkpath(__is_global(uid)
+                                       ? TZ_SYS_RW_APP : TZ_USER_APP, pkgid));
+               tzplatform_reset_user();
+#if 0 /* installed at external storage is not supported yet */
+       } else if (type == STORAGE_TYPE_EXTERNAL) {
+               snprintf(app_root_dir, MAX_PATH_LENGTH, "%s%s/",
+                               APP_BASE_EXTERNAL_PATH, pkgid);
+#endif
+       } else {
+               ERR("Invalid STORAGE_TYPE");
+               return -1;
+       }
+
+       dir = opendir(app_root_dir);
+       if (dir == NULL) {
+               ERR("opendir() failed, path: %s, errno: %d (%s)",
+                               app_root_dir, errno, strerror(errno));
+               return -1;
+       }
+
+       dfd = dirfd(dir);
+       ret = fstat(dfd, &st);
+       if (ret < 0) {
+               ERR("fstat() failed, path: %s, errno: %d (%s)", app_root_dir,
+                               errno, strerror(errno));
+               goto error;
+       }
+       *app_size += __stat_size(&st);
+
+       while ((ent = readdir(dir))) {
+               const char *name = ent->d_name;
+               if (name[0] == '.') {
+                       if (name[1] == '\0')
+                               continue;
+                       if ((name[1] == '.') && (name[2] == '\0'))
+                               continue;
+               }
+
+               if (ent->d_type != DT_DIR)
+                       continue;
+
+               subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
+               if (subfd < 0) {
+                       if (errno != ENOENT) {
+                               ERR("openat() failed, errno: %d (%s)",
+                                               errno, strerror(errno));
+                               goto error;
+                       }
+                       continue;
+               }
+               if (strncmp(name, "data", strlen("data")) == 0) {
+                       DBG("traverse path: %s/%s", app_root_dir, name);
+                       size = __calculate_directory_size(subfd, true);
+                       if (size < 0) {
+                               ERR("Calculating data directory failed.");
+                               goto error;
+                       }
+                       *data_size += size;
+                       DBG("data_size: %lld", *data_size);
+               } else if (strncmp(name, "cache", strlen("cache")) == 0) {
+                       DBG("traverse path: %s/%s", app_root_dir, name);
+                       size = __calculate_directory_size(subfd, true);
+                       if (size < 0) {
+                               ERR("Calculating cache directory failed.");
+                               goto error;
+                       }
+                       *cache_size += size;
+                       DBG("cache_size: %lld", *cache_size);
+               } else if (strncmp(name, "shared", strlen("shared")) == 0) {
+                       size = __calculate_shared_dir_size(dfd, app_root_dir,
+                                       data_size, app_size);
+                       if (size < 0) {
+                               ERR("Calculating shared directory failed.");
+                               goto error;
+                       }
+                       *app_size += size;
+                       DBG("app_size: %lld", *app_size);
+               } else {
+                       DBG("traverse path: %s/%s", app_root_dir, name);
+                       size = __calculate_directory_size(subfd, true);
+                       if (size < 0) {
+                               ERR("Calculating %s directory failed.", name);
+                               goto error;
+                       }
+                       *app_size += size;
+                       DBG("app_size: %lld", *app_size);
+               }
+               close(subfd);
+       }
+       closedir(dir);
+       return 0;
+
+error:
+       if (dir)
+               closedir(dir);
+       if (subfd != -1)
+               close(subfd);
+
+       return -1;
+}
+
+static char *__get_pkg_size_info_str(const pkg_size_info_t* pkg_size_info)
+{
+       char *size_info_str;
+
+       size_info_str = (char *)malloc(MAX_SIZE_INFO_SIZE);
+       if (size_info_str == NULL) {
+               ERR("Out of memory.");
+               return NULL;
+       }
+
+       snprintf(size_info_str, MAX_LONGLONG_LENGTH, "%lld",
+                       pkg_size_info->data_size);
+       strcat(size_info_str, ":");
+       snprintf(size_info_str + strlen(size_info_str), MAX_LONGLONG_LENGTH,
+                       "%lld", pkg_size_info->cache_size);
+       strcat(size_info_str, ":");
+       snprintf(size_info_str + strlen(size_info_str), MAX_LONGLONG_LENGTH,
+                       "%lld", pkg_size_info->app_size);
+       strcat(size_info_str, ":");
+       snprintf(size_info_str + strlen(size_info_str), MAX_LONGLONG_LENGTH,
+                       "%lld", pkg_size_info->ext_data_size);
+       strcat(size_info_str, ":");
+       snprintf(size_info_str + strlen(size_info_str), MAX_LONGLONG_LENGTH,
+                       "%lld", pkg_size_info->ext_cache_size);
+       strcat(size_info_str, ":");
+       snprintf(size_info_str + strlen(size_info_str), MAX_LONGLONG_LENGTH,
+                       "%lld", pkg_size_info->ext_app_size);
+       strcat(size_info_str, ":");
+
+       DBG("size_info_str: %s", size_info_str);
+
+       return size_info_str;
+}
+
+static int __get_pkg_size_info(const char *pkgid,
+               pkg_size_info_t *pkg_size_info)
+{
+       int ret;
+
+       ret = __calculate_pkg_size_info(STORAGE_TYPE_INTERNAL, pkgid,
+                       &pkg_size_info->data_size, &pkg_size_info->cache_size,
+                       &pkg_size_info->app_size);
+       if (ret < 0)
+               DBG("Calculating internal package size info failed: %d", ret);
+       DBG("size_info: %lld %lld %lld", pkg_size_info->data_size,
+                       pkg_size_info->cache_size, pkg_size_info->app_size);
+
+#if 0
+       ret = __calculate_pkg_size_info(STORAGE_TYPE_EXTERNAL, pkgid,
+                       &pkg_size_info->ext_data_size,
+                       &pkg_size_info->ext_cache_size,
+                       &pkg_size_info->ext_app_size);
+       if (ret < 0)
+               DBG("Calculating external package size info failed: %d", ret);
+       DBG("size_info(external): %lld %lld %lld", pkg_size_info->ext_data_size,
+                       pkg_size_info->ext_cache_size,
+                       pkg_size_info->ext_app_size);
+#endif
+       return ret;
+}
+
+static int __get_total_pkg_size_info_cb(const pkgmgrinfo_pkginfo_h handle,
+               void *user_data)
+{
+       int ret;
+       char *pkgid;
+       pkg_size_info_t temp_pkg_size_info = {0,};
+       pkg_size_info_t *pkg_size_info = (void *)user_data;
+
+       ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
+       if (ret != PMINFO_R_OK) {
+               ERR("pkgmgrinfo_pkginfo_get_pkgid() failed");
+               return -1;
+       }
+
+       __get_pkg_size_info(pkgid, &temp_pkg_size_info);
+
+       pkg_size_info->app_size += temp_pkg_size_info.app_size;
+       pkg_size_info->data_size += temp_pkg_size_info.data_size;
+       pkg_size_info->cache_size += temp_pkg_size_info.cache_size;
+       pkg_size_info->ext_app_size += temp_pkg_size_info.ext_app_size;
+       pkg_size_info->ext_data_size += temp_pkg_size_info.ext_data_size;
+       pkg_size_info->ext_cache_size += temp_pkg_size_info.ext_cache_size;
+
+       return 0;
+}
+
+int __make_size_info_file(char *req_key, long long size)
+{
+       FILE *file;
+       int fd = 0;
+       char buf[MAX_PKG_BUF_LEN];
+       char info_file[MAX_PKG_BUF_LEN];
+
+       if (req_key == NULL)
+               return -1;
+
+       snprintf(info_file, sizeof(info_file), "%s/%s", PKG_SIZE_INFO_PATH,
+                       req_key);
+       ERR("File path = %s", info_file);
+
+       file = fopen(info_file, "w");
+       if (file == NULL) {
+               ERR("Couldn't open the file %s", info_file);
+               return -1;
+       }
+
+       snprintf(buf, MAX_LONGLONG_LENGTH, "%lld", size);
+       fwrite(buf, 1, strlen(buf), file);
+
+       fflush(file);
+       fd = fileno(file);
+       fsync(fd);
+       fclose(file);
+
+       return 0;
+}
+
+static int __send_sizeinfo_cb(const pkgmgrinfo_pkginfo_h handle,
+               void *user_data)
+{
+       int ret;
+       char *pkgid;
+       int data_size = 0;
+       int total_size = 0;
+       char total_buf[MAX_PKG_BUF_LEN];
+       char data_buf[MAX_PKG_BUF_LEN];
+       pkgmgr_installer *pi = (pkgmgr_installer *)user_data;
+
+       pkg_size_info_t temp_pkg_size_info = {0, };
+
+       ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
+       if (ret != PMINFO_R_OK) {
+               ERR("pkgmgrinfo_pkginfo_get_pkgid() failed");
+               return -1;
+       }
+
+       __get_pkg_size_info(pkgid, &temp_pkg_size_info);
+
+       total_size = temp_pkg_size_info.app_size +
+               temp_pkg_size_info.data_size + temp_pkg_size_info.cache_size;
+       data_size = temp_pkg_size_info.data_size +
+               temp_pkg_size_info.cache_size;
+
+       /* send size info to client */
+       snprintf(total_buf, sizeof(total_buf), "%d", total_size);
+       snprintf(data_buf, sizeof(data_buf), "%d", data_size);
+
+       return pkgmgr_installer_send_signal(pi,
+                       PKGMGR_INSTALLER_GET_SIZE_KEY_STR,
+                       pkgid, data_buf, total_buf);
+}
+
+static int __send_result_to_signal(pkgmgr_installer *pi, const char *req_key,
+               const char *pkgid, pkg_size_info_t *info)
+{
+       int ret;
+       char *info_str;
+
+       info_str = __get_pkg_size_info_str(info);
+       if (info_str == NULL)
+               return -1;
+
+       ret = pkgmgr_installer_send_signal(pi,
+                       PKGMGR_INSTALLER_GET_SIZE_KEY_STR,
+                       pkgid, PKGMGR_INSTALLER_GET_SIZE_KEY_STR, info_str);
+       free(info_str);
+
+       return ret;
+}
+
+int main(int argc, char *argv[])
+{
+       int ret;
+       int get_type;
+       char *pkgid;
+       char *req_key;
+       long long size = 0;
+       pkgmgr_installer *pi;
+       pkg_size_info_t info = {0, };
+
+       // argv has bellowed meaning
+       // argv[0] = pkgid
+       // argv[1] = get type
+       // argv[3] = req_key
+
+       if (argv[0] == NULL) {
+               ERR("pkgid is NULL\n");
+               return -1;
+       }
+
+       pkgid = argv[0];
+       get_type = atoi(argv[1]);
+       req_key = argv[3];
+
+       DBG("start get size : [pkgid=%s, request type=%d]", pkgid, get_type);
+
+       pi = pkgmgr_installer_new();
+       if (pi == NULL) {
+               ERR("failed to create installer");
+               return -1;
+       }
+       pkgmgr_installer_receive_request(pi, argc, argv);
+
+       switch (get_type) {
+       case PM_GET_TOTAL_SIZE:
+               /* send result to file */
+               ret = __get_pkg_size_info(pkgid, &info);
+               if (ret == 0)
+                       size = info.app_size + info.data_size + info.cache_size;
+               ret = __make_size_info_file(req_key, size);
+               break;
+       case PM_GET_DATA_SIZE:
+               /* send result to file */
+               ret = __get_pkg_size_info(pkgid, &info);
+               if (ret == 0)
+                       size = info.data_size + info.cache_size;
+               ret = __make_size_info_file(req_key, size);
+               break;
+       case PM_GET_ALL_PKGS:
+               /* send result to file */
+               ret = pkgmgrinfo_pkginfo_get_usr_list(
+                               __get_total_pkg_size_info_cb, &info, getuid());
+               if (ret == 0)
+                       size = info.app_size + info.data_size + info.cache_size;
+               ret = __make_size_info_file(req_key, size);
+               break;
+       case PM_GET_SIZE_INFO:
+               /* send each result to signal */
+               ret = pkgmgrinfo_pkginfo_get_usr_list(__send_sizeinfo_cb, pi,
+                               getuid());
+               ret = __make_size_info_file(req_key, 0);
+               break;
+       case PM_GET_PKG_SIZE_INFO:
+               /* send result to signal */
+               ret = __get_pkg_size_info(pkgid, &info);
+               if (ret == 0)
+                       ret = __send_result_to_signal(pi, req_key,
+                                       pkgid, &info);
+               ret = __make_size_info_file(req_key, 0);
+               break;
+       case PM_GET_TOTAL_PKG_SIZE_INFO:
+               /* send result to signal */
+               ret = pkgmgrinfo_pkginfo_get_usr_list(
+                               __get_total_pkg_size_info_cb, &info, getuid());
+               if (ret == 0)
+                       ret = __send_result_to_signal(pi, req_key,
+                                       PKG_SIZE_INFO_TOTAL, &info);
+               ret = __make_size_info_file(req_key, 0);
+               break;
+       default:
+               ret = -1;
+               ERR("unsupported or depreated type");
+               break;
+       }
+
+       if (pkgmgr_installer_send_signal(pi, PKGMGR_INSTALLER_GET_SIZE_KEY_STR,
+                               pkgid, PKGMGR_INSTALLER_GET_SIZE_KEY_STR,
+                               PKGMGR_INSTALLER_END_KEY_STR))
+               ERR("failed to send finished signal");
+
+       DBG("get size result = %d", ret);
+       pkgmgr_installer_free(pi);
+
+       return ret;
+}
diff --git a/src/pkg_info.c b/src/pkg_info.c
new file mode 100644 (file)
index 0000000..3964e32
--- /dev/null
@@ -0,0 +1,2167 @@
+/*
+ * slp-pkgmgr
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
+ * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <pkgmgr_parser.h>
+#include <pkgmgr-info.h>
+
+#include <package-manager.h>
+#include <package-manager-types.h>
+#include <pkgmgr_installer.h>
+
+#define OWNER_ROOT 0
+
+static void __print_usage();
+static int __get_pkg_info(char *pkgid, uid_t uid);
+static int __get_app_info(char *appid);
+static int __get_app_list(char *pkgid, uid_t uid);
+static int __get_app_category_list(char *appid);
+static int __get_app_metadata_list(char *appid);
+static int __get_app_control_list(char *appid);
+static int __get_pkg_list(uid_t uid);
+static int __get_installed_app_list(uid_t uid);
+static int __add_app_filter(uid_t uid);
+static int __add_pkg_filter(uid_t uid);
+static int __insert_manifest_in_db(char *manifest, uid_t uid);
+static int __remove_manifest_from_db(char *manifest, uid_t uid);
+static int __set_certinfo_in_db(char *pkgid, uid_t uid);
+static int __get_certinfo_from_db(char *pkgid, uid_t uid);
+static int __del_certinfo_from_db(char *pkgid);
+static int __get_integer_input_data(void);
+char *__get_string_input_data(void);
+static int __pkg_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data);
+static int __app_category_list_cb(const char *category_name, void *user_data);
+static int __app_control_list_cb(const char *operation, const char *uri, const char *mime, void *user_data);
+static int __app_metadata_list_cb(const char *metadata_name, const char *metadata_value, void *user_data);
+int app_func(const pkgmgrinfo_appinfo_h handle, void *user_data);
+
+static void __get_pkgmgrinfo_pkginfo(const pkgmgrinfo_pkginfo_h handle, void *user_data)
+{
+       int ret = -1;
+       char *type = NULL;
+       char *version = NULL;
+       char *author_name = NULL;
+       char *author_email = NULL;
+       char *author_href = NULL;
+       char *root_path = NULL;
+       char *mainappid = NULL;
+       pkgmgrinfo_install_location location = 0;
+       char *icon = NULL;
+       char *label = NULL;
+       char *desc = NULL;
+       bool removable = 0;
+       bool preload = 0;
+       bool readonly = 0;
+       bool update = 0;
+       bool system = 0;
+       int installed_time = -1;
+
+       ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
+       if (ret < 0) {
+               printf("Failed to get pkg type\n");
+       }
+       if (type)
+               printf("Type: %s\n", type);
+
+       ret = pkgmgrinfo_pkginfo_get_version(handle, &version);
+       if (ret < 0) {
+               printf("Failed to get version\n");
+       }
+       if (version)
+               printf("Version: %s\n", version);
+
+       ret = pkgmgrinfo_pkginfo_get_install_location(handle, &location);
+       if (ret < 0) {
+               printf("Failed to get install location\n");
+       }
+       printf("Install Location: %d\n", location);
+
+       ret = pkgmgrinfo_pkginfo_get_icon(handle, &icon);
+       if (ret < 0) {
+               printf("Failed to get icon\n");
+       }
+       if (icon)
+               printf("Icon: %s\n", icon);
+
+       ret = pkgmgrinfo_pkginfo_get_label(handle, &label);
+       if (ret < 0) {
+               printf("Failed to get label\n");
+       }
+       if (label)
+               printf("Label: %s\n", label);
+
+       ret = pkgmgrinfo_pkginfo_get_description(handle, &desc);
+       if (ret < 0) {
+               printf("Failed to get description\n");
+       }
+       if (desc)
+               printf("Description: %s\n", desc);
+
+       ret = pkgmgrinfo_pkginfo_get_author_name(handle, &author_name);
+       if (ret < 0) {
+               printf("Failed to get author name\n");
+       }
+       if (author_name)
+               printf("Author Name: %s\n", author_name);
+
+       ret = pkgmgrinfo_pkginfo_get_author_email(handle, &author_email);
+       if (ret < 0) {
+               printf("Failed to get author email\n");
+       }
+       if (author_email)
+               printf("Author Email: %s\n", author_email);
+
+       ret = pkgmgrinfo_pkginfo_get_author_href(handle, &author_href);
+       if (ret < 0) {
+               printf("Failed to get author href\n");
+       }
+       if (author_href)
+               printf("Author Href: %s\n", author_href);
+
+       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &root_path);
+       if (ret < 0) {
+               printf("Failed to get root_path\n");
+       }
+       if (author_href)
+               printf("root_path : %s\n", root_path);
+
+       ret = pkgmgrinfo_pkginfo_get_mainappid(handle, &mainappid);
+       if (ret < 0) {
+               printf("Failed to get mainappid\n");
+       }
+       if (author_href)
+               printf("mainappid : %s\n", mainappid);
+
+       ret = pkgmgrinfo_pkginfo_get_installed_time(handle, &installed_time);
+       if (ret < 0) {
+               printf("Failed to get install time\n");
+       }
+       printf("Install time: %d\n", installed_time);
+
+       ret = pkgmgrinfo_pkginfo_is_removable(handle, &removable);
+       if (ret < 0) {
+               printf("Failed to get removable\n");
+       }
+       else
+               printf("Removable: %d\n", removable);
+
+       ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
+       if (ret < 0) {
+               printf("Failed to get preload\n");
+       }
+       else
+               printf("Preload: %d\n", preload);
+
+       ret = pkgmgrinfo_pkginfo_is_readonly(handle, &readonly);
+       if (ret < 0) {
+               printf("Failed to get readonly\n");
+       }
+       else
+               printf("Readonly: %d\n", readonly);
+
+       ret = pkgmgrinfo_pkginfo_is_update(handle, &update);
+       if (ret < 0) {
+               printf("Failed to get update\n");
+       }
+       else
+               printf("update: %d\n", update);
+
+       ret = pkgmgrinfo_pkginfo_is_system(handle, &system);
+       if (ret < 0) {
+               printf("Failed to get system\n");
+       }
+       else
+               printf("system: %d\n", system);
+}
+
+int __get_app_id(const pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+       char *appid = NULL;
+       char *apptype = NULL;
+       int ret = -1;
+
+       ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
+       if (ret < 0) {
+               printf("Failed to get appid\n");
+       }
+
+       ret = pkgmgrinfo_appinfo_get_apptype(handle, &apptype);
+       if (ret < 0) {
+               printf("Failed to get package\n");
+       }
+       printf("apptype [%s]\t appid [%s]\n", apptype, appid);
+
+       return 0;
+}
+
+static int __get_integer_input_data(void)
+{
+       char input_str[32] = { 0, };
+       int data = 0;
+
+       if (fgets(input_str, sizeof(input_str), stdin) == NULL) {
+               printf("fgets() failed....\n");
+               return -1;
+       }
+
+       if (sscanf(input_str, "%4d", &data) != 1) {
+               printf("Input only integer option....\n");
+               return -1;
+       }
+
+       return data;
+}
+
+
+char *__get_string_input_data(void)
+{
+       char *data = (char *)malloc(1024);
+       if (data == NULL) {
+               printf("Malloc Failed\n");
+               return NULL;
+       }
+       if (fgets(data,1024,stdin) == NULL){
+               printf("Buffer overflow!!! try again\n");
+               exit(-1);
+       }
+       data[strlen(data) - 1] = '\0';
+       return data;
+}
+
+static void __print_usage()
+{
+       printf("For Getting package|App Info\n");
+       printf("\tpkginfo --[pkg|app] <pkgid|appid>\n\n");
+       printf("For Getting list of installed packages\n");
+       printf("\tpkginfo --listpkg \n\n");
+       printf("For Getting list of installed applications\n");
+       printf("\tpkginfo --listapp \n\n");
+       printf("For Getting app list for a particular package\n");
+       printf("\tpkginfo --list <pkgid>\n\n");
+       printf("For Getting app category list for a particular application\n");
+       printf("\tpkginfo --category <appid>\n\n");
+       printf("For Getting app metadata  list for a particular application\n");
+       printf("\tpkginfo --metadata <appid>\n\n");
+       printf("For Getting app control list for a particular application\n");
+       printf("\tpkginfo --appcontrol <appid>\n\n");
+       printf("To insert|remove manifest info in DB\n");
+       printf("\tpkginfo --[imd|rmd] <manifest file name>\n\n");
+       printf("To set manifest validation\n");
+       printf("\tpkginfo --check <manifest file name>\n\n");
+       printf("To set cert info in DB\n");
+       printf("\tpkginfo --setcert <pkgid>\n\n");
+       printf("To get cert info from DB\n");
+       printf("\tpkginfo --getcert <pkgid>\n\n");
+       printf("To compare pkg cert info from DB\n");
+       printf("\tpkginfo --cmp-pkgcert <lhs_pkgid> <rhs_pkgid>\n\n");
+       printf("To compare app cert info from DB\n");
+       printf("\tpkginfo --cmp-appcert <lhs_appid> <rhs_appid>\n\n");
+       printf("To delete all cert info from DB\n");
+       printf("\tpkginfo --delcert <pkgid>\n\n");
+       printf("To add application filter values [Multiple values can be added]\n");
+       printf("\tpkginfo --app-flt\n\n");
+       printf("To add package filter values [Multiple values can be added]\n");
+       printf("\tpkginfo --pkg-flt\n\n");
+       printf("To add metadata filter values\n");
+       printf("\tpkginfo --metadata-flt\n\n");
+}
+
+static void __print_arg_filter_usage()
+{
+       printf("=========================================\n");
+       printf("pkginfo --arg-flt key value\n");
+       printf("ex : pkginfo --arg-flt 6 webapp\n");
+       printf("key list is bellowed\n");
+       printf("2  --> filter by app ID\n");
+       printf("3  --> filter by app component\n");
+       printf("4  --> filter by app exec\n");
+       printf("5  --> filter by app icon\n");
+       printf("6  --> filter by app type\n");
+       printf("7  --> filter by app operation\n");
+       printf("8  --> filter by app uri\n");
+       printf("9  --> filter by app mime\n");
+       printf("10 --> filter by app category\n");
+       printf("11 --> filter by app nodisplay [0|1]\n");
+       printf("12 --> filter by app multiple [0|1]\n");
+       printf("13 --> filter by app onboot [0|1]\n");
+       printf("14 --> filter by app autorestart [0|1]\n");
+       printf("15 --> filter by app taskmanage [0|1]\n");
+       printf("16 --> filter by app hwacceleration\n");
+       printf("17 --> filter by app screenreader\n");
+       printf("=========================================\n");
+}
+
+static int __app_list_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+       char *appid = NULL;
+       pkgmgrinfo_appinfo_get_appid(handle, &appid);
+       printf("appid : %s\n", appid);
+       return 0;
+}
+
+static int __add_metadata_filter()
+{
+       int ret = 0;
+       pkgmgrinfo_appinfo_metadata_filter_h handle;
+       char *key = NULL;
+       char *value = NULL;
+
+       ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
+       if (ret != PMINFO_R_OK){
+               printf("pkgmgrinfo_appinfo_metadata_filter_create() failed\n");
+               return ret;
+       }
+
+       printf("enter metadata - key\n");
+       key = __get_string_input_data();
+       printf("enter metadata - value\n");
+       value = __get_string_input_data();
+
+       printf("filter condition : key=[%s], value=[%s]\n", key, value);
+
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, key, value);
+       if (ret != PMINFO_R_OK){
+               printf("pkgmgrinfo_appinfo_metadata_filter_add() failed\n");
+               goto err;
+       }
+
+       ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, __app_list_cb, NULL);
+       if (ret != PMINFO_R_OK){
+               printf("pkgmgrinfo_appinfo_metadata_filter_add() failed\n");
+               goto err;
+       }
+
+err:
+       pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
+       if (key) {
+               free(key);
+               key = NULL;
+       }
+       if (value) {
+               free(value);
+               value = NULL;
+       }
+       return ret;
+}
+
+static int __add_app_filter(uid_t uid)
+{
+       int ret = 0;
+       int choice = -1;
+       char *value = NULL;
+       int val = -1;
+       int count = 0;
+       pkgmgrinfo_appinfo_filter_h handle;
+       ret = pkgmgrinfo_appinfo_filter_create(&handle);
+       if (ret > 0) {
+               printf("appinfo filter handle create failed\n");
+               return -1;
+       }
+       while (choice != 0 && choice != 1)
+       {
+               printf("Enter Choice\n");
+               printf("0  --> Finalize filter and get count of apps\n");
+               printf("1  --> Finalize filter and get list of apps\n");
+               printf("2  --> filter by app ID\n");
+               printf("3  --> filter by app component\n");
+               printf("4  --> filter by app exec\n");
+               printf("5  --> filter by app icon\n");
+               printf("6  --> filter by app type\n");
+               printf("7  --> filter by app operation\n");
+               printf("8  --> filter by app uri\n");
+               printf("9  --> filter by app mime\n");
+               printf("10 --> filter by app category\n");
+               printf("11 --> filter by app nodisplay [0|1]\n");
+               printf("12 --> filter by app multiple [0|1]\n");
+               printf("13 --> filter by app onboot [0|1]\n");
+               printf("14 --> filter by app autorestart [0|1]\n");
+               printf("15 --> filter by app taskmanage [0|1]\n");
+               printf("16 --> filter by app hwacceleration\n");
+               printf("17 --> filter by app screenreader\n");
+               printf("18 --> filter by app ui-gadget [0|1]\n");
+               printf("19 --> filter by app support disable [0|1]\n");
+               choice = __get_integer_input_data();
+               switch (choice) {
+               case 0:
+                       ret = pkgmgrinfo_appinfo_filter_count(handle, &count);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_count() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       printf("App count = %d\n", count);
+                       break;
+               case 1:
+                       if (uid != GLOBAL_USER)
+                               ret = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_func, NULL, uid);
+                       else
+                               ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, app_func, NULL);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_foreach_appinfo() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 2:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_ID, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 3:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_COMPONENT, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 4:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_EXEC, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 5:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_ICON, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 6:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_TYPE, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 7:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_OPERATION, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 8:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_URI, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 9:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_MIME, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 10:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_CATEGORY, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 11:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_APP_NODISPLAY, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 12:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_APP_MULTIPLE, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 13:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_APP_ONBOOT, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 14:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_APP_AUTORESTART, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 15:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_bool(handle,
+                               PMINFO_APPINFO_PROP_APP_TASKMANAGE, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 16:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_HWACCELERATION, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 17:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_string(handle,
+                               PMINFO_APPINFO_PROP_APP_SCREENREADER, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 18:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_bool(handle,
+                                       PMINFO_APPINFO_PROP_APP_UI_GADGET, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 19:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_appinfo_filter_add_bool(handle,
+                                       PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               default:
+                       printf("Invalid filter property\n");
+                               pkgmgrinfo_appinfo_filter_destroy(handle);
+                       ret = -1;
+                       goto err;
+               }
+       }
+       ret = 0;
+err:
+       pkgmgrinfo_appinfo_filter_destroy(handle);
+       if (value) {
+               free(value);
+               value = NULL;
+       }
+       return ret;
+}
+
+static int __add_pkg_filter(uid_t uid)
+{
+       int ret = 0;
+       int choice = -1;
+       char *value = NULL;
+       int val = -1;
+       int count = 0;
+       pkgmgrinfo_pkginfo_filter_h handle;
+
+       ret = pkgmgrinfo_pkginfo_filter_create(&handle);
+       if (ret > 0) {
+               printf("pkginfo filter handle create failed\n");
+               return -1;
+       }
+       while (choice != 0 && choice !=1)
+       {
+               printf("Enter Choice\n");
+               printf("0  --> Finalize filter and get count of packages\n");
+               printf("1  --> Finalize filter and get list of packages\n");
+               printf("2  --> filter by package ID\n");
+               printf("3  --> filter by package version\n");
+               printf("4  --> filter by package type\n");
+               printf("5  --> filter by package install location\n");
+               printf("6  --> filter by author name\n");
+               printf("7  --> filter by author email\n");
+               printf("8  --> filter by author href\n");
+               printf("9  --> filter by package removable [0|1]\n");
+               printf("10 --> filter by package readonly [0|1]\n");
+               printf("11 --> filter by package preload [0|1]\n");
+               printf("12 --> filter by package update [0|1]\n");
+               printf("13 --> filter by package appsetting [0|1]\n");
+               printf("14 --> filter by package installed storage[installed_internal | installed_external]\n");
+               printf("15 --> filter by package privilege\n");
+               printf("16 --> filter by package support disable [0|1]\n");
+               choice = __get_integer_input_data();
+               switch (choice) {
+               case 0:
+                       if (uid != GLOBAL_USER)
+                               ret = pkgmgrinfo_pkginfo_usr_filter_count(handle, &count, uid);
+                       else
+                               ret = pkgmgrinfo_pkginfo_filter_count(handle, &count);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_count() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       printf("Package count = %d\n", count);
+                       break;
+               case 1:
+                       if (uid != GLOBAL_USER)
+                               ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(handle, __pkg_list_cb, NULL, uid);
+                       else
+                               ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __pkg_list_cb, NULL);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_(usr)_filter_foreach_pkginfo() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 2:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_ID, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 3:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_VERSION, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 4:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_TYPE, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 5:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 6:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 7:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 8:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 9:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_bool(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 10:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_bool(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_READONLY, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 11:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_bool(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 12:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_bool(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_UPDATE, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 13:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_bool(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               case 14:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE, value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 15:
+                       value = __get_string_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
+                                       PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,
+                                       value);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 16:
+                       val = __get_integer_input_data();
+                       ret = pkgmgrinfo_pkginfo_filter_add_bool(handle,
+                               PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE, val);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_filter_add_bool() failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       break;
+               default:
+                       printf("Invalid filter property\n");
+                       ret = -1;
+                       goto err;
+               }
+       }
+       ret = 0;
+err:
+       pkgmgrinfo_pkginfo_filter_destroy(handle);
+       if (value) {
+               free(value);
+               value = NULL;
+       }
+       return ret;
+}
+
+static int __add_arg_filter(char *key, char *value, uid_t uid)
+{
+       int ret = 0;
+       int choice = -1;
+       int val = -1;
+       pkgmgrinfo_appinfo_filter_h handle;
+       ret = pkgmgrinfo_appinfo_filter_create(&handle);
+       if (ret > 0) {
+               printf("appinfo filter handle create failed\n");
+               return -1;
+       }
+       choice = atoi(key);
+
+       switch (choice) {
+       case 2:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_ID, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 3:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_COMPONENT, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 4:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_EXEC, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 5:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_ICON, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 6:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_TYPE, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 7:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_OPERATION, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 8:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_URI, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 9:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_MIME, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               free(value);
+               value = NULL;
+               break;
+       case 10:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_CATEGORY, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_string() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 11:
+               val = atoi(value);
+               ret = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_NODISPLAY, val);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 12:
+               val = atoi(value);
+               ret = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_MULTIPLE, val);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 13:
+               val = atoi(value);
+               ret = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_ONBOOT, val);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 14:
+               val = atoi(value);
+               ret = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_AUTORESTART, val);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 15:
+               val = atoi(value);
+               ret = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_TASKMANAGE, val);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 16:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_HWACCELERATION, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+       case 17:
+               ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_SCREENREADER, value);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_filter_add_bool() failed\n");
+                       ret = -1;
+                       goto err;
+               }
+               break;
+
+       default:
+               __print_arg_filter_usage();
+               goto err;
+       }
+       if (uid != GLOBAL_USER)
+               ret = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, __get_app_id, NULL, uid);
+       else
+               ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, __get_app_id, NULL);
+       if (ret < 0) {
+               printf("pkgmgrinfo_appinfo_filter_foreach_appinfo() failed\n");
+               ret = -1;
+               goto err;
+       }
+
+err:
+       pkgmgrinfo_appinfo_filter_destroy(handle);
+       return ret;
+}
+static int __del_certinfo_from_db(char *pkgid)
+{
+       int ret = 0;
+       if (pkgid == NULL) {
+               printf("pkgid is NULL\n");
+               return -1;
+       }
+       ret = pkgmgr_installer_delete_certinfo(pkgid);
+       if (ret < 0) {
+               printf("pkgmgr_installer_delete_certinfo failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+static int __get_certinfo_from_db(char *pkgid, uid_t uid)
+{
+       if (pkgid == NULL) {
+               printf("pkgid is NULL\n");
+               return -1;
+       }
+       int ret = 0;
+       int choice = -1;
+       int i = 0;
+       const char *value = NULL;
+       pkgmgrinfo_certinfo_h handle = NULL;
+       ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
+       if (ret < 0) {
+               printf("pkgmgrinfo_pkginfo_create_certinfo failed\n");
+               return -1;
+       }
+       ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle, uid);
+       if (ret < 0) {
+               printf("pkgmgrinfo_pkginfo_load_certinfo failed\n");
+               return -1;
+       }
+       while (choice != 10)
+       {
+               printf("Enter the choice to get\n");
+               printf("0 --> to get all cert values\n");
+               printf("1 --> author root certificate\n");
+               printf("2 --> author intermediate certificate\n");
+               printf("3 --> author signer certificate\n");
+               printf("4 --> distributor root certificate\n");
+               printf("5 --> distributor intermediate certificate\n");
+               printf("6 --> distributor signer certificate\n");
+               printf("7 --> distributor2 root certificate\n");
+               printf("8 --> distributor2 intermediate certificate\n");
+               printf("9 --> distributor2 signer certificate\n");
+               printf("10 --> exit\n");
+               choice = __get_integer_input_data();
+               switch (choice) {
+               case 0:
+                       for (i = 0; i < 9; i++)
+                       {
+                               pkgmgrinfo_pkginfo_get_cert_value(handle, i, &value);
+                               if (value)
+                                       printf("cert type[%d] value = %s\n", i, value);
+                       }
+                       ret = pkgmgrinfo_pkginfo_destroy_certinfo(handle);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_destroy_certinfo failed\n");
+                               ret = -1;
+                       }
+                       break;
+               case 1:
+               case 2:
+               case 3:
+               case 4:
+               case 5:
+               case 6:
+               case 7:
+               case 8:
+               case 9:
+                       ret = pkgmgrinfo_pkginfo_get_cert_value(handle, choice - 1, &value);
+                       if (value)
+                               printf("cert type[%d] value = %s\n", choice - 1, value);
+                       if (ret < 0)
+                               ret = -1;
+                       break;
+               case 10:
+                       ret = pkgmgrinfo_pkginfo_destroy_certinfo(handle);
+                       if (ret < 0) {
+                               printf("pkgmgrinfo_pkginfo_destroy_certinfo failed\n");
+                               ret = -1;
+                       }
+                       break;
+               default:
+                       printf("Invalid choice entered\n");
+                       ret = -1;
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static int __compare_pkg_certinfo_from_db(char *lhs_pkgid, char *rhs_pkgid, uid_t uid)
+{
+       if (lhs_pkgid == NULL || rhs_pkgid == NULL) {
+               printf("pkgid is NULL\n");
+               return -1;
+       }
+
+       int ret = 0;
+       pkgmgrinfo_cert_compare_result_type_e result;
+       if (uid != GLOBAL_USER)
+               ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lhs_pkgid, rhs_pkgid, uid, &result);
+       else
+               ret = pkgmgrinfo_pkginfo_compare_pkg_cert_info(lhs_pkgid, rhs_pkgid, &result);
+       if (ret != PMINFO_R_OK) {
+               return -1;
+       }
+
+       printf("Compare [match=0, mismatch=1, lhs_no=2, rhs_no=3, both_no=4]\n");
+       printf("pkgid =[%s] and [%s] compare result = [%d] \n", lhs_pkgid, rhs_pkgid, result);
+       return 0;
+}
+
+static int __compare_app_certinfo_from_db(char *lhs_appid, char *rhs_appid, uid_t uid)
+{
+       if (lhs_appid == NULL || rhs_appid == NULL) {
+               printf("appid is NULL\n");
+               return -1;
+       }
+
+       int ret = 0;
+       pkgmgrinfo_cert_compare_result_type_e result;
+       if (uid != GLOBAL_USER)
+               ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(lhs_appid, rhs_appid, uid, &result);
+       else
+               ret = pkgmgrinfo_pkginfo_compare_app_cert_info(lhs_appid, rhs_appid, &result);
+       if (ret != PMINFO_R_OK) {
+               return -1;
+       }
+
+       printf("Compare [match=0, mismatch=1, lhs_no=2, rhs_no=3, both_no=4]\n");
+       printf("appid =[%s] and [%s] compare result = [%d] \n", lhs_appid, rhs_appid, result);
+       return 0;
+}
+
+static int __set_certinfo_in_db(char *pkgid, uid_t uid)
+{
+       if (pkgid == NULL) {
+               printf("pkgid is NULL\n");
+               return -1;
+       }
+       int ret = 0;
+       int choice = -1;
+       char *value = NULL;
+       pkgmgr_instcertinfo_h handle = NULL;
+       ret = pkgmgr_installer_create_certinfo_set_handle(&handle);
+       if (ret < 0) {
+               printf("pkgmgr_installer_create_certinfo_set_handle failed\n");
+               return -1;
+       }
+       while (choice != 0)
+       {
+               printf("Enter the choice you want to set\n");
+               printf("0 --> to set data in DB\n");
+               printf("1 --> author root certificate\n");
+               printf("2 --> author intermediate certificate\n");
+               printf("3 --> author signer certificate\n");
+               printf("4 --> distributor root certificate\n");
+               printf("5 --> distributor intermediate certificate\n");
+               printf("6 --> distributor signer certificate\n");
+               printf("7 --> distributor2 root certificate\n");
+               printf("8 --> distributor2 intermediate certificate\n");
+               printf("9 --> distributor2 signer certificate\n");
+               choice = __get_integer_input_data();
+               switch (choice) {
+               case 0:
+                       ret = pkgmgr_installer_save_certinfo(pkgid, handle, uid);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_save_certinfo failed\n");
+                               pkgmgr_installer_destroy_certinfo_set_handle(handle);
+                               return -1;
+                       }
+                       ret = pkgmgr_installer_destroy_certinfo_set_handle(handle);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_destroy_certinfo_set_handle failed\n");
+                               return -1;
+                       }
+                       return 0;
+               case 1:
+                       printf("Enter Author Root Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 0, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 2:
+                       printf("Enter Author Intermediate Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 1, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 3:
+                       printf("Enter Author Signer Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 2, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 4:
+                       printf("Enter Distributor Root Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 3, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 5:
+                       printf("Enter Distributor Intermediate Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 4, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 6:
+                       printf("Enter Distributor Signer Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 5, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 7:
+                       printf("Enter Distributor2 Root Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 6, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 8:
+                       printf("Enter Distributor2 Intermediate Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 7, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               case 9:
+                       printf("Enter Distributor2 Signer Certificate Value: \n");
+                       value = __get_string_input_data();
+                       ret = pkgmgr_installer_set_cert_value(handle, 8, value);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_set_cert_value failed\n");
+                               ret = -1;
+                               goto err;
+                       }
+                       free(value);
+                       value = NULL;
+                       break;
+               default:
+                       printf("Invalid Number Entered\n");
+                       choice = 0;
+                       ret = pkgmgr_installer_destroy_certinfo_set_handle(handle);
+                       if (ret < 0) {
+                               printf("pkgmgr_installer_destroy_certinfo_set_handle failed\n");
+                               return -1;
+                       }
+                       break;
+               }
+       }
+err:
+       if (value) {
+               free(value);
+               value = NULL;
+       }
+       pkgmgr_installer_destroy_certinfo_set_handle(handle);
+       return ret;
+}
+
+static int __insert_manifest_in_db(char *manifest, uid_t uid)
+{
+       int ret = 0;
+       if (manifest == NULL) {
+               printf("Manifest file is NULL\n");
+               return -1;
+       }
+       if (uid == GLOBAL_USER || uid == OWNER_ROOT)
+               ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
+       else
+               ret = pkgmgr_parser_parse_usr_manifest_for_installation(manifest, uid, NULL);
+       if (ret < 0) {
+               printf("insert in db failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+static int __fota_insert_manifest_in_db(char *manifest, uid_t uid)
+{
+       int ret = 0;
+
+       if (manifest == NULL) {
+               printf("Manifest file is NULL\n");
+               return -1;
+       }
+       if (uid != GLOBAL_USER)
+               ret = pkgmgr_parser_parse_usr_manifest_for_installation(manifest, uid, NULL);
+       else
+               ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
+       if (ret < 0) {
+               printf("insert in db failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+static int __remove_manifest_from_db(char *manifest, uid_t uid)
+{
+       int ret = 0;
+       if (manifest == NULL) {
+               printf("Manifest file is NULL\n");
+               return -1;
+       }
+       if (uid != GLOBAL_USER)
+               ret = pkgmgr_parser_parse_usr_manifest_for_uninstallation(manifest, uid, NULL);
+       else
+               ret = pkgmgr_parser_parse_manifest_for_uninstallation(manifest, NULL);
+       if (ret < 0) {
+               printf("remove from db failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+int app_func(const pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+       char *appid;
+       char *data = NULL;
+       if (user_data) {
+               data = (char *)user_data;
+       }
+       int ret = -1;
+       char *exec = NULL;
+       char *icon = NULL;
+       char *label = NULL;
+       pkgmgrinfo_app_component component = 0;
+       char *apptype = NULL;
+       bool nodisplay = 0;
+       bool multiple = 0;
+       bool taskmanage = 0;
+       pkgmgrinfo_app_hwacceleration hwacceleration;
+       pkgmgrinfo_app_screenreader screenreader;
+       bool onboot = 0;
+       bool autorestart = 0;
+       char *package = NULL;
+
+       ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
+       if (ret < 0) {
+               printf("Failed to get appid\n");
+       }
+       if (appid)
+               printf("Appid: %s\n", appid);
+
+       ret = pkgmgrinfo_appinfo_get_pkgid(handle, &package);
+       if (ret < 0) {
+               printf("Failed to get package\n");
+       }
+       if (package)
+               printf("Package: %s\n", package);
+
+       ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
+       if (ret < 0) {
+               printf("Failed to get exec\n");
+       }
+       if (exec)
+               printf("Exec: %s\n", exec);
+
+       ret = pkgmgrinfo_appinfo_get_icon(handle, &icon);
+       if (ret < 0) {
+               printf("Failed to get icon\n");
+       }
+       if (icon)
+               printf("Icon: %s\n", icon);
+
+       ret = pkgmgrinfo_appinfo_get_label(handle, &label);
+       if (ret < 0) {
+               printf("Failed to get label\n");
+       }
+       if (label)
+               printf("Label: %s\n", label);
+
+       ret = pkgmgrinfo_appinfo_get_component(handle, &component);
+       if (ret < 0) {
+               printf("Failed to get component\n");
+       }
+
+       ret = pkgmgrinfo_appinfo_get_apptype(handle, &apptype);
+       if (ret < 0) {
+               printf("Failed to get apptype\n");
+       }
+       if (apptype)
+               printf("Apptype: %s\n", apptype);
+
+       if (component == PMINFO_UI_APP) {
+               printf("component: uiapp\n");
+               ret = pkgmgrinfo_appinfo_is_multiple(handle, &multiple);
+               if (ret < 0) {
+                       printf("Failed to get multiple\n");
+               } else {
+                       printf("Multiple: %d\n", multiple);
+               }
+
+               ret = pkgmgrinfo_appinfo_is_nodisplay(handle, &nodisplay);
+               if (ret < 0) {
+                       printf("Failed to get nodisplay\n");
+               } else {
+                       printf("Nodisplay: %d \n", nodisplay);
+               }
+
+               ret = pkgmgrinfo_appinfo_is_taskmanage(handle, &taskmanage);
+               if (ret < 0) {
+                       printf("Failed to get taskmanage\n");
+               } else {
+                       printf("Taskmanage: %d\n", taskmanage);
+               }
+
+               ret = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacceleration);
+               if (ret < 0) {
+                       printf("Failed to get hwacceleration\n");
+               } else {
+                       printf("hw-acceleration: %d\n", hwacceleration);
+               }
+
+               ret = pkgmgrinfo_appinfo_get_screenreader(handle, &screenreader);
+               if (ret < 0) {
+                       printf("Failed to get screenreader\n");
+               } else {
+                       printf("screenreader: %d\n", screenreader);
+               }
+
+       }
+       if (component == PMINFO_SVC_APP) {
+               printf("component: svcapp\n");
+               ret = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
+               if (ret < 0) {
+                       printf("Failed to get onboot\n");
+               } else {
+                       printf("Onboot: %d\n", onboot);
+               }
+
+               ret = pkgmgrinfo_appinfo_is_autorestart(handle, &autorestart);
+               if (ret < 0) {
+                       printf("Failed to get autorestart\n");
+               } else {
+                       printf("Autorestart: %d \n", autorestart);
+               }
+       }
+       if (data)
+               printf("user_data : %s\n\n", data);
+
+       return 0;
+}
+
+
+static int __pkg_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
+{
+       char *test_data = "test data";
+       int ret = -1;
+       char *pkgid;
+       char *pkg_type;
+       char *pkg_version;
+       bool preload = 0;
+       int installed_time = -1;
+
+       pkgmgrinfo_uidinfo_t *uid_info = (pkgmgrinfo_uidinfo_t *) handle;
+       ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
+       if(ret < 0) {
+               printf("pkgmgrinfo_pkginfo_get_pkgid() failed\n");
+       }
+       ret = pkgmgrinfo_pkginfo_get_type(handle, &pkg_type);
+       if(ret < 0) {
+               printf("pkgmgrinfo_pkginfo_get_type() failed\n");
+       }
+       ret = pkgmgrinfo_pkginfo_get_version(handle, &pkg_version);
+       if(ret < 0) {
+               printf("pkgmgrinfo_pkginfo_get_version() failed\n");
+       }
+       ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
+       if(ret < 0) {
+               printf("pkgmgrinfo_pkginfo_is_preload() failed\n");
+       }
+       ret = pkgmgrinfo_pkginfo_get_installed_time(handle, &installed_time);
+       if(ret < 0) {
+               printf("pkgmgrinfo_pkginfo_get_installed_time() failed\n");
+       }
+
+
+       printf("---------------------------------------\n");
+       printf("pkg_type [%s]\tpkgid [%s]\tversion [%s]\tpreload [%d]\tinstalled_time [%d]\n", pkg_type,
+              pkgid, pkg_version, preload, installed_time);
+
+       if (uid_info->uid != GLOBAL_USER) {
+               printf("**List of Ui-Apps**\n");
+               ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_UI_APP, app_func, (void *)test_data, uid_info->uid);
+               if (ret < 0) {
+                       printf("pkgmgr_get_info_app() failed\n");
+               }
+               printf("**List of Svc-Apps**\n");
+               ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_SVC_APP, app_func, (void *)test_data, uid_info->uid);
+               if (ret < 0) {
+                       printf("pkgmgr_get_info_app() failed\n");
+               }
+       } else {
+               printf("**List of Ui-Apps**\n");
+               ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, app_func, (void *)test_data);
+               if (ret < 0) {
+                       printf("pkgmgr_get_info_app() failed\n");
+               }
+               printf("**List of Svc-Apps**\n");
+               ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, app_func, (void *)test_data);
+               if (ret < 0) {
+                       printf("pkgmgr_get_info_app() failed\n");
+               }
+       }
+       printf("---------------------------------------\n");
+
+       return 0;
+}
+
+static int __get_pkg_list(uid_t uid)
+{
+       int ret = -1;
+       if (uid != GLOBAL_USER)
+               ret = pkgmgrinfo_pkginfo_get_usr_list(__pkg_list_cb, NULL, uid);
+       else
+               ret = pkgmgrinfo_pkginfo_get_list(__pkg_list_cb, NULL);
+       if (ret < 0) {
+               printf("pkgmgrinfo_pkginfo_get_list() failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+static int __get_installed_app_list(uid_t uid)
+{
+       int ret = -1;
+       if(uid != GLOBAL_USER)
+               ret = pkgmgrinfo_appinfo_get_usr_installed_list(app_func, uid, NULL);
+       else
+               ret = pkgmgrinfo_appinfo_get_installed_list(app_func, NULL);
+       if (ret < 0) {
+               printf("pkgmgrinfo_appinfo_get_installed_list() failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+
+static int __app_category_list_cb(const char *category_name, void *user_data)
+{
+       if (category_name)
+               printf("Category: %s\n", category_name);
+       return 0;
+}
+
+static int __app_metadata_list_cb(const char *metadata_name, const char *metadata_value, void *user_data)
+{
+       if (metadata_name && metadata_value) {
+               printf("Name: %s\n", metadata_name);
+               printf("Value: %s\n",   metadata_value);
+               printf("\n");
+       }
+       return 0;
+}
+
+static int __app_control_list_cb(const char *operation, const char *uri, const char *mime, void *user_data)
+{
+       printf("-------------------------------------------------------\n");
+       printf("Operation: %s\n", operation);
+       printf("Uri: %s\n", uri);
+       printf("Mime: %s\n", mime);
+       printf("-------------------------------------------------------\n\n");
+       return 0;
+}
+
+
+static int __get_app_category_list(char *appid)
+{
+       int ret = -1;
+       pkgmgrinfo_appinfo_h handle;
+       ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, getuid(), &handle);
+       if (ret < 0) {
+               printf("Failed to get handle\n");
+               return -1;
+       }
+       ret = pkgmgrinfo_appinfo_foreach_category(handle, __app_category_list_cb, NULL);
+       if (ret < 0) {
+               printf("pkgmgrinfo_appinfo_foreach_category() failed\n");
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return -1;
+       }
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       return 0;
+}
+
+static int __get_app_metadata_list(char *appid)
+{
+       int ret = -1;
+       pkgmgrinfo_appinfo_h handle;
+       ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, getuid(), &handle);
+       if (ret < 0) {
+               printf("Failed to get handle\n");
+               return -1;
+       }
+       ret = pkgmgrinfo_appinfo_foreach_metadata(handle, __app_metadata_list_cb, NULL);
+       if (ret < 0) {
+               printf("pkgmgrinfo_appinfo_foreach_metadata() failed\n");
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return -1;
+       }
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       return 0;
+}
+
+static int __get_app_control_list(char *appid)
+{
+       int ret = -1;
+       pkgmgrinfo_appinfo_h handle;
+       ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, getuid(), &handle);
+       if (ret < 0) {
+               printf("Failed to get handle\n");
+               return -1;
+       }
+       ret = pkgmgrinfo_appinfo_foreach_appcontrol(handle, __app_control_list_cb, NULL);
+       if (ret < 0) {
+               printf("pkgmgrinfo_appinfo_foreach_appcontrol() failed\n");
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return -1;
+       }
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       return 0;
+}
+
+static int __set_app_enabled(char *appid, bool enabled)
+{
+       int ret = -1;
+       ret = pkgmgrinfo_appinfo_set_state_enabled(appid, enabled);
+       if (ret < 0) {
+               printf("Failed to get handle\n");
+               return -1;
+       }
+       return 0;
+}
+
+static int __get_app_list(char *pkgid, uid_t uid)
+{
+       pkgmgrinfo_pkginfo_h handle;
+       int ret = -1;
+       char *test_data = "test data";
+       if(uid != GLOBAL_USER)
+               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
+       else
+               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+       if (ret < 0) {
+               printf("Failed to get handle\n");
+               return -1;
+       }
+       if (uid != GLOBAL_USER) {
+               printf("List of Ui-Apps\n\n");
+               ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_UI_APP, app_func, (void *)test_data, uid);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_get_list() failed\n");
+               }
+               printf("List of Svc-Apps\n\n");
+               ret = pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_SVC_APP, app_func, (void *)test_data, uid);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_get_list() failed\n");
+               }
+       } else {
+               printf("List of Ui-Apps\n\n");
+               ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, app_func, (void *)test_data);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_get_list() failed\n");
+               }
+               printf("List of Svc-Apps\n\n");
+               ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, app_func, (void *)test_data);
+               if (ret < 0) {
+                       printf("pkgmgrinfo_appinfo_get_list() failed\n");
+               }
+       }
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       return 0;
+}
+
+static int __get_pkg_info(char *pkgid, uid_t uid)
+{
+       pkgmgrinfo_pkginfo_h handle;
+       int ret = -1;
+
+       printf("Get Pkg Info Called [%s]\n", pkgid);
+       if(uid != GLOBAL_USER)
+               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
+       else
+               ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+       if (ret < 0) {
+               printf("Failed to get handle\n");
+               return -1;
+       }
+
+       __get_pkgmgrinfo_pkginfo(handle, NULL);
+
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       return 0;
+}
+
+static int __get_app_info(char *appid)
+{
+       printf("Get App Info Called [%s]\n", appid);
+       char *exec = NULL;
+       char *app_id = NULL;
+       char *apptype = NULL;
+       char *icon = NULL;
+       char *label = NULL;
+       char *package = NULL;
+       pkgmgrinfo_app_component component = 0;
+       bool nodisplay = 0;
+       bool multiple = 0;
+       bool taskmanage = 0;
+       pkgmgrinfo_app_hwacceleration hwacceleration;
+       pkgmgrinfo_app_screenreader screenreader;
+       bool onboot = 0;
+       bool autorestart = 0;
+       bool enabled = 0;
+       bool preload = 0;
+       pkgmgrinfo_appinfo_h handle;
+       int ret = -1;
+
+       ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, getuid(), &handle);
+       if (ret < 0) {
+               printf("Failed to get handle\n");
+               return -1;
+       }
+
+       ret = pkgmgrinfo_appinfo_get_pkgid(handle, &package);
+       if (ret < 0) {
+               printf("Failed to get package\n");
+       }
+
+       ret = pkgmgrinfo_appinfo_get_appid(handle, &app_id);
+       if (ret < 0) {
+               printf("Failed to get exec\n");
+       }
+
+       ret = pkgmgrinfo_appinfo_get_label(handle, &label);
+       if (ret < 0) {
+               printf("Failed to get label\n");
+       }
+       ret = pkgmgrinfo_appinfo_get_icon(handle, &icon);
+       if (ret < 0) {
+               printf("Failed to get icon\n");
+       }
+
+       ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
+       if (ret < 0) {
+               printf("Failed to get exec\n");
+       }
+       ret = pkgmgrinfo_appinfo_get_component(handle, &component);
+       if (ret < 0) {
+               printf("Failed to get component\n");
+       }
+       ret = pkgmgrinfo_appinfo_get_apptype(handle, &apptype);
+       if (ret < 0) {
+               printf("Failed to get apptype\n");
+       }
+       ret = pkgmgrinfo_appinfo_is_nodisplay(handle, &nodisplay);
+       if (ret < 0) {
+               printf("Failed to get nodisplay\n");
+       }
+       ret = pkgmgrinfo_appinfo_is_multiple(handle, &multiple);
+       if (ret < 0) {
+               printf("Failed to get multiple\n");
+       }
+       ret = pkgmgrinfo_appinfo_is_taskmanage(handle, &taskmanage);
+       if (ret < 0) {
+               printf("Failed to get taskmanage\n");
+       }
+       ret = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacceleration);
+       if (ret < 0) {
+               printf("Failed to get hwacceleration\n");
+       }
+       ret = pkgmgrinfo_appinfo_get_screenreader(handle, &screenreader);
+       if (ret < 0) {
+               printf("Failed to get screenreader\n");
+       }
+       ret = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
+       if (ret < 0) {
+               printf("Failed to get onboot\n");
+       }
+       ret = pkgmgrinfo_appinfo_is_autorestart(handle, &autorestart);
+       if (ret < 0) {
+               printf("Failed to get autorestart\n");
+       }
+       ret = pkgmgrinfo_appinfo_is_enabled(handle, &enabled);
+       if (ret < 0) {
+               printf("Failed to get enabled\n");
+       }
+       ret = pkgmgrinfo_appinfo_is_preload(handle, &preload);
+       if (ret < 0) {
+               printf("Failed to get preload\n");
+       }
+
+       if (app_id)
+               printf("Appid: %s\n", app_id);
+
+       if (package)
+               printf("Package: %s\n", package);
+
+       if (exec)
+               printf("Exec: %s\n", exec);
+       if (apptype)
+               printf("Apptype: %s\n", apptype);
+
+       if (component == PMINFO_UI_APP) {
+               printf("component: uiapp\n");
+
+               if (icon)
+                       printf("Icon: %s\n", icon);
+               if (label)
+                       printf("Label: %s\n", label);
+
+               printf("Nodisplay: %d\n", nodisplay);
+               printf("Multiple: %d\n", multiple);
+               printf("Taskmanage: %d\n", taskmanage);
+               printf("Hw-Acceleration: %d\n", hwacceleration);
+               printf("Screenreader: %d\n", screenreader);
+       } else if (component == PMINFO_SVC_APP) {
+               printf("component: svcapp\n");
+
+               if (icon)
+                       printf("Icon: %s\n", icon);
+               if (label)
+                       printf("Label: %s\n", label);
+
+               printf("Autorestart: %d\n", autorestart);
+               printf("Onboot: %d\n", onboot);
+       } else {
+               printf("Invalid Component Type\n");
+       }
+
+       printf("Enabled: %d\n", enabled);
+       printf("Preload: %d\n", preload);
+
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       return 0;
+
+}
+
+static int __check_manifest_validation(char *manifest)
+{
+       int ret = 0;
+       if (manifest == NULL) {
+               printf("Manifest file is NULL\n");
+               return -1;
+       }
+       ret = pkgmgr_parser_check_manifest_validation(manifest);
+       if (ret < 0) {
+               printf("check manifest validation failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int ret = 0;
+       long starttime;
+       long endtime;
+       struct timeval tv;
+
+       gettimeofday(&tv, NULL);
+       starttime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
+
+       if (argc == 2) {
+               if (strcmp(argv[1], "--listpkg") == 0) {
+                       ret = __get_pkg_list(getuid());
+                       if (ret == -1) {
+                               printf("get pkg list failed\n");
+                               goto end;
+                       } else {
+                               goto end;
+                       }
+               } else if (strcmp(argv[1], "--app-flt") == 0) {
+                       ret = __add_app_filter(getuid());
+                       if (ret == -1) {
+                               printf("Adding app filter failed\n");
+                               goto end;
+                       } else {
+                               goto end;
+                       }
+               } else if (strcmp(argv[1], "--pkg-flt") == 0) {
+                       ret = __add_pkg_filter(getuid());
+                       if (ret == -1) {
+                               printf("Adding pkg filter failed\n");
+                               goto end;
+                       } else {
+                               goto end;
+                       }
+               } else if (strcmp(argv[1], "--metadata-flt") == 0) {
+                       ret = __add_metadata_filter();
+                       if (ret == -1) {
+                               printf("Adding pkg filter failed\n");
+                               goto end;
+                       } else {
+                               goto end;
+                       }
+               } else if (strcmp(argv[1], "--listapp") == 0) {
+                       ret = __get_installed_app_list(getuid());
+                       if (ret == -1) {
+                               printf("get installed app list failed\n");
+                               goto end;
+                       } else {
+                               goto end;
+                       }
+               } else {
+                       __print_usage();
+                       ret = -1;
+                       goto end;
+               }
+       }else if (argc == 4) {
+               if (strcmp(argv[1], "--setappenabled") == 0) {
+                       ret = __set_app_enabled(argv[2], (strcmp(argv[3], "0")==0)?false:true);
+                       if (ret == -1) {
+                               printf("set app enabled failed\n");
+                               goto end;
+                       }
+                       goto end;
+               } else if(strcmp(argv[1], "--setpkgenabled") == 0) {
+                       ret = __set_app_enabled(argv[2], (strcmp(argv[3], "0")==0)?false:true);
+                       if (ret == -1) {
+                               printf("set pkg enabled failed\n");
+                               goto end;
+                       }
+                       goto end;
+               } else if (strcmp(argv[1], "--cmp-pkgcert") == 0) {
+                       ret = __compare_pkg_certinfo_from_db(argv[2], argv[3], getuid());
+                       if (ret == -1) {
+                               printf("compare certinfo from db failed\n");
+                               goto end;
+                       }
+                       goto end;
+               } else if (strcmp(argv[1], "--cmp-appcert") == 0) {
+                       ret = __compare_app_certinfo_from_db(argv[2], argv[3], getuid());
+                       if (ret == -1) {
+                               printf("compare certinfo from db failed\n");
+                               goto end;
+                       }
+                       goto end;
+               } else if (strcmp(argv[1], "--arg-flt") == 0) {
+                       ret = __add_arg_filter(argv[2], argv[3], getuid());
+                       if (ret == -1) {
+                               printf("compare certinfo from db failed\n");
+                               goto end;
+                       }
+                       goto end;
+               } else {
+                       __print_usage();
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       if (argc != 3) {
+               __print_usage();
+               ret = -1;
+               goto end;
+       }
+       if (!argv[1] || !argv[2]) {
+                       __print_usage();
+                       ret = -1;
+                       goto end;
+       }
+
+       if (strcmp(argv[1], "--pkg") == 0) {
+               ret = __get_pkg_info(argv[2], getuid());
+               if (ret == -1) {
+                       printf("get pkg info failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--app") == 0) {
+               ret = __get_app_info(argv[2]);
+               if (ret == -1) {
+                       printf("get app info failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--list") == 0) {
+               ret = __get_app_list(argv[2], getuid());
+               if (ret == -1) {
+                       printf("get app list failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--imd") == 0) {
+               ret = __insert_manifest_in_db(argv[2], getuid());
+               if (ret == -1) {
+                       printf("insert in db failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--fota") == 0) {
+               ret = __fota_insert_manifest_in_db(argv[2], getuid());
+               if (ret == -1) {
+                       printf("insert in db failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--rmd") == 0) {
+               ret = __remove_manifest_from_db(argv[2], getuid());
+               if (ret == -1) {
+                       printf("remove from db failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--setcert") == 0) {
+               ret = __set_certinfo_in_db(argv[2], getuid());
+               if (ret == -1) {
+                       printf("set certinfo in db failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--getcert") == 0) {
+               ret = __get_certinfo_from_db(argv[2], getuid());
+               if (ret == -1) {
+                       printf("get certinfo from db failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--delcert") == 0) {
+               ret = __del_certinfo_from_db(argv[2]);
+               if (ret == -1) {
+                       printf("del certinfo from db failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--check") == 0) {
+               ret = __check_manifest_validation(argv[2]);
+               if (ret == -1) {
+                       printf("check manifest failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--category") == 0) {
+               ret = __get_app_category_list(argv[2]);
+               if (ret == -1) {
+                       printf("get app category list failed\n");
+                       goto end;
+               }
+       } else if (strcmp(argv[1], "--metadata") == 0) {
+               ret = __get_app_metadata_list(argv[2]);
+               if (ret == -1) {
+                       printf("get app metadata list failed\n");
+                       goto end;
+               }
+       }  else if (strcmp(argv[1], "--appcontrol") == 0) {
+               ret = __get_app_control_list(argv[2]);
+               if (ret == -1) {
+                       printf("get app control list failed\n");
+                       goto end;
+               }
+       } else
+               __print_usage();
+
+end:
+
+       gettimeofday(&tv, NULL);
+       endtime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
+
+       printf("spend time for pkginfo is [%d]ms\n", (int)(endtime - starttime));
+
+       return ret;
+}
diff --git a/src/pkg_initdb.c b/src/pkg_initdb.c
new file mode 100644 (file)
index 0000000..91e32e9
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * slp-pkgmgr
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
+ * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <pkgmgr_parser.h>
+#include <pkgmgr_parser_db.h>
+#include <pkgmgr-info.h>
+
+#include <sys/smack.h>
+/* For multi-user support */
+#include <tzplatform_config.h>
+
+#define OWNER_ROOT 0
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
+#define BUFSZE 1024
+
+#ifdef _E
+#undef _E
+#endif
+#define _E(fmt, arg...) fprintf(stderr, "[PKG_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
+
+#ifdef _D
+#undef _D
+#endif
+#define _D(fmt, arg...) fprintf(stderr, "[PKG_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
+
+#define PKGINFO_CMD "/usr/bin/pkginfo --imd"
+#define PKGINSTALLUG_CMD "/usr/bin/pkg_install_ug"
+#define PKGPRIVILEGE_CMD "/usr/bin/pkg_privilege -i"
+
+static int _is_global(uid_t uid)
+{
+       return (uid == OWNER_ROOT || uid == GLOBAL_USER) ? 1 : 0;
+}
+
+static int _initdb_load_directory(uid_t uid, const char *directory)
+{
+       DIR *dir;
+       struct dirent entry, *result;
+       int ret;
+       char buf[BUFSZE];
+       char buf2[BUFSZE];
+
+       dir = opendir(directory);
+       if (!dir) {
+               _E("Failed to access the [%s] because %s", directory,
+                               strerror_r(errno, buf, sizeof(buf)));
+               return -1;
+       }
+
+       _D("Loading manifest files from %s", directory);
+
+       for (ret = readdir_r(dir, &entry, &result);
+                       ret == 0 && result != NULL;
+                       ret = readdir_r(dir, &entry, &result)) {
+               if (entry.d_name[0] == '.')
+                       continue;
+
+               snprintf(buf, sizeof(buf), "%s/%s", directory, entry.d_name);
+               _D("manifest file %s", buf);
+
+               ret = pkgmgr_parser_check_manifest_validation(buf);
+               if (ret < 0) {
+                       _E("check manifest validation failed code[%d] %s",
+                                       ret, buf);
+                       continue;
+               }
+
+               if (setresuid(uid, uid, OWNER_ROOT)) {
+                       _E("Failed to setresuid: %s",
+                                       strerror_r(errno, buf, sizeof(buf)));
+                       closedir(dir);
+                       return -1;
+               }
+               snprintf(buf2, sizeof(buf2), "%s %s", PKGINFO_CMD, buf);
+               if (system(buf2))
+                       _E("[%s %s] returns error", PKGINFO_CMD, buf);
+               snprintf(buf2, sizeof(buf2), "%s %s", PKGINSTALLUG_CMD, buf);
+               if (system(buf2))
+                       _E("[%s %s] returns error", PKGINSTALLUG_CMD, buf);
+               snprintf(buf2, sizeof(buf2), "%s %s", PKGPRIVILEGE_CMD, buf);
+               if (system(buf2))
+                       _E("[%s %s] returns error", PKGPRIVILEGE_CMD, buf);
+               if (setresuid(OWNER_ROOT, OWNER_ROOT, OWNER_ROOT)) {
+                       _E("Failed to setresuid: %s",
+                                       strerror_r(errno, buf, sizeof(buf)));
+                       closedir(dir);
+                       return -1;
+               }
+       }
+
+       closedir(dir);
+
+       return 0;
+}
+
+static int _is_authorized()
+{
+       /* pkg_init db should be called by as root privilege. */
+       uid_t uid = getuid();
+
+       if ((uid_t) OWNER_ROOT == uid)
+               return 1;
+       else
+               return 0;
+}
+
+static void _remove_old_dbs(uid)
+{
+       const char *info_db_path;
+       const char *info_db_journal_path;
+       const char *cert_db_path;
+       const char *cert_db_journal_path;
+
+       if (!_is_global(uid))
+               tzplatform_set_user(uid);
+
+       info_db_path = tzplatform_mkpath(
+                       _is_global(uid) ? TZ_SYS_DB : TZ_USER_DB,
+                       ".pkgmgr_parser.db");
+       info_db_journal_path = tzplatform_mkpath(
+                       _is_global(uid) ? TZ_SYS_DB : TZ_USER_DB,
+                       ".pkgmgr_parser.db-journal");
+       cert_db_path = tzplatform_mkpath(
+                       _is_global(uid) ? TZ_SYS_DB : TZ_USER_DB,
+                       ".pkgmgr_cert.db");
+       cert_db_journal_path = tzplatform_mkpath(
+                       _is_global(uid) ? TZ_SYS_DB : TZ_USER_DB,
+                       ".pkgmgr_cert.db-journal");
+
+       if (remove(info_db_path))
+               _E(" %s is not removed", info_db_path);
+       if (remove(info_db_journal_path))
+               _E(" %s is not removed", info_db_journal_path);
+       if (remove(cert_db_path))
+               _E(" %s is not removed", cert_db_path);
+       if (remove(cert_db_journal_path))
+               _E(" %s is not removed", cert_db_journal_path);
+
+       tzplatform_reset_user();
+}
+
+int main(int argc, char *argv[])
+{
+       int ret;
+       const char *dir;
+       uid_t uid = 0;
+
+       if (!_is_authorized()) {
+               _E("You are not an authorized user!");
+               return -1;
+       }
+
+       if (argc > 1)
+               uid = (uid_t)atoi(argv[1]);
+
+       _remove_old_dbs(uid);
+
+       ret = pkgmgr_parser_create_and_initialize_db(uid);
+       if (ret < 0) {
+               _E("cannot create db");
+               return -1;
+       }
+
+       if (!_is_global(uid))
+               tzplatform_set_user(uid);
+       dir = tzplatform_getenv(
+                       _is_global(uid) ? TZ_SYS_RW_PACKAGES : TZ_USER_PACKAGES);
+       tzplatform_reset_user();
+
+       return _initdb_load_directory(uid, dir);
+}
diff --git a/src/pkg_install_ug.c b/src/pkg_install_ug.c
new file mode 100644 (file)
index 0000000..7b3bb48
--- /dev/null
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <linux/limits.h>
+
+#include <glib.h>
+
+#include <pkgmgr_parser.h>
+#include <tzplatform_config.h>
+
+#define OWNER_ROOT 0
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
+#define UG_CLIENT tzplatform_mkpath(TZ_SYS_BIN, "ug-client")
+
+static int _check_bin_directory(const char *pkgid)
+{
+       const char *path;
+       char buf[PATH_MAX];
+
+       path = tzplatform_mkpath(TZ_SYS_RO_APP, pkgid);
+       snprintf(buf, sizeof(buf), "%s/bin", path);
+
+       if (access(buf, F_OK) == 0)
+               return 0;
+
+       if (mkdir(buf, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH)) {
+               printf("create bin directory(%s) failed: %s\n", buf,
+                               strerror(errno));
+               return -1;
+       }
+
+       return 0;
+}
+
+static void _application_cb(gpointer data, gpointer user_data)
+{
+       int ret;
+       application_x *app = (application_x *)data;
+       package_x *pkg = (package_x *)user_data;
+
+       if (app->exec == NULL || app->ui_gadget == NULL ||
+                       strcasecmp(app->ui_gadget, "true") != 0)
+               return;
+
+       if (_check_bin_directory(pkg->package))
+               return;
+
+       ret = symlink(UG_CLIENT, app->exec);
+       if (ret != 0)
+               printf("failed to install ug %s: %s\n", app->exec,
+                               strerror(errno));
+}
+
+static int _install_ug(char *manifest)
+{
+       package_x *pkg;
+
+       pkg = pkgmgr_parser_process_manifest_xml(manifest);
+       if (pkg == NULL) {
+               printf("Parse manifest failed\n");
+               return -1;
+       }
+
+       g_list_foreach(pkg->application, _application_cb, pkg);
+       pkgmgr_parser_free_manifest_xml(pkg);
+
+       return 0;
+}
+
+static void _print_usage(const char *cmd)
+{
+       printf("usage: %s <manifest>\n", cmd);
+}
+
+int main(int argc, char *argv[])
+{
+       if (getuid() != OWNER_ROOT && getuid() != GLOBAL_USER) {
+               printf("Only root or tizenglobalapp user is allowed\n");
+               return -1;
+       }
+
+       if (argc < 2) {
+               _print_usage(argv[0]);
+               return -1;
+       }
+
+       return _install_ug(argv[1]);
+}
diff --git a/src/pkg_privilege.c b/src/pkg_privilege.c
new file mode 100644 (file)
index 0000000..5eb411c
--- /dev/null
@@ -0,0 +1,220 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <linux/limits.h>
+
+#include <glib.h>
+
+#include <tzplatform_config.h>
+#include <security-manager.h>
+#include <pkgmgr_parser.h>
+
+#define OWNER_ROOT 0
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
+
+static const char *_get_pkg_root_path(const char *pkgid)
+{
+       const char *path;
+       uid_t uid = getuid();
+
+       tzplatform_set_user(uid);
+       path = tzplatform_mkpath((uid == OWNER_ROOT || uid == GLOBAL_USER) ?
+                       TZ_SYS_RO_APP : TZ_USER_APP, pkgid);
+       tzplatform_reset_user();
+
+       return path;
+}
+
+struct path_type {
+       const char *path;
+       enum app_install_path_type type;
+};
+
+struct path_type path_type_map[] = {
+       {"/", SECURITY_MANAGER_PATH_PUBLIC_RO},
+       {"/bin", SECURITY_MANAGER_PATH_RO},
+       {"/data", SECURITY_MANAGER_PATH_RW},
+       {"/cache", SECURITY_MANAGER_PATH_RW},
+       {"/lib", SECURITY_MANAGER_PATH_RO},
+       {"/res", SECURITY_MANAGER_PATH_RO},
+       {"/shared", SECURITY_MANAGER_PATH_PUBLIC_RO},
+       {NULL, SECURITY_MANAGER_ENUM_END}
+};
+
+static app_inst_req *_prepare_request(const char *pkgid, const char *appid)
+{
+       int ret;
+       app_inst_req *req;
+       const char *root_path;
+       char buf[PATH_MAX];
+       int i;
+
+       if (security_manager_app_inst_req_new(&req)) {
+               printf("security_manager_app_inst_req_new failed\n");
+               return NULL;
+       }
+
+       ret = security_manager_app_inst_req_set_pkg_id(req, pkgid);
+       if (ret != SECURITY_MANAGER_SUCCESS) {
+               printf("set pkgid failed: %d\n", ret);
+               security_manager_app_inst_req_free(req);
+               return NULL;
+       }
+
+       ret = security_manager_app_inst_req_set_app_id(req, appid);
+       if (ret != SECURITY_MANAGER_SUCCESS) {
+               printf("set appid failed: %d\n", ret);
+               security_manager_app_inst_req_free(req);
+               return NULL;
+       }
+
+       root_path = _get_pkg_root_path(pkgid);
+       /* TODO: should be fixed */
+       if (access(root_path, F_OK) == -1) {
+               printf("cannot find %s, but the smack rule for %s "
+                               "will be installed\n", root_path, appid);
+               return req;
+       }
+
+       for (i = 0; path_type_map[i].path; i++) {
+               snprintf(buf, sizeof(buf), "%s%s", root_path,
+                               path_type_map[i].path);
+               if (access(buf, F_OK) == -1)
+                       continue;
+               ret = security_manager_app_inst_req_add_path(req, buf,
+                               path_type_map[i].type);
+               if (ret != SECURITY_MANAGER_SUCCESS) {
+                       printf("set path failed: %d\n", ret);
+                       security_manager_app_inst_req_free(req);
+                       return NULL;
+               }
+       }
+
+       return req;
+}
+
+static void _insert_privilege_cb(gpointer data, gpointer user_data)
+{
+       const char *privilege = (const char *)data;
+       app_inst_req *req = (app_inst_req *)user_data;
+
+       security_manager_app_inst_req_add_privilege(req, privilege);
+}
+
+/* NOTE: We cannot use cert-svc api which checks signature level in this tool,
+ * because cert-svc does not provide c apis in Tizen 3.0.
+ * So we set default privilege as platform level temporarily.
+ */
+#define DEFAULT_PRIVILEGE_PUBLIC "http://tizen.org/privilege/internal/default/public"
+#define DEFAULT_PRIVILEGE_PARTNER "http://tizen.org/privilege/internal/default/partner"
+#define DEFAULT_PRIVILEGE_PLATFORM "http://tizen.org/privilege/internal/default/platform"
+static void _insert_application_cb(gpointer data, gpointer user_data)
+{
+       int ret;
+       app_inst_req *req;
+       application_x *app = (application_x *)data;
+       package_x *pkg = (package_x *)user_data;
+
+       req = _prepare_request(pkg->package, app->appid);
+       if (req == NULL) {
+               printf("out of memory\n");
+               return;
+       }
+
+       if (getuid() == OWNER_ROOT) {
+               security_manager_app_inst_req_add_privilege(req,
+                               DEFAULT_PRIVILEGE_PUBLIC);
+               security_manager_app_inst_req_add_privilege(req,
+                               DEFAULT_PRIVILEGE_PARTNER);
+               security_manager_app_inst_req_add_privilege(req,
+                               DEFAULT_PRIVILEGE_PLATFORM);
+       }
+
+       g_list_foreach(pkg->privileges, _insert_privilege_cb, (gpointer)req);
+
+       ret = security_manager_app_install(req);
+       if (ret != SECURITY_MANAGER_SUCCESS)
+               printf("app install failed: %d\n", ret);
+       security_manager_app_inst_req_free(req);
+}
+
+static int _insert_privilege(char *manifest)
+{
+       package_x *pkg;
+
+       pkg = pkgmgr_parser_process_manifest_xml(manifest);
+       if (pkg == NULL) {
+               printf("Parse manifest failed\n");
+               return -1;
+       }
+
+       g_list_foreach(pkg->application, _insert_application_cb, (gpointer)pkg);
+       pkgmgr_parser_free_manifest_xml(pkg);
+
+       return 0;
+}
+
+static void _remove_application_cb(gpointer data, gpointer user_data)
+{
+       int ret;
+       app_inst_req *req;
+       application_x *app = (application_x *)data;
+       package_x *pkg = (package_x *)user_data;
+
+       req = _prepare_request(pkg->package, app->appid);
+       if (req == NULL) {
+               printf("out of memory\n");
+               return;
+       }
+
+       ret = security_manager_app_uninstall(req);
+       if (ret != SECURITY_MANAGER_SUCCESS)
+               printf("app uninstall failed: %d\n", ret);
+
+       security_manager_app_inst_req_free(req);
+}
+
+static int _remove_privilege(char *manifest)
+{
+       package_x *pkg;
+
+       pkg = pkgmgr_parser_process_manifest_xml(manifest);
+       if (pkg == NULL) {
+               printf("Parse manifest failed\n");
+               return -1;
+       }
+
+       g_list_foreach(pkg->application, _remove_application_cb, (gpointer)pkg);
+       pkgmgr_parser_free_manifest_xml(pkg);
+
+       return 0;
+}
+
+static void _print_usage(const char *cmd)
+{
+       printf("usage: %s <option> <manifest>\n"
+              "   -i \t\t install privilege\n"
+              "   -u \t\t uninstall privilege\n", cmd);
+}
+
+int main(int argc, char **argv)
+{
+       int ret;
+
+       if (argc < 3) {
+               _print_usage(argv[0]);
+               return -1;
+       }
+
+       if (!strcmp(argv[1], "-i")) {
+               ret = _insert_privilege(argv[2]);
+       } else if (!strcmp(argv[1], "-u")) {
+               ret = _remove_privilege(argv[2]);
+       } else {
+               _print_usage(argv[0]);
+               ret = -1;
+       }
+
+       return ret;
+}