1 /*
2 * Copyright (c) 2022-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 <map>
17 #include <string>
18 #include <tuple>
19 #include <vector>
20
21 #include "b_error/b_error.h"
22 #include "b_json/b_json_entity_ext_manage.h"
23 #include "b_resources/b_constants.h"
24 #include "filemgmt_libhilog.h"
25 #include "json/value.h"
26
27 namespace OHOS::FileManagement::Backup {
28 using namespace std;
29
Stat2JsonValue(struct stat sta)30 Json::Value Stat2JsonValue(struct stat sta)
31 {
32 Json::Value value;
33
34 value["st_size"] = static_cast<int64_t>(sta.st_size);
35 value["st_atim"]["tv_sec"] = static_cast<int64_t>(sta.st_atim.tv_sec);
36 value["st_atim"]["tv_nsec"] = static_cast<int64_t>(sta.st_atim.tv_nsec);
37 value["st_mtim"]["tv_sec"] = static_cast<int64_t>(sta.st_mtim.tv_sec);
38 value["st_mtim"]["tv_nsec"] = static_cast<int64_t>(sta.st_mtim.tv_nsec);
39
40 return value;
41 }
42
JsonValue2Stat(const Json::Value & value)43 struct stat JsonValue2Stat(const Json::Value &value)
44 {
45 struct stat sta = {};
46
47 sta.st_size = value.isMember("st_size") ? value["st_size"].asInt64() : 0;
48 if (value.isMember("st_atim")) {
49 sta.st_atim.tv_sec = value["st_atim"].isMember("tv_sec") ? value["st_atim"]["tv_sec"].asInt64() : 0;
50 sta.st_atim.tv_nsec = value["st_atim"].isMember("tv_nsec") ? value["st_atim"]["tv_nsec"].asInt64() : 0;
51 }
52 if (value.isMember("st_mtim")) {
53 sta.st_mtim.tv_sec = value["st_mtim"].isMember("tv_sec") ? value["st_mtim"]["tv_sec"].asInt64() : 0;
54 sta.st_mtim.tv_nsec = value["st_mtim"].isMember("tv_nsec") ? value["st_mtim"]["tv_nsec"].asInt64() : 0;
55 }
56
57 return sta;
58 }
59
SetExtManage(const map<string,pair<string,struct stat>> & info) const60 void BJsonEntityExtManage::SetExtManage(const map<string, pair<string, struct stat>> &info) const
61 {
62 obj_.clear();
63
64 vector<bool> vec(info.size(), false);
65
66 auto FindLinks = [&vec](map<string, pair<string, struct stat>>::const_iterator it,
67 unsigned long index) -> set<string> {
68 if (it->second.second.st_dev == 0 || it->second.second.st_ino == 0) {
69 return {};
70 }
71
72 set<string> lks;
73 auto item = it;
74 item++;
75
76 for (auto i = index + 1; i < vec.size(); ++i, ++item) {
77 if (it->second.second.st_dev == item->second.second.st_dev &&
78 it->second.second.st_ino == item->second.second.st_ino) {
79 vec[i] = true;
80 lks.insert(item->second.first);
81 }
82 }
83 return lks;
84 };
85
86 unsigned long index = 0;
87 for (auto item = info.begin(); item != info.end(); ++item, ++index) {
88 if (vec[index]) {
89 HILOGI("skipped file is %{public}s", item->first.c_str());
90 continue;
91 }
92 HILOGI("file name is %{public}s", item->first.c_str());
93
94 Json::Value value;
95 value["fileName"] = item->first;
96 if (item->second.first == BConstants::RESTORE_INSTALL_PATH) {
97 throw BError(BError::Codes::UTILS_INVAL_JSON_ENTITY, "Failed to set ext manage, invalid path");
98 }
99 value["information"]["path"] = item->second.first;
100 value["information"]["stat"] = Stat2JsonValue(item->second.second);
101 set<string> lks = FindLinks(item, index);
102 for (const auto &lk : lks) {
103 value["hardlinks"].append(lk);
104 }
105
106 obj_.append(value);
107 }
108 }
109
GetExtManage() const110 set<string> BJsonEntityExtManage::GetExtManage() const
111 {
112 if (!obj_) {
113 HILOGE("Uninitialized JSon Object reference");
114 return {};
115 }
116 if (!obj_.isArray()) {
117 HILOGE("json object isn't an array");
118 return {};
119 }
120
121 set<string> info;
122 for (Json::Value &item : obj_) {
123 string fileName = item.isMember("fileName") ? item["fileName"].asString() : "";
124 info.emplace(fileName);
125 }
126 return info;
127 }
128
GetExtManageInfo() const129 map<string, pair<string, struct stat>> BJsonEntityExtManage::GetExtManageInfo() const
130 {
131 if (!obj_) {
132 HILOGE("Uninitialized JSon Object reference");
133 return {};
134 }
135 if (!obj_.isArray()) {
136 HILOGE("json object isn't an array");
137 return {};
138 }
139
140 map<string, pair<string, struct stat>> info;
141 for (const Json::Value &item : obj_) {
142 string fileName = item.isMember("fileName") ? item["fileName"].asString() : "";
143
144 if (!item.isMember("information")) {
145 continue;
146 }
147
148 struct stat sta = {};
149 string path = item["information"].isMember("path") ? item["information"]["path"].asString() : "";
150 if (path == BConstants::RESTORE_INSTALL_PATH) {
151 throw BError(BError::Codes::UTILS_INVAL_JSON_ENTITY, "Failed to get ext manage info, invalid path");
152 }
153 if (item["information"].isMember("stat")) {
154 sta = JsonValue2Stat(item["information"]["stat"]);
155 }
156
157 if (!fileName.empty() && !path.empty()) {
158 info.emplace(fileName, make_pair(path, sta));
159 }
160 }
161
162 return info;
163 }
164
SetHardLinkInfo(const string origin,const set<string> hardLinks)165 bool BJsonEntityExtManage::SetHardLinkInfo(const string origin, const set<string> hardLinks)
166 {
167 if (origin.empty()) {
168 HILOGE("origin file name can not empty");
169 return false;
170 }
171 if (!obj_) {
172 HILOGE("Uninitialized JSon Object reference");
173 return false;
174 }
175 if (!obj_.isArray()) {
176 HILOGE("json object isn't an array");
177 return false;
178 }
179
180 for (Json::Value &item : obj_) {
181 string fileName = item.isMember("fileName") ? item["fileName"].asString() : "";
182 if (origin == fileName) {
183 for (const auto &lk : hardLinks) {
184 item["hardlinks"].append(lk);
185 }
186 return true;
187 }
188 }
189
190 return false;
191 }
192
GetHardLinkInfo(const string origin)193 const set<string> BJsonEntityExtManage::GetHardLinkInfo(const string origin)
194 {
195 if (origin.empty()) {
196 HILOGE("origin file name can not empty");
197 return {};
198 }
199 if (!obj_) {
200 HILOGE("Uninitialized JSon Object reference");
201 return {};
202 }
203 if (!obj_.isArray()) {
204 HILOGE("json object isn't an array");
205 return {};
206 }
207
208 set<string> hardlinks;
209 for (const Json::Value &item : obj_) {
210 string fileName = item.isMember("fileName") ? item["fileName"].asString() : "";
211 if (origin == fileName) {
212 if (!(item.isMember("hardlinks") && item["hardlinks"].isArray())) {
213 break;
214 }
215 for (const auto &lk : item["hardlinks"]) {
216 hardlinks.emplace(lk.asString());
217 }
218 }
219 }
220
221 return hardlinks;
222 }
223 } // namespace OHOS::FileManagement::Backup