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 <sstream>
17 #include "cloud_sync_common.h"
18 
19 #include "utils_log.h"
20 
21 namespace OHOS::FileManagement::CloudSync {
22 namespace {
23 constexpr uint32_t MAX_MAP_SIZE = 1024;
24 }
Marshalling(Parcel & parcel) const25 bool SwitchDataObj::Marshalling(Parcel &parcel) const
26 {
27     if (!parcel.WriteUint32(switchData.size())) {
28         LOGE("failed to write switch data size");
29         return false;
30     }
31     for (const auto& it : switchData) {
32         if (!parcel.WriteString(it.first)) {
33             LOGE("failed to write key");
34             return false;
35         }
36         if (!parcel.WriteBool(it.second)) {
37             LOGE("failed to write value");
38             return false;
39         }
40     }
41     return true;
42 }
43 
MarshallingBatch(Parcel & parcel) const44 bool DownloadProgressObj::MarshallingBatch(Parcel &parcel) const
45 {
46     if (!parcel.WriteInt64(batchDownloadSize)) {
47         LOGE("failed to write batchDownloadSize");
48         return false;
49     }
50     if (!parcel.WriteInt64(batchTotalSize)) {
51         LOGE("failed to write batchTotalSize");
52         return false;
53     }
54     if (!parcel.WriteInt64(batchSuccNum)) {
55         LOGE("failed to write batchSuccNum");
56         return false;
57     }
58     if (!parcel.WriteInt64(batchFailNum)) {
59         LOGE("failed to write batchFailNum");
60         return false;
61     }
62     if (!parcel.WriteInt64(batchTotalNum)) {
63         LOGE("failed to write batchTotalNum");
64         return false;
65     }
66     if (!parcel.WriteInt32(batchState)) {
67         LOGE("failed to write batchState");
68         return false;
69     }
70 
71     return true;
72 }
73 
Marshalling(Parcel & parcel) const74 bool DownloadProgressObj::Marshalling(Parcel &parcel) const
75 {
76     if (!parcel.WriteString(path)) {
77         LOGE("failed to write download path");
78         return false;
79     }
80     if (!parcel.WriteInt32(state)) {
81         LOGE("failed to write download state");
82         return false;
83     }
84     if (!parcel.WriteInt64(downloadedSize)) {
85         LOGE("failed to write downloadedSize");
86         return false;
87     }
88     if (!parcel.WriteInt64(totalSize)) {
89         LOGE("failed to write totalSize");
90         return false;
91     }
92     if (!parcel.WriteInt32(downloadErrorType)) {
93         LOGE("failed to write downloadErrorType");
94         return false;
95     }
96     if (!parcel.WriteInt64(downloadId)) {
97         LOGE("failed to write downloadId");
98         return false;
99     }
100 
101     if (!MarshallingBatch(parcel)) {
102         return false;
103     }
104 
105     return true;
106 }
107 
Marshalling(Parcel & parcel) const108 bool CleanOptions::Marshalling(Parcel &parcel) const
109 {
110     if (!parcel.WriteUint32(appActionsData.size())) {
111         LOGE("failed to write appActions data size");
112         return false;
113     }
114     for (const auto& it : appActionsData) {
115         if (!parcel.WriteString(it.first)) {
116             LOGE("failed to write key");
117             return false;
118         }
119         if (!parcel.WriteInt32(it.second)) {
120             LOGE("failed to write value");
121             return false;
122         }
123     }
124     return true;
125 }
126 
Unmarshalling(Parcel & parcel)127 SwitchDataObj *SwitchDataObj::Unmarshalling(Parcel &parcel)
128 {
129     SwitchDataObj *info = new (std::nothrow) SwitchDataObj();
130     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
131         LOGW("read from parcel failed");
132         delete info;
133         info = nullptr;
134     }
135     return info;
136 }
137 
Unmarshalling(Parcel & parcel)138 DownloadProgressObj *DownloadProgressObj::Unmarshalling(Parcel &parcel)
139 {
140     DownloadProgressObj *info = new (std::nothrow) DownloadProgressObj();
141     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
142         LOGW("read from parcel failed");
143         delete info;
144         info = nullptr;
145     }
146     return info;
147 }
148 
Unmarshalling(Parcel & parcel)149 CleanOptions *CleanOptions::Unmarshalling(Parcel &parcel)
150 {
151     CleanOptions *info = new (std::nothrow) CleanOptions();
152     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
153         LOGW("read from parcel failed");
154         delete info;
155         info = nullptr;
156     }
157     return info;
158 }
159 
ReadFromParcel(Parcel & parcel)160 bool SwitchDataObj::ReadFromParcel(Parcel &parcel)
161 {
162     switchData.clear();
163     uint32_t size = 0;
164     if (!parcel.ReadUint32(size)) {
165         LOGE("fail to read switch data size");
166         return false;
167     }
168     if (size > MAX_MAP_SIZE) {
169         LOGE("switch data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
170         return false;
171     }
172     for (uint32_t i = 0; i < size; ++i) {
173         std::string key;
174         if (!parcel.ReadString(key)) {
175             LOGE("fail to read switch data key");
176             return false;
177         }
178         bool value = false;
179         if (!parcel.ReadBool(value)) {
180             LOGE("fail to read switch data value");
181             return false;
182         }
183         switchData.emplace(key, value);
184     }
185     return true;
186 }
187 
ReadFromParcel(Parcel & parcel)188 bool CleanOptions::ReadFromParcel(Parcel &parcel)
189 {
190     appActionsData.clear();
191     uint32_t size = 0;
192     if (!parcel.ReadUint32(size)) {
193         LOGE("fail to read appActions data size");
194         return false;
195     }
196     if (size > MAX_MAP_SIZE) {
197         LOGE("appActions data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
198         return false;
199     }
200     for (uint32_t i = 0; i < size; ++i) {
201         std::string key;
202         if (!parcel.ReadString(key)) {
203             LOGE("fail to read appActions data key");
204             return false;
205         }
206         int value = 0;
207         if (!parcel.ReadInt32(value)) {
208             LOGE("fail to read appActions data value");
209             return false;
210         }
211         appActionsData.emplace(key, value);
212     }
213     return true;
214 }
215 
ReadBatchFromParcel(Parcel & parcel)216 bool DownloadProgressObj::ReadBatchFromParcel(Parcel &parcel)
217 {
218     if (!parcel.ReadInt64(batchDownloadSize)) {
219         LOGE("failed to read batchDownloadSize");
220         return false;
221     }
222     if (!parcel.ReadInt64(batchTotalSize)) {
223         LOGE("failed to read batchTotalSize");
224         return false;
225     }
226     if (!parcel.ReadInt64(batchSuccNum)) {
227         LOGE("failed to read batchSuccNum");
228         return false;
229     }
230     if (!parcel.ReadInt64(batchFailNum)) {
231         LOGE("failed to read batchFailNum");
232         return false;
233     }
234     if (!parcel.ReadInt64(batchTotalNum)) {
235         LOGE("failed to read batchTotalNum");
236         return false;
237     }
238     int32_t tempBatchState = 0;
239     if (!parcel.ReadInt32(tempBatchState)) {
240         LOGE("failed to read download batchState");
241         return false;
242     }
243     batchState = static_cast<Status>(tempBatchState);
244 
245     return true;
246 }
247 
ReadFromParcel(Parcel & parcel)248 bool DownloadProgressObj::ReadFromParcel(Parcel &parcel)
249 {
250     if (!parcel.ReadString(path)) {
251         LOGE("failed to read download path");
252         return false;
253     }
254     int32_t tempState = 0;
255     if (!parcel.ReadInt32(tempState)) {
256         LOGE("failed to read download state");
257         return false;
258     }
259     state = static_cast<Status>(tempState);
260     if (!parcel.ReadInt64(downloadedSize)) {
261         LOGE("failed to read downloadedSize");
262         return false;
263     }
264     if (!parcel.ReadInt64(totalSize)) {
265         LOGE("failed to read totalSize");
266         return false;
267     }
268     if (!parcel.ReadInt32(downloadErrorType)) {
269         LOGE("failed to read downloadErrorType");
270         return false;
271     }
272     if (!parcel.ReadInt64(downloadId)) {
273         LOGE("failed to read downloadId");
274         return false;
275     }
276 
277     if (!ReadBatchFromParcel(parcel)) {
278         return false;
279     }
280 
281     return true;
282 }
283 
to_string()284 std::string DownloadProgressObj::to_string()
285 {
286     std::stringstream ss;
287     std::string pathAnony = GetAnonyString(path);
288     ss << "DownloadProgressObj [path: " << pathAnony;
289     ss << " state: " << state;
290     ss << " downloaded: " << downloadedSize;
291     ss << " total: " << totalSize;
292     ss << " downloadErrorType: " << downloadErrorType;
293 
294     ss << " downloadId: " << downloadId;
295     ss << " batchState: " << batchState;
296     ss << " batchDownloadSize: " << batchDownloadSize;
297     ss << " batchTotalSize: " << batchTotalSize;
298     ss << " batchSuccNum: " << batchSuccNum;
299     ss << " batchFailNum: " << batchFailNum;
300     ss << " batchTotalNum: " << batchTotalNum << "]";
301     return ss.str();
302 }
303 
ReadFromParcel(Parcel & parcel)304 bool AssetInfoObj::ReadFromParcel(Parcel &parcel)
305 {
306     parcel.ReadString(uri);
307     parcel.ReadString(recordType);
308     parcel.ReadString(recordId);
309     parcel.ReadString(fieldKey);
310     parcel.ReadString(assetName);
311     return true;
312 }
313 
Marshalling(Parcel & parcel) const314 bool AssetInfoObj::Marshalling(Parcel &parcel) const
315 {
316     parcel.WriteString(uri);
317     parcel.WriteString(recordType);
318     parcel.WriteString(recordId);
319     parcel.WriteString(fieldKey);
320     parcel.WriteString(assetName);
321     return true;
322 }
323 
Unmarshalling(Parcel & parcel)324 AssetInfoObj *AssetInfoObj::Unmarshalling(Parcel &parcel)
325 {
326     AssetInfoObj *info = new (std::nothrow) AssetInfoObj();
327     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
328         LOGW("read from parcel failed");
329         delete info;
330         info = nullptr;
331     }
332     return info;
333 }
334 } // namespace OHOS::FileManagement::CloudSync
335