1 //
2 // Copyright (C) 2020 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 #include "misc_info.h"
17 
18 #include <algorithm>
19 
20 #include <android-base/logging.h>
21 #include <android-base/strings.h>
22 
ParseMiscInfo(const std::string & misc_info_contents)23 MiscInfo ParseMiscInfo(const std::string& misc_info_contents) {
24   auto lines = android::base::Split(misc_info_contents, "\n");
25   MiscInfo misc_info;
26   for (auto& line : lines) {
27     line = android::base::Trim(line);
28     if (line.size() == 0) {
29       continue;
30     }
31     auto eq_pos = line.find('=');
32     if (eq_pos == std::string::npos) {
33       LOG(WARNING) << "Line in unknown format: \"" << line << "\"";
34       continue;
35     }
36     // Not using android::base::Split here to only capture the first =
37     auto key = android::base::Trim(line.substr(0, eq_pos));
38     auto value = android::base::Trim(line.substr(eq_pos + 1));
39     if (misc_info.find(key) != misc_info.end() && misc_info[key] != value) {
40       LOG(ERROR) << "Duplicate key: \"" << key << "\". First value: \""
41                  << misc_info[key] << "\", Second value: \"" << value << "\"";
42       return {};
43     }
44     misc_info[key] = value;
45   }
46   return misc_info;
47 }
48 
WriteMiscInfo(const MiscInfo & misc_info)49 std::string WriteMiscInfo(const MiscInfo& misc_info) {
50   std::stringstream out;
51   for (const auto& entry : misc_info) {
52     out << entry.first << "=" << entry.second << "\n";
53   }
54   return out.str();
55 }
56 
57 static const std::string kDynamicPartitions = "dynamic_partition_list";
58 
SuperPartitionComponents(const MiscInfo & info)59 std::vector<std::string> SuperPartitionComponents(const MiscInfo& info) {
60   auto value_it = info.find(kDynamicPartitions);
61   if (value_it == info.end()) {
62     return {};
63   }
64   auto components = android::base::Split(value_it->second, " ");
65   for (auto& component : components) {
66     component = android::base::Trim(component);
67   }
68   components.erase(std::remove(components.begin(), components.end(), ""),
69                    components.end());
70   return components;
71 }
72 
73 static const std::string kGoogleDynamicPartitions =
74     "super_google_dynamic_partitions_partition_list";
75 
SetSuperPartitionComponents(const std::vector<std::string> & components,MiscInfo * misc_info)76 void SetSuperPartitionComponents(const std::vector<std::string>& components,
77                                  MiscInfo* misc_info) {
78   (*misc_info)[kDynamicPartitions] = android::base::Join(components, " ");
79   (*misc_info)[kGoogleDynamicPartitions] = android::base::Join(components, " ");
80 }
81