copy file when move font file to proper shared path 73/112673/1 tizen_3.0_tv accepted/tizen/3.0/common/20170206.110222 accepted/tizen/3.0/ivi/20170206.034337 accepted/tizen/3.0/mobile/20170206.033925 accepted/tizen/3.0/tv/20170206.034110 accepted/tizen/3.0/wearable/20170206.034217 submit/tizen_3.0/20170203.055501
authorYoungbok Shin <youngb.shin@samsung.com>
Thu, 2 Feb 2017 07:00:26 +0000 (16:00 +0900)
committerYoungbok Shin <youngb.shin@samsung.com>
Thu, 2 Feb 2017 07:00:26 +0000 (16:00 +0900)
It resolve smack issue when .wgt font application is installed.

Change-Id: I3ea71a0ffbbac0eea15526ad31e22c42a5544daa

pkgmgr_font/src/font_service_register.c

index 726a57c..b059dbb 100755 (executable)
@@ -54,7 +54,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);
@@ -182,68 +182,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, *out;
+       char *buf;
+       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("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);
+                       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("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;
 }
 
@@ -384,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;
        }