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:
FakeBlockDevice(std::string block_device,std::string mount_point,std::string temp_file_path)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:
BuildInfo(const std::string_view work_dir,bool keep_images)46   BuildInfo(const std::string_view work_dir, bool keep_images)
47       : work_dir_(work_dir), keep_images_(keep_images) {}
48   // Returns the value of the build properties.
49   std::string GetProperty(const std::string_view key, const std::string_view default_value) const;
50   // Returns the path to the mock block device.
51   std::string FindBlockDeviceName(const std::string_view name) const;
52   // Parses the given target-file, initializes the build properties and extracts the images.
53   bool ParseTargetFile(const std::string_view target_file_path, bool extracted_input);
54 
GetOemSettings()55   std::string GetOemSettings() const {
56     return oem_settings_;
57   }
SetOemSettings(const std::string_view oem_settings)58   void SetOemSettings(const std::string_view oem_settings) {
59     oem_settings_ = oem_settings;
60   }
61 
62  private:
63   // A map to store the system properties during simulation.
64   std::map<std::string, std::string, std::less<>> build_props_;
65   // A file that contains the oem properties.
66   std::string oem_settings_;
67   // A map from the blockdev_name to the FakeBlockDevice object, which contains the path to the
68   // temporary file.
69   std::map<std::string, FakeBlockDevice, std::less<>> blockdev_map_;
70 
71   std::list<TemporaryFile> temp_files_;
72   std::string work_dir_;  // A temporary directory to store the extracted image files
73   bool keep_images_;
74 };
75