blob: f47889ba61739cb2a32bbeb512d5438eb76fc37c [file] [log] [blame]
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +00001//
2// Copyright (C) 2021 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
Nikita Ioffeaedfef32021-04-28 13:54:14 +010017#include <memory>
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000018#include <utility>
19
20#include <base/files/file_util.h>
21
Nikita Ioffeaedfef32021-04-28 13:54:14 +010022#include <ApexProperties.sysprop.h>
23
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000024#include "update_engine/aosp/apex_handler_android.h"
25#include "update_engine/common/utils.h"
26
27namespace chromeos_update_engine {
28
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000029namespace {
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000030
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000031android::apex::CompressedApexInfoList CreateCompressedApexInfoList(
32 const std::vector<ApexInfo>& apex_infos) {
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000033 android::apex::CompressedApexInfoList compressed_apex_info_list;
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000034 for (const auto& apex_info : apex_infos) {
35 if (!apex_info.is_compressed()) {
36 continue;
37 }
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000038 android::apex::CompressedApexInfo compressed_apex_info;
39 compressed_apex_info.moduleName = apex_info.package_name();
40 compressed_apex_info.versionCode = apex_info.version();
41 compressed_apex_info.decompressedSize = apex_info.decompressed_size();
42 compressed_apex_info_list.apexInfos.emplace_back(
43 std::move(compressed_apex_info));
44 }
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000045 return compressed_apex_info_list;
46}
47
48} // namespace
49
Kelvin Zhang8251dc02022-06-14 09:46:46 -070050std::unique_ptr<ApexHandlerInterface>
51ApexHandlerInterface::CreateApexHandler() {
Nikita Ioffeaedfef32021-04-28 13:54:14 +010052 if (android::sysprop::ApexProperties::updatable().value_or(false)) {
53 return std::make_unique<ApexHandlerAndroid>();
54 } else {
55 return std::make_unique<FlattenedApexHandlerAndroid>();
56 }
57}
58
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000059android::base::Result<uint64_t> ApexHandlerAndroid::CalculateSize(
60 const std::vector<ApexInfo>& apex_infos) const {
61 // We might not need to decompress every APEX. Communicate with apexd to get
62 // accurate requirement.
63 auto apex_service = GetApexService();
64 if (apex_service == nullptr) {
65 return android::base::Error() << "Failed to get hold of apexservice";
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000066 }
67
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000068 auto compressed_apex_info_list = CreateCompressedApexInfoList(apex_infos);
Kelvin Zhang8251dc02022-06-14 09:46:46 -070069 int64_t size_from_apexd = 0;
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000070 auto result = apex_service->calculateSizeForCompressedApex(
71 compressed_apex_info_list, &size_from_apexd);
72 if (!result.isOk()) {
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000073 return android::base::Error()
74 << "Failed to get size required from apexservice";
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000075 }
76 return size_from_apexd;
77}
78
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000079bool ApexHandlerAndroid::AllocateSpace(
80 const std::vector<ApexInfo>& apex_infos) const {
81 auto apex_service = GetApexService();
82 if (apex_service == nullptr) {
83 return false;
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000084 }
Mohammad Samiul Islamb0ab8652021-02-26 14:04:17 +000085 auto compressed_apex_info_list = CreateCompressedApexInfoList(apex_infos);
86 auto result =
87 apex_service->reserveSpaceForCompressedApex(compressed_apex_info_list);
88 return result.isOk();
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000089}
90
91android::sp<android::apex::IApexService> ApexHandlerAndroid::GetApexService()
92 const {
93 auto binder = android::defaultServiceManager()->waitForService(
94 android::String16("apexservice"));
95 if (binder == nullptr) {
96 return nullptr;
97 }
98 return android::interface_cast<android::apex::IApexService>(binder);
99}
100
Nikita Ioffeaedfef32021-04-28 13:54:14 +0100101android::base::Result<uint64_t> FlattenedApexHandlerAndroid::CalculateSize(
102 const std::vector<ApexInfo>& apex_infos) const {
103 return 0;
104}
105
106bool FlattenedApexHandlerAndroid::AllocateSpace(
107 const std::vector<ApexInfo>& apex_infos) const {
108 return true;
109}
110
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +0000111} // namespace chromeos_update_engine