Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 "updater/simulator_runtime.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | #include <sys/mount.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/properties.h> |
| 28 | #include <android-base/strings.h> |
| 29 | #include <android-base/unique_fd.h> |
| 30 | #include <ext4_utils/wipe.h> |
| 31 | #include <selinux/label.h> |
| 32 | |
| 33 | #include "otautil/mounts.h" |
| 34 | #include "otautil/sysutil.h" |
| 35 | |
| 36 | std::string SimulatorRuntime::GetProperty(const std::string_view key, |
| 37 | const std::string_view default_value) const { |
| 38 | return source_->GetProperty(key, default_value); |
| 39 | } |
| 40 | |
| 41 | int SimulatorRuntime::Mount(const std::string_view location, const std::string_view mount_point, |
| 42 | const std::string_view /* fs_type */, |
| 43 | const std::string_view /* mount_options */) { |
| 44 | if (auto mounted_location = mounted_partitions_.find(mount_point); |
| 45 | mounted_location != mounted_partitions_.end() && mounted_location->second != location) { |
| 46 | LOG(ERROR) << mount_point << " has been mounted at " << mounted_location->second; |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | mounted_partitions_.emplace(mount_point, location); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | bool SimulatorRuntime::IsMounted(const std::string_view mount_point) const { |
| 55 | return mounted_partitions_.find(mount_point) != mounted_partitions_.end(); |
| 56 | } |
| 57 | |
| 58 | std::pair<bool, int> SimulatorRuntime::Unmount(const std::string_view mount_point) { |
| 59 | if (!IsMounted(mount_point)) { |
| 60 | return { false, -1 }; |
| 61 | } |
| 62 | |
| 63 | mounted_partitions_.erase(std::string(mount_point)); |
| 64 | return { true, 0 }; |
| 65 | } |
| 66 | |
| 67 | std::string SimulatorRuntime::FindBlockDeviceName(const std::string_view name) const { |
| 68 | return source_->FindBlockDeviceName(name); |
| 69 | } |
| 70 | |
| 71 | // TODO(xunchang) implement the utility functions in simulator. |
| 72 | int SimulatorRuntime::RunProgram(const std::vector<std::string>& args, bool /* is_vfork */) const { |
| 73 | LOG(INFO) << "Running program with args " << android::base::Join(args, " "); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int SimulatorRuntime::Tune2Fs(const std::vector<std::string>& args) const { |
| 78 | LOG(INFO) << "Running Tune2Fs with args " << android::base::Join(args, " "); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int SimulatorRuntime::WipeBlockDevice(const std::string_view filename, size_t /* len */) const { |
| 83 | LOG(INFO) << "SKip wiping block device " << filename; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | bool SimulatorRuntime::ReadFileToString(const std::string_view filename, |
| 88 | std::string* /* content */) const { |
| 89 | LOG(INFO) << "SKip reading filename " << filename; |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool SimulatorRuntime::WriteStringToFile(const std::string_view content, |
| 94 | const std::string_view filename) const { |
| 95 | LOG(INFO) << "SKip writing " << content.size() << " bytes to file " << filename; |
| 96 | return true; |
| 97 | } |