blob: c8718c71aaeb4e4b9ccff5daf0b1b14386707563 [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
35#include "otautil/mounts.h"
36#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,
90 std::string* /* content */) const {
91 LOG(INFO) << "SKip reading filename " << filename;
92 return true;
93}
94
95bool SimulatorRuntime::WriteStringToFile(const std::string_view content,
96 const std::string_view filename) const {
97 LOG(INFO) << "SKip writing " << content.size() << " bytes to file " << filename;
98 return true;
99}
Tianjie Xud1188332019-05-24 16:08:45 -0700100
101bool SimulatorRuntime::MapPartitionOnDeviceMapper(const std::string& partition_name,
102 std::string* path) {
103 *path = partition_name;
104 return true;
105}
106
107bool SimulatorRuntime::UnmapPartitionOnDeviceMapper(const std::string& partition_name) {
108 LOG(INFO) << "Skip unmapping " << partition_name;
109 return true;
110}
111
112bool SimulatorRuntime::UpdateDynamicPartitions(const std::string_view op_list_value) {
113 const std::unordered_set<std::string> commands{
114 "resize", "remove", "add", "move",
115 "add_group", "resize_group", "remove_group", "remove_all_groups",
116 };
117
118 std::vector<std::string> lines = android::base::Split(std::string(op_list_value), "\n");
119 for (const auto& line : lines) {
120 if (line.empty() || line[0] == '#') continue;
121 auto tokens = android::base::Split(line, " ");
122 if (commands.find(tokens[0]) == commands.end()) {
123 LOG(ERROR) << "Unknown operation in op_list: " << line;
124 return false;
125 }
126 }
127 return true;
128}