Add ail_initdb_user & ail_get_app_DB_journal() 57/28057/3
authorSabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
Mon, 22 Sep 2014 15:27:21 +0000 (17:27 +0200)
committerSabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
Tue, 30 Sep 2014 11:27:48 +0000 (13:27 +0200)
Change-Id: Id4521033a5f64b0ea4d7e72abdbfd51dc7125618
Signed-off-by: Sabera Djelti (sdi2) <sabera.djelti@open.eurogiciel.org>
packaging/ail.spec
src/ail_db.c
src/ail_db.h
tool/CMakeLists.txt
tool/src/initdb_user.c [new file with mode: 0755]

index c20ebfd..6bb442f 100755 (executable)
@@ -101,6 +101,7 @@ fi
 %manifest %{name}.manifest
 %license LICENSE
 %attr(06775,root,root) %{_bindir}/ail_initdb
+%attr(0775,root,root) %{_bindir}/ail_initdb_user
 %{_bindir}/ail_fota
 %{_bindir}/ail_desktop
 %{_bindir}/ail_filter
index e878317..c47aed9 100755 (executable)
@@ -160,10 +160,19 @@ char* ail_get_icon_path(uid_t uid)
        return result;
 }
 
-static char* ail_get_app_DB(uid_t uid)
+char* ail_get_app_DB_journal(uid_t uid)
+{
+
+       char *app_path = ail_get_app_DB(uid);
+       char* result = NULL;
+
+       asprintf(&result, "%s-journal", app_path);
+       return  result;
+}
+
+char* ail_get_app_DB(uid_t uid)
 {
        char *result = NULL;
-       char *journal = NULL;
        struct group *grpinfo = NULL;
        char *dir = NULL;
        struct passwd *userinfo = getpwuid(uid);
@@ -188,10 +197,8 @@ static char* ail_get_app_DB(uid_t uid)
                        return NULL;
                }
                asprintf(&result, "%s/.applications/dbspace/.app_info.db", userinfo->pw_dir);
-               asprintf(&journal, "%s/.applications/dbspace/.app_info.db-journal", userinfo->pw_dir);
        } else {
                        result = strdup(APP_INFO_DB_FILE);
-                       journal = strdup(APP_INFO_DB_FILE_JOURNAL);
        }
        char *temp = strdup(result);
        dir = strrchr(temp, '/');
