Fix Prevent and remove compile warning messages
[platform/core/appfw/ail.git] / tool / src / initdb_user.c
index d6b102a..5de4b8c 100755 (executable)
 #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);
+#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 _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)
+       do { \
+               if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
+                       _E("failed chsmack -a \"*\" %s", x); \
+               else \
+                       _D("chsmack -a \"*\" %s", x); \
+       } while (0)
 
 static int initdb_user_count_app(void)
 {
@@ -56,9 +59,8 @@ static int initdb_user_count_app(void)
        int total = 0;
 
        ret = ail_filter_new(&filter);
-       if (ret != AIL_ERROR_OK) {
+       if (ret != AIL_ERROR_OK)
                return -1;
-       }
 
        ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
        if (ret != AIL_ERROR_OK) {
@@ -76,9 +78,7 @@ static int initdb_user_count_app(void)
        return total;
 }
 
-
-
-char* _desktop_to_package(const char* desktop)
+char *_desktop_to_package(const char* desktop)
 {
        char *package, *tmp;
 
@@ -88,7 +88,7 @@ char* _desktop_to_package(const char* desktop)
        retv_if(!package, NULL);
 
        tmp = strrchr(package, '.');
-       if(tmp == NULL) {
+       if (tmp == NULL) {
                _E("[%s] is not a desktop file", package);
                free(package);
                return NULL;
@@ -105,18 +105,16 @@ char* _desktop_to_package(const char* desktop)
        return package;
 }
 
-
-
 int initdb_user_load_directory(const char *directory)
 {
        DIR *dir;
        struct dirent entry, *result;
-       int len, ret;
+       int ret;
        char buf[BUFSZE];
        int total_cnt = 0;
        int ok_cnt = 0;
 
-       // desktop file
+       /* desktop file */
        dir = opendir(directory);
        if (!dir) {
                if (strerror_r(errno, buf, sizeof(buf)) == 0)
@@ -124,7 +122,6 @@ int initdb_user_load_directory(const char *directory)
                return AIL_ERROR_FAIL;
        }
 
-       len = strlen(directory) + 1;
        _D("Loading desktop files from %s", directory);
 
        for (ret = readdir_r(dir, &entry, &result);
@@ -140,11 +137,11 @@ int initdb_user_load_directory(const char *directory)
                        continue;
                }
 
-               if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK) {
+               if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK)
                        _E("Failed to add a package[%s]", package);
-               } else {
+               else
                        ok_cnt++;
-               }
+
                free(package);
        }
 
@@ -157,44 +154,59 @@ int initdb_user_load_directory(const char *directory)
 static int __is_authorized()
 {
        /* ail_init db should be called by an user. */
-
        uid_t uid = getuid();
-       if ((uid_t) OWNER_ROOT != uid)
+       if ((uid_t)OWNER_ROOT != uid)
                return 1;
        else
                return 0;
 }
 
-
 int main(int argc, char *argv[])
 {
        int ret;
+       char *db;
+       char *path;
 
        if (!__is_authorized()) {
                fprintf(stderr, "You are not an authorized user!\n");
                _D("You are root user! Please switch to a regular user\n");
+               return -1;
        }
-       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()));
+
+       db = ail_get_app_DB(getuid());
+       if (db) {
+               if (remove(db))
+                       _E(" %s is not removed", db);
+
+               free(db);
+       }
+
+       db = ail_get_app_DB_journal(getuid());
+       if (db) {
+               if (remove(db))
+                       _E(" %s is not removed", db);
+
+               free(db);
        }
+
        ret = setenv("AIL_INITDB", "1", 1);
        _D("AIL_INITDB : %d", ret);
+
        ret = initdb_user_count_app();
-       if (ret > 0) {
+       if (ret > 0)
                _D("Some Apps in the App Info DB.");
+
+       path = ail_get_desktop_path(getuid());
+       if (path == NULL) {
+               _E("Failed to get desktop path");
+               return -1;
        }
 
-       ret = initdb_user_load_directory(al_get_desktop_path(getuid()));
-       if (ret == AIL_ERROR_FAIL) {
+       ret = initdb_user_load_directory(path);
+       if (ret == AIL_ERROR_FAIL)
                _E("cannot load usr desktop directory.");
-       }
+
+       free(path);
 
        return AIL_ERROR_OK;
 }
-
-
-
-// END