fix a wrong error log message
[platform/core/uifw/download-fonts-service.git] / pkgmgr_font / src / font_service_register.c
index 31e62e7..1dd1173 100755 (executable)
 #include <fontconfig/fontconfig.h>
 #include <Elementary.h>
 #include <pkgmgr-info.h>
-#include <security-server.h>
+#include <pkgmgr_installer_info.h>
 #include "system_settings.h"
 
-
 #define FONT_SERVICE_TAG               "FONT_SERVICE"
 #define DEBUG_LOG(frmt, args...)               \
        do { SLOG(LOG_DEBUG,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
 #define DEBUG_ERROR(frmt, args...)             \
        do { SLOG(LOG_ERROR,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
        __func__, ##args);} while (0)
+#define DEBUG_ERROR_ERRNO(frmt, args...)               \
+       do { \
+               char error_string[255] = { 0 }; \
+               strerror_r(errno, error_string, sizeof(error_string)); \
+               SLOG(LOG_ERROR,FONT_SERVICE_TAG, "[font_service] %s: "frmt": %s\n",\
+       __func__, ##args, error_string);} while (0)
 
 static const char *PARENT_PATH = "/opt/share/fonts";
 static const char *DOWNLOAD_PATH = "/opt/share/fonts/download";
 static const char *PRELOADED_PATH = "/opt/share/fonts/preloaded";
 
-static const char *ELM_PROFILE_CFG = "/opt/home/app/.elementary/config/profile.cfg";
 #define MAX_FILE_LEN 4096
-#define APP_OWNER_ID 5000
-#define APP_GROUP_ID 5000
+#define APP_OWNER_ID 5001
+#define APP_GROUP_ID 100
 
 static int make_dir(const char *path);
 static int symbolic_link(const char *srcpath, const char *destpath);
 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle);
 static int do_uninstall(const char *deletedir);
-static int move_file(const char *srcpath, const char *destpath);
+static int move_path(const char *srcpath, const char *destpath);
 static const char* check_preloaded(const char *app_root_path);
 #ifdef CHECK_FAMILY_NAME
 static int check_family_name(const char *srcpath);
@@ -67,8 +71,8 @@ static const char* check_preloaded(const char *app_root_path)
        char tpk_path[MAX_FILE_LEN];
        char wgt_path[MAX_FILE_LEN];
 
-       sprintf(tpk_path, "%s/preloaded", app_root_path);
-       sprintf(wgt_path, "%s/res/wgt/preloaded", app_root_path);
+       snprintf(tpk_path, sizeof(tpk_path), "%s/preloaded", app_root_path);
+       snprintf(wgt_path, sizeof(wgt_path), "%s/res/wgt/preloaded", app_root_path);
 
        if ((access(tpk_path, F_OK) == 0) || (access(wgt_path, F_OK) == 0))
        {
@@ -95,7 +99,14 @@ static int make_dir(const char *path)
                        return ret;
                }
 
-               ret = chmod (path, 0755);
+               ret = chown(path, getuid(), APP_GROUP_ID);
+               if (ret < 0)
+               {
+                       DEBUG_ERROR("chown is failed %s\n",path);
+                       return ret;
+               }
+
+               ret = chmod(path, 0755);
                if (ret < 0)
                {
                        DEBUG_ERROR("chmod is failed %s\n",path);
@@ -103,13 +114,6 @@ static int make_dir(const char *path)
                        return ret;
                }
        }
-       ret = security_server_label_access(path, "_");
-
-       if (ret < 0)
-       {
-               DEBUG_ERROR("security_server_label_access is failed %s\n",path);
-               return ret;
-       }
 
        return ret;
 }
@@ -141,8 +145,8 @@ static int symbolic_link(const char *srcpath, const char *destpath)
                                goto FAIL;
                        }
 
-                       sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
-                       sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
+                       snprintf(srcdir, sizeof(srcdir), "%s/%s", srcpath, (char *)e->d_name);
+                       snprintf(destdir, sizeof(destdir), "%s/%s", destpath, (char *)e->d_name);
                        if (stat (srcdir, &statb) == -1)
                        {
                                DEBUG_ERROR("stat %s is failed \n",srcdir);
@@ -174,14 +178,6 @@ static int symbolic_link(const char *srcpath, const char *destpath)
                                DEBUG_ERROR("symlink is failed \n");
                                goto FAIL;
                        }
-
-                       ret = security_server_label_access(destdir, "_");
-
-                       if (ret < 0)
-                       {
-                               DEBUG_ERROR("security_server_label_access is failed %s\n",destdir);
-                               goto FAIL;
-                       }
                }
        }
        closedir (d);
@@ -192,76 +188,143 @@ FAIL:
        return -1;
 }
 
