1 /*
2 * Copyright (c) 2023 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 #include "custom_utd_store.h"
16 #include <fstream>
17 #include <sys/stat.h>
18 #include "unistd.h"
19 #include "error_code.h"
20 #include "logger.h"
21 #ifdef WITH_SELINUX
22 #include <policycoreutils.h>
23 #endif
24 namespace OHOS {
25 namespace UDMF {
26 constexpr const char* UTD_CFG_FILE = "utd-adt.json";
27
CustomUtdStore()28 CustomUtdStore::CustomUtdStore()
29 {
30 }
31
~CustomUtdStore()32 CustomUtdStore::~CustomUtdStore()
33 {
34 }
35
GetInstance()36 CustomUtdStore &CustomUtdStore::GetInstance()
37 {
38 static CustomUtdStore utdCustomPersistence;
39 return utdCustomPersistence;
40 }
41
GetTypeCfgs(const std::string & cfgFilePath)42 std::vector<TypeDescriptorCfg> CustomUtdStore::GetTypeCfgs(const std::string &cfgFilePath)
43 {
44 LOG_DEBUG(UDMF_CLIENT, "get utdcustom from cfg, Path:%{public}s.", cfgFilePath.c_str());
45 std::string jsonStr;
46 std::ifstream fin(cfgFilePath);
47 while (fin.good()) {
48 std::string line;
49 std::getline(fin, line);
50 jsonStr += line;
51 }
52 std::vector<TypeDescriptorCfg> customUtdTypes;
53 utdJsonParser_.ParseStoredCustomUtdJson(jsonStr, customUtdTypes);
54 LOG_DEBUG(UDMF_CLIENT, "GetTypeCfgs, customUtdTypes total:%{public}zu.", customUtdTypes.size());
55 return customUtdTypes;
56 }
57
SaveTypeCfgs(const std::vector<TypeDescriptorCfg> & customUtdTypes,const std::string & cfgFilePath)58 int32_t CustomUtdStore::SaveTypeCfgs(const std::vector<TypeDescriptorCfg> &customUtdTypes,
59 const std::string &cfgFilePath)
60 {
61 std::string jsonData;
62 std::string cfgFileName = UTD_CFG_FILE;
63 std::string cfgFileDir = cfgFilePath.substr(0, cfgFilePath.length() - cfgFileName.length());
64 if (utdJsonParser_.ConvertUtdCfgsToJson(customUtdTypes, jsonData) && CreateDirectory(cfgFileDir)) {
65 SavaCfgFile(jsonData, cfgFilePath);
66 }
67 return 0;
68 }
69
SavaCfgFile(const std::string & jsonData,const std::string & cfgFilePath)70 int32_t CustomUtdStore::SavaCfgFile(const std::string &jsonData, const std::string &cfgFilePath)
71 {
72 std::ofstream ofs;
73 LOG_DEBUG(UDMF_CLIENT, "set cfg start, path:%{public}s ", cfgFilePath.c_str());
74 ofs.open(cfgFilePath, 0x02);
75 LOG_DEBUG(UDMF_CLIENT, "set cfg, is_open= %{public}d", ofs.is_open());
76 ofs << jsonData << std::endl;
77 ofs.close();
78 LOG_DEBUG(UDMF_CLIENT, "set cfg end.");
79 return 0;
80 }
81
CreateDirectory(const std::string & path) const82 bool CustomUtdStore::CreateDirectory(const std::string &path) const
83 {
84 LOG_DEBUG(UDMF_CLIENT, "CreateDirectory start, path:%{public}s ", path.c_str());
85 if (access(path.c_str(), F_OK) == 0) {
86 return true;
87 }
88
89 std::string::size_type index = 0;
90 do {
91 std::string subPath;
92 index = path.find('/', index + 1);
93 if (index == std::string::npos) {
94 subPath = path;
95 } else {
96 subPath = path.substr(0, index);
97 }
98
99 if (access(subPath.c_str(), F_OK) != 0) {
100 if (mkdir(subPath.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0) {
101 LOG_WARN(UDMF_CLIENT, "CreateDirectory, fail. path:%{public}s, subPath:%{public}s.",
102 path.c_str(), subPath.c_str());
103 return false;
104 }
105 }
106 } while (index != std::string::npos);
107
108 if (access(path.c_str(), F_OK) == 0) {
109 #ifdef WITH_SELINUX
110 Restorecon(path.c_str());
111 #endif
112 return true;
113 }
114
115 return false;
116 }
117 } // namespace UDMF
118 } // namespace OHOS