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 | |
| 17 | #include <utility> |
| 18 | |
| 19 | #include <base/files/file_util.h> |
| 20 | |
| 21 | #include "update_engine/aosp/apex_handler_android.h" |
| 22 | #include "update_engine/common/utils.h" |
| 23 | |
| 24 | namespace chromeos_update_engine { |
| 25 | |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame^] | 26 | namespace { |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 27 | |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame^] | 28 | android::apex::CompressedApexInfoList CreateCompressedApexInfoList( |
| 29 | const std::vector<ApexInfo>& apex_infos) { |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 30 | android::apex::CompressedApexInfoList compressed_apex_info_list; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 31 | for (const auto& apex_info : apex_infos) { |
| 32 | if (!apex_info.is_compressed()) { |
| 33 | continue; |
| 34 | } |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 35 | android::apex::CompressedApexInfo compressed_apex_info; |
| 36 | compressed_apex_info.moduleName = apex_info.package_name(); |
| 37 | compressed_apex_info.versionCode = apex_info.version(); |
| 38 | compressed_apex_info.decompressedSize = apex_info.decompressed_size(); |
| 39 | compressed_apex_info_list.apexInfos.emplace_back( |
| 40 | std::move(compressed_apex_info)); |
| 41 | } |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame^] | 42 | return compressed_apex_info_list; |
| 43 | } |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | android::base::Result<uint64_t> ApexHandlerAndroid::CalculateSize( |
| 48 | const std::vector<ApexInfo>& apex_infos) const { |
| 49 | // We might not need to decompress every APEX. Communicate with apexd to get |
| 50 | // accurate requirement. |
| 51 | auto apex_service = GetApexService(); |
| 52 | if (apex_service == nullptr) { |
| 53 | return android::base::Error() << "Failed to get hold of apexservice"; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame^] | 56 | auto compressed_apex_info_list = CreateCompressedApexInfoList(apex_infos); |
| 57 | int64_t size_from_apexd; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 58 | auto result = apex_service->calculateSizeForCompressedApex( |
| 59 | compressed_apex_info_list, &size_from_apexd); |
| 60 | if (!result.isOk()) { |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame^] | 61 | return android::base::Error() |
| 62 | << "Failed to get size required from apexservice"; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 63 | } |
| 64 | return size_from_apexd; |
| 65 | } |
| 66 | |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame^] | 67 | bool ApexHandlerAndroid::AllocateSpace( |
| 68 | const std::vector<ApexInfo>& apex_infos) const { |
| 69 | auto apex_service = GetApexService(); |
| 70 | if (apex_service == nullptr) { |
| 71 | return false; |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 72 | } |
Mohammad Samiul Islam | b0ab865 | 2021-02-26 14:04:17 +0000 | [diff] [blame^] | 73 | auto compressed_apex_info_list = CreateCompressedApexInfoList(apex_infos); |
| 74 | auto result = |
| 75 | apex_service->reserveSpaceForCompressedApex(compressed_apex_info_list); |
| 76 | return result.isOk(); |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | android::sp<android::apex::IApexService> ApexHandlerAndroid::GetApexService() |
| 80 | const { |
| 81 | auto binder = android::defaultServiceManager()->waitForService( |
| 82 | android::String16("apexservice")); |
| 83 | if (binder == nullptr) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | return android::interface_cast<android::apex::IApexService>(binder); |
| 87 | } |
| 88 | |
| 89 | } // namespace chromeos_update_engine |