fix a wrong error log message
[platform/core/uifw/download-fonts-service.git] / pkgmgr_font / src / font_service_register.c
index 55f072f..1dd1173 100755 (executable)
 #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";
@@ -54,7 +60,7 @@ 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);
@@ -65,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))
        {
@@ -139,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);
@@ -182,68 +188,143 @@ FAIL:
        return -1;
 }
 
+#define COPY_BUF_SIZE 16777216
 
-static int move_file(const char *srcpath, const char *destpath)
+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;
+       }
+
+       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);
+                       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("symlink is failed \n");
+                               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;
 }
 
@@ -357,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)
        {
@@ -378,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;
        }
 
@@ -397,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;
        }
 
@@ -438,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);
@@ -578,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(%s) is not exist: %s\n", deletedir, strerror(errno));
+               DEBUG_ERROR_ERRNO("dest directory(%s) is not exist", deletedir);
                goto FAIL;
        }
 
@@ -596,14 +677,14 @@ 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(%s) is failed: %s\n", PARENT_PATH, strerror(errno));
+               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(%s) is failed: %s\n", dest_path, strerror(errno));
+               DEBUG_ERROR_ERRNO("make current directory(%s) is failed", dest_path);
                goto FAIL;
        }
 
@@ -633,7 +714,7 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
        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);
@@ -668,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)
@@ -696,7 +779,9 @@ 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)
        {
@@ -739,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: %s\n", deletedir, strerror(errno));
+               DEBUG_ERROR_ERRNO("dest directory(%s) is not exist", deletedir);
                goto FAIL;
        }
 
@@ -749,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);