1 /*
2  * Copyright (c) 2024 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 "b_incremental_data.h"
17 
18 #include "filemgmt_libhilog.h"
19 #include "message_parcel.h"
20 
21 namespace OHOS::FileManagement::Backup {
22 using namespace std;
23 
Marshalling(Parcel & parcel) const24 bool BIncrementalData::Marshalling(Parcel &parcel) const
25 {
26     if (!parcel.WriteString(bundleName) || !parcel.WriteInt64(lastIncrementalTime) ||
27         !parcel.WriteString(backupParameters) || !parcel.WriteInt32(backupPriority)) {
28         HILOGE("Failed");
29         return false;
30     }
31     auto msgParcel = static_cast<MessageParcel *>(&parcel);
32     msgParcel->WriteFileDescriptor(manifestFd);
33     return true;
34 }
35 
ReadFromParcel(Parcel & parcel)36 bool BIncrementalData::ReadFromParcel(Parcel &parcel)
37 {
38     if (!parcel.ReadString(bundleName) || !parcel.ReadInt64(lastIncrementalTime) ||
39         !parcel.ReadString(backupParameters) || !parcel.ReadInt32(backupPriority)) {
40         HILOGE("Failed");
41         return false;
42     }
43     auto msgParcel = static_cast<MessageParcel *>(&parcel);
44     manifestFd = msgParcel->ReadFileDescriptor();
45     return true;
46 }
47 
Unmarshalling(Parcel & parcel)48 BIncrementalData *BIncrementalData::Unmarshalling(Parcel &parcel)
49 {
50     try {
51         auto result = make_unique<BIncrementalData>();
52         if (!result->ReadFromParcel(parcel)) {
53             return nullptr;
54         }
55         return result.release();
56     } catch (const bad_alloc &e) {
57         HILOGE("Failed to unmarshall BIncrementalData because of %{public}s", e.what());
58     }
59     return nullptr;
60 }
61 } // namespace OHOS::FileManagement::Backup