Add pkg_initdb_user 59/28059/3
authorSabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
Mon, 22 Sep 2014 15:28:01 +0000 (17:28 +0200)
committerSabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
Wed, 1 Oct 2014 09:09:18 +0000 (11:09 +0200)
Change-Id: Ia5d480922c86e5b9d926b7ec63d411c5baed9aa3
Signed-off-by: Sabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
packaging/pkgmgr.spec
tool/CMakeLists.txt
tool/pkg_initdb_user.c [new file with mode: 0755]

index e51868bac576fe88b28960306c31e147c19facb8..973a4419d1bd051d1e23d917429dd6c5c27a832c 100644 (file)
@@ -143,6 +143,7 @@ update-mime-database /usr/share/mime
 %{_sysconfdir}/opt/upgrade/pkgmgr.patch.sh
 %{_bindir}/pkgcmd
 %attr(06755,root,root) %{_bindir}/pkg_initdb
+%attr(755,root,root) %{_bindir}/pkg_initdb_user
 %{_bindir}/pkg_getsize
 %{_bindir}/pkginfo
 %{_bindir}/pkgmgr-install
index 0eeb65b73c9374359d177c10162bd1aa9853f088..1a4a4c26ddf8323cab2221ec954456a3fb70e60c 100755 (executable)
@@ -47,6 +47,10 @@ add_executable(pkg_initdb pkg_initdb.c)
 target_link_libraries(pkg_initdb ${pkgs_initdb_LDFLAGS})
 INSTALL(TARGETS pkg_initdb DESTINATION bin)
 
+add_executable(pkg_initdb_user pkg_initdb_user.c)
+target_link_libraries(pkg_initdb_user ${pkgs_initdb_LDFLAGS})
+INSTALL(TARGETS pkg_initdb_user DESTINATION bin)
+
 add_executable(pkgmgr-install pkgmgr-install.c)
 target_link_libraries(pkgmgr-install pkgmgr-client pkgmgr-info ${toolpkgs_LDFLAGS})
 install(TARGETS pkgmgr-install DESTINATION bin)
diff --git a/tool/pkg_initdb_user.c b/tool/pkg_initdb_user.c
new file mode 100755 (executable)
index 0000000..040b072
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * 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-info.h>
+
+#include <sys/smack.h>
+/* For multi-user support */
+#include <tzplatform_config.h>
+
+#define OWNER_ROOT 0
+#define GROUP_MENU 6010
+#define BUFSZE 1024
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
+
+
+#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 SET_DEFAULT_LABEL(x) \
+       if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
+    else  _D("chsmack -a \"*\" %s", x)
+         
+static int initdb_user_count_package(void)
+{
+       int total = 0;
+
+       return total;
+}
+
+
+char* _manifest_to_package(const char* manifest)
+{
+       char *package;
+
+       if(manifest == NULL)
+               return NULL;
+
+       package = strdup(manifest);
+       if(package == NULL)
+               return NULL;
+
+
+       if (!strstr(package, ".xml")) {
+               _E("%s is not a manifest file", manifest);
+               free(package);
+               return NULL;
+       }
+
+       return package;
+}
+
+
+
+int initdb_user_load_directory(const char *directory)
+{
+       DIR *dir;
+       struct dirent entry, *result;
+       int ret;
+       char buf[BUFSZE];
+
+       // desktop file
+       dir = opendir(directory);
+       if (!dir) {
+               if (strerror_r(errno, buf, sizeof(buf)) == 0)
+                       _E("Failed to access the [%s] because %s\n", directory, buf);
+               return -1;
+       }
+
+       _D("Loading manifest files from %s\n", directory);
+
+       for (ret = readdir_r(dir, &entry, &result);
+                       ret == 0 && result != NULL;
+                       ret = readdir_r(dir, &entry, &result)) {
+               char *manifest;
+
+               if (entry.d_name[0] == '.') continue;
+
+               manifest = _manifest_to_package(entry.d_name);
+               if (!manifest) {
+                       _E("Failed to convert file to package[%s]\n", entry.d_name);
+                       continue;
+               }
+
+               snprintf(buf, sizeof(buf), "%s/%s", directory, manifest);
+
+               fprintf(stderr, "pkg_initdb : manifest file %s\n", buf);
+
+               ret = pkgmgr_parser_check_manifest_validation(buf);
+               if (ret < 0) {
+                       _E("check manifest validation failed code[%d] %s\n", ret, buf);
+                       fprintf(stderr, "check manifest validation failed code[%d] %s\n", ret, buf);
+                       free(manifest);
+                       continue;
+               }
+
+
+               /*temporarily fixed due to glib abort */
+               // pkgmgr_parser_parse_manifest_for_installation(buf, NULL);
+
+               char buf2[BUFSZE];
+               snprintf(buf2, sizeof(buf2), "/usr/bin/pkginfo --imd %s", buf);
+               system(buf2);
+
+               free(manifest);
+       }
+
+       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;
+}
+
+
+int main(int argc, char *argv[])
+{
+       int ret;
+       char *journal = NULL;
+
+       if (!__is_authorized()) {
+               _E("You are not an authorized user!\n");
+               return -1;
+       } else {
+               if(remove(getUserPkgParserDBPathUID(getuid())))
+                       _E(" %s is not removed", getUserPkgParserDBPathUID(getuid()));
+               asprintf(&journal, "%s-journal", getUserPkgParserDBPathUID(getuid()));
+               if(remove(journal))
+                       _E(" %s is not removed", journal);
+       }
+
+       /* This is for AIL initializing */
+       ret = setenv("INITDB", "1", 1);
+       _D("INITDB : %d", ret);
+
+       ret = initdb_user_count_package();
+       if (ret > 0) {
+               _D("Some Packages in the Package Info DB.");
+               return 0;
+       }
+       ret = initdb_user_load_directory(getUserManifestPath(getuid()));
+       if (ret == -1) {
+               _E("cannot load opt manifest directory.");
+       }
+
+       return 0;
+}