1 /*
2 * Copyright (c) 2024 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 "file_operator.h"
17
18 #include <fstream>
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 #include "global.h"
23 namespace OHOS {
24 namespace MiscServices {
25 // LCOV_EXCL_START
26 constexpr int32_t SUCCESS = 0;
Create(const std::string & path,mode_t mode)27 bool FileOperator::Create(const std::string &path, mode_t mode)
28 {
29 auto ret = mkdir(path.c_str(), mode);
30 if (ret != SUCCESS) {
31 IMSA_HILOGE("%{public}s mkdir failed, errno:%{public}d!", path.c_str(), errno);
32 return false;
33 }
34 return true;
35 }
36 // LCOV_EXCL_STOP
IsExist(const std::string & path)37 bool FileOperator::IsExist(const std::string &path)
38 {
39 return access(path.c_str(), F_OK) == SUCCESS;
40 }
41
Read(const std::string & path,std::string & content)42 bool FileOperator::Read(const std::string &path, std::string &content)
43 {
44 std::ifstream file(path);
45 if (!file.is_open()) {
46 IMSA_HILOGE("%{public}s open fail!", path.c_str());
47 return false;
48 }
49 std::string sLine;
50 while (getline(file, sLine)) {
51 content.append(sLine);
52 }
53 return true;
54 }
55 // LCOV_EXCL_START
IsValidPath(const std::string & filePath)56 bool FileOperator::IsValidPath(const std::string &filePath)
57 {
58 if (filePath.find("../") != std::string::npos) {
59 IMSA_HILOGE("FilePath contains '../'");
60 return false;
61 }
62 if (filePath.find("./") != std::string::npos) {
63 IMSA_HILOGE("FilePath contains './'");
64 return false;
65 }
66 if (!filePath.empty() && filePath[0] != '/') {
67 IMSA_HILOGE("FilePath is not an absolute path");
68 return false;
69 }
70 return true;
71 }
72
CheckImeCfgFilePath(const std::string & path)73 bool FileOperator::CheckImeCfgFilePath(const std::string &path)
74 {
75 if (path.empty()) {
76 IMSA_HILOGE("Path is empty");
77 return false;
78 }
79 if (!IsValidPath(path)) {
80 IMSA_HILOGE("Path is IsValidPath");
81 return false;
82 }
83 return true;
84 }
85
Write(const std::string & path,const std::string & content,uint32_t flags,mode_t mode)86 bool FileOperator::Write(const std::string &path, const std::string &content, uint32_t flags, mode_t mode)
87 {
88 IMSA_HILOGD("content: %{public}s.", content.c_str());
89 const char* fopenMode;
90 if (flags & O_TRUNC) {
91 fopenMode = "w";
92 } else if (flags & O_APPEND) {
93 fopenMode = "a";
94 } else if (flags & O_CREAT) {
95 fopenMode = "w";
96 } else {
97 fopenMode = "r+";
98 }
99
100 if (!CheckImeCfgFilePath(path)) {
101 IMSA_HILOGE("path check fail");
102 return false;
103 }
104
105 FILE* file = fopen(path.c_str(), fopenMode);
106 if (file == nullptr) {
107 IMSA_HILOGE("%{public}s open fail, errno: %{public}d", path.c_str(), errno);
108 return false;
109 }
110 auto ret = fwrite(content.c_str(), 1, content.size(), file);
111 if (ret != content.size()) {
112 IMSA_HILOGE("%{public}s write fail, ret: %{public}zd, errno: %{public}d", path.c_str(), ret, errno);
113 if (fclose(file) != 0) {
114 IMSA_HILOGE("%{public}s close fail, errno: %{public}d", path.c_str(), errno);
115 }
116 return false;
117 }
118 if (fclose(file) != 0) {
119 IMSA_HILOGE("%{public}s close fail, errno: %{public}d", path.c_str(), errno);
120 }
121
122 return true;
123 }
124 // LCOV_EXCL_STOP
Read(const std::string & path,const std::string & key,std::string & content)125 bool FileOperator::Read(const std::string &path, const std::string &key, std::string &content)
126 {
127 if (key.empty()) {
128 IMSA_HILOGE("key is empty!");
129 return false;
130 }
131 CfgFiles *cfgFiles = GetCfgFiles(path.c_str());
132 if (cfgFiles == nullptr) {
133 IMSA_HILOGE("%{public}s cfgFiles is nullptr!", path.c_str());
134 return false;
135 }
136 // parse config files, ordered by priority from high to low
137 for (int32_t i = MAX_CFG_POLICY_DIRS_CNT - 1; i >= 0; i--) {
138 auto realPath = GetRealPath(cfgFiles->paths[i]);
139 if (realPath.empty()) {
140 continue;
141 }
142 content = Read(realPath, key);
143 if (!content.empty()) {
144 break;
145 }
146 }
147 FreeCfgFiles(cfgFiles);
148 return !content.empty();
149 }
150
Read(const std::string & path,const std::string & key)151 std::string FileOperator::Read(const std::string &path, const std::string &key)
152 {
153 std::string content;
154 bool ret = Read(path, content);
155 if (!ret) {
156 IMSA_HILOGE("%{public}s read failed!", path.c_str());
157 return "";
158 }
159 if (content.find(key) == std::string::npos) {
160 IMSA_HILOGD("%{public}s not contain %{public}s!", path.c_str(), key.c_str());
161 return "";
162 }
163 return content;
164 }
165
GetRealPath(const char * path)166 std::string FileOperator::GetRealPath(const char *path)
167 {
168 if (path == nullptr) {
169 return "";
170 }
171 auto size = strnlen(path, PATH_MAX);
172 if (size == 0 || size == PATH_MAX) {
173 return "";
174 }
175 char realPath[PATH_MAX] = { 0x00 };
176 if (realpath(path, realPath) == nullptr) {
177 IMSA_HILOGE("failed to get realpath!");
178 return "";
179 }
180 return std::string(realPath);
181 }
182 } // namespace MiscServices
183 } // namespace OHOS