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)
45 static const char *PARENT_PATH = "/opt/share/fonts";
46 static const char *DOWNLOAD_PATH = "/opt/share/fonts/download";
47 static const char *PRELOADED_PATH = "/opt/share/fonts/preloaded";
49 #define MAX_FILE_LEN 4096
50 #define APP_OWNER_ID 5001
51 #define APP_GROUP_ID 100
53 static int make_dir(const char *path);
54 static int symbolic_link(const char *srcpath, const char *destpath);
55 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle);
56 static int do_uninstall(const char *deletedir);
57 static int move_path(const char *srcpath, const char *destpath);
58 static const char* check_preloaded(const char *app_root_path);
59 #ifdef CHECK_FAMILY_NAME
60 static int check_family_name(const char *srcpath);
63 static const char* check_preloaded(const char *app_root_path)
65 char tpk_path[MAX_FILE_LEN];
66 char wgt_path[MAX_FILE_LEN];
68 sprintf(tpk_path, "%s/preloaded", app_root_path);
69 sprintf(wgt_path, "%s/res/wgt/preloaded", app_root_path);
71 if ((access(tpk_path, F_OK) == 0) || (access(wgt_path, F_OK) == 0))
73 return PRELOADED_PATH;
79 static int make_dir(const char *path)
83 if (access(path, F_OK) == 0)
89 ret = mkdir(path, 0755);
92 DEBUG_ERROR("make current directory %s is failed \n",path);
96 ret = chown(path, getuid(), APP_GROUP_ID);
99 DEBUG_ERROR("chown is failed %s\n",path);
103 ret = chmod(path, 0755);
106 DEBUG_ERROR("chmod is failed %s\n",path);
115 static int symbolic_link(const char *srcpath, const char *destpath)
121 d = opendir(srcpath);
122 while ((e = readdir (d)))
124 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
126 char srcdir[MAX_FILE_LEN];
127 char destdir[MAX_FILE_LEN];
130 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
132 DEBUG_ERROR("srcdir length is not available \n");
136 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
138 DEBUG_ERROR("destdir length is not available \n");
142 sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
143 sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
144 if (stat (srcdir, &statb) == -1)
146 DEBUG_ERROR("stat %s is failed \n",srcdir);
150 if (S_ISDIR (statb.st_mode))
152 if (make_dir(destdir)<0)
154 DEBUG_ERROR("make current directory %s is failed \n",destdir);
157 ret = symbolic_link(srcdir,destdir);
160 DEBUG_ERROR("symlink is failed \n");
165 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
166 DEBUG_LOG("destdir =%s\n",(char *) destdir);
167 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
169 ret = symlink((const char *)srcdir,(const char *)destdir);
172 DEBUG_ERROR("symlink is failed \n");
185 #define COPY_BUF_SIZE 16777216
187 static int copy_file(const char *srcpath, const char *destpath) {
192 if (!srcpath || !destpath)
194 DEBUG_ERROR("copyfile is failed. A given param is NULL ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
198 if (!strcmp(srcpath, destpath))
200 DEBUG_ERROR("copyfile is failed. The both path are same ... srcpath[%s], destpath[%s]\n", srcpath, destpath);
204 if ((in = fopen(srcpath, "rb")) == NULL)
206 DEBUG_ERROR("copyfile is failed. The srcpath[%s] is not opened with read permission. fopen(): %s\n", srcpath, strerror(errno));
210 if ((out = fopen(destpath, "wb")) == NULL)
212 DEBUG_ERROR("copyfile is failed. The destpath[%s] is not opened with write permission. fopen(): %s\n", destpath, strerror(errno));
216 if ((buf = (char *) malloc(COPY_BUF_SIZE)) == NULL)
218 DEBUG_ERROR("copyfile is failed. Memory allocation for copy buffer is failed. Request size [%d]. malloc(): %s\n", COPY_BUF_SIZE, strerror(errno));
222 while ((len = fread(buf, sizeof(char), sizeof(buf), in)) != 0)
224 if (fwrite(buf, sizeof(char), len, out) == 0)
226 /* Reads "len" string. But, it fails to write file.
227 We need to remove wrong result. */
228 DEBUG_ERROR("copyfile is failed. fwrite fails to write. fwrite(): %s\n", strerror(errno));
233 if (remove(destpath) == -1)
234 DEBUG_ERROR("removing copied file with error is failed. remove(): %s\n", strerror(errno));
240 /* Copy file DONE! */
242 if (out) fclose(out);
248 if (out) fclose(out);
253 static int move_path(const char *srcpath, const char *destpath)
259 d = opendir(srcpath);
260 while ((e = readdir(d)))
262 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
264 char srcdir[MAX_FILE_LEN];
265 char destdir[MAX_FILE_LEN];
268 if (strlen(srcpath) + strlen((char *)e->d_name) + 2 >= MAX_FILE_LEN)
270 DEBUG_ERROR("srcdir length is not available \n");
274 if (strlen(destpath) + strlen((char *)e->d_name) + 2 >= MAX_FILE_LEN)
276 DEBUG_ERROR("destdir length is not available \n");
280 sprintf(srcdir, "%s/%s", srcpath, (char *)e->d_name);
281 sprintf(destdir, "%s/%s", destpath, (char *)e->d_name);
283 if (stat(srcdir, &statb) == -1)
285 DEBUG_ERROR("stat %s is failed \n",srcdir);
289 if (S_ISDIR(statb.st_mode))
291 if (make_dir(destdir) < 0)
293 DEBUG_ERROR("make current directory %s is failed \n", destdir);
299 DEBUG_LOG("srcdir =%s\n", (char *)srcdir);
300 DEBUG_LOG("destdir =%s\n", (char *)destdir);
301 DEBUG_LOG("file name =%s\n", (char *)e->d_name);
303 /* We need copy font file instead of rename() function.
304 To apply smack label of parent directory, we need create new file to the path. */
305 ret = copy_file(srcdir, destdir);
308 DEBUG_ERROR("copy_file is failed \n");
312 if (remove(srcdir) == -1)
313 DEBUG_ERROR("removing src file[%s] is failed. remove(): %s\n", srcdir, strerror(errno));
325 #ifdef CHECK_FAMILY_NAME
326 static int check_family_name(const char *srcpath)
328 FcObjectSet *os = NULL;
329 FcFontSet* fs = NULL;
330 FcPattern* pat = NULL;
331 FcConfig* font_config = NULL;
332 bool is_only_one_family = false;
334 font_config = FcInitLoadConfigAndFonts();
336 if(font_config == NULL) {
337 DEBUG_ERROR("Failed: FcInitLoadConfigAndFonts");
341 pat = FcPatternCreate();
343 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
346 fs = FcFontList(font_config, pat, os);
347 FcObjectSetDestroy(os);
352 FcPatternDestroy(pat);
359 char* only_one_family = NULL;
361 for (j = 0; j < fs->nfont; j++)
363 FcChar8 *family = NULL;
364 FcChar8 *file = NULL;
366 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
368 int font_path_len = strlen(srcpath);
370 /* always shown for src path */
371 if (strncmp((const char*)file, srcpath, font_path_len) == 0)
373 if (FcPatternGetString(fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
375 DEBUG_ERROR("Family name is invalid");
378 //DEBUG_ERROR("-------- FOUND FONT - family = %s", (char *)family);
380 // first found family name
381 if (only_one_family == NULL && strlen((char *)family) > 0)
383 is_only_one_family = true;
384 only_one_family = (char *)family;
385 //DEBUG_ERROR("--------First FOUND FONT - family = %s", only_one_family);
387 else if (only_one_family != NULL && strlen((char *)family) > 0)// after first
389 if (strcmp(only_one_family, (char *)family) != 0)
391 DEBUG_ERROR("Not supported various font famliy. only one font famliy. %s %s", only_one_family, (char *)family);
392 is_only_one_family = false;
398 DEBUG_ERROR("invalid fonts");
405 FcFontSetDestroy(fs);
409 FcConfigDestroy(font_config);
412 if (is_only_one_family == false)
414 //DEBUG_ERROR("Not supported various font famliy. only one font famliy");
422 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle)
424 char destdir[MAX_FILE_LEN];
425 char path[MAX_FILE_LEN];
429 if (strlen (parent) + strlen (appid) + 2 >= MAX_FILE_LEN )
431 DEBUG_ERROR("appid length is not available \n");
435 sprintf(destdir,"%s/%s",parent,appid);
436 ret = make_dir(destdir);
439 DEBUG_ERROR("make current directory %s is failed \n", destdir);
443 if (strlen (rootpath) + 2 >= MAX_FILE_LEN )
445 DEBUG_ERROR("rootpath length is not available \n");
449 ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
452 DEBUG_ERROR("pkgmgrinfo_pkginfo_get_type is failed\n");
456 sprintf(path,"%s/shared/res", rootpath);
458 if (!strcmp(type,"wgt"))
460 char srcpath[MAX_FILE_LEN];
461 sprintf(srcpath,"%s/res/wgt/shared/res", rootpath);
462 ret = move_path(srcpath, path);
467 DEBUG_ERROR("move_path is failed\n");
471 ret = symbolic_link(path, destdir);
475 DEBUG_ERROR("install is failed \n", destdir);
479 #ifdef CHECK_FAMILY_NAME
480 ret = check_family_name(destdir);
484 DEBUG_ERROR("Invaid font files\n");
492 do_uninstall(destdir);
497 static int do_uninstall(const char *deletedir)
502 d = opendir(deletedir);
503 while((e = readdir (d)))
505 if (e->d_name[0] != '.')
507 char destfile[MAX_FILE_LEN];
510 if (strlen (deletedir) + strlen ((char *) e->d_name) + 2 >= MAX_FILE_LEN )
512 DEBUG_ERROR("destfile length is not available \n");
516 sprintf(destfile,"%s/%s",deletedir,(char *) e->d_name);
517 if (lstat (destfile, &statb) == -1)
519 DEBUG_ERROR("lstat %s is failed \n",destfile);
523 if (S_ISDIR (statb.st_mode))
525 if (do_uninstall(destfile)<0)
528 else if (unlink(destfile) < 0)
530 DEBUG_ERROR("unlink is failed %s\n",destfile);
533 DEBUG_LOG("destfile =%s\n",destfile);
539 if (rmdir(deletedir) < 0)
541 DEBUG_ERROR("rmdir is failed \n");
545 DEBUG_LOG("rmdir =%s\n",deletedir);
554 int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
557 pkgmgrinfo_pkginfo_h handle = NULL;
558 const char *app_root_path = NULL;
559 const char *dest_path = NULL;
562 pkgmgr_installer_info_get_target_uid(&uid);
563 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
567 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
571 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
574 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
580 DEBUG_ERROR("appid is NULL \n");
584 ret = make_dir(PARENT_PATH);
588 DEBUG_ERROR("make current directory is failed \n");
592 dest_path = check_preloaded(app_root_path);
594 if (make_dir(dest_path)<0)
596 DEBUG_ERROR("make current directory is failed \n");
600 ret = do_install(dest_path,appid, app_root_path, handle);
603 DEBUG_ERROR("font install is failed \n");
607 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
612 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
617 int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
619 char deletedir[MAX_FILE_LEN];
621 pkgmgrinfo_pkginfo_h handle = NULL;
622 const char* app_root_path = NULL;
623 const char* dest_path = NULL;
626 pkgmgr_installer_info_get_target_uid(&uid);
627 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
631 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
635 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
638 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
644 DEBUG_ERROR("appid is NULL \n");
648 dest_path = check_preloaded(app_root_path);
650 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
652 DEBUG_ERROR("appid length is not available \n");
656 sprintf(deletedir,"%s/%s", dest_path, appid);
658 if (access(deletedir, F_OK) == -1)
660 DEBUG_ERROR("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
664 ret = do_uninstall(deletedir);
667 DEBUG_ERROR("do_uninstall is failed \n");
671 ret = make_dir(PARENT_PATH);
674 DEBUG_ERROR("make current directory(%s) is failed: %s\n", PARENT_PATH, strerror(errno));
678 ret = make_dir(dest_path);
681 DEBUG_ERROR("make current directory(%s) is failed: %s\n", dest_path, strerror(errno));
685 ret = do_install(dest_path, appid, app_root_path, handle);
688 DEBUG_ERROR("do_install is failed \n");
692 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
696 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
702 int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
704 char deletedir[MAX_FILE_LEN];
705 FcObjectSet *os = NULL;
706 FcPattern *pat = NULL;
707 FcFontSet *fs = NULL;
708 pkgmgrinfo_pkginfo_h handle = NULL;
709 const char* app_root_path = NULL;
710 const char *dest_path = NULL;
711 int ret, deletedir_len;
718 DEBUG_ERROR("appid is NULL \n");
722 pkgmgr_installer_info_get_target_uid(&uid);
723 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
727 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
731 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
734 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
738 dest_path = check_preloaded(app_root_path);
740 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
742 DEBUG_ERROR("appid length is not available \n");
746 /* It must contain "/" character at end of the delete dir path.
747 * It prevents file path comparing issues when there are many similar path. */
748 sprintf(deletedir,"%s/%s/", dest_path, appid);
750 //check if current using font is same with uninstall font
751 deletedir_len = strlen(deletedir);
753 pat = FcPatternCreate();
756 DEBUG_ERROR("FcPatternCreate is NULL \n");
760 char *current_font_name = NULL;
761 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, ¤t_font_name);
764 DEBUG_ERROR("Get current font is failed \n");
768 DEBUG_LOG("current_font_name =%s\n",current_font_name);
770 FcInitLoadConfigAndFonts();
771 FcPatternAddString (pat, FC_FAMILY,(FcChar8*)current_font_name);
772 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
773 fs = FcFontList(NULL, pat, os);
775 FcPatternDestroy(pat);
776 free(current_font_name);
782 for (j = 0; j < fs->nfont; j++)
784 FcChar8 *file = NULL;
785 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
787 DEBUG_LOG("file =%s, deletedir =%s\n", file, deletedir);
788 if (strncmp((const char*)file, deletedir, deletedir_len) == 0)
790 DEBUG_LOG("change to default font\n");
791 char *default_font_name = NULL;
793 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEFAULT_FONT_TYPE, &default_font_name);
796 DEBUG_ERROR("Get default font is failed \n");
800 DEBUG_LOG("default_font_name = %s \n",default_font_name);
801 ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, default_font_name);
804 DEBUG_ERROR("Set default font is failed ret=%d \n",ret);
805 free(default_font_name);
809 free(default_font_name);
810 DEBUG_LOG("success change to default font\n");
817 if (access(deletedir, F_OK) == -1)
819 DEBUG_ERROR("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
824 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
826 return do_uninstall(deletedir);
830 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
836 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
838 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
841 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
843 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
846 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
848 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
852 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
854 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
857 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
859 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
862 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
864 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);