1 /*
2 * Copyright (c) 2021-2022 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 "account_file_operator.h"
16 #include <fstream>
17 #include <nlohmann/json.hpp>
18 #include <sstream>
19 #include <string>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include "account_log_wrapper.h"
23 #include "directory_ex.h"
24
25 namespace OHOS {
26 namespace AccountSA {
AccountFileOperator()27 AccountFileOperator::AccountFileOperator()
28 {}
29
~AccountFileOperator()30 AccountFileOperator::~AccountFileOperator()
31 {}
32
CreateDir(const std::string & path)33 ErrCode AccountFileOperator::CreateDir(const std::string &path)
34 {
35 ACCOUNT_LOGI("enter");
36
37 if (!OHOS::ForceCreateDirectory(path)) {
38 return ERR_OSACCOUNT_SERVICE_FILE_CREATE_DIR_ERROR;
39 }
40 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH;
41 mode |= (false ? S_IROTH : 0);
42 bool createFlag = OHOS::ChangeModeDirectory(path, mode);
43 if (!createFlag) {
44 ACCOUNT_LOGE("failed to create dir, path = %{public}s", path.c_str());
45 return ERR_OSACCOUNT_SERVICE_FILE_CHANGE_DIR_MODE_ERROR;
46 }
47
48 return ERR_OK;
49 }
50
DeleteDirOrFile(const std::string & path)51 ErrCode AccountFileOperator::DeleteDirOrFile(const std::string &path)
52 {
53 bool delFlag = false;
54 if (IsExistFile(path)) {
55 delFlag = OHOS::RemoveFile(path);
56 }
57 if (IsExistDir(path)) {
58 delFlag = OHOS::ForceRemoveDirectory(path);
59 }
60 if (!delFlag) {
61 return ERR_OSACCOUNT_SERVICE_FILE_DELE_ERROR;
62 }
63
64 return ERR_OK;
65 }
66
InputFileByPathAndContent(const std::string & path,const std::string & content)67 ErrCode AccountFileOperator::InputFileByPathAndContent(const std::string &path, const std::string &content)
68 {
69 std::string str = path;
70 str.erase(str.rfind('/'));
71 if (!IsExistDir(str)) {
72 ErrCode errCode = CreateDir(str);
73 if (errCode != ERR_OK) {
74 ACCOUNT_LOGE("failed to create dir, str = %{public}s", str.c_str());
75 return ERR_OSACCOUNT_SERVICE_FILE_FIND_DIR_ERROR;
76 }
77 }
78 std::ofstream o(path);
79 if (!o.is_open()) {
80 ACCOUNT_LOGE("failed to open file, path = %{public}s", path.c_str());
81 return ERR_OSACCOUNT_SERVICE_FILE_CREATE_FILE_FAILED_ERROR;
82 }
83 o << content;
84 o.close();
85 ACCOUNT_LOGI("end");
86
87 return ERR_OK;
88 }
89
GetFileContentByPath(const std::string & path,std::string & content)90 ErrCode AccountFileOperator::GetFileContentByPath(const std::string &path, std::string &content)
91 {
92 if (!IsExistFile(path)) {
93 ACCOUNT_LOGE("cannot find file, path = %{public}s", path.c_str());
94 return ERR_OSACCOUNT_SERVICE_FILE_FIND_FILE_ERROR;
95 }
96 std::stringstream buffer;
97 std::ifstream i(path);
98 if (!i.is_open()) {
99 ACCOUNT_LOGE("cannot open file, path = %{public}s", path.c_str());
100 return ERR_OSACCOUNT_SERVICE_FILE_CREATE_FILE_FAILED_ERROR;
101 }
102 buffer << i.rdbuf();
103 content = buffer.str();
104 i.close();
105 return ERR_OK;
106 }
107
IsExistFile(const std::string & path)108 bool AccountFileOperator::IsExistFile(const std::string &path)
109 {
110 if (path.empty()) {
111 return false;
112 }
113
114 struct stat buf = {};
115 if (stat(path.c_str(), &buf) != 0) {
116 return false;
117 }
118
119 return S_ISREG(buf.st_mode);
120 }
121
IsJsonFormat(const std::string & path)122 bool AccountFileOperator::IsJsonFormat(const std::string &path)
123 {
124 std::ifstream fin(path);
125 if (!fin) {
126 return false;
127 }
128
129 nlohmann::json jsonData = nlohmann::json::parse(fin, nullptr, false);
130 fin.close();
131 if (!jsonData.is_structured()) {
132 return false;
133 }
134 return true;
135 }
136
IsExistDir(const std::string & path)137 bool AccountFileOperator::IsExistDir(const std::string &path)
138 {
139 if (path.empty()) {
140 return false;
141 }
142
143 struct stat buf = {};
144 if (stat(path.c_str(), &buf) != 0) {
145 return false;
146 }
147
148 return S_ISDIR(buf.st_mode);
149 }
150 } // namespace AccountSA
151 } // namespace OHOS
152