@@ -333,7 +340,7 @@ ail_error_e db_open(db_open_mode mode, uid_t uid)
                        }
                        if(AIL_ERROR_OK != ail_db_change_perm(ail_get_app_DB(uid), uid)) {
                                _E("Failed to change permission\n");
-               }
+                       }
                } else {
                        dbInit = NULL;
                        _E("Failed to create table %s\n", ail_get_app_DB(uid));
index b551d20..a04a5e0 100755 (executable)
@@ -37,6 +37,8 @@ typedef enum {
 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
 
 char* ail_get_icon_path(uid_t uid);
+char* ail_get_app_DB_journal(uid_t uid);
+char* ail_get_app_DB(uid_t uid);
 char* al_get_desktop_path(uid_t uid);
 ail_error_e db_open(db_open_mode mode, uid_t uid);
 ail_error_e db_prepare(const char *query, sqlite3_stmt **stmt);
index a8128fa..b174b89 100755 (executable)
@@ -3,6 +3,9 @@
 SET(INITDB ail_initdb)
 SET(SRCS src/initdb.c ../src/ail_db.c)
 
+SET(INITDB_USER ail_initdb_user)
+SET(USERSRCS src/initdb_user.c  ../src/ail_db.c)
+
 SET(FOTA ail_fota)
 SET(FOTASRCS src/ail_fota.c)
 
@@ -31,6 +34,13 @@ SET_TARGET_PROPERTIES(${INITDB} PROPERTIES SKIP_BUILD_RPATH true)
 
 INSTALL(TARGETS ${INITDB} DESTINATION ${BINDIR})
 
+ADD_EXECUTABLE(${INITDB_USER} ${USERSRCS})
+TARGET_LINK_LIBRARIES(${INITDB_USER} ${LIBNAME} ${INITDB_PKGS_LIBRARIES})
+SET_TARGET_PROPERTIES(${INITDB_USER} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}")
+SET_TARGET_PROPERTIES(${INITDB_USER} PROPERTIES SKIP_BUILD_RPATH true)
+
+INSTALL(TARGETS ${INITDB_USER} DESTINATION ${BINDIR})
+
 ADD_EXECUTABLE(${FOTA} ${FOTASRCS})
 TARGET_LINK_LIBRARIES(${FOTA} ${LIBNAME} ${INITDB_PKGS_LIBRARIES})
 SET_TARGET_PROPERTIES(${FOTA} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}")
diff --git a/tool/src/initdb_user.c b/tool/src/initdb_user.c
new file mode 100755 (executable)
index 0000000..d6b102a
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * ail
+ *
+ * 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>
+ *
+ * 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.
+ *
+ */
+
+
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <sys/smack.h>
+
+#include "ail.h"
+#include "ail_db.h"
+#include "ail_private.h"
+
+
+#ifdef _E
+#undef _E
+#endif
+#define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
+
+#ifdef _D
+#undef _D
+#endif
+#define _D(fmt, arg...) fprintf(stderr, "[AIL_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_app(void)
+{
+       ail_filter_h filter;
+       ail_error_e ret;
+       int total = 0;
+
+       ret = ail_filter_new(&filter);
+       if (ret != AIL_ERROR_OK) {
+               return -1;
+       }
+
+       ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
+       if (ret != AIL_ERROR_OK) {
+               ail_filter_destroy(filter);
+               return -1;
+       }
+       ret = ail_filter_count_usr_appinfo(filter, &total, getuid());
+       if (ret != AIL_ERROR_OK) {
+               ail_filter_destroy(filter);
+               return -1;
+       }
+
+       ail_filter_destroy(filter);
+
+       return total;
+}
+
+
+
+char* _desktop_to_package(const char* desktop)
+{
+       char *package, *tmp;
+
+       retv_if(!desktop, NULL);
+
+       package = strdup(desktop);
+       retv_if(!package, NULL);
+
+       tmp = strrchr(package, '.');
+       if(tmp == NULL) {
+               _E("[%s] is not a desktop file", package);
+               free(package);
+               return NULL;
+       }
+
+       if (strcmp(tmp, ".desktop")) {
+               _E("%s is not a desktop file", desktop);
+               free(package);
+               return NULL;
+       }
+
+       *tmp = '\0';
+
+       return package;
+}
+
+
+
+int initdb_user_load_directory(const char *directory)
+{
+       DIR *dir;
+       struct dirent entry, *result;
+       int len, ret;
+       char buf[BUFSZE];
+       int total_cnt = 0;
+       int ok_cnt = 0;
+
+       // 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 AIL_ERROR_FAIL;
+       }
+
+       len = strlen(directory) + 1;
+       _D("Loading desktop files from %s", directory);
+
+       for (ret = readdir_r(dir, &entry, &result);
+                       ret == 0 && result != NULL;
+                       ret = readdir_r(dir, &entry, &result)) {
+               char *package;
+
+               if (entry.d_name[0] == '.') continue;
+               total_cnt++;
+               package = _desktop_to_package(entry.d_name);
+               if (!package) {
+                       _E("Failed to convert file to package[%s]", entry.d_name);
+                       continue;
+               }
+
+               if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK) {
+                       _E("Failed to add a package[%s]", package);
+               } else {
+                       ok_cnt++;
+               }
+               free(package);
+       }
+
+       _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
+       closedir(dir);
+
+       return AIL_ERROR_OK;
+}
+
+static int __is_authorized()
+{
+       /* ail_init db should be called by an user. */
+
+       uid_t uid = getuid();
+       if ((uid_t) OWNER_ROOT != uid)
+               return 1;
+       else
+               return 0;
+}
+
+
+int main(int argc, char *argv[])
+{
+       int ret;
+
+       if (!__is_authorized()) {
+               fprintf(stderr, "You are not an authorized user!\n");
+               _D("You are root user! Please switch to a regular user\n");
+       }
+       else {
+               if(remove(ail_get_app_DB(getuid())))
+                       _E(" %s is not removed", ail_get_app_DB(getuid()));
+               if(remove(ail_get_app_DB_journal(getuid())))
+                       _E(" %s is not removed", ail_get_app_DB_journal(getuid()));
+       }
+       ret = setenv("AIL_INITDB", "1", 1);
+       _D("AIL_INITDB : %d", ret);
+       ret = initdb_user_count_app();
+       if (ret > 0) {
+               _D("Some Apps in the App Info DB.");
+       }
+
+       ret = initdb_user_load_directory(al_get_desktop_path(getuid()));
+       if (ret == AIL_ERROR_FAIL) {
+               _E("cannot load usr desktop directory.");
+       }
+
+       return AIL_ERROR_OK;
+}
+
+
+
+// END