+#define COPY_BUF_SIZE 16777216
+
+static int copy_file(const char *srcpath, const char *destpath) {
+       FILE *in = NULL, *out = NULL;
+       char *buf = NULL;
+       size_t len;
+
+       if (!srcpath || !destpath)
+       {
+               DEBUG_ERROR("copyfile is failed. A given param is NULL ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
+               goto FAIL;
+       }
+
+       if (!strcmp(srcpath, destpath))
+       {
+               DEBUG_ERROR("copyfile is failed. The both path are same ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
+               goto FAIL;
+       }
 
-static int move_file(const char *srcpath, const char *destpath)
+       if ((in  = fopen(srcpath, "rb")) == NULL)
+       {
+               DEBUG_ERROR_ERRNO("copyfile is failed. The srcpath[%s] is not opened with read permission. fopen()", srcpath);
+               goto FAIL;
+       }
+
+       if ((out = fopen(destpath, "wb")) == NULL)
+       {
+               DEBUG_ERROR_ERRNO("copyfile is failed. The destpath[%s] is not opened with write permission. fopen()", destpath);
+               goto FAIL;
+       }
+
+       if ((buf = (char *) malloc(COPY_BUF_SIZE)) == NULL)
+       {
+               DEBUG_ERROR_ERRNO("copyfile is failed. Memory allocation for copy buffer is failed. Request size [%d]. malloc()", COPY_BUF_SIZE);
+               goto FAIL;
+       }
+
+       while ((len = fread(buf, sizeof(char), COPY_BUF_SIZE, in)) != 0)
+       {
+               if (fwrite(buf, sizeof(char), len, out) == 0)
+               {
+                       /* Reads "len" string. But, it fails to write file.
+                          We need to remove wrong result. */
+                       DEBUG_ERROR_ERRNO("copyfile is failed. fwrite fails to write. fwrite()");
+
+                       fclose(out);
+                       out = NULL;
+
+                       if (remove(destpath) == -1)
+                               DEBUG_ERROR_ERRNO("removing copied file with error is failed. remove()");
+
+                       goto FAIL;
+               }
+       }
+
+       /* Copy file DONE! */
+       if (in) fclose(in);
+       if (out) fclose(out);
+       if (buf) free(buf);
+       return 0;
+
+FAIL:
+       if (in) fclose(in);
+       if (out) fclose(out);
+       if (buf) free(buf);
+       return -1;
+}
+
+static int move_path(const char *srcpath, const char *destpath)
 {
        DIR *d;
        struct dirent *e;
        int ret = 0;
 
-       d = opendir (srcpath);
-       while ((e = readdir (d)))
+       d = opendir(srcpath);
+       while ((e = readdir(d)))
        {
                if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
                {
                        char srcdir[MAX_FILE_LEN];
                        char destdir[MAX_FILE_LEN];
-                       struct stat       statb;
+                       struct stat statb;
 
-                       if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
+                       if (strlen(srcpath) + strlen((char *)e->d_name) + 2 >= MAX_FILE_LEN)
                        {
                                DEBUG_ERROR("srcdir length is not available \n");
                                goto FAIL;
                        }
 
-                       if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
+                       if (strlen(destpath) + strlen((char *)e->d_name) + 2 >= MAX_FILE_LEN)
                        {
                                DEBUG_ERROR("destdir length is not available \n");
                                goto FAIL;
                        }
 
-                       sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
-                       sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
-                       if (stat (srcdir, &statb) == -1)
+                       snprintf(srcdir, sizeof(srcdir), "%s/%s", srcpath, (char *)e->d_name);
+                       snprintf(destdir, sizeof(destdir), "%s/%s", destpath, (char *)e->d_name);
+
+                       if (stat(srcdir, &statb) == -1)
                        {
                                DEBUG_ERROR("stat %s is failed \n",srcdir);
                                goto FAIL;
                        }
 
-                       if (S_ISDIR (statb.st_mode))
+                       if (S_ISDIR(statb.st_mode))
                        {
-                               if (make_dir(destdir)<0)
+                               if (make_dir(destdir) < 0)
                                {
-                                       DEBUG_ERROR("make current directory %s is failed \n",destdir);
+                                       DEBUG_ERROR("make current directory %s is failed \n", destdir);
                                        goto FAIL;
                                }
                                continue;
                        }
-                       DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
-                       DEBUG_LOG("destdir =%s\n",(char *) destdir);
-                       DEBUG_LOG("file name =%s\n",(char *) e->d_name);
-
-                       ret = rename((const char *)srcdir,(const char *)destdir);
-                       if (ret < 0)
-                       {
-                               DEBUG_ERROR("symlink is failed \n");
-                               goto FAIL;
-                       }
 
-                       ret = security_server_label_access(destdir, "_");
+                       DEBUG_LOG("srcdir =%s\n", (char *)srcdir);
+                       DEBUG_LOG("destdir =%s\n", (char *)destdir);
+                       DEBUG_LOG("file name =%s\n", (char *)e->d_name);
 
+                       /* We need copy font file instead of rename() function.
+                          To apply smack label of parent directory, we need create new file to the path. */
+                       ret = copy_file(srcdir, destdir);
                        if (ret < 0)
                        {
-                               DEBUG_ERROR("security_server_label_access is failed %s\n",destdir);
+                               DEBUG_ERROR("copy_file is failed \n");
                                goto FAIL;
                        }
+
+                       if (remove(srcdir) == -1)
+                               DEBUG_ERROR_ERRNO("removing src file[%s] is failed. remove()", srcdir);
                }
        }
-       closedir (d);
+
+       closedir(d);
        return ret;
 
 FAIL:
-       closedir (d);
+       closedir(d);
        return -1;
 }
 
@@ -375,7 +438,7 @@ static int do_install(const char *parent, const char *appid, const char *rootpat
                return -1;
        }
 
-       sprintf(destdir,"%s/%s",parent,appid);
+       snprintf(destdir, sizeof(destdir), "%s/%s", parent, appid);
        ret = make_dir(destdir);
        if (ret < 0)
        {
@@ -396,18 +459,18 @@ static int do_install(const char *parent, const char *appid, const char *rootpat
                goto FAIL;
        }
 
-       sprintf(path,"%s/shared/res", rootpath);
+       snprintf(path, sizeof(path), "%s/shared/res", rootpath);
 
        if (!strcmp(type,"wgt"))
        {
                char srcpath[MAX_FILE_LEN];
-               sprintf(srcpath,"%s/res/wgt/shared/res", rootpath);
-               ret = move_file(srcpath, path);
+               snprintf(srcpath, sizeof(srcpath), "%s/res/wgt/shared/res", rootpath);
+               ret = move_path(srcpath, path);
        }
 
        if (ret < 0)
        {
-               DEBUG_ERROR("move_file is failed\n");
+               DEBUG_ERROR("move_path is failed\n");
                goto FAIL;
        }
 
@@ -415,7 +478,7 @@ static int do_install(const char *parent, const char *appid, const char *rootpat
 
        if (ret < 0)
        {
-               DEBUG_ERROR("install is failed \n", destdir);
+               DEBUG_ERROR("install is failed to [%s]\n", destdir);
                goto FAIL;
        }
 
@@ -456,7 +519,7 @@ static int do_uninstall(const char *deletedir)
                                goto FAIL;
                        }
 
-                       sprintf(destfile,"%s/%s",deletedir,(char *) e->d_name);
+                       snprintf(destfile, sizeof(destfile), "%s/%s", deletedir, (char *)e->d_name);
                        if (lstat (destfile, &statb) == -1)
                        {
                                DEBUG_ERROR("lstat %s is failed \n",destfile);
@@ -500,8 +563,11 @@ int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *li
        pkgmgrinfo_pkginfo_h handle = NULL;
        const char *app_root_path = NULL;
        const char *dest_path = NULL;
+       uid_t uid = 0;
+
+       pkgmgr_installer_info_get_target_uid(&uid);
+       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
 
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
        if (ret < 0)
        {
                DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
@@ -561,8 +627,11 @@ int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *li
        pkgmgrinfo_pkginfo_h handle = NULL;
        const char* app_root_path = NULL;
        const char* dest_path = NULL;
+       uid_t uid = 0;
+
+       pkgmgr_installer_info_get_target_uid(&uid);
+       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
 
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
        if (ret < 0)
        {
                DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
@@ -590,11 +659,11 @@ int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *li
                goto FAIL;
        }
 
-       sprintf(deletedir,"%s/%s", dest_path, appid);
+       snprintf(deletedir, sizeof(deletedir), "%s/%s", dest_path, appid);
 
        if (access(deletedir, F_OK) == -1)
        {
-               DEBUG_ERROR("dest directory is not exist \n");
+               DEBUG_ERROR_ERRNO("dest directory(%s) is not exist", deletedir);
                goto FAIL;
        }
 
@@ -606,18 +675,16 @@ int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *li
        }
 
        ret = make_dir(PARENT_PATH);
-
        if (ret < 0)
        {
-               DEBUG_ERROR("make current directory is failed \n");
+               DEBUG_ERROR_ERRNO("make current directory(%s) is failed", PARENT_PATH);
                goto FAIL;
        }
 
        ret = make_dir(dest_path);
-
        if (ret < 0)
        {
-               DEBUG_ERROR("make current directory is failed \n");
+               DEBUG_ERROR_ERRNO("make current directory(%s) is failed", dest_path);
                goto FAIL;
        }
 
@@ -644,9 +711,11 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
        FcObjectSet *os = NULL;
        FcPattern *pat = NULL;
        FcFontSet *fs = NULL;
+       pkgmgrinfo_pkginfo_h handle = NULL;
        const char* app_root_path = NULL;
        const char *dest_path = NULL;
-       int ret;
+       int ret, deletedir_len;
+       uid_t uid = 0;
 
        elm_init(0, NULL);
 
@@ -656,10 +725,9 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
                return -1;
        }
 
-       pkgmgrinfo_pkginfo_h handle = NULL;
-
+       pkgmgr_installer_info_get_target_uid(&uid);
+       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
 
-       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
        if (ret < 0)
        {
                DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
@@ -681,10 +749,12 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
                goto FAIL;
        }
 
-       sprintf(deletedir,"%s/%s", dest_path, appid);
+       /* It must contain "/" character at end of the delete dir path.
+        * It prevents file path comparing issues when there are many similar path. */
+       snprintf(deletedir, sizeof(deletedir), "%s/%s/", dest_path, appid);
 
        //check if current using font is same with uninstall font
-       int deletedir_len = strlen(deletedir);
+       deletedir_len = strlen(deletedir);
 
        pat = FcPatternCreate();
        if (pat == NULL)
@@ -709,18 +779,21 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
        fs = FcFontList(NULL, pat, os);
 
        FcPatternDestroy(pat);
+       pat = NULL;
        free(current_font_name);
+       current_font_name = NULL;
 
        if (fs)
        {
                int j;
+
                for (j = 0; j < fs->nfont; j++)
                {
                        FcChar8 *file = NULL;
                        if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
                        {
-                               DEBUG_LOG("file =%s\n",file);
-                               if (strncmp((const char*)file , deletedir , deletedir_len) == 0 )
+                               DEBUG_LOG("file =%s, deletedir =%s\n", file, deletedir);
+                               if (strncmp((const char*)file, deletedir, deletedir_len) == 0)
                                {
                                        DEBUG_LOG("change to default font\n");
                                        char *default_font_name = NULL;
@@ -733,8 +806,6 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
                                        }
 
                                        DEBUG_LOG("default_font_name = %s \n",default_font_name);
-                                       setenv("HOME", "/opt/home/app", 1);
-
                                        ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, default_font_name);
                                        if (ret < 0)
                                        {
@@ -743,59 +814,7 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
                                                break;
                                        }
 
-                                       char *elm_profile_path = (char*)elm_config_profile_dir_get(elm_config_profile_get(), EINA_TRUE);
-
-                                       setenv("HOME", "/root", 1);
-
-                                       ret = chown(ELM_PROFILE_CFG, APP_OWNER_ID, APP_GROUP_ID);
-                                       if (ret < 0)
-                                       {
-                                               chmod (ELM_PROFILE_CFG, 0777);
-                                       }
-
-                                       ret = security_server_label_access(ELM_PROFILE_CFG, "system::homedir");
-                                       if (ret < 0)
-                                       {
-                                               chmod (ELM_PROFILE_CFG, 0777);
-                                       }
-
-                                       DIR *d = NULL;
-                                       struct dirent *e;
-
-                                       if (elm_profile_path)
-                                               d = opendir (elm_profile_path);
-
-                                       if (d)
-                                       {
-                                               while ((e = readdir (d)))
-                                               {
-                                                       if (e->d_name[0] != '.' &&  (strstr(e->d_name, ".cfg") != 0 || strstr(e->d_name, ".CFG") != 0))
-                                                       {
-                                                               char file_full_path[100];
-
-                                                               sprintf(file_full_path, "%s/%s", elm_profile_path, e->d_name);
-
-                                                               ret = chown(file_full_path, APP_OWNER_ID, APP_GROUP_ID);
-                                                               if (ret < 0)
-                                                               {
-                                                                       DEBUG_LOG("chown is failed %s", file_full_path);
-                                                                       chmod (file_full_path, 0777);
-                                                               }
-
-                                                               ret = security_server_label_access(file_full_path, "system::homedir");
-                                                               if (ret < 0)
-                                                               {
-                                                                       DEBUG_LOG("chsmack is failed %s", file_full_path);
-                                                                       chmod (file_full_path, 0777);
-                                                               }
-                                                       }
-                                               }
-
-                                               closedir (d);
-                                       }
-
                                        free(default_font_name);
-                                       free(elm_profile_path);
                                        DEBUG_LOG("success change to default font\n");
                                        break;
                                }
@@ -805,7 +824,7 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
 
        if (access(deletedir, F_OK) == -1)
        {
-               DEBUG_ERROR("dest directory(%s) is not exist \n", deletedir);
+               DEBUG_ERROR_ERRNO("dest directory(%s) is not exist", deletedir);
                goto FAIL;
        }
 
@@ -815,6 +834,7 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
        return do_uninstall(deletedir);
 
 FAIL:
+       if (pat) FcPatternDestroy(pat);
        elm_shutdown();
        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);