blob: 57dfb32d4d099ad8d182a2ff4e727deed069c07d [file] [log] [blame]
Tianjie Xuc1a5e262019-05-22 14:34:12 -07001/*
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
Tianjie Xud1188332019-05-24 16:08:45 -070025#include <unordered_set>
26
Tianjie Xuc1a5e262019-05-22 14:34:12 -070027#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/properties.h>
30#include <android-base/strings.h>
31#include <android-base/unique_fd.h>
32#include <ext4_utils/wipe.h>
33#include <selinux/label.h>
34
Tao Baod628cfc2019-10-01 12:08:33 -070035#include "mounts.h"
Tianjie Xuc1a5e262019-05-22 14:34:12 -070036#include "otautil/sysutil.h"
37
38std::string SimulatorRuntime::GetProperty(const std::string_view key,
39 const std::string_view default_value) const {
40 return source_->GetProperty(key, default_value);
41}
42
43int SimulatorRuntime::Mount(const std::string_view location, const std::string_view mount_point,
44 const std::string_view /* fs_type */,
45 const std::string_view /* mount_options */) {
46 if (auto mounted_location = mounted_partitions_.find(mount_point);
47 mounted_location != mounted_partitions_.end() && mounted_location->second != location) {
48 LOG(ERROR) << mount_point << " has been mounted at " << mounted_location->second;
49 return -1;
50 }
51
52 mounted_partitions_.emplace(mount_point, location);
53 return 0;
54}
55
56bool SimulatorRuntime::IsMounted(const std::string_view mount_point) const {
57 return mounted_partitions_.find(mount_point) != mounted_partitions_.end();
58}
59
60std::pair<bool, int> SimulatorRuntime::Unmount(const std::string_view mount_point) {
61 if (!IsMounted(mount_point)) {
62 return { false, -1 };
63 }
64
65 mounted_partitions_.erase(std::string(mount_point));
66 return { true, 0 };
67}
68
69std::string SimulatorRuntime::FindBlockDeviceName(const std::string_view name) const {
70 return source_->FindBlockDeviceName(name);
71}
72
73// TODO(xunchang) implement the utility functions in simulator.
74int SimulatorRuntime::RunProgram(const std::vector<std::string>& args, bool /* is_vfork */) const {
75 LOG(INFO) << "Running program with args " << android::base::Join(args, " ");
76 return 0;
77}
78
79int SimulatorRuntime::Tune2Fs(const std::vector<std::string>& args) const {
80 LOG(INFO) << "Running Tune2Fs with args " << android::base::Join(args, " ");
81 return 0;
82}
83
84int SimulatorRuntime::WipeBlockDevice(const std::string_view filename, size_t /* len */) const {
85 LOG(INFO) << "SKip wiping block device " << filename;
86 return 0;
87}
88
89bool SimulatorRuntime::ReadFileToString(const std::string_view filename,
Tianjie Xu7efd2332019-07-15 16:13:49 -070090 std::string* content) const {
91 if (android::base::EndsWith(filename, "oem.prop")) {
92 return android::base::ReadFileToString(source_->GetOemSettings(), content);
93 }
94
Tianjie Xuc1a5e262019-05-22 14:34:12 -070095 LOG(INFO) << "SKip reading filename " << filename;
96 return true;
97}
98
99bool SimulatorRuntime::WriteStringToFile(const std::string_view content,
100 const std::string_view filename) const {
101 LOG(INFO) << "SKip writing " << content.size() << " bytes to file " << filename;
102 return true;
103}
Tianjie Xud1188332019-05-24 16:08:45 -0700104
105bool SimulatorRuntime::MapPartitionOnDeviceMapper(const std::string& partition_name,
106 std::string* path) {
107 *path = partition_name;
108 return true;
109}
110
111bool SimulatorRuntime::UnmapPartitionOnDeviceMapper(const std::string& partition_name) {
112 LOG(INFO) << "Skip unmapping " << partition_name;
113 return true;
114}
115
116bool SimulatorRuntime::UpdateDynamicPartitions(const std::string_view op_list_value) {
117 const std::unordered_set<std::string> commands{
118 "resize", "remove", "add", "move",
119 "add_group", "resize_group", "remove_group", "remove_all_groups",
120 };
121
122 std::vector<std::string> lines = android::base::Split(std::string(op_list_value), "\n");
123 for (const auto& line : lines) {
124 if (line.empty() || line[0] == '#') continue;
125 auto tokens = android::base::Split(line, " ");
126 if (commands.find(tokens[0]) == commands.end()) {
127 LOG(ERROR) << "Unknown operation in op_list: " << line;
128 return false;
129 }
130 }
131 return true;
132}
Yifan Hongdff80042020-04-28 13:31:11 -0700133
134std::string SimulatorRuntime::AddSlotSuffix(const std::string_view arg) const {
135 LOG(INFO) << "Skip adding slot suffix to " << arg;
136 return std::string(arg);
137}