fix a wrong error log message
[platform/core/uifw/download-fonts-service.git] / pkgmgr_font / src / font_service_register.c
index b059dbb..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";
@@ -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);
@@ -185,8 +191,8 @@ FAIL:
 #define COPY_BUF_SIZE 16777216
 
 static int copy_file(const char *srcpath, const char *destpath) {
-       FILE *in, *out;
-       char *buf;
+       FILE *in = NULL, *out = NULL;
+       char *buf = NULL;
        size_t len;
 
        if (!srcpath || !destpath)
@@ -203,35 +209,35 @@ static int copy_file(const char *srcpath, const char *destpath) {
 
        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));
+               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("copyfile is failed. The destpath[%s] is not opened with write permission. fopen(): %s\n", destpath, strerror(errno));
+               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("copyfile is failed. Memory allocation for copy buffer is failed. Request size [%d]. malloc(): %s\n", COPY_BUF_SIZE, strerror(errno));
+               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), sizeof(buf), in)) != 0)
+       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("copyfile is failed. fwrite fails to write. fwrite(): %s\n", strerror(errno));
+                       DEBUG_ERROR_ERRNO("copyfile is failed. fwrite fails to write. fwrite()");
 
                        fclose(out);
                        out = NULL;
 
                        if (remove(destpath) == -1)
-                               DEBUG_ERROR("removing copied file with error is failed. remove(): %s\n", strerror(errno));
+                               DEBUG_ERROR_ERRNO("removing copied file with error is failed. remove()");
 
                        goto FAIL;
                }
@@ -277,8 +283,8 @@ static int move_path(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)
                        {
@@ -310,7 +316,7 @@ static int move_path(const char *srcpath, const char *destpath)
                        }
 
                        if (remove(srcdir) == -1)
-                               DEBUG_ERROR("removing src file[%s] is failed. remove(): %s\n", srcdir, strerror(errno));
+                               DEBUG_ERROR_ERRNO("removing src file[%s] is failed. remove()", srcdir);
                }
        }
 
@@ -432,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)
        {
@@ -453,12 +459,12 @@ 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);
+               snprintf(srcpath, sizeof(srcpath), "%s/res/wgt/shared/res", rootpath);
                ret = move_path(srcpath, path);
        }
 
@@ -472,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;
        }
 
@@ -513,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);
@@ -653,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;
        }
 
@@ -671,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;
        }
 
@@ -745,7 +751,7 @@ int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *
 
        /* 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);
+       snprintf(deletedir, sizeof(deletedir), "%s/%s/", dest_path, appid);
 
        //check if current using font is same with uninstall font
        deletedir_len = strlen(deletedir);
@@ -773,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)
        {
@@ -816,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;
        }
 
@@ -826,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);