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 constexpr int32_t SUCCESS = 0;
Create(const std::string & path,mode_t mode)26 bool FileOperator::Create(const std::string &path, mode_t mode)
27 {
28 auto ret = mkdir(path.c_str(), mode);
29 if (ret != SUCCESS) {
30 IMSA_HILOGE("%{public}s mkdir failed, errno:%{public}d!", path.c_str(), errno);
31 return false;
32 }
33 return true;
34 }
35
IsExist(const std::string & path)36 bool FileOperator::IsExist(const std::string &path)
37 {
38 return access(path.c_str(), F_OK) == SUCCESS;
39 }
40
Read(const std::string & path,std::string & content)41 bool FileOperator::Read(const std::string &path, std::string &content)
42 {
43 std::ifstream file(path);
44 if (!file.is_open()) {
45 IMSA_HILOGE("%{public}s open fail!", path.c_str());
46 return false;
47 }
48 std::string sLine;
49 while (getline(file, sLine)) {
50 content.append(sLine);
51 }
52 return true;
53 }
54
Write(const std::string & path,const std::string & content,int32_t flags,mode_t mode)55 bool FileOperator::Write(const std::string &path, const std::string &content, int32_t flags, mode_t mode)
56 {
57 const char* fopenMode;
58 if (flags & O_TRUNC) {
59 fopenMode = "w";
60 } else if (flags & O_APPEND) {
61 fopenMode = "a";
62 } else if (flags & O_CREAT) {
63 fopenMode = "w";
64 } else {
65 fopenMode = "r+";
66 }
67 FILE* file = fopen(path.c_str(), fopenMode);
68 if (file == nullptr) {
69 IMSA_HILOGE("%{public}s open fail, errno: %{public}d", path.c_str(), errno);
70 return false;
71 }
72 auto ret = fwrite(content.c_str(), 1, content.size(), file);
73 if (ret != content.size()) {
74 IMSA_HILOGE("%{public}s write fail, ret: %{public}zd, errno: %{public}d", path.c_str(), ret, errno);
75 if (fclose(file) != 0) {
76 IMSA_HILOGE("%{public}s close fail, errno: %{public}d", path.c_str(), errno);
77 }
78 return false;
79 }
80 if (fclose(file) != 0) {
81 IMSA_HILOGE("%{public}s close fail, errno: %{public}d", path.c_str(), errno);
82 }
83 return true;
84 }
85
Read(const std::string & path,const std::string & key,std::string & content)86 bool FileOperator::Read(const std::string &path, const std::string &key, std::string &content)
87 {
88 if (key.empty()) {
89 IMSA_HILOGE("key is empty!");
90 return false;
91 }
92 CfgFiles *cfgFiles = GetCfgFiles(path.c_str());
93 if (cfgFiles == nullptr) {
94 IMSA_HILOGE("%{public}s cfgFiles is nullptr!", path.c_str());
95 return false;
96 }
97 // parse config files, ordered by priority from high to low
98 for (int32_t i = MAX_CFG_POLICY_DIRS_CNT - 1; i >= 0; i--) {
99 auto realPath = GetRealPath(cfgFiles->paths[i]);
100 if (realPath.empty()) {
101 continue;
102 }
103 content = Read(realPath, key);
104 if (!content.empty()) {
105 break;
106 }
107 }
108 FreeCfgFiles(cfgFiles);
109 return !content.empty();
110 }
111
Read(const std::string & path,const std::string & key)112 std::string FileOperator::Read(const std::string &path, const std::string &key)
113 {
114 std::string content;
115 bool ret = Read(path, content);
116 if (!ret) {
117 IMSA_HILOGE("%{public}s read failed!", path.c_str());
118 return "";
119 }
120 if (content.find(key) == std::string::npos) {
121 IMSA_HILOGD("%{public}s not contain %{public}s!", path.c_str(), key.c_str());
122 return "";
123 }
124 return content;
125 }
126
GetRealPath(const char * path)127 std::string FileOperator::GetRealPath(const char *path)
128 {
129 if (path == nullptr) {
130 return "";
131 }
132 auto size = strnlen(path, PATH_MAX);
133 if (size == 0 || size == PATH_MAX) {
134 return "";
135 }
136 char realPath[PATH_MAX] = { 0x00 };
137 if (realpath(path, realPath) == nullptr) {
138 IMSA_HILOGE("failed to get realpath!");
139 return "";
140 }
141 return std::string(realPath);
142 }
143 } // namespace MiscServices
144 } // namespace OHOS