1 /*
2 * Copyright (C) 2024 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 <string>
18 #include <string_view>
19 #include <unordered_map>
20
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/result.h>
24 #include <tinyxml2.h>
25 #include <utils/Errors.h>
26
27 #include "ogki_builds_utils.h"
28
29 using android::base::Result;
30 using android::base::ResultError;
31
32 namespace ogki {
33
34 const std::string approved_builds_config_path =
35 "/system/etc/kernel/approved-ogki-builds.xml";
36
GetApprovedBuilds(std::string_view branch_name)37 Result<std::unordered_map<std::string, BuildInfo>> GetApprovedBuilds(
38 std::string_view branch_name) {
39 std::string approved_builds_content;
40 if (!android::base::ReadFileToString(approved_builds_config_path,
41 &approved_builds_content)) {
42 return ResultError("Failed to read approved OGKI builds config at " +
43 approved_builds_config_path,
44 -errno);
45 }
46
47 tinyxml2::XMLDocument approved_builds_xml;
48 if (auto xml_error =
49 approved_builds_xml.Parse(approved_builds_content.c_str());
50 xml_error != tinyxml2::XMLError::XML_SUCCESS) {
51 return ResultError(
52 std::format("Failed to parse approved builds config: {}",
53 tinyxml2::XMLDocument::ErrorIDToName(xml_error)),
54 android::UNKNOWN_ERROR);
55 }
56
57 tinyxml2::XMLElement* branch_element = nullptr;
58 const auto ogki_element = approved_builds_xml.RootElement();
59 for (auto branch = ogki_element->FirstChildElement("branch"); branch;
60 branch = branch->NextSiblingElement("branch")) {
61 if (branch->Attribute("name", branch_name.data())) {
62 branch_element = branch;
63 break;
64 }
65 }
66 if (!branch_element) {
67 return ResultError(
68 std::format("Branch '{}' not found in approved builds config",
69 branch_name.data()),
70 android::NAME_NOT_FOUND);
71 }
72
73 std::unordered_map<std::string, BuildInfo> approved_builds;
74 for (auto build = branch_element->FirstChildElement("build"); build;
75 build = build->NextSiblingElement("build")) {
76 approved_builds.emplace(build->Attribute("id"), BuildInfo{});
77 }
78 return approved_builds;
79 }
80
81 } // namespace ogki
82