copy file when move font file to proper shared path
[platform/core/uifw/download-fonts-service.git] / pkgmgr_font / src / font_service_register.c
index 4de31df..b059dbb 100755 (executable)
@@ -28,9 +28,9 @@
 #include <fontconfig/fontconfig.h>
 #include <Elementary.h>
 #include <pkgmgr-info.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",\
@@ -46,16 +46,15 @@ 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);
@@ -94,7 +93,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);
@@ -102,13 +108,6 @@ static int make_dir(const char *path)
                        return ret;
                }
        }
-       ret = lsetxattr(path, "security.SMACK64", "_", 1, 0);
-
-       if (ret < 0)
-       {
-               DEBUG_ERROR("lsetxattr is failed %s\n",path);
-               return ret;
-       }
 
        return ret;
 }
@@ -173,14 +172,6 @@ static int symbolic_link(const char *srcpath, const char *destpath)
                                DEBUG_ERROR("symlink is failed \n");
                                goto FAIL;
                        }
-
-                       ret = lsetxattr(destdir, "security.SMACK64", "_", 1, 0);
-
-                       if (ret < 0)
-                       {
-                               DEBUG_ERROR("lsetxattr is failed %s\n",destdir);
-                               goto FAIL;
-                       }
                }
        }
        closedir (d);
@@ -191,76 +182,143 @@ FAIL:
        return -1;
 }
 
+#define COPY_BUF_SIZE 16777216
+
+static int copy_file(const char *srcpath, const char *destpath) {
+       FILE *in, *out;
+       char *buf;
+       size_t len;
 
-static int move_file(const char *srcpath, const char *destpath)
+       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;
+       }
+
+       if ((in  = fopen(srcpath, "rb")) == NULL)
+       {
+               DEBUG_ERROR("copyfile is failed. The srcpath[%s] is not opened with read permission. fopen(): %s\n", srcpath, strerror(errno));
+               goto FAIL;
+       }
+
+       if ((out = fopen(destpath, "wb")) == NULL)
+       {
+               DEBUG_ERROR("copyfile is failed. The destpath[%s] is not opened with write permission. fopen(): %s\n", destpath, strerror(errno));
+               goto FAIL;
+       }
+
+       if ((buf = (char *) malloc(COPY_BUF_SIZE)) == NULL)
+       {
+               DEBUG_ERROR("copyfile is failed. Memory allocation for copy buffer is failed. Request size [%d]. malloc(): %s\n", COPY_BUF_SIZE, strerror(errno));
+               goto FAIL;
+       }
+
+       while ((len = fread(buf, sizeof(char), sizeof(buf), 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("copyfile is failed. fwrite fails to write. fwrite(): %s\n", strerror(errno));
+
+                       fclose(out);
+                       out = NULL;
+
+                       if (remove(destpath) == -1)
+                               DEBUG_ERROR("removing copied file with error is failed. remove(): %s\n", strerror(errno));
+
+                       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)
+                       sprintf(srcdir, "%s/%s", srcpath, (char *)e->d_name);
+                       sprintf(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 = lsetxattr(destdir, "security.SMACK64", "_", 1, 0);
+                       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("lsetxattr is failed %s\n",destdir);
+                               DEBUG_ERROR("copy_file is failed \n");
                                goto FAIL;
                        }
+
+                       if (remove(srcdir) == -1)
+                               DEBUG_ERROR("removing src file[%s] is failed. remove(): %s\n", srcdir, strerror(errno));
                }
        }
-       closedir (d);
+
+       closedir(d);
        return ret;
 
 FAIL:
-       closedir (d);
+       closedir(d);
        return -1;
 }
 
@@ -401,12 +459,12 @@ static int do_install(const char *parent, const char *appid, const char *rootpat
        {
                char srcpath[MAX_FILE_LEN];
                sprintf(srcpath,"%s/res/wgt/shared/res", rootpath);
-               ret = move_file(srcpath, path);
+               ret = move_path(srcpath, path);
        }
 
        if (ret < 0)
        {
-               DEBUG_ERROR("move_file is failed\n");
+               DEBUG_ERROR("move_path is failed\n");
                goto FAIL;
        }
 
@@ -499,8 +557,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);
@@ -560,8 +621,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);
@@ -593,7 +657,7 @@ int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *li
 
        if (access(deletedir, F_OK) == -1)
        {
-               DEBUG_ERROR("dest directory is not exist \n");
+               DEBUG_ERROR("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
                goto FAIL;
        }
 
@@ -605,18 +669,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("make current directory(%s) is failed: %s\n", PARENT_PATH, strerror(errno));
                goto FAIL;
        }
 
        ret = make_dir(dest_path);
-
        if (ret < 0)
        {
-               DEBUG_ERROR("make current directory is failed \n");
+               DEBUG_ERROR("make current directory(%s) is failed: %s\n", dest_path, strerror(errno));
                goto FAIL;
        }
 
@@ -643,9 +705,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);
 
@@ -655,10 +719,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);
@@ -680,10 +743,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. */
+       sprintf(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)
@@ -713,13 +778,14 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
        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;
@@ -732,8 +798,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)
                                        {
@@ -742,55 +806,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 = lsetxattr(ELM_PROFILE_CFG, "security.SMACK64", "system::homedir", 15, 0);
-                                       if (ret < 0)
-                                       {
-                                               chmod (ELM_PROFILE_CFG, 0777);
-                                       }
-
-                                       DIR *d;
-                                       struct dirent *e;
-
-                                       d = opendir (elm_profile_path);
-
-                                       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 = lsetxattr(file_full_path, "security.SMACK64", "system::homedir", 15, 0);
-                                                       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;
                                }
@@ -800,7 +816,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("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
                goto FAIL;
        }