• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "font_manager_client.h"
17 
18 #include <fcntl.h>
19 
20 #include "font_define.h"
21 #include "font_hilog.h"
22 #include "font_service_load_manager.h"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace FontManager {
27 
InstallFont(const std::string & fontPath,int & outValue)28 int32_t FontManagerClient::InstallFont(const std::string &fontPath, int &outValue)
29 {
30     sptr<IFontService> service = FontServiceLoadManager::GetInstance()->GetFontServiceAbility(FONT_SA_ID);
31     if (service == nullptr) {
32         FONT_LOGE("Service is null");
33         outValue = ERR_INSTALL_FAIL;
34         return SUCCESS;
35     }
36     std::string realPath;
37     if (!PathToRealPath(fontPath, realPath)) {
38         FONT_LOGE("failed to get real path %{private}s, errno %{public}d", fontPath.c_str(), errno);
39         outValue = ERR_FILE_NOT_EXISTS;
40         return SUCCESS;
41     }
42     int fd = open(realPath.c_str(), O_RDONLY);
43     if (fd < 0) {
44         FONT_LOGE("open font file failed, errno: %{public}d", errno);
45         outValue = ERR_FILE_NOT_EXISTS;
46         return SUCCESS;
47     }
48 
49     int32_t ret = service->InstallFont(fd, outValue);
50     if (fd >= 0) {
51         close(fd);
52     }
53     return ret;
54 }
55 
UninstallFont(const std::string & fontName,int & outValue)56 int32_t FontManagerClient::UninstallFont(const std::string &fontName, int &outValue)
57 {
58     sptr<IFontService> service = FontServiceLoadManager::GetInstance()->GetFontServiceAbility(FONT_SA_ID);
59     if (service == nullptr) {
60         FONT_LOGE("Service is null");
61         outValue = ERR_UNINSTALL_FAIL;
62         return SUCCESS;
63     }
64     return service->UninstallFont(fontName, outValue);
65 }
66 
PathToRealPath(const std::string & path,std::string & realPath)67 bool FontManagerClient::PathToRealPath(const std::string& path, std::string& realPath)
68 {
69     if (path.empty()) {
70         FONT_LOGE("path is empty!");
71         return false;
72     }
73 
74     if ((path.length() >= PATH_MAX)) {
75         FONT_LOGE("path len is error, the len is: [%{public}zu]", path.length());
76         return false;
77     }
78 
79     char tmpPath[PATH_MAX] = {0};
80     if (realpath(path.c_str(), tmpPath) == nullptr) {
81         FONT_LOGE("path (%{public}s) to realpath error: %{public}s", path.c_str(), strerror(errno));
82         return false;
83     }
84 
85     realPath = tmpPath;
86     if (access(realPath.c_str(), F_OK) != 0) {
87         FONT_LOGE("check realpath (%{private}s) error: %{public}s", realPath.c_str(), strerror(errno));
88         return false;
89     }
90     return true;
91 }
92 } // namespace FontManager
93 } // namespace Global
94 } // namespace OHOS