1 2 /* 3 * Copyright (C) 2019 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <android-base/logging.h> 19 #include <android-base/properties.h> 20 #include <libsnapshot/snapshot.h> 21 22 #include "recovery_ui/device.h" 23 #include "recovery_ui/ui.h" 24 #include "recovery_utils/roots.h" 25 26 using android::snapshot::CancelResult; 27 using android::snapshot::CreateResult; 28 using android::snapshot::SnapshotManager; 29 IsCancelUpdateSafe(Device * device)30bool IsCancelUpdateSafe(Device* device) { 31 auto sm = SnapshotManager::New(); 32 if (!sm) { 33 RecoveryUI* ui = device->GetUI(); 34 ui->Print("Could not create SnapshotManager.\n"); 35 return false; 36 } 37 return sm->IsCancelUpdateSafe(); 38 } 39 FinishPendingSnapshotMerges(Device * device)40bool FinishPendingSnapshotMerges(Device* device) { 41 if (!android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) { 42 return true; 43 } 44 45 RecoveryUI* ui = device->GetUI(); 46 auto sm = SnapshotManager::New(); 47 if (!sm) { 48 ui->Print("Could not create SnapshotManager.\n"); 49 return false; 50 } 51 52 auto callback = [&]() -> void { 53 double progress; 54 sm->GetUpdateState(&progress); 55 ui->Print("Waiting for merge to complete: %.2f\n", progress); 56 }; 57 if (!sm->HandleImminentDataWipe(callback)) { 58 ui->Print("Unable to check merge status and/or complete update merge.\n"); 59 return false; 60 } 61 return true; 62 } 63 CreateSnapshotPartitions()64bool CreateSnapshotPartitions() { 65 if (!android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) { 66 // If the device does not support Virtual A/B, there's no need to create 67 // snapshot devices. 68 return true; 69 } 70 71 auto sm = SnapshotManager::New(); 72 if (!sm) { 73 // SnapshotManager could not be created. The device is still in a 74 // consistent state and can continue with the mounting of the existing 75 // devices, but cannot initialize snapshot devices. 76 LOG(WARNING) << "Could not create SnapshotManager"; 77 return true; 78 } 79 80 auto ret = sm->RecoveryCreateSnapshotDevices(); 81 if (ret == CreateResult::ERROR) { 82 return false; 83 } 84 return true; 85 } 86