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 <sys/stat.h>
19 #include <unistd.h>
20 #include <climits>
21 
22 #include <fstream>
23 
24 #include "global.h"
25 namespace OHOS {
26 namespace MiscServices {
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 
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 
Write(const std::string & path,const std::string & content,int32_t flags,mode_t mode)56 bool FileOperator::Write(const std::string &path, const std::string &content, int32_t flags, mode_t mode)
57 {
58     IMSA_HILOGD("content: %{public}s.", content.c_str());
59     auto fd = open(path.c_str(), flags, mode);
60     if (fd < 0) {
61         IMSA_HILOGE("%{public}s open fail, errno: %{public}d", path.c_str(), errno);
62         return false;
63     }
64     auto ret = write(fd, content.c_str(), content.size());
65     if (ret <= 0) {
66         IMSA_HILOGE("%{public}s write fail, ret: %{public}zd, errno: %{public}d", path.c_str(), ret, errno);
67         close(fd);
68         return false;
69     }
70     close(fd);
71     return true;
72 }
73 
Read(const std::string & path,const std::string & key,std::string & content)74 bool FileOperator::Read(const std::string &path, const std::string &key, std::string &content)
75 {
76     if (key.empty()) {
77         IMSA_HILOGE("key is empty!");
78         return false;
79     }
80     CfgFiles *cfgFiles = GetCfgFiles(path.c_str());
81     if (cfgFiles == nullptr) {
82         IMSA_HILOGE("%{public}s cfgFiles is nullptr!", path.c_str());
83         return false;
84     }
85     // parse config files, ordered by priority from high to low
86     for (int32_t i = MAX_CFG_POLICY_DIRS_CNT - 1; i >= 0; i--) {
87         auto realPath = GetRealPath(cfgFiles->paths[i]);
88         if (realPath.empty()) {
89             continue;
90         }
91         content = Read(realPath, key);
92         if (!content.empty()) {
93             break;
94         }
95     }
96     FreeCfgFiles(cfgFiles);
97     return !content.empty();
98 }
99 
Read(const std::string & path,const std::string & key)100 std::string FileOperator::Read(const std::string &path, const std::string &key)
101 {
102     std::string content;
103     bool ret = Read(path, content);
104     if (!ret) {
105         IMSA_HILOGE("%{public}s read failed!", path.c_str());
106         return "";
107     }
108     if (content.find(key) == std::string::npos) {
109         IMSA_HILOGD("%{public}s not contain %{public}s!", path.c_str(), key.c_str());
110         return "";
111     }
112     return content;
113 }
114 
GetRealPath(const char * path)115 std::string FileOperator::GetRealPath(const char *path)
116 {
117     if (path == nullptr) {
118         return "";
119     }
120     auto size = strnlen(path, PATH_MAX);
121     if (size == 0 || size == PATH_MAX) {
122         return "";
123     }
124     char realPath[PATH_MAX] = { 0x00 };
125     if (realpath(path, realPath) == nullptr) {
126         IMSA_HILOGE("failed to get realpath!");
127         return "";
128     }
129     return std::string(realPath);
130 }
131 } // namespace MiscServices
132 } // namespace OHOS