Tianjie Xu | 74b0f7c | 2019-05-22 13:59:57 -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 | #pragma once |
| 18 | |
| 19 | #include <list> |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | #include <string_view> |
| 23 | |
| 24 | #include <android-base/file.h> |
| 25 | |
| 26 | // This class serves as the aggregation of the fake block device information during update |
| 27 | // simulation on host. In specific, it has the name of the block device, its mount point, and the |
| 28 | // path to the temporary file that fakes this block device. |
| 29 | class FakeBlockDevice { |
| 30 | public: |
| 31 | FakeBlockDevice(std::string block_device, std::string mount_point, std::string temp_file_path) |
| 32 | : blockdev_name(std::move(block_device)), |
| 33 | mount_point(std::move(mount_point)), |
| 34 | mounted_file_path(std::move(temp_file_path)) {} |
| 35 | |
| 36 | std::string blockdev_name; |
| 37 | std::string mount_point; |
| 38 | std::string mounted_file_path; // path to the temp file that mocks the block device |
| 39 | }; |
| 40 | |
| 41 | // This class stores the information of the source build. For example, it creates and maintains |
| 42 | // the temporary files to simulate the block devices on host. Therefore, the simulator runtime can |
| 43 | // query the information and run the update on host. |
| 44 | class BuildInfo { |
| 45 | public: |
| 46 | explicit BuildInfo(const std::string_view work_dir) : work_dir_(work_dir) {} |
| 47 | // Returns the value of the build properties. |
| 48 | std::string GetProperty(const std::string_view key, const std::string_view default_value) const; |
| 49 | // Returns the path to the mock block device. |
| 50 | std::string FindBlockDeviceName(const std::string_view name) const; |
| 51 | // Parses the given target-file, initializes the build properties and extracts the images. |
| 52 | bool ParseTargetFile(const std::string_view target_file_path, bool extracted_input); |
| 53 | |
| 54 | private: |
| 55 | // A map to store the system properties during simulation. |
| 56 | std::map<std::string, std::string, std::less<>> build_props_; |
| 57 | // A map from the blockdev_name to the FakeBlockDevice object, which contains the path to the |
| 58 | // temporary file. |
| 59 | std::map<std::string, FakeBlockDevice, std::less<>> blockdev_map_; |
| 60 | |
| 61 | std::list<TemporaryFile> temp_files_; |
| 62 | std::string work_dir_; // A temporary directory to store the extracted image files |
| 63 | }; |