1 /* 2 * Copyright (C) 2018 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 #define LOG_TAG "apexd" 18 19 #include "apexd_checkpoint_vold.h" 20 21 #include <android-base/logging.h> 22 #include <android/os/IVold.h> 23 #include <binder/IServiceManager.h> 24 25 using android::sp; 26 using android::base::Error; 27 using android::base::Result; 28 using android::os::IVold; 29 30 namespace android { 31 namespace apex { 32 33 Result<VoldCheckpointInterface> VoldCheckpointInterface::Create() { 34 auto vold_service = 35 defaultServiceManager()->getService(android::String16("vold")); 36 if (vold_service != nullptr) { 37 return VoldCheckpointInterface( 38 android::interface_cast<android::os::IVold>(vold_service)); 39 } 40 return Errorf("Failed to retrieve vold service."); 41 } 42 43 VoldCheckpointInterface::VoldCheckpointInterface(sp<IVold>&& vold_service) { 44 vold_service_ = vold_service; 45 supports_fs_checkpoints_ = false; 46 android::binder::Status status = 47 vold_service_->supportsCheckpoint(&supports_fs_checkpoints_); 48 if (!status.isOk()) { 49 LOG(ERROR) << "Failed to check if filesystem checkpoints are supported: " 50 << status.toString8().c_str(); 51 } 52 } 53 54 VoldCheckpointInterface::VoldCheckpointInterface( 55 VoldCheckpointInterface&& other) noexcept { 56 vold_service_ = std::move(other.vold_service_); 57 supports_fs_checkpoints_ = other.supports_fs_checkpoints_; 58 } 59 60 VoldCheckpointInterface::~VoldCheckpointInterface() { 61 // Just here to be able to forward-declare IVold. 62 } 63 64 Result<bool> VoldCheckpointInterface::SupportsFsCheckpoints() { 65 return supports_fs_checkpoints_; 66 } 67 68 Result<bool> VoldCheckpointInterface::NeedsCheckpoint() { 69 if (supports_fs_checkpoints_) { 70 bool needs_checkpoint = false; 71 android::binder::Status status = 72 vold_service_->needsCheckpoint(&needs_checkpoint); 73 if (!status.isOk()) { 74 return Error() << status.toString8().c_str(); 75 } 76 return needs_checkpoint; 77 } 78 return false; 79 } 80 81 Result<bool> VoldCheckpointInterface::NeedsRollback() { 82 if (supports_fs_checkpoints_) { 83 bool needs_rollback = false; 84 android::binder::Status status = 85 vold_service_->needsRollback(&needs_rollback); 86 if (!status.isOk()) { 87 return Error() << status.toString8().c_str(); 88 } 89 return needs_rollback; 90 } 91 return false; 92 } 93 94 Result<void> VoldCheckpointInterface::StartCheckpoint(int32_t num_retries) { 95 if (supports_fs_checkpoints_) { 96 android::binder::Status status = 97 vold_service_->startCheckpoint(num_retries); 98 if (!status.isOk()) { 99 return Error() << status.toString8().c_str(); 100 } 101 return {}; 102 } 103 return Errorf("Device does not support filesystem checkpointing"); 104 } 105 106 Result<void> VoldCheckpointInterface::AbortChanges(const std::string& msg, 107 bool retry) { 108 vold_service_->abortChanges(msg, retry); 109 return {}; 110 } 111 112 } // namespace apex 113 } // namespace android 114