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 <string>
20 #include <string_view>
21 #include <vector>
22 
23 // This class serves as the base to updater runtime. It wraps the runtime dependent functions; and
24 // updates on device and host simulations can have different implementations. e.g. block devices
25 // during host simulation merely a temporary file. With this class, the caller side in registered
26 // updater's functions will stay the same for both update and simulation.
27 class UpdaterRuntimeInterface {
28  public:
29   virtual ~UpdaterRuntimeInterface() = default;
30 
31   // Returns true if it's a runtime instance for simulation.
32   virtual bool IsSimulator() const = 0;
33 
34   // Returns the value of system property |key|. If the property doesn't exist, returns
35   // |default_value|.
36   virtual std::string GetProperty(const std::string_view key,
37                                   const std::string_view default_value) const = 0;
38 
39   // Given the name of the block device, returns |name| for updates on the device; or the file path
40   // to the fake block device for simulations.
41   virtual std::string FindBlockDeviceName(const std::string_view name) const = 0;
42 
43   // Mounts the |location| on |mount_point|. Returns 0 on success.
44   virtual int Mount(const std::string_view location, const std::string_view mount_point,
45                     const std::string_view fs_type, const std::string_view mount_options) = 0;
46 
47   // Returns true if |mount_point| is mounted.
48   virtual bool IsMounted(const std::string_view mount_point) const = 0;
49 
50   // Unmounts the |mount_point|. Returns a pair of results with the first value indicating
51   // if the |mount_point| is mounted, and the second value indicating the result of umount(2).
52   virtual std::pair<bool, int> Unmount(const std::string_view mount_point) = 0;
53 
54   // Reads |filename| and puts its value to |content|.
55   virtual bool ReadFileToString(const std::string_view filename, std::string* content) const = 0;
56 
57   // Updates the content of |filename| with |content|.
58   virtual bool WriteStringToFile(const std::string_view content,
59                                  const std::string_view filename) const = 0;
60 
61   // Wipes the first |len| bytes of block device in |filename|.
62   virtual int WipeBlockDevice(const std::string_view filename, size_t len) const = 0;
63 
64   // Starts a child process and runs the program with |args|. Uses vfork(2) if |is_vfork| is true.
65   virtual int RunProgram(const std::vector<std::string>& args, bool is_vfork) const = 0;
66 
67   // Runs tune2fs with arguments |args|.
68   virtual int Tune2Fs(const std::vector<std::string>& args) const = 0;
69 
70   // Dynamic partition related functions.
71   virtual bool MapPartitionOnDeviceMapper(const std::string& partition_name, std::string* path) = 0;
72   virtual bool UnmapPartitionOnDeviceMapper(const std::string& partition_name) = 0;
73   virtual bool UpdateDynamicPartitions(const std::string_view op_list_value) = 0;
74 
75   // On devices supports A/B, add current slot suffix to arg. Otherwise, return |arg| as is.
76   virtual std::string AddSlotSuffix(const std::string_view arg) const = 0;
77 };
78