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
16 #include "file_ex.h"
17 #include <string>
18 #include <fstream>
19 #include <iostream>
20 #include <algorithm>
21 #include <iterator>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <cstdio>
26 #include <securec.h>
27 #include <cstring>
28 #include "directory_ex.h"
29 #include "utils_log.h"
30
31 using namespace std;
32
33 const int MAX_FILE_LENGTH = 32 * 1024 * 1024;
34
35 namespace OHOS {
LoadStringFromFile(const string & filePath,string & content)36 bool LoadStringFromFile(const string& filePath, string& content)
37 {
38 ifstream file(filePath.c_str());
39 if (!file.is_open()) {
40 UTILS_LOGD("open file failed! filePath:%{public}s", filePath.c_str());
41 return false;
42 }
43
44 file.seekg(0, ios::end);
45 int fileLength = file.tellg();
46 if (fileLength > MAX_FILE_LENGTH) {
47 UTILS_LOGD("invalid file length(%{public}d)", fileLength);
48 return false;
49 }
50
51 content.clear();
52 file.seekg(0, ios::beg);
53 copy(istreambuf_iterator<char>(file), istreambuf_iterator<char>(), back_inserter(content));
54 return true;
55 }
56
GetFileNameByFd(const int fd)57 string GetFileNameByFd(const int fd)
58 {
59 if (fd <= 0) {
60 return string();
61 }
62
63 string fdPath = "/proc/self/fd/" + std::to_string(fd);
64 char fileName[PATH_MAX + 1] = {0};
65
66 int ret = readlink(fdPath.c_str(), fileName, PATH_MAX);
67 if (ret < 0 || ret > PATH_MAX) {
68 UTILS_LOGD("Get fileName failed, ret is: %{public}d!", ret);
69 return string();
70 }
71 fileName[ret] = '\0';
72 return string(fileName);
73 }
74
LoadStringFromFdToFile(int fd,string & content)75 bool LoadStringFromFdToFile(int fd, string& content)
76 {
77 string fileName = GetFileNameByFd(fd);
78 if (fileName.empty()) {
79 UTILS_LOGD("LoadStringFromFd get file name by fd failed!");
80 return false;
81 }
82
83 if (!LoadStringFromFile(fileName, content)) {
84 UTILS_LOGE("LoadStringFromFd get string from file failed!");
85 return false;
86 }
87 return true;
88 }
89
LoadStringFromFd(int fd,string & content)90 bool LoadStringFromFd(int fd, string& content)
91 {
92 if (fd <= 0) {
93 UTILS_LOGD("invalid fd:%{public}d", fd);
94 return false;
95 }
96
97 const long fileLength = lseek(fd, 0, SEEK_END);
98 if (fileLength > MAX_FILE_LENGTH) {
99 UTILS_LOGE("invalid file length");
100 return false;
101 }
102
103 // lseek is not support the linux file node
104 if (fileLength < 0) {
105 return LoadStringFromFdToFile(fd, content);
106 }
107
108 if (fileLength == 0) {
109 return true;
110 }
111
112 content.resize(fileLength);
113 int loc = lseek(fd, 0, SEEK_SET);
114 if (loc == -1) {
115 UTILS_LOGE("lseek file to begin failed!");
116 return false;
117 }
118
119 const long len = read(fd, content.data(), fileLength);
120 if (len != fileLength) {
121 UTILS_LOGE("the length read from file is not equal to fileLength");
122 return false;
123 }
124
125 return true;
126 }
127
SaveStringToFile(const std::string & filePath,const std::string & content,bool truncated)128 bool SaveStringToFile(const std::string& filePath, const std::string& content, bool truncated /* = true */)
129 {
130 if (content.empty()) {
131 UTILS_LOGI("content is empty, no need to save!");
132 return true;
133 }
134
135 ofstream file;
136 if (truncated) {
137 file.open(filePath.c_str(), ios::out | ios::trunc);
138 } else {
139 file.open(filePath.c_str(), ios::out | ios::app);
140 }
141
142 if (!file.is_open()) {
143 UTILS_LOGD("open file failed! filePath:%{private}s", filePath.c_str());
144 return false;
145 }
146
147 file.write(content.c_str(), content.length());
148 if (file.fail()) {
149 UTILS_LOGE("write content to file failed!file:%{private}s, content:%{private}s",
150 filePath.c_str(), content.c_str());
151 return false;
152 }
153 return true;
154 }
155
SaveStringToFd(int fd,const std::string & content)156 bool SaveStringToFd(int fd, const std::string& content)
157 {
158 if (fd <= 0) {
159 UTILS_LOGD("invalid fd:%{public}d", fd);
160 return false;
161 }
162
163 if (content.empty()) {
164 UTILS_LOGI("content is empty, no need to save!");
165 return true;
166 }
167
168 const long len = write(fd, content.c_str(), content.length());
169 if (len < 0) {
170 UTILS_LOGE("write file failed!errno:%{public}d, err:%{public}s", errno, strerror(errno));
171 return false;
172 }
173
174 if (static_cast<unsigned long>(len) != content.length()) {
175 UTILS_LOGE("the length write to file is not equal to fileLength");
176 return false;
177 }
178
179 return true;
180 }
181
LoadBufferFromNodeFile(const string & filePath,vector<char> & content)182 bool LoadBufferFromNodeFile(const string& filePath, vector<char>& content)
183 {
184 string realPath;
185 if (!PathToRealPath(filePath, realPath)) {
186 UTILS_LOGD("filePath to realPath failed! filePath:%{private}s", filePath.c_str());
187 return false;
188 }
189
190 FILE *fp = fopen(realPath.c_str(), "r");
191 if (fp == nullptr) {
192 UTILS_LOGD("open file failed! filePath:%{private}s", realPath.c_str());
193 return false;
194 }
195
196 int byteCount = 1;
197 while (!feof(fp)) {
198 if (byteCount > MAX_FILE_LENGTH) {
199 UTILS_LOGE("LoadBufferFromNodeFile invalid file length(%{public}d)!", byteCount);
200 fclose(fp);
201 fp = nullptr;
202 content.clear();
203 return false;
204 }
205 char ch = fgetc(fp);
206 content.push_back(ch);
207 byteCount++;
208 }
209
210 fclose(fp);
211 fp = nullptr;
212 return true;
213 }
214
215 /* load file to buffer. If the buffer is not empty,then overwrite */
LoadBufferFromFile(const string & filePath,vector<char> & content)216 bool LoadBufferFromFile(const string& filePath, vector<char>& content)
217 {
218 ifstream file;
219 file.open(filePath.c_str(), ios::in | ios::binary);
220 if (!file.is_open()) {
221 UTILS_LOGD("open file failed! filePath:%{private}s", filePath.c_str());
222 return false;
223 }
224
225 file.seekg(0, std::ios::end);
226 const long fileLength = file.tellg();
227 if (fileLength > MAX_FILE_LENGTH) {
228 UTILS_LOGD("invalid file length");
229 return false;
230 }
231
232 // lseek is not support the linux file node
233 if (fileLength < 0) {
234 return LoadBufferFromNodeFile(filePath, content);
235 }
236
237 if (fileLength == 0) {
238 content.clear();
239 return true;
240 }
241
242 file.seekg(0, std::ios::beg);
243 if (file.fail()) {
244 UTILS_LOGE("seekg file to begin failed!filePath:%{private}s", filePath.c_str());
245 return false;
246 }
247
248 content.resize(fileLength);
249 file.read(&content[0], fileLength);
250 return true;
251 }
252
SaveBufferToFile(const string & filePath,const vector<char> & content,bool truncated)253 bool SaveBufferToFile(const string& filePath, const vector<char>& content, bool truncated /* = true */)
254 {
255 if (content.empty()) {
256 UTILS_LOGI("content is empty, no need to save!");
257 return true;
258 }
259
260 // if the file is not exist,create it first!
261 ios_base::openmode mode = truncated ? (ios::out | ios::binary | ios::trunc) : (ios::out | ios::binary | ios::app);
262 ofstream file;
263 file.open(filePath.c_str(), mode);
264 if (!file.is_open()) {
265 UTILS_LOGD("open file failed! filePath:%{private}s, mode:%{private}d", filePath.c_str(), mode);
266 return false;
267 }
268
269 file.write(&content[0], content.size());
270 return true;
271 }
272
FileExists(const string & fileName)273 bool FileExists(const string& fileName)
274 {
275 return (access(fileName.c_str(), F_OK) == 0);
276 }
277
StringExistsInFile(const string & fileName,const string & subStr,bool caseSensitive)278 bool StringExistsInFile(const string& fileName, const string& subStr, bool caseSensitive /* = true */)
279 {
280 if (subStr.empty()) {
281 UTILS_LOGD("String is empty");
282 return false;
283 }
284
285 string str;
286 if (!LoadStringFromFile(fileName, str)) {
287 UTILS_LOGD("File load fail, filePath:%{private}s", fileName.c_str());
288 return false;
289 }
290
291 if (caseSensitive) {
292 return (str.find(subStr) != string::npos);
293 }
294
295 string strlower(str);
296 string sublower(subStr);
297 transform(str.begin(), str.end(), strlower.begin(), ::tolower);
298 transform(subStr.begin(), subStr.end(), sublower.begin(), ::tolower);
299 return (strlower.find(sublower) != string::npos);
300 }
301
CountStrInStr(const string & str,const string & subStr)302 int CountStrInStr(const string& str, const string& subStr)
303 {
304 if (subStr.empty()) {
305 UTILS_LOGD("subStr is empty");
306 return 0;
307 }
308
309 size_t position = 0;
310 int count = 0;
311 size_t length = subStr.length();
312 while ((position = str.find(subStr, position)) != string::npos) {
313 position += length;
314 count++;
315 }
316
317 return count;
318 }
319
CountStrInFile(const string & fileName,const string & subStr,bool caseSensitive)320 int CountStrInFile(const string& fileName, const string& subStr, bool caseSensitive /* = true */)
321 {
322 if (subStr.empty()) {
323 UTILS_LOGD("String is empty");
324 return -1;
325 }
326
327 string str;
328 if (!LoadStringFromFile(fileName, str)) {
329 UTILS_LOGD("File load fail, filePath:%{private}s", fileName.c_str());
330 return -1;
331 }
332
333 // If case-insensitive, strings are converted to lowercase.
334 if (caseSensitive) {
335 return CountStrInStr(str, subStr);
336 }
337
338 string strlower(str);
339 string sublower(subStr);
340 transform(str.begin(), str.end(), strlower.begin(), ::tolower);
341 transform(subStr.begin(), subStr.end(), sublower.begin(), ::tolower);
342 return CountStrInStr(strlower, sublower);
343 }
344 }
345