| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 1 | // | 
|  | 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 Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 17 | #include <memory> | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 18 | #include <utility> | 
|  | 19 |  | 
|  | 20 | #include <base/files/file_util.h> | 
|  | 21 |  | 
| Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 22 | #include <ApexProperties.sysprop.h> | 
|  | 23 |  | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 24 | #include "update_engine/aosp/apex_handler_android.h" | 
|  | 25 | #include "update_engine/common/utils.h" | 
|  | 26 |  | 
|  | 27 | namespace chromeos_update_engine { | 
|  | 28 |  | 
| Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 29 | namespace { | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 30 |  | 
| Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 31 | android::apex::CompressedApexInfoList CreateCompressedApexInfoList( | 
|  | 32 | const std::vector<ApexInfo>& apex_infos) { | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 33 | android::apex::CompressedApexInfoList compressed_apex_info_list; | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 34 | for (const auto& apex_info : apex_infos) { | 
|  | 35 | if (!apex_info.is_compressed()) { | 
|  | 36 | continue; | 
|  | 37 | } | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 38 | 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 Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 45 | return compressed_apex_info_list; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | }  // namespace | 
|  | 49 |  | 
| Kelvin Zhang | 8251dc0 | 2022-06-14 09:46:46 -0700 | [diff] [blame] | 50 | std::unique_ptr<ApexHandlerInterface> | 
|  | 51 | ApexHandlerInterface::CreateApexHandler() { | 
| Nikita Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 52 | 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 Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 59 | android::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 Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 68 | auto compressed_apex_info_list = CreateCompressedApexInfoList(apex_infos); | 
| Kelvin Zhang | 8251dc0 | 2022-06-14 09:46:46 -0700 | [diff] [blame] | 69 | int64_t size_from_apexd = 0; | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 70 | auto result = apex_service->calculateSizeForCompressedApex( | 
|  | 71 | compressed_apex_info_list, &size_from_apexd); | 
|  | 72 | if (!result.isOk()) { | 
| Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 73 | return android::base::Error() | 
|  | 74 | << "Failed to get size required from apexservice"; | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 75 | } | 
|  | 76 | return size_from_apexd; | 
|  | 77 | } | 
|  | 78 |  | 
| Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 79 | bool 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 Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 84 | } | 
| Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame] | 85 | 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 Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
|  | 91 | android::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 Ioffe | aedfef3 | 2021-04-28 13:54:14 +0100 | [diff] [blame] | 101 | android::base::Result<uint64_t> FlattenedApexHandlerAndroid::CalculateSize( | 
|  | 102 | const std::vector<ApexInfo>& apex_infos) const { | 
|  | 103 | return 0; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | bool FlattenedApexHandlerAndroid::AllocateSpace( | 
|  | 107 | const std::vector<ApexInfo>& apex_infos) const { | 
|  | 108 | return true; | 
|  | 109 | } | 
|  | 110 |  | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 111 | }  // namespace chromeos_update_engine |