• 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 DowngradeProgress::Marshalling(Parcel &parcel) const
109 {
110     if (!parcel.WriteInt32(state)) {
111         LOGE("failed to write download state");
112         return false;
113     }
114     if (!parcel.WriteInt32(stopReason)) {
115         LOGE("failed to write stopReason");
116         return false;
117     }
118     if (!parcel.WriteInt64(downloadedSize)) {
119         LOGE("failed to write downloadedSize");
120         return false;
121     }
122     if (!parcel.WriteInt64(totalSize)) {
123         LOGE("failed to write totalSize");
124         return false;
125     }
126     if (!parcel.WriteInt32(successfulCount)) {
127         LOGE("failed to write successfulCount");
128         return false;
129     }
130     if (!parcel.WriteInt32(failedCount)) {
131         LOGE("failed to write failedCount");
132         return false;
133     }
134     if (!parcel.WriteInt32(totalCount)) {
135         LOGE("failed to write totalCount");
136         return false;
137     }
138 
139     return true;
140 }
141 
Marshalling(Parcel & parcel) const142 bool CloudFileInfo::Marshalling(Parcel &parcel) const
143 {
144     if (!parcel.WriteInt32(cloudfileCount)) {
145         LOGE("failed to write cloudfileCount");
146         return false;
147     }
148     if (!parcel.WriteInt64(cloudFileTotalSize)) {
149         LOGE("failed to write cloudFileTotalSize");
150         return false;
151     }
152     if (!parcel.WriteInt32(localFileCount)) {
153         LOGE("failed to write localFileCount");
154         return false;
155     }
156     if (!parcel.WriteInt64(localFileTotalSize)) {
157         LOGE("failed to write localFileTotalSize");
158         return false;
159     }
160     if (!parcel.WriteInt32(bothFileCount)) {
161         LOGE("failed to write bothFileCount");
162         return false;
163     }
164     if (!parcel.WriteInt64(bothFileTotalSize)) {
165         LOGE("failed to write bothFileTotalSize");
166         return false;
167     }
168 
169     return true;
170 }
171 
Marshalling(Parcel & parcel) const172 bool HistoryVersion::Marshalling(Parcel &parcel) const
173 {
174     if (!parcel.WriteInt64(editedTime)) {
175         LOGE("failed to write editedTime");
176         return false;
177     }
178     if (!parcel.WriteUint64(fileSize)) {
179         LOGE("failed to write fileSize");
180         return false;
181     }
182     if (!parcel.WriteUint64(versionId)) {
183         LOGE("failed to write versionId");
184         return false;
185     }
186     if (!parcel.WriteString(originalFileName)) {
187         LOGE("failed to write originalFileName");
188         return false;
189     }
190     if (!parcel.WriteString(sha256)) {
191         LOGE("failed to write sha256");
192         return false;
193     }
194     if (!parcel.WriteBool(resolved)) {
195         LOGE("failed to write resolved");
196         return false;
197     }
198 
199     return true;
200 }
201 
Marshalling(Parcel & parcel) const202 bool CleanOptions::Marshalling(Parcel &parcel) const
203 {
204     if (!parcel.WriteUint32(appActionsData.size())) {
205         LOGE("failed to write appActions data size");
206         return false;
207     }
208     for (const auto& it : appActionsData) {
209         if (!parcel.WriteString(it.first)) {
210             LOGE("failed to write key");
211             return false;
212         }
213         if (!parcel.WriteInt32(it.second)) {
214             LOGE("failed to write value");
215             return false;
216         }
217     }
218     return true;
219 }
220 
Unmarshalling(Parcel & parcel)221 SwitchDataObj *SwitchDataObj::Unmarshalling(Parcel &parcel)
222 {
223     SwitchDataObj *info = new (std::nothrow) SwitchDataObj();
224     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
225         LOGW("read from parcel failed");
226         delete info;
227         info = nullptr;
228     }
229     return info;
230 }
231 
Unmarshalling(Parcel & parcel)232 DownloadProgressObj *DownloadProgressObj::Unmarshalling(Parcel &parcel)
233 {
234     DownloadProgressObj *info = new (std::nothrow) DownloadProgressObj();
235     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
236         LOGW("read from parcel failed");
237         delete info;
238         info = nullptr;
239     }
240     return info;
241 }
242 
Unmarshalling(Parcel & parcel)243 DowngradeProgress *DowngradeProgress::Unmarshalling(Parcel &parcel)
244 {
245     DowngradeProgress *info = new (std::nothrow) DowngradeProgress();
246     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
247         LOGW("read from parcel failed");
248         delete info;
249         info = nullptr;
250     }
251     return info;
252 }
253 
Unmarshalling(Parcel & parcel)254 CloudFileInfo *CloudFileInfo::Unmarshalling(Parcel &parcel)
255 {
256     CloudFileInfo *info = new (std::nothrow) CloudFileInfo();
257     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
258         LOGW("read from parcel failed");
259         delete info;
260         info = nullptr;
261     }
262     return info;
263 }
264 
Unmarshalling(Parcel & parcel)265 HistoryVersion *HistoryVersion::Unmarshalling(Parcel &parcel)
266 {
267     HistoryVersion *info = new (std::nothrow) HistoryVersion();
268     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
269         LOGW("read from parcel failed");
270         delete info;
271         info = nullptr;
272     }
273     return info;
274 }
275 
Unmarshalling(Parcel & parcel)276 CleanOptions *CleanOptions::Unmarshalling(Parcel &parcel)
277 {
278     CleanOptions *info = new (std::nothrow) CleanOptions();
279     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
280         LOGW("read from parcel failed");
281         delete info;
282         info = nullptr;
283     }
284     return info;
285 }
286 
Unmarshalling(Parcel & parcel)287 OptimizeSpaceOptions *OptimizeSpaceOptions::Unmarshalling(Parcel &parcel)
288 {
289     OptimizeSpaceOptions *info = new (std::nothrow) OptimizeSpaceOptions();
290     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
291         LOGW("read from parcel failed");
292         delete info;
293         info = nullptr;
294     }
295     return info;
296 }
297 
ReadFromParcel(Parcel & parcel)298 bool OptimizeSpaceOptions::ReadFromParcel(Parcel &parcel)
299 {
300     if (!parcel.ReadInt64(totalSize)) {
301         LOGE("failed to read totalSize");
302         return false;
303     }
304     if (!parcel.ReadInt32(agingDays)) {
305         LOGE("failed to read agingDays");
306         return false;
307     }
308     return true;
309 }
310 
Marshalling(Parcel & parcel) const311 bool OptimizeSpaceOptions::Marshalling(Parcel &parcel) const
312 {
313     if (!parcel.WriteInt64(totalSize)) {
314         LOGE("failed to write totalSize");
315         return false;
316     }
317     if (!parcel.WriteInt32(agingDays)) {
318         LOGE("failed to write agingDays");
319         return false;
320     }
321 
322     return true;
323 }
324 
ReadFromParcel(Parcel & parcel)325 bool SwitchDataObj::ReadFromParcel(Parcel &parcel)
326 {
327     switchData.clear();
328     uint32_t size = 0;
329     if (!parcel.ReadUint32(size)) {
330         LOGE("fail to read switch data size");
331         return false;
332     }
333     if (size > MAX_MAP_SIZE) {
334         LOGE("switch data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
335         return false;
336     }
337     for (uint32_t i = 0; i < size; ++i) {
338         std::string key;
339         if (!parcel.ReadString(key)) {
340             LOGE("fail to read switch data key");
341             return false;
342         }
343         bool value = false;
344         if (!parcel.ReadBool(value)) {
345             LOGE("fail to read switch data value");
346             return false;
347         }
348         switchData.emplace(key, value);
349     }
350     return true;
351 }
352 
ReadFromParcel(Parcel & parcel)353 bool CleanOptions::ReadFromParcel(Parcel &parcel)
354 {
355     appActionsData.clear();
356     uint32_t size = 0;
357     if (!parcel.ReadUint32(size)) {
358         LOGE("fail to read appActions data size");
359         return false;
360     }
361     if (size > MAX_MAP_SIZE) {
362         LOGE("appActions data is oversize, the limit is %{public}d", MAX_MAP_SIZE);
363         return false;
364     }
365     for (uint32_t i = 0; i < size; ++i) {
366         std::string key;
367         if (!parcel.ReadString(key)) {
368             LOGE("fail to read appActions data key");
369             return false;
370         }
371         int value = 0;
372         if (!parcel.ReadInt32(value)) {
373             LOGE("fail to read appActions data value");
374             return false;
375         }
376         appActionsData.emplace(key, value);
377     }
378     return true;
379 }
380 
ReadBatchFromParcel(Parcel & parcel)381 bool DownloadProgressObj::ReadBatchFromParcel(Parcel &parcel)
382 {
383     if (!parcel.ReadInt64(batchDownloadSize)) {
384         LOGE("failed to read batchDownloadSize");
385         return false;
386     }
387     if (!parcel.ReadInt64(batchTotalSize)) {
388         LOGE("failed to read batchTotalSize");
389         return false;
390     }
391     if (!parcel.ReadInt64(batchSuccNum)) {
392         LOGE("failed to read batchSuccNum");
393         return false;
394     }
395     if (!parcel.ReadInt64(batchFailNum)) {
396         LOGE("failed to read batchFailNum");
397         return false;
398     }
399     if (!parcel.ReadInt64(batchTotalNum)) {
400         LOGE("failed to read batchTotalNum");
401         return false;
402     }
403     int32_t tempBatchState = 0;
404     if (!parcel.ReadInt32(tempBatchState)) {
405         LOGE("failed to read download batchState");
406         return false;
407     }
408     batchState = static_cast<Status>(tempBatchState);
409 
410     return true;
411 }
412 
ReadFromParcel(Parcel & parcel)413 bool DownloadProgressObj::ReadFromParcel(Parcel &parcel)
414 {
415     if (!parcel.ReadString(path)) {
416         LOGE("failed to read download path");
417         return false;
418     }
419     int32_t tempState = 0;
420     if (!parcel.ReadInt32(tempState)) {
421         LOGE("failed to read download state");
422         return false;
423     }
424     state = static_cast<Status>(tempState);
425     if (!parcel.ReadInt64(downloadedSize)) {
426         LOGE("failed to read downloadedSize");
427         return false;
428     }
429     if (!parcel.ReadInt64(totalSize)) {
430         LOGE("failed to read totalSize");
431         return false;
432     }
433     if (!parcel.ReadInt32(downloadErrorType)) {
434         LOGE("failed to read downloadErrorType");
435         return false;
436     }
437     if (!parcel.ReadInt64(downloadId)) {
438         LOGE("failed to read downloadId");
439         return false;
440     }
441 
442     if (!ReadBatchFromParcel(parcel)) {
443         return false;
444     }
445 
446     return true;
447 }
448 
ReadFromParcel(Parcel & parcel)449 bool DowngradeProgress::ReadFromParcel(Parcel &parcel)
450 {
451     int32_t temp = 0;
452     if (!parcel.ReadInt32(temp)) {
453         LOGE("failed to read download state");
454         return false;
455     }
456     state = static_cast<State>(temp);
457     if (!parcel.ReadInt32(temp)) {
458         LOGE("failed to read stopReason");
459         return false;
460     }
461     stopReason = static_cast<StopReason>(temp);
462     if (!parcel.ReadInt64(downloadedSize)) {
463         LOGE("failed to read downloadedSize");
464         return false;
465     }
466     if (!parcel.ReadInt64(totalSize)) {
467         LOGE("failed to read totalSize");
468         return false;
469     }
470     if (!parcel.ReadInt32(successfulCount)) {
471         LOGE("failed to read successfulCount");
472         return false;
473     }
474     if (!parcel.ReadInt32(failedCount)) {
475         LOGE("failed to read failedCount");
476         return false;
477     }
478     if (!parcel.ReadInt32(totalCount)) {
479         LOGE("failed to read totalCount");
480         return false;
481     }
482 
483     return true;
484 }
485 
ReadFromParcel(Parcel & parcel)486 bool CloudFileInfo::ReadFromParcel(Parcel &parcel)
487 {
488     if (!parcel.ReadInt32(cloudfileCount)) {
489         LOGE("failed to read cloudfileCount");
490         return false;
491     }
492     if (!parcel.ReadInt64(cloudFileTotalSize)) {
493         LOGE("failed to read cloudFileTotalSize");
494         return false;
495     }
496     if (!parcel.ReadInt32(localFileCount)) {
497         LOGE("failed to read localFileCount");
498         return false;
499     }
500     if (!parcel.ReadInt64(localFileTotalSize)) {
501         LOGE("failed to read localFileTotalSize");
502         return false;
503     }
504     if (!parcel.ReadInt32(bothFileCount)) {
505         LOGE("failed to read bothFileCount");
506         return false;
507     }
508     if (!parcel.ReadInt64(bothFileTotalSize)) {
509         LOGE("failed to read bothFileTotalSize");
510         return false;
511     }
512 
513     return true;
514 }
515 
ReadFromParcel(Parcel & parcel)516 bool HistoryVersion::ReadFromParcel(Parcel &parcel)
517 {
518     if (!parcel.ReadInt64(editedTime)) {
519         LOGE("failed to read editedTime");
520         return false;
521     }
522     if (!parcel.ReadUint64(fileSize)) {
523         LOGE("failed to read fileSize");
524         return false;
525     }
526     if (!parcel.ReadUint64(versionId)) {
527         LOGE("failed to read versionId");
528         return false;
529     }
530     if (!parcel.ReadString(originalFileName)) {
531         LOGE("failed to read originalFileName");
532         return false;
533     }
534     if (!parcel.ReadString(sha256)) {
535         LOGE("failed to read sha256");
536         return false;
537     }
538     if (!parcel.ReadBool(resolved)) {
539         LOGE("failed to read resolved");
540         return false;
541     }
542 
543     return true;
544 }
545 
to_string()546 std::string DownloadProgressObj::to_string()
547 {
548     std::stringstream ss;
549     std::string pathAnony = GetAnonyString(path);
550     ss << "DownloadProgressObj [path: " << pathAnony;
551     ss << " state: " << state;
552     ss << " downloaded: " << downloadedSize;
553     ss << " total: " << totalSize;
554     ss << " downloadErrorType: " << downloadErrorType;
555 
556     ss << " downloadId: " << downloadId;
557     ss << " batchState: " << batchState;
558     ss << " batchDownloadSize: " << batchDownloadSize;
559     ss << " batchTotalSize: " << batchTotalSize;
560     ss << " batchSuccNum: " << batchSuccNum;
561     ss << " batchFailNum: " << batchFailNum;
562     ss << " batchTotalNum: " << batchTotalNum << "]";
563     return ss.str();
564 }
565 
to_string() const566 std::string DowngradeProgress::to_string() const
567 {
568     std::stringstream ss;
569     ss << "DowngradeProgress [DownloadState: " << state;
570     ss << " downloadStopReason: " << stopReason;
571     ss << " downloadedSize: " << downloadedSize;
572     ss << " totalSize: " << totalSize;
573     ss << " successfulCount: " << successfulCount;
574     ss << " failedCount: " << failedCount;
575     ss << " totalCount: " << totalCount << "]";
576     return ss.str();
577 }
578 
ReadFromParcel(Parcel & parcel)579 bool DentryFileInfoObj::ReadFromParcel(Parcel &parcel)
580 {
581     parcel.ReadString(cloudId);
582     parcel.ReadInt64(size);
583     parcel.ReadInt64(modifiedTime);
584     parcel.ReadString(path);
585     parcel.ReadString(fileName);
586     parcel.ReadString(fileType);
587     return true;
588 }
589 
Marshalling(Parcel & parcel) const590 bool DentryFileInfoObj::Marshalling(Parcel &parcel) const
591 {
592     parcel.WriteString(cloudId);
593     parcel.WriteInt64(size);
594     parcel.WriteInt64(modifiedTime);
595     parcel.WriteString(path);
596     parcel.WriteString(fileName);
597     parcel.WriteString(fileType);
598     return true;
599 }
600 
Unmarshalling(Parcel & parcel)601 DentryFileInfoObj *DentryFileInfoObj::Unmarshalling(Parcel &parcel)
602 {
603     DentryFileInfoObj *info = new (std::nothrow) DentryFileInfoObj();
604     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
605         LOGW("read from parcel failed");
606         delete info;
607         info = nullptr;
608     }
609     return info;
610 }
611 
ReadFromParcel(Parcel & parcel)612 bool AssetInfoObj::ReadFromParcel(Parcel &parcel)
613 {
614     parcel.ReadString(uri);
615     parcel.ReadString(recordType);
616     parcel.ReadString(recordId);
617     parcel.ReadString(fieldKey);
618     parcel.ReadString(assetName);
619     return true;
620 }
621 
Marshalling(Parcel & parcel) const622 bool AssetInfoObj::Marshalling(Parcel &parcel) const
623 {
624     parcel.WriteString(uri);
625     parcel.WriteString(recordType);
626     parcel.WriteString(recordId);
627     parcel.WriteString(fieldKey);
628     parcel.WriteString(assetName);
629     return true;
630 }
631 
Unmarshalling(Parcel & parcel)632 AssetInfoObj *AssetInfoObj::Unmarshalling(Parcel &parcel)
633 {
634     AssetInfoObj *info = new (std::nothrow) AssetInfoObj();
635     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
636         LOGW("read from parcel failed");
637         delete info;
638         info = nullptr;
639     }
640     return info;
641 }
642 
ReadFromParcel(Parcel & parcel)643 bool CleanFileInfoObj::ReadFromParcel(Parcel &parcel)
644 {
645     parcel.ReadString(cloudId);
646     parcel.ReadInt64(size);
647     parcel.ReadInt64(modifiedTime);
648     parcel.ReadString(path);
649     parcel.ReadString(fileName);
650     parcel.ReadStringVector(&attachment);
651     return true;
652 }
653 
Marshalling(Parcel & parcel) const654 bool CleanFileInfoObj::Marshalling(Parcel &parcel) const
655 {
656     parcel.WriteString(cloudId);
657     parcel.WriteInt64(size);
658     parcel.WriteInt64(modifiedTime);
659     parcel.WriteString(path);
660     parcel.WriteString(fileName);
661     parcel.WriteStringVector(attachment);
662     return true;
663 }
664 
Unmarshalling(Parcel & parcel)665 CleanFileInfoObj *CleanFileInfoObj::Unmarshalling(Parcel &parcel)
666 {
667     CleanFileInfoObj *info = new (std::nothrow) CleanFileInfoObj();
668     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
669         LOGW("read from parcel failed");
670         delete info;
671         info = nullptr;
672     }
673     return info;
674 }
675 
676 } // namespace OHOS::FileManagement::CloudSync
677