2 * Copyright 2013 Samsung Electronics Co., Ltd
5 * Contact: Minsu Seo <minsu15.seo@samsung.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <sys/types.h>
22 #include <sys/xattr.h>
28 #include <fontconfig/fontconfig.h>
29 #include <Elementary.h>
30 #include <pkgmgr-info.h>
31 #include <pkgmgr_installer_info.h>
32 #include "system_settings.h"
34 #define FONT_SERVICE_TAG "FONT_SERVICE"
35 #define DEBUG_LOG(frmt, args...) \
36 do { SLOG(LOG_DEBUG,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
37 __func__, ##args);} while (0)
38 #define DEBUG_WARNING(frmt, args...) \
39 do { SLOG(LOG_WARN,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
40 __func__, ##args);} while (0)
41 #define DEBUG_ERROR(frmt, args...) \
42 do { SLOG(LOG_ERROR,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
43 __func__, ##args);} while (0)
44 #define DEBUG_ERROR_ERRNO(frmt, args...) \
46 char error_string[255] = { 0 }; \
47 strerror_r(errno, error_string, sizeof(error_string)); \
48 SLOG(LOG_ERROR,FONT_SERVICE_TAG, "[font_service] %s: "frmt": %s\n",\
49 __func__, ##args, error_string);} while (0)
51 static const char *PARENT_PATH = "/opt/share/fonts";
52 static const char *DOWNLOAD_PATH = "/opt/share/fonts/download";
53 static const char *PRELOADED_PATH = "/opt/share/fonts/preloaded";
55 #define MAX_FILE_LEN 4096
56 #define APP_OWNER_ID 5001
57 #define APP_GROUP_ID 100
59 static int make_dir(const char *path);
60 static int symbolic_link(const char *srcpath, const char *destpath);
61 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle);
62 static int do_uninstall(const char *deletedir);
63 static int move_path(const char *srcpath, const char *destpath);
64 static const char* check_preloaded(const char *app_root_path);
65 #ifdef CHECK_FAMILY_NAME
66 static int check_family_name(const char *srcpath);
69 static const char* check_preloaded(const char *app_root_path)
71 char tpk_path[MAX_FILE_LEN];
72 char wgt_path[MAX_FILE_LEN];
74 snprintf(tpk_path, sizeof(tpk_path), "%s/preloaded", app_root_path);
75 snprintf(wgt_path, sizeof(wgt_path), "%s/res/wgt/preloaded", app_root_path);
77 if ((access(tpk_path, F_OK) == 0) || (access(wgt_path, F_OK) == 0))
79 return PRELOADED_PATH;
85 static int make_dir(const char *path)
89 if (access(path, F_OK) == 0)
95 ret = mkdir(path, 0755);
98 DEBUG_ERROR("make current directory %s is failed \n",path);
102 ret = chown(path, getuid(), APP_GROUP_ID);
105 DEBUG_ERROR("chown is failed %s\n",path);
109 ret = chmod(path, 0755);
112 DEBUG_ERROR("chmod is failed %s\n",path);
121 static int symbolic_link(const char *srcpath, const char *destpath)
127 d = opendir(srcpath);
128 while ((e = readdir (d)))
130 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
132 char srcdir[MAX_FILE_LEN];
133 char destdir[MAX_FILE_LEN];
136 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
138 DEBUG_ERROR("srcdir length is not available \n");
142 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
144 DEBUG_ERROR("destdir length is not available \n");
148 snprintf(srcdir, sizeof(srcdir), "%s/%s", srcpath, (char *)e->d_name);
149 snprintf(destdir, sizeof(destdir), "%s/%s", destpath, (char *)e->d_name);
150 if (stat (srcdir, &statb) == -1)
152 DEBUG_ERROR("stat %s is failed \n",srcdir);
156 if (S_ISDIR (statb.st_mode))
158 if (make_dir(destdir)<0)
160 DEBUG_ERROR("make current directory %s is failed \n",destdir);
163 ret = symbolic_link(srcdir,destdir);
166 DEBUG_ERROR("symlink is failed \n");
171 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
172 DEBUG_LOG("destdir =%s\n",(char *) destdir);
173 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
175 ret = symlink((const char *)srcdir,(const char *)destdir);
178 DEBUG_ERROR("symlink is failed \n");
191 #define COPY_BUF_SIZE 16777216
193 static int copy_file(const char *srcpath, const char *destpath) {
194 FILE *in = NULL, *out = NULL;
198 if (!srcpath || !destpath)
200 DEBUG_ERROR("copyfile is failed. A given param is NULL ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
204 if (!strcmp(srcpath, destpath))
206 DEBUG_ERROR("copyfile is failed. The both path are same ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
210 if ((in = fopen(srcpath, "rb")) == NULL)
212 DEBUG_ERROR_ERRNO("copyfile is failed. The srcpath[%s] is not opened with read permission. fopen()", srcpath);
216 if ((out = fopen(destpath, "wb")) == NULL)
218 DEBUG_ERROR_ERRNO("copyfile is failed. The destpath[%s] is not opened with write permission. fopen()", destpath);
222 if ((buf = (char *) malloc(COPY_BUF_SIZE)) == NULL)
224 DEBUG_ERROR_ERRNO("copyfile is failed. Memory allocation for copy buffer is failed. Request size [%d]. malloc()", COPY_BUF_SIZE);
228 while ((len = fread(buf, sizeof(char), sizeof(buf), in)) != 0)
230 if (fwrite(buf, sizeof(char), len, out) == 0)
232 /* Reads "len" string. But, it fails to write file.
233 We need to remove wrong result. */
234 DEBUG_ERROR_ERRNO("copyfile is failed. fwrite fails to write. fwrite()");
239 if (remove(destpath) == -1)
240 DEBUG_ERROR_ERRNO("removing copied file with error is failed. remove()");
246 /* Copy file DONE! */
248 if (out) fclose(out);
254 if (out) fclose(out);
259 static int move_path(const char *srcpath, const char *destpath)
265 d = opendir(srcpath);
266 while ((e = readdir(d)))
268 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
270 char srcdir[MAX_FILE_LEN];
271 char destdir[MAX_FILE_LEN];
274 if (strlen(srcpath) + strlen((char *)e->d_name) + 2 >= MAX_FILE_LEN)
276 DEBUG_ERROR("srcdir length is not available \n");
280 if (strlen(destpath) + strlen((char *)e->d_name) + 2 >= MAX_FILE_LEN)
282 DEBUG_ERROR("destdir length is not available \n");
286 snprintf(srcdir, sizeof(srcdir), "%s/%s", srcpath, (char *)e->d_name);
287 snprintf(destdir, sizeof(destdir), "%s/%s", destpath, (char *)e->d_name);
289 if (stat(srcdir, &statb) == -1)
291 DEBUG_ERROR("stat %s is failed \n",srcdir);
295 if (S_ISDIR(statb.st_mode))
297 if (make_dir(destdir) < 0)
299 DEBUG_ERROR("make current directory %s is failed \n", destdir);
305 DEBUG_LOG("srcdir =%s\n", (char *)srcdir);
306 DEBUG_LOG("destdir =%s\n", (char *)destdir);
307 DEBUG_LOG("file name =%s\n", (char *)e->d_name);
309 /* We need copy font file instead of rename() function.
310 To apply smack label of parent directory, we need create new file to the path. */
311 ret = copy_file(srcdir, destdir);
314 DEBUG_ERROR("copy_file is failed \n");
318 if (remove(srcdir) == -1)
319 DEBUG_ERROR_ERRNO("removing src file[%s] is failed. remove()", srcdir);
331 #ifdef CHECK_FAMILY_NAME
332 static int check_family_name(const char *srcpath)
334 FcObjectSet *os = NULL;
335 FcFontSet* fs = NULL;
336 FcPattern* pat = NULL;
337 FcConfig* font_config = NULL;
338 bool is_only_one_family = false;
340 font_config = FcInitLoadConfigAndFonts();
342 if(font_config == NULL) {
343 DEBUG_ERROR("Failed: FcInitLoadConfigAndFonts");
347 pat = FcPatternCreate();
349 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
352 fs = FcFontList(font_config, pat, os);
353 FcObjectSetDestroy(os);
358 FcPatternDestroy(pat);
365 char* only_one_family = NULL;
367 for (j = 0; j < fs->nfont; j++)
369 FcChar8 *family = NULL;
370 FcChar8 *file = NULL;
372 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
374 int font_path_len = strlen(srcpath);
376 /* always shown for src path */
377 if (strncmp((const char*)file, srcpath, font_path_len) == 0)
379 if (FcPatternGetString(fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
381 DEBUG_ERROR("Family name is invalid");
384 //DEBUG_ERROR("-------- FOUND FONT - family = %s", (char *)family);
386 // first found family name
387 if (only_one_family == NULL && strlen((char *)family) > 0)
389 is_only_one_family = true;
390 only_one_family = (char *)family;
391 //DEBUG_ERROR("--------First FOUND FONT - family = %s", only_one_family);
393 else if (only_one_family != NULL && strlen((char *)family) > 0)// after first
395 if (strcmp(only_one_family, (char *)family) != 0)
397 DEBUG_ERROR("Not supported various font famliy. only one font famliy. %s %s", only_one_family, (char *)family);
398 is_only_one_family = false;
404 DEBUG_ERROR("invalid fonts");
411 FcFontSetDestroy(fs);
415 FcConfigDestroy(font_config);
418 if (is_only_one_family == false)
420 //DEBUG_ERROR("Not supported various font famliy. only one font famliy");
428 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle)
430 char destdir[MAX_FILE_LEN];
431 char path[MAX_FILE_LEN];
435 if (strlen (parent) + strlen (appid) + 2 >= MAX_FILE_LEN )
437 DEBUG_ERROR("appid length is not available \n");
441 snprintf(destdir, sizeof(destdir), "%s/%s", parent, appid);
442 ret = make_dir(destdir);
445 DEBUG_ERROR("make current directory %s is failed \n", destdir);
449 if (strlen (rootpath) + 2 >= MAX_FILE_LEN )
451 DEBUG_ERROR("rootpath length is not available \n");
455 ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
458 DEBUG_ERROR("pkgmgrinfo_pkginfo_get_type is failed\n");
462 snprintf(path, sizeof(path), "%s/shared/res", rootpath);
464 if (!strcmp(type,"wgt"))
466 char srcpath[MAX_FILE_LEN];
467 snprintf(srcpath, sizeof(srcpath), "%s/res/wgt/shared/res", rootpath);
468 ret = move_path(srcpath, path);
473 DEBUG_ERROR("move_path is failed\n");
477 ret = symbolic_link(path, destdir);
481 DEBUG_ERROR("install is failed \n", destdir);
485 #ifdef CHECK_FAMILY_NAME
486 ret = check_family_name(destdir);
490 DEBUG_ERROR("Invaid font files\n");
498 do_uninstall(destdir);
503 static int do_uninstall(const char *deletedir)
508 d = opendir(deletedir);
509 while((e = readdir (d)))
511 if (e->d_name[0] != '.')
513 char destfile[MAX_FILE_LEN];
516 if (strlen (deletedir) + strlen ((char *) e->d_name) + 2 >= MAX_FILE_LEN )
518 DEBUG_ERROR("destfile length is not available \n");
522 snprintf(destfile, sizeof(destfile), "%s/%s", deletedir, (char *)e->d_name);
523 if (lstat (destfile, &statb) == -1)
525 DEBUG_ERROR("lstat %s is failed \n",destfile);
529 if (S_ISDIR (statb.st_mode))
531 if (do_uninstall(destfile)<0)
534 else if (unlink(destfile) < 0)
536 DEBUG_ERROR("unlink is failed %s\n",destfile);
539 DEBUG_LOG("destfile =%s\n",destfile);
545 if (rmdir(deletedir) < 0)
547 DEBUG_ERROR("rmdir is failed \n");
551 DEBUG_LOG("rmdir =%s\n",deletedir);
560 int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
563 pkgmgrinfo_pkginfo_h handle = NULL;
564 const char *app_root_path = NULL;
565 const char *dest_path = NULL;
568 pkgmgr_installer_info_get_target_uid(&uid);
569 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
573 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
577 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
580 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
586 DEBUG_ERROR("appid is NULL \n");
590 ret = make_dir(PARENT_PATH);
594 DEBUG_ERROR("make current directory is failed \n");
598 dest_path = check_preloaded(app_root_path);
600 if (make_dir(dest_path)<0)
602 DEBUG_ERROR("make current directory is failed \n");
606 ret = do_install(dest_path,appid, app_root_path, handle);
609 DEBUG_ERROR("font install is failed \n");
613 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
618 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
623 int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
625 char deletedir[MAX_FILE_LEN];
627 pkgmgrinfo_pkginfo_h handle = NULL;
628 const char* app_root_path = NULL;
629 const char* dest_path = NULL;
632 pkgmgr_installer_info_get_target_uid(&uid);
633 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
637 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
641 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
644 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
650 DEBUG_ERROR("appid is NULL \n");
654 dest_path = check_preloaded(app_root_path);
656 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
658 DEBUG_ERROR("appid length is not available \n");
662 snprintf(deletedir, sizeof(deletedir), "%s/%s", dest_path, appid);
664 if (access(deletedir, F_OK) == -1)
666 DEBUG_ERROR_ERRNO("dest directory(%s) is not exist", deletedir);
670 ret = do_uninstall(deletedir);
673 DEBUG_ERROR("do_uninstall is failed \n");
677 ret = make_dir(PARENT_PATH);
680 DEBUG_ERROR_ERRNO("make current directory(%s) is failed", PARENT_PATH);
684 ret = make_dir(dest_path);
687 DEBUG_ERROR_ERRNO("make current directory(%s) is failed", dest_path);
691 ret = do_install(dest_path, appid, app_root_path, handle);
694 DEBUG_ERROR("do_install is failed \n");
698 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
702 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
708 int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
710 char deletedir[MAX_FILE_LEN];
711 FcObjectSet *os = NULL;
712 FcPattern *pat = NULL;
713 FcFontSet *fs = NULL;
714 pkgmgrinfo_pkginfo_h handle = NULL;
715 const char* app_root_path = NULL;
716 const char *dest_path = NULL;
717 int ret, deletedir_len;
724 DEBUG_ERROR("appid is NULL \n");
728 pkgmgr_installer_info_get_target_uid(&uid);
729 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
733 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
737 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
740 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
744 dest_path = check_preloaded(app_root_path);
746 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
748 DEBUG_ERROR("appid length is not available \n");
752 /* It must contain "/" character at end of the delete dir path.
753 * It prevents file path comparing issues when there are many similar path. */
754 snprintf(deletedir, sizeof(deletedir), "%s/%s/", dest_path, appid);
756 //check if current using font is same with uninstall font
757 deletedir_len = strlen(deletedir);
759 pat = FcPatternCreate();
762 DEBUG_ERROR("FcPatternCreate is NULL \n");
766 char *current_font_name = NULL;
767 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, ¤t_font_name);
770 DEBUG_ERROR("Get current font is failed \n");
774 DEBUG_LOG("current_font_name =%s\n",current_font_name);
776 FcInitLoadConfigAndFonts();
777 FcPatternAddString (pat, FC_FAMILY,(FcChar8*)current_font_name);
778 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
779 fs = FcFontList(NULL, pat, os);
781 FcPatternDestroy(pat);
783 free(current_font_name);
784 current_font_name = NULL;
790 for (j = 0; j < fs->nfont; j++)
792 FcChar8 *file = NULL;
793 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
795 DEBUG_LOG("file =%s, deletedir =%s\n", file, deletedir);
796 if (strncmp((const char*)file, deletedir, deletedir_len) == 0)
798 DEBUG_LOG("change to default font\n");
799 char *default_font_name = NULL;
801 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEFAULT_FONT_TYPE, &default_font_name);
804 DEBUG_ERROR("Get default font is failed \n");
808 DEBUG_LOG("default_font_name = %s \n",default_font_name);
809 ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, default_font_name);
812 DEBUG_ERROR("Set default font is failed ret=%d \n",ret);
813 free(default_font_name);
817 free(default_font_name);
818 DEBUG_LOG("success change to default font\n");
825 if (access(deletedir, F_OK) == -1)
827 DEBUG_ERROR_ERRNO("dest directory(%s) is not exist", deletedir);
832 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
834 return do_uninstall(deletedir);
837 if (pat) FcPatternDestroy(pat);
839 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
845 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
847 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
850 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
852 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
855 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
857 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
861 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
863 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
866 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
868 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
871 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
873 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);