• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Unmarshalling(Parcel & parcel)160 OptimizeSpaceOptions *OptimizeSpaceOptions::Unmarshalling(Parcel &parcel)
161 {
162     OptimizeSpaceOptions *info = new (std::nothrow) OptimizeSpaceOptions();
163     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
164         LOGW("read from parcel failed");
165         delete info;
166         info = nullptr;
167     }
168     return info;
169 }
170 
ReadFromParcel(Parcel & parcel)171 bool OptimizeSpaceOptions::ReadFromParcel(Parcel &parcel)
172 {
173     if (!parcel.ReadInt64(totalSize)) {
174         LOGE("failed to read totalSize");
175         return false;
176     }
177     if (!parcel.ReadInt32(agingDays)) {
178         LOGE("failed to read agingDays");
179         return false;
180     }
181     return true;
182 }
183 
Marshalling(Parcel & parcel) const184 bool OptimizeSpaceOptions::Marshalling(Parcel &parcel) const
185 {
186     if (!parcel.WriteInt64(totalSize)) {
187         LOGE("failed to write totalSize");
188         return false;
189     }
190     if (!parcel.WriteInt32(agingDays)) {
191         LOGE("failed to write agingDays");
192         return false;
193     }
194 
195     return true;
196 }
197 
ReadFromParcel(Parcel & parcel)198 bool SwitchDataObj::ReadFromParcel(Parcel &parcel)
199 {
200     switchData.clear();
201     uint32_t size = 0;
202     if (!parcel.ReadUint32(size)) {
203         LOGE("fail to read switch data size");
204         return false;
205     }
206     if (size > MAX_MAP_SIZE) {
207         LOGE("switch data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
208         return false;
209     }
210     for (uint32_t i = 0; i < size; ++i) {
211         std::string key;
212         if (!parcel.ReadString(key)) {
213             LOGE("fail to read switch data key");
214             return false;
215         }
216         bool value = false;
217         if (!parcel.ReadBool(value)) {
218             LOGE("fail to read switch data value");
219             return false;
220         }
221         switchData.emplace(key, value);
222     }
223     return true;
224 }
225 
ReadFromParcel(Parcel & parcel)226 bool CleanOptions::ReadFromParcel(Parcel &parcel)
227 {
228     appActionsData.clear();
229     uint32_t size = 0;
230     if (!parcel.ReadUint32(size)) {
231         LOGE("fail to read appActions data size");
232         return false;
233     }
234     if (size > MAX_MAP_SIZE) {
235         LOGE("appActions data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
236         return false;
237     }
238     for (uint32_t i = 0; i < size; ++i) {
239         std::string key;
240         if (!parcel.ReadString(key)) {
241             LOGE("fail to read appActions data key");
242             return false;
243         }
244         int value = 0;
245         if (!parcel.ReadInt32(value)) {
246             LOGE("fail to read appActions data value");
247             return false;
248         }
249         appActionsData.emplace(key, value);
250     }
251     return true;
252 }
253 
ReadBatchFromParcel(Parcel & parcel)254 bool DownloadProgressObj::ReadBatchFromParcel(Parcel &parcel)
255 {
256     if (!parcel.ReadInt64(batchDownloadSize)) {
257         LOGE("failed to read batchDownloadSize");
258         return false;
259     }
260     if (!parcel.ReadInt64(batchTotalSize)) {
261         LOGE("failed to read batchTotalSize");
262         return false;
263     }
264     if (!parcel.ReadInt64(batchSuccNum)) {
265         LOGE("failed to read batchSuccNum");
266         return false;
267     }
268     if (!parcel.ReadInt64(batchFailNum)) {
269         LOGE("failed to read batchFailNum");
270         return false;
271     }
272     if (!parcel.ReadInt64(batchTotalNum)) {
273         LOGE("failed to read batchTotalNum");
274         return false;
275     }
276     int32_t tempBatchState = 0;
277     if (!parcel.ReadInt32(tempBatchState)) {
278         LOGE("failed to read download batchState");
279         return false;
280     }
281     batchState = static_cast<Status>(tempBatchState);
282 
283     return true;
284 }
285 
ReadFromParcel(Parcel & parcel)286 bool DownloadProgressObj::ReadFromParcel(Parcel &parcel)
287 {
288     if (!parcel.ReadString(path)) {
289         LOGE("failed to read download path");
290         return false;
291     }
292     int32_t tempState = 0;
293     if (!parcel.ReadInt32(tempState)) {
294         LOGE("failed to read download state");
295         return false;
296     }
297     state = static_cast<Status>(tempState);
298     if (!parcel.ReadInt64(downloadedSize)) {
299         LOGE("failed to read downloadedSize");
300         return false;
301     }
302     if (!parcel.ReadInt64(totalSize)) {
303         LOGE("failed to read totalSize");
304         return false;
305     }
306     if (!parcel.ReadInt32(downloadErrorType)) {
307         LOGE("failed to read downloadErrorType");
308         return false;
309     }
310     if (!parcel.ReadInt64(downloadId)) {
311         LOGE("failed to read downloadId");
312         return false;
313     }
314 
315     if (!ReadBatchFromParcel(parcel)) {
316         return false;
317     }
318 
319     return true;
320 }
321 
to_string()322 std::string DownloadProgressObj::to_string()
323 {
324     std::stringstream ss;
325     std::string pathAnony = GetAnonyString(path);
326     ss << "DownloadProgressObj [path: " << pathAnony;
327     ss << " state: " << state;
328     ss << " downloaded: " << downloadedSize;
329     ss << " total: " << totalSize;
330     ss << " downloadErrorType: " << downloadErrorType;
331 
332     ss << " downloadId: " << downloadId;
333     ss << " batchState: " << batchState;
334     ss << " batchDownloadSize: " << batchDownloadSize;
335     ss << " batchTotalSize: " << batchTotalSize;
336     ss << " batchSuccNum: " << batchSuccNum;
337     ss << " batchFailNum: " << batchFailNum;
338     ss << " batchTotalNum: " << batchTotalNum << "]";
339     return ss.str();
340 }
341 
ReadFromParcel(Parcel & parcel)342 bool DentryFileInfoObj::ReadFromParcel(Parcel &parcel)
343 {
344     parcel.ReadString(cloudId);
345     parcel.ReadInt64(size);
346     parcel.ReadInt64(modifiedTime);
347     parcel.ReadString(path);
348     parcel.ReadString(fileName);
349     parcel.ReadString(fileType);
350     return true;
351 }
352 
Marshalling(Parcel & parcel) const353 bool DentryFileInfoObj::Marshalling(Parcel &parcel) const
354 {
355     parcel.WriteString(cloudId);
356     parcel.WriteInt64(size);
357     parcel.WriteInt64(modifiedTime);
358     parcel.WriteString(path);
359     parcel.WriteString(fileName);
360     parcel.WriteString(fileType);
361     return true;
362 }
363 
Unmarshalling(Parcel & parcel)364 DentryFileInfoObj *DentryFileInfoObj::Unmarshalling(Parcel &parcel)
365 {
366     DentryFileInfoObj *info = new (std::nothrow) DentryFileInfoObj();
367     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
368         LOGW("read from parcel failed");
369         delete info;
370         info = nullptr;
371     }
372     return info;
373 }
374 
ReadFromParcel(Parcel & parcel)375 bool AssetInfoObj::ReadFromParcel(Parcel &parcel)
376 {
377     parcel.ReadString(uri);
378     parcel.ReadString(recordType);
379     parcel.ReadString(recordId);
380     parcel.ReadString(fieldKey);
381     parcel.ReadString(assetName);
382     return true;
383 }
384 
Marshalling(Parcel & parcel) const385 bool AssetInfoObj::Marshalling(Parcel &parcel) const
386 {
387     parcel.WriteString(uri);
388     parcel.WriteString(recordType);
389     parcel.WriteString(recordId);
390     parcel.WriteString(fieldKey);
391     parcel.WriteString(assetName);
392     return true;
393 }
394 
Unmarshalling(Parcel & parcel)395 AssetInfoObj *AssetInfoObj::Unmarshalling(Parcel &parcel)
396 {
397     AssetInfoObj *info = new (std::nothrow) AssetInfoObj();
398     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
399         LOGW("read from parcel failed");
400         delete info;
401         info = nullptr;
402     }
403     return info;
404 }
405 
ReadFromParcel(Parcel & parcel)406 bool CleanFileInfoObj::ReadFromParcel(Parcel &parcel)
407 {
408     parcel.ReadString(cloudId);
409     parcel.ReadInt64(size);
410     parcel.ReadInt64(modifiedTime);
411     parcel.ReadString(path);
412     parcel.ReadString(fileName);
413     parcel.ReadStringVector(&attachment);
414     return true;
415 }
416 
Marshalling(Parcel & parcel) const417 bool CleanFileInfoObj::Marshalling(Parcel &parcel) const
418 {
419     parcel.WriteString(cloudId);
420     parcel.WriteInt64(size);
421     parcel.WriteInt64(modifiedTime);
422     parcel.WriteString(path);
423     parcel.WriteString(fileName);
424     parcel.WriteStringVector(attachment);
425     return true;
426 }
427 
Unmarshalling(Parcel & parcel)428 CleanFileInfoObj *CleanFileInfoObj::Unmarshalling(Parcel &parcel)
429 {
430     CleanFileInfoObj *info = new (std::nothrow) CleanFileInfoObj();
431     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
432         LOGW("read from parcel failed");
433         delete info;
434         info = nullptr;
435     }
436     return info;
437 }
438 
439 } // namespace OHOS::FileManagement::CloudSync